summaryrefslogtreecommitdiffstats
path: root/Python/Python-ast.c
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2025-05-10 16:17:38 (GMT)
committerGitHub <noreply@github.com>2025-05-10 16:17:38 (GMT)
commit7dddb4e667b5eb76cbe11755051ec139b0f437a9 (patch)
treed8ec5065fd20f56f8844ba1a8ade9a1bb2b53eeb /Python/Python-ast.c
parent47f1722d8053fb4f79e68cba07cbf08fb58a511c (diff)
downloadcpython-7dddb4e667b5eb76cbe11755051ec139b0f437a9.zip
cpython-7dddb4e667b5eb76cbe11755051ec139b0f437a9.tar.gz
cpython-7dddb4e667b5eb76cbe11755051ec139b0f437a9.tar.bz2
gh-133783: Fix __replace__ on AST nodes for optional attributes (#133797)
Diffstat (limited to 'Python/Python-ast.c')
-rw-r--r--Python/Python-ast.c26
1 files changed, 26 insertions, 0 deletions
diff --git a/Python/Python-ast.c b/Python/Python-ast.c
index df03556..f7625ab 100644
--- a/Python/Python-ast.c
+++ b/Python/Python-ast.c
@@ -5528,6 +5528,32 @@ ast_type_replace_check(PyObject *self,
Py_DECREF(unused);
}
}
+
+ // Discard fields from 'expecting' that default to None
+ PyObject *field_types = NULL;
+ if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self),
+ &_Py_ID(_field_types),
+ &field_types) < 0)
+ {
+ Py_DECREF(expecting);
+ return -1;
+ }
+ if (field_types != NULL) {
+ Py_ssize_t pos = 0;
+ PyObject *field_name, *field_type;
+ while (PyDict_Next(field_types, &pos, &field_name, &field_type)) {
+ if (_PyUnion_Check(field_type)) {
+ // optional field
+ if (PySet_Discard(expecting, field_name) < 0) {
+ Py_DECREF(expecting);
+ Py_DECREF(field_types);
+ return -1;
+ }
+ }
+ }
+ Py_DECREF(field_types);
+ }
+
// Now 'expecting' contains the fields or attributes
// that would not be filled inside ast_type_replace().
Py_ssize_t m = PySet_GET_SIZE(expecting);