From 4186e14befb4f7aa9955926d538873ce4e6bbebc Mon Sep 17 00:00:00 2001 From: sobolevn Date: Tue, 29 Oct 2024 11:46:13 +0300 Subject: [PATCH] gh-126105: Fix crash in `ast` module, when `._fields` is deleted --- Lib/test/test_ast/test_ast.py | 17 +++++++++++++++++ ...4-10-29-11-45-44.gh-issue-126105.cOL-R6.rst | 1 + Python/Python-ast.c | 18 ++++++++++-------- 3 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py index dd1c6447fca337..61c9ef01b7ecb6 100644 --- a/Lib/test/test_ast/test_ast.py +++ b/Lib/test/test_ast/test_ast.py @@ -84,6 +84,23 @@ def test_AST_objects(self): # "ast.AST constructor takes 0 positional arguments" ast.AST(2) + def test_AST_fields_NULL_check(self): + # See: https://github.com/python/cpython/issues/126105 + old_value = ast.AST._fields + + def cleanup(): + ast.AST._fields = old_value + self.addCleanup(cleanup) + + del ast.AST._fields + + msg = 'AST has no fields' + # Both examples used to crash: + with self.assertRaisesRegex(TypeError, msg): + ast.AST(arg1=123) + with self.assertRaisesRegex(TypeError, msg): + ast.AST() + def test_AST_garbage_collection(self): class X: pass diff --git a/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst b/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst new file mode 100644 index 00000000000000..de8702b3cf65be --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst @@ -0,0 +1 @@ +Fix a crash in :mod:`ast` when ``_fields`` attribute is deleted. diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 38d74b48d232f8..50aef75d08bee2 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -5086,16 +5086,18 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { goto cleanup; } - if (fields) { - numfields = PySequence_Size(fields); - if (numfields == -1) { - goto cleanup; - } - remaining_fields = PySet_New(fields); + if (fields == NULL) { + PyErr_Format(PyExc_TypeError, + "%.400s has no fields", + _PyType_Name(Py_TYPE(self))); + goto cleanup; } - else { - remaining_fields = PySet_New(NULL); + + numfields = PySequence_Size(fields); + if (numfields == -1) { + goto cleanup; } + remaining_fields = PySet_New(fields); if (remaining_fields == NULL) { goto cleanup; }