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):
--- /dev/null
+Fix crashes for certain user-created subclasses of :class:`ast.AST`. Such
+classes are now expected to set the ``_field_types`` attribute.
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;
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;