8000 gh-117266: Fix crashes on user-created AST subclasses by JelleZijlstra · Pull Request #117276 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-117266: Fix crashes on user-created AST subclasses #117276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 28, 2024
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
41 changes: 41 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,47 @@ def test_FunctionDef(self):
self.assertEqual(node.name, 'foo')
self.assertEqual(node.decorator_list, [])

def test_custom_subclass(self):
class NoInit(ast.AST):
pass

obj = NoInit()
self.assertIsInstance(obj, NoInit)
self.assertEqual(obj.__dict__, {})

class Fields(ast.AST):
_fields = ('a',)

with self.assertWarnsRegex(DeprecationWarning,
r"Fields provides _fields but not _field_types."):
obj = Fields()
with self.assertRaises(AttributeError):
obj.a
obj = Fields(a=1)
self.assertEqual(obj.a, 1)

class FieldsAndTypes(ast.AST):
_fields = ('a',)
_field_types = {'a': int | None}
a: int | None = None

obj = FieldsAndTypes()
self.assertIs(obj.a, None)
obj = FieldsAndTypes(a=1)
self.assertEqual(obj.a, 1)

class FieldsAndTypesNoDefault(ast.AST):
_fields = ('a',)
_field_types = {'a': int}

with self.assertWarnsRegex(DeprecationWarning,
r"FieldsAndTypesNoDefault\.__init__ missing 1 required positional argument: 'a'\."):
obj = FieldsAndTypesNoDefault()
with self.assertRaises(AttributeError):
obj.a
obj = FieldsAndTypesNoDefault(a=1)
self.assertEqual(obj.a, 1)


@support.cpython_only
class ModuleStateTests(unittest.TestCase):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix crashes for certain user-created subclasses of :class:`ast.AST`. Such
classes are now expected to set the ``_field_types`` attribute.
15 changes: 13 additions & 2 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,22 @@ def visitModule(self, mod):
Py_ssize_t size = PySet_Size(remaining_fields);
PyObject *field_types = NULL, *remaining_list = NULL;
if (size > 0) {
if (!PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), &_Py_ID(_field_types),
&field_types)) {
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), &_Py_ID(_field_types),
&field_types) < 0) {
res = -1;
goto cleanup;
}
if (field_types == NULL) {
if (PyErr_WarnFormat(
PyExc_DeprecationWarning, 1,
"%.400s provides _fields but not _field_types. "
"This will become an error in Python 3.15.",
Py_TYPE(self)->tp_name
) < 0) {
res = -1;
}
goto cleanup;
}
remaining_list = PySequence_List(remaining_fields);
if (!remaining_list) {
goto set_remaining_cleanup;
Expand Down
15 changes: 13 additions & 2 deletions Python/Python-ast.c

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

0