diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py index 739a020f708..67ab8cf6baf 100644 --- a/Lib/test/test_ast/test_ast.py +++ b/Lib/test/test_ast/test_ast.py @@ -84,6 +84,23 @@ class AST_Tests(unittest.TestCase): # "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 = "type object 'ast.AST' has no attribute '_fields'" + # Both examples used to crash: + with self.assertRaisesRegex(AttributeError, msg): + ast.AST(arg1=123) + with self.assertRaisesRegex(AttributeError, 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 00000000000..547eb3af1ca --- /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 the :attr:`ast.AST._fields` attribute is deleted. diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 32eac3afafa..853a3e99807 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -884,19 +884,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) Py_ssize_t i, numfields = 0; int res = -1; PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL; - if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { + + fields = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_fields); + if (fields == NULL) { goto cleanup; } - if (fields) { - numfields = PySequence_Size(fields); - if (numfields == -1) { - goto cleanup; - } - remaining_fields = PySet_New(fields); - } - 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; } diff --git a/Python/Python-ast.c b/Python/Python-ast.c index 38d74b48d23..41299b29705 100644 --- a/Python/Python-ast.c +++ b/Python/Python-ast.c @@ -5083,19 +5083,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) Py_ssize_t i, numfields = 0; int res = -1; PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL; - if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { + + fields = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_fields); + if (fields == NULL) { goto cleanup; } - if (fields) { - numfields = PySequence_Size(fields); - if (numfields == -1) { - goto cleanup; - } - remaining_fields = PySet_New(fields); - } - 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; }