Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-121141: add support for copy.replace to AST nodes #121162

Merged
merged 25 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ def _compare_fields(a, b):
b_field = getattr(b, field, sentinel)
if a_field is sentinel and b_field is sentinel:
# both nodes are missing a field at runtime
return True
continue
if a_field is sentinel or b_field is sentinel:
# one of the node is missing a field
return False
Expand All @@ -473,7 +473,7 @@ def _compare_attributes(a, b):
b_attr = getattr(b, attr, sentinel)
if a_attr is sentinel and b_attr is sentinel:
# both nodes are missing an attribute at runtime
return True
continue
if a_attr != b_attr:
return False
else:
Expand Down
36 changes: 35 additions & 1 deletion Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1268,7 +1268,7 @@ def test_replace_accept_known_class_fields(self):
# assert that there is no side-effect
self.assertIs(node.id, nid)
self.assertIs(node.ctx, ctx)
# assert the changes
# check the changes
self.assertIs(repl.id, new_nid)
self.assertIs(repl.ctx, node.ctx) # no changes

Expand Down Expand Up @@ -1387,6 +1387,40 @@ def test_replace_ignore_known_custom_instance_fields(self):
self.assertIs(repl.ctx, context)
self.assertRaises(AttributeError, getattr, repl, 'extra')

def test_replace_reject_missing_field(self):
# case: warn if deleted field is not replaced
node = ast.parse('x').body[0].value
context = node.ctx
del node.id

self.assertRaises(AttributeError, getattr, node, 'id')
self.assertIs(node.ctx, context)
msg = (
"Name.__init__ missing 1 required positional argument: 'id'. "
"This will become an error in Python 3.15."
)
with self.assertWarnsRegex(DeprecationWarning, re.escape(msg)):
repl = copy.replace(node)
# assert that there is no side-effect
self.assertRaises(AttributeError, getattr, node, 'id')
self.assertIs(node.ctx, context)
self.assertRaises(AttributeError, getattr, repl, 'id')
self.assertIs(repl.ctx, context)

# case: do not warn if deleted field is replaced
node = ast.parse('x').body[0].value
context = node.ctx
del node.id

self.assertRaises(AttributeError, getattr, node, 'id')
self.assertIs(node.ctx, context)
repl = copy.replace(node, id='y')
# assert that there is no side-effect
self.assertRaises(AttributeError, getattr, node, 'id')
self.assertIs(node.ctx, context)
self.assertIs(repl.id, 'y')
self.assertIs(repl.ctx, context)

def test_replace_reject_known_custom_instance_fields_commits(self):
node = ast.parse('x').body[0].value
node.extra = extra = object() # add instance 'extra' field
Expand Down
52 changes: 30 additions & 22 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1148,62 +1148,70 @@ def visitModule(self, mod):
// known AST class fields
PyObject *fields = NULL;
// current instance dictionary
PyObject *__dict__ = NULL;
PyObject *dict = NULL;
// constructor positional and keyword arguments
PyObject *empty_tuple = NULL, *payload = NULL;
// for iterating over 'fields' and 'kwargs'
PyObject *key = NULL, *value = NULL;

PyTypeObject *type = Py_TYPE(self);
if (PyObject_GetOptionalAttr((PyObject *)type, state->_fields, &fields) < 0) {
PyObject *type = (PyObject *)Py_TYPE(self);
if (PyObject_GetOptionalAttr(type, state->_fields, &fields) < 0) {
goto cleanup;
}
if (PyObject_GetOptionalAttr(self, state->__dict__, &__dict__) < 0) {
if (PyObject_GetOptionalAttr(self, state->__dict__, &dict) < 0) {
goto cleanup;
}
if (!(empty_tuple = PyTuple_New(0))) {
empty_tuple = PyTuple_New(0);
if (empty_tuple == NULL) {
goto cleanup;
}
if (!(payload = PyDict_New())) {
payload = PyDict_New();
if (payload == NULL) {
goto cleanup;
}
if (fields) {
Py_ssize_t numfields = PySequence_Size(fields);
if (numfields == -1) {
goto cleanup;
}
int rc;
// for iterating over 'fields' and 'kwargs'
PyObject *key = NULL, *value = NULL;
picnixz marked this conversation as resolved.
Show resolved Hide resolved
for (Py_ssize_t i = 0; i < numfields; i++) {
if (!(key = PySequence_GetItem(fields, i))) {
key = PySequence_GetItem(fields, i);
if (key == NULL) {
goto cleanup;
}
if (PyDict_GetItemRef(__dict__, key, &value) < 0) {
rc = PyDict_GetItemRef(dict, key, &value);
if (rc < 0) {
Py_DECREF(key);
goto cleanup;
}
if (!value) {
break;
if (rc == 0) {
Py_DECREF(key);
Py_DECREF(value);
picnixz marked this conversation as resolved.
Show resolved Hide resolved
// If a field is not present at runtime, we hope that it will
// be explicitly given in 'kwargs'. If not, the constructor
// will issue a warning (which becomes an error in 3.15).
continue;
}
if (PyDict_SetItem(payload, key, value) < 0) {
rc = PyDict_SetItem(payload, key, value);
Py_DECREF(key);
Py_DECREF(value);
if (rc < 0) {
goto cleanup;
}
}
}
if (kwargs && PyDict_Update(payload, kwargs) < 0) {
goto cleanup;
}
if (!(result = type->tp_new(type, empty_tuple, payload))) {
Py_CLEAR(result);
goto cleanup;
}
if (ast_type_init(result, empty_tuple, payload) < 0) {
Py_CLEAR(result);
result = PyObject_Call(type, empty_tuple, payload);
if (result == NULL) {
goto cleanup;
}
cleanup:
Py_XDECREF(value);
Py_XDECREF(key);
Py_XDECREF(payload);
Py_XDECREF(empty_tuple);
Py_XDECREF(__dict__);
Py_XDECREF(dict);
Py_XDECREF(fields);
return result;
}
Expand Down
53 changes: 30 additions & 23 deletions Python/Python-ast.c

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading