Skip to content
Open
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
14 changes: 14 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,20 @@ def test_repr_large_input_crash(self):
r"Exceeds the limit \(\d+ digits\)"):
repr(ast.Constant(value=eval(source)))

def test_repr_missing_fields_crash(self):
class FieldsMissingMeta(type):
def __getattribute__(self, name):
if name == "_fields":
raise AttributeError
return super().__getattribute__(name)

class FieldsMissing(ast.AST, metaclass=FieldsMissingMeta):
def __init__(self):
pass

node = FieldsMissing()
self.assertEqual(repr(node), "FieldsMissing()")

def test_tstring(self):
# Test AST structure for simple t-string
tree = ast.parse('t"Hello"')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a crash in :func:`repr` of AST nodes when the AST node's type lacks the
``_fields`` attribute. Patched by Shamil Abdulaev.
5 changes: 5 additions & 0 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1498,6 +1498,11 @@ def visitModule(self, mod):
return NULL;
}
if (fields == NULL) {
Py_ReprLeave((PyObject *)self);
return PyUnicode_FromFormat("%s()", Py_TYPE(self)->tp_name);
}
Py_ssize_t numfields = PySequence_Size(fields);
if (numfields < 0) {
Py_ReprLeave((PyObject *)self);
Expand Down
5 changes: 5 additions & 0 deletions Python/Python-ast.c

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

Loading