summaryrefslogtreecommitdiffstats
path: root/Parser
diff options
context:
space:
mode:
authorJelle Zijlstra <jelle.zijlstra@gmail.com>2024-07-11 14:34:53 (GMT)
committerGitHub <noreply@github.com>2024-07-11 14:34:53 (GMT)
commit58e8cf2bb61f82df9eabd1209fe5e3d146e4c8cd (patch)
tree8f84a55655b0c4738a68e92c7fa392c6414aaa2c /Parser
parent44937d11a6a045a624918db78aa36e715ffabcd4 (diff)
downloadcpython-58e8cf2bb61f82df9eabd1209fe5e3d146e4c8cd.zip
cpython-58e8cf2bb61f82df9eabd1209fe5e3d146e4c8cd.tar.gz
cpython-58e8cf2bb61f82df9eabd1209fe5e3d146e4c8cd.tar.bz2
gh-121332: Make AST node constructor check _attributes instead of hardcoding attributes (#121334)
Diffstat (limited to 'Parser')
-rwxr-xr-xParser/asdl_c.py39
1 files changed, 25 insertions, 14 deletions
diff --git a/Parser/asdl_c.py b/Parser/asdl_c.py
index f366780..e6867f1 100755
--- a/Parser/asdl_c.py
+++ b/Parser/asdl_c.py
@@ -880,7 +880,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
Py_ssize_t i, numfields = 0;
int res = -1;
- PyObject *key, *value, *fields, *remaining_fields = NULL;
+ PyObject *key, *value, *fields, *attributes = NULL, *remaining_fields = NULL;
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
goto cleanup;
}
@@ -947,22 +947,32 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
goto cleanup;
}
}
- else if (
- PyUnicode_CompareWithASCIIString(key, "lineno") != 0 &&
- PyUnicode_CompareWithASCIIString(key, "col_offset") != 0 &&
- PyUnicode_CompareWithASCIIString(key, "end_lineno") != 0 &&
- PyUnicode_CompareWithASCIIString(key, "end_col_offset") != 0
- ) {
- if (PyErr_WarnFormat(
- PyExc_DeprecationWarning, 1,
- "%.400s.__init__ got an unexpected keyword argument '%U'. "
- "Support for arbitrary keyword arguments is deprecated "
- "and will be removed in Python 3.15.",
- Py_TYPE(self)->tp_name, key
- ) < 0) {
+ else {
+ // Lazily initialize "attributes"
+ if (attributes == NULL) {
+ attributes = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_attributes);
+ if (attributes == NULL) {
+ res = -1;
+ goto cleanup;
+ }
+ }
+ int contains = PySequence_Contains(attributes, key);
+ if (contains == -1) {
res = -1;
goto cleanup;
}
+ else if (contains == 0) {
+ if (PyErr_WarnFormat(
+ PyExc_DeprecationWarning, 1,
+ "%.400s.__init__ got an unexpected keyword argument '%U'. "
+ "Support for arbitrary keyword arguments is deprecated "
+ "and will be removed in Python 3.15.",
+ Py_TYPE(self)->tp_name, key
+ ) < 0) {
+ res = -1;
+ goto cleanup;
+ }
+ }
}
res = PyObject_SetAttr(self, key, value);
if (res < 0) {
@@ -1045,6 +1055,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
Py_DECREF(field_types);
}
cleanup:
+ Py_XDECREF(attributes);
Py_XDECREF(fields);
Py_XDECREF(remaining_fields);
return res;