diff options
author | Jelle Zijlstra <jelle.zijlstra@gmail.com> | 2024-05-06 22:57:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-06 22:57:27 (GMT) |
commit | e0422198fb4de0a5d81edd3de0d0ed32c119e9bb (patch) | |
tree | 8fedfb2ee456f4a96ddb673fbea256085c631e74 /Parser | |
parent | 040571f258d13a807f5c8e4ce0a182d5f9a2e81b (diff) | |
download | cpython-e0422198fb4de0a5d81edd3de0d0ed32c119e9bb.zip cpython-e0422198fb4de0a5d81edd3de0d0ed32c119e9bb.tar.gz cpython-e0422198fb4de0a5d81edd3de0d0ed32c119e9bb.tar.bz2 |
gh-117486: Improve behavior for user-defined AST subclasses (#118212)
Now, such classes will no longer require changes in Python 3.13 in the normal case.
The test suite for robotframework passes with no DeprecationWarnings under this PR.
I also added a new DeprecationWarning for the case where `_field_types` exists
but is incomplete, since that seems likely to indicate a user mistake.
Diffstat (limited to 'Parser')
-rwxr-xr-x | Parser/asdl_c.py | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 1f0be45..11d59fa 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -979,14 +979,9 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) 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; - } + // Probably a user-defined subclass of AST that lacks _field_types. + // This will continue to work as it did before 3.13; i.e., attributes + // that are not passed in simply do not exist on the instance. goto cleanup; } remaining_list = PySequence_List(remaining_fields); @@ -997,12 +992,21 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) PyObject *name = PyList_GET_ITEM(remaining_list, i); PyObject *type = PyDict_GetItemWithError(field_types, name); if (!type) { - if (!PyErr_Occurred()) { - PyErr_SetObject(PyExc_KeyError, name); + if (PyErr_Occurred()) { + goto set_remaining_cleanup; + } + else { + if (PyErr_WarnFormat( + PyExc_DeprecationWarning, 1, + "Field '%U' is missing from %.400s._field_types. " + "This will become an error in Python 3.15.", + name, Py_TYPE(self)->tp_name + ) < 0) { + goto set_remaining_cleanup; + } } - goto set_remaining_cleanup; } - if (_PyUnion_Check(type)) { + else if (_PyUnion_Check(type)) { // optional field // do nothing, we'll have set a None default on the class } @@ -1026,8 +1030,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) "This will become an error in Python 3.15.", Py_TYPE(self)->tp_name, name ) < 0) { - res = -1; - goto cleanup; + goto set_remaining_cleanup; } } } |