mirror of https://github.com/python/cpython
gh-121332: Make AST node constructor check _attributes instead of hardcoding attributes (#121334)
This commit is contained in:
parent
44937d11a6
commit
58e8cf2bb6
|
@ -1386,14 +1386,6 @@ class CopyTests(unittest.TestCase):
|
||||||
self.assertEqual(node.y, 1)
|
self.assertEqual(node.y, 1)
|
||||||
|
|
||||||
y = object()
|
y = object()
|
||||||
# custom attributes are currently not supported and raise a warning
|
|
||||||
# because the allowed attributes are hard-coded !
|
|
||||||
msg = (
|
|
||||||
"MyNode.__init__ got an unexpected keyword argument 'y'. "
|
|
||||||
"Support for arbitrary keyword arguments is deprecated and "
|
|
||||||
"will be removed in Python 3.15"
|
|
||||||
)
|
|
||||||
with self.assertWarnsRegex(DeprecationWarning, re.escape(msg)):
|
|
||||||
repl = copy.replace(node, y=y)
|
repl = copy.replace(node, y=y)
|
||||||
# assert that there is no side-effect
|
# assert that there is no side-effect
|
||||||
self.assertEqual(node.x, 0)
|
self.assertEqual(node.x, 0)
|
||||||
|
@ -3250,6 +3242,18 @@ class ASTConstructorTests(unittest.TestCase):
|
||||||
obj = FieldsAndTypes(a=1)
|
obj = FieldsAndTypes(a=1)
|
||||||
self.assertEqual(obj.a, 1)
|
self.assertEqual(obj.a, 1)
|
||||||
|
|
||||||
|
def test_custom_attributes(self):
|
||||||
|
class MyAttrs(ast.AST):
|
||||||
|
_attributes = ("a", "b")
|
||||||
|
|
||||||
|
obj = MyAttrs(a=1, b=2)
|
||||||
|
self.assertEqual(obj.a, 1)
|
||||||
|
self.assertEqual(obj.b, 2)
|
||||||
|
|
||||||
|
with self.assertWarnsRegex(DeprecationWarning,
|
||||||
|
r"MyAttrs.__init__ got an unexpected keyword argument 'c'."):
|
||||||
|
obj = MyAttrs(c=3)
|
||||||
|
|
||||||
def test_fields_and_types_no_default(self):
|
def test_fields_and_types_no_default(self):
|
||||||
class FieldsAndTypesNoDefault(ast.AST):
|
class FieldsAndTypesNoDefault(ast.AST):
|
||||||
_fields = ('a',)
|
_fields = ('a',)
|
||||||
|
|
|
@ -0,0 +1,4 @@
|
||||||
|
Fix constructor of :mod:`ast` nodes with custom ``_attributes``. Previously,
|
||||||
|
passing custom attributes would raise a :py:exc:`DeprecationWarning`. Passing
|
||||||
|
arguments to the constructor that are not in ``_fields`` or ``_attributes``
|
||||||
|
remains deprecated. Patch by Jelle Zijlstra.
|
|
@ -880,7 +880,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
|
|
||||||
Py_ssize_t i, numfields = 0;
|
Py_ssize_t i, numfields = 0;
|
||||||
int res = -1;
|
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) {
|
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
@ -947,12 +947,21 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (
|
else {
|
||||||
PyUnicode_CompareWithASCIIString(key, "lineno") != 0 &&
|
// Lazily initialize "attributes"
|
||||||
PyUnicode_CompareWithASCIIString(key, "col_offset") != 0 &&
|
if (attributes == NULL) {
|
||||||
PyUnicode_CompareWithASCIIString(key, "end_lineno") != 0 &&
|
attributes = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_attributes);
|
||||||
PyUnicode_CompareWithASCIIString(key, "end_col_offset") != 0
|
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(
|
if (PyErr_WarnFormat(
|
||||||
PyExc_DeprecationWarning, 1,
|
PyExc_DeprecationWarning, 1,
|
||||||
"%.400s.__init__ got an unexpected keyword argument '%U'. "
|
"%.400s.__init__ got an unexpected keyword argument '%U'. "
|
||||||
|
@ -964,6 +973,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
res = PyObject_SetAttr(self, key, value);
|
res = PyObject_SetAttr(self, key, value);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
@ -1045,6 +1055,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
Py_DECREF(field_types);
|
Py_DECREF(field_types);
|
||||||
}
|
}
|
||||||
cleanup:
|
cleanup:
|
||||||
|
Py_XDECREF(attributes);
|
||||||
Py_XDECREF(fields);
|
Py_XDECREF(fields);
|
||||||
Py_XDECREF(remaining_fields);
|
Py_XDECREF(remaining_fields);
|
||||||
return res;
|
return res;
|
||||||
|
|
|
@ -5081,7 +5081,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
|
|
||||||
Py_ssize_t i, numfields = 0;
|
Py_ssize_t i, numfields = 0;
|
||||||
int res = -1;
|
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) {
|
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
@ -5148,12 +5148,21 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else if (
|
else {
|
||||||
PyUnicode_CompareWithASCIIString(key, "lineno") != 0 &&
|
// Lazily initialize "attributes"
|
||||||
PyUnicode_CompareWithASCIIString(key, "col_offset") != 0 &&
|
if (attributes == NULL) {
|
||||||
PyUnicode_CompareWithASCIIString(key, "end_lineno") != 0 &&
|
attributes = PyObject_GetAttr((PyObject*)Py_TYPE(self), state->_attributes);
|
||||||
PyUnicode_CompareWithASCIIString(key, "end_col_offset") != 0
|
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(
|
if (PyErr_WarnFormat(
|
||||||
PyExc_DeprecationWarning, 1,
|
PyExc_DeprecationWarning, 1,
|
||||||
"%.400s.__init__ got an unexpected keyword argument '%U'. "
|
"%.400s.__init__ got an unexpected keyword argument '%U'. "
|
||||||
|
@ -5165,6 +5174,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
res = PyObject_SetAttr(self, key, value);
|
res = PyObject_SetAttr(self, key, value);
|
||||||
if (res < 0) {
|
if (res < 0) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
@ -5246,6 +5256,7 @@ ast_type_init(PyObject *self, PyObject *args, PyObject *kw)
|
||||||
Py_DECREF(field_types);
|
Py_DECREF(field_types);
|
||||||
}
|
}
|
||||||
cleanup:
|
cleanup:
|
||||||
|
Py_XDECREF(attributes);
|
||||||
Py_XDECREF(fields);
|
Py_XDECREF(fields);
|
||||||
Py_XDECREF(remaining_fields);
|
Py_XDECREF(remaining_fields);
|
||||||
return res;
|
return res;
|
||||||
|
|
Loading…
Reference in New Issue