diff options
author | sobolevn <mail@sobolevn.me> | 2024-10-29 15:42:48 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-10-29 15:42:48 (GMT) |
commit | b2eaa75b176e07730215d76d8dce4d63fb493391 (patch) | |
tree | 69f1cbd485328cf6753cd1b5c30fca5dd2b84bf4 /Parser | |
parent | 0bbbe15f5688552236c48f2b6e320c5312720b8e (diff) | |
download | cpython-b2eaa75b176e07730215d76d8dce4d63fb493391.zip cpython-b2eaa75b176e07730215d76d8dce4d63fb493391.tar.gz cpython-b2eaa75b176e07730215d76d8dce4d63fb493391.tar.bz2 |
gh-126105: Fix crash in `ast` module, when `._fields` is deleted (#126115)
Previously, if the `ast.AST._fields` attribute was deleted, attempts to create a new `as`t node would crash due to the assumption that `_fields` always had a non-NULL value. Now it has been fixed by adding an extra check to ensure that `_fields` does not have a NULL value (this can happen when you manually remove `_fields` attribute).
Diffstat (limited to 'Parser')
-rwxr-xr-x | Parser/asdl_c.py | 18 |
1 files changed, 8 insertions, 10 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py index 32eac3a..853a3e9 100755 --- a/Parser/asdl_c.py +++ b/Parser/asdl_c.py @@ -884,19 +884,17 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw) Py_ssize_t i, numfields = 0; int res = -1; PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL; - if (PyObject_GetOptionalAttr((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; - } - remaining_fields = PySet_New(fields); - } - else { - remaining_fields = PySet_New(NULL); + + numfields = PySequence_Size(fields); + if (numfields == -1) { + goto cleanup; } + remaining_fields = PySet_New(fields); if (remaining_fields == NULL) { goto cleanup; } |