https://github.com/python/cpython/commit/515a5d3498b572057056f0eef143a29838978705
commit: 515a5d3498b572057056f0eef143a29838978705
branch: 3.12
author: Kirill Podoprigora <kirill.ba...@mail.ru>
committer: Eclips4 <kirill.ba...@mail.ru>
date: 2024-10-29T20:20:40+02:00
summary:

[3.12] gh-126105: Fix crash in `ast` module, when `._fields` is delet… (#126132)

[3.12] gh-126105: Fix crash in `ast` module, when `._fields` is deleted 
(GH-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).
(cherry picked from commit b2eaa75b176e07730215d76d8dce4d63fb493391)

Co-authored-by: sobolevn <m...@sobolevn.me>

files:
A Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst
M Lib/test/test_ast/test_ast.py
M Parser/asdl_c.py
M Python/Python-ast.c

diff --git a/Lib/test/test_ast/test_ast.py b/Lib/test/test_ast/test_ast.py
index 7510995b20e9d4..f07e2d027b1624 100644
--- a/Lib/test/test_ast/test_ast.py
+++ b/Lib/test/test_ast/test_ast.py
@@ -66,6 +66,23 @@ def test_AST_objects(self):
             # "ast.AST constructor takes 0 positional arguments"
             ast.AST(2)
 
+    def test_AST_fields_NULL_check(self):
+        # See: https://github.com/python/cpython/issues/126105
+        old_value = ast.AST._fields
+
+        def cleanup():
+            ast.AST._fields = old_value
+        self.addCleanup(cleanup)
+
+        del ast.AST._fields
+
+        msg = "type object 'ast.AST' has no attribute '_fields'"
+        # Both examples used to crash:
+        with self.assertRaisesRegex(AttributeError, msg):
+            ast.AST(arg1=123)
+        with self.assertRaisesRegex(AttributeError, msg):
+            ast.AST()
+
     def test_AST_garbage_collection(self):
         class X:
             pass
diff --git 
a/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst 
b/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst
new file mode 100644
index 00000000000000..547eb3af1ca064
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2024-10-29-11-45-44.gh-issue-126105.cOL-R6.rst
@@ -0,0 +1 @@
+Fix a crash in :mod:`ast` when the :attr:`ast.AST._fields` attribute is 
deleted.
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index 2c34f5c1bc2675..6f5a772dccbd3f 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -813,14 +813,15 @@ def visitModule(self, mod):
     Py_ssize_t i, numfields = 0;
     int res = -1;
     PyObject *key, *value, *fields;
-    if (_PyObject_LookupAttr((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;
-        }
+
+    numfields = PySequence_Size(fields);
+    if (numfields == -1) {
+        goto cleanup;
     }
 
     res = 0; /* if no error occurs, this stays 0 to the end */
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index ecaff2041f9565..3d5aeff837953b 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -863,14 +863,15 @@ ast_type_init(PyObject *self, PyObject *args, PyObject 
*kw)
     Py_ssize_t i, numfields = 0;
     int res = -1;
     PyObject *key, *value, *fields;
-    if (_PyObject_LookupAttr((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;
-        }
+
+    numfields = PySequence_Size(fields);
+    if (numfields == -1) {
+        goto cleanup;
     }
 
     res = 0; /* if no error occurs, this stays 0 to the end */

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to