Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,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 = "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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a crash in :mod:`ast` when the :attr:`ast.AST._fields` attribute is deleted.
13 changes: 7 additions & 6 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -813,14 +813,15 @@ def visitModule(self, mod):
Py_ssize_t i, numfields = 0;
int res = -1;
PyObject *key, *value, *fields;
if (_PyObject_LookupAttr((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;
}

numfields = PySequence_Size(fields);
if (numfields == -1) {
goto cleanup;
}

res = 0; /* if no error occurs, this stays 0 to the end */
Expand Down
13 changes: 7 additions & 6 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading