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 8 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
8 changes: 6 additions & 2 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,12 @@ Improved Modules
ast
---

Added :func:`ast.compare` for comparing two ASTs.
(Contributed by Batuhan Taskaya and Jeremy Hylton in :issue:`15987`.)
* Added :func:`ast.compare` for comparing two ASTs.
(Contributed by Batuhan Taskaya and Jeremy Hylton in :issue:`15987`.)

* Add support for :func:`copy.replace` for AST nodes.

(Contributed by Bénédikt Tran in :gh:`121141`.)

os
--
Expand Down
19 changes: 15 additions & 4 deletions Lib/ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,8 @@ def compare(
might differ in whitespace or similar details.
"""

sentinel = object() # handle the possibility of a missing attribute/field

def _compare(a, b):
# Compare two fields on an AST object, which may themselves be
# AST objects, lists of AST objects, or primitive ASDL types
Expand Down Expand Up @@ -449,8 +451,14 @@ def _compare_fields(a, b):
if a._fields != b._fields:
return False
for field in a._fields:
a_field = getattr(a, field)
b_field = getattr(b, field)
a_field = getattr(a, field, sentinel)
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
if a_field is sentinel or b_field is sentinel:
# one of the node is missing a field
return False
picnixz marked this conversation as resolved.
Show resolved Hide resolved
if not _compare(a_field, b_field):
return False
else:
Expand All @@ -461,8 +469,11 @@ def _compare_attributes(a, b):
return False
# Attributes are always ints.
for attr in a._attributes:
a_attr = getattr(a, attr)
b_attr = getattr(b, attr)
a_attr = getattr(a, attr, sentinel)
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
if a_attr != b_attr:
return False
else:
Expand Down
224 changes: 224 additions & 0 deletions Lib/test/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,23 @@ def test_none_checks(self) -> None:
class CopyTests(unittest.TestCase):
"""Test copying and pickling AST nodes."""

@staticmethod
def iter_ast_classes():
"""Iterate over the subclasses of ast.AST recursively.

This excludes the special class ast.Index since its constructor
returns an integer.
"""
def do(cls):
if cls is ast.Index:
return

yield cls
for sub in cls.__subclasses__():
yield from do(sub)

yield from do(ast.AST)

def test_pickling(self):
import pickle

Expand Down Expand Up @@ -1199,6 +1216,213 @@ def test_copy_with_parents(self):
)):
self.assertEqual(to_tuple(child.parent), to_tuple(node))

def test_replace_interface(self):
for klass in self.iter_ast_classes():
with self.subTest(klass=klass):
self.assertTrue(hasattr(klass, '__replace__'))

fields = set(klass._fields)
with self.subTest(klass=klass, fields=fields):
node = klass(**dict.fromkeys(fields))
# forbid positional arguments in replace()
self.assertRaises(TypeError, copy.replace, node, 1)
self.assertRaises(TypeError, node.__replace__, 1)

def test_replace_native(self):
for klass in self.iter_ast_classes():
fields = set(klass._fields)
picnixz marked this conversation as resolved.
Show resolved Hide resolved
with self.subTest(klass=klass, fields=fields):
# use of object() to ensure that '==' and 'is'
# behave similarly in ast.compare(node, repl)
old_value, new_value = object(), object()

# check shallow copy
node = klass(**dict.fromkeys(fields, old_value))
picnixz marked this conversation as resolved.
Show resolved Hide resolved
repl = copy.replace(node)
self.assertTrue(ast.compare(node, repl, compare_attributes=True))

for field in fields:
node = klass(**dict.fromkeys(fields, old_value))
# only change a single field
repl = copy.replace(node, **{field: new_value})
for f in fields:
# assert that there is no side-effect
self.assertIs(getattr(node, f), old_value)
# check the changes
if f != field:
self.assertIs(getattr(repl, f), old_value)
else:
self.assertIs(getattr(repl, f), new_value)

self.assertFalse(ast.compare(node, repl, compare_attributes=True))

def test_replace_accept_known_class_fields(self):
nid, ctx = object(), object()

node = ast.Name(id=nid, ctx=ctx)
self.assertIs(node.id, nid)
self.assertIs(node.ctx, ctx)

new_nid = object()
repl = copy.replace(node, id=new_nid)
# assert that there is no side-effect
self.assertIs(node.id, nid)
self.assertIs(node.ctx, ctx)
# assert the changes
self.assertIs(repl.id, new_nid)
self.assertIs(repl.ctx, node.ctx) # no changes

def test_replace_accept_known_class_attributes(self):
node = ast.parse('x').body[0].value
self.assertEqual(node.id, 'x')
self.assertEqual(node.lineno, 1)

# constructor allows any type so replace() should do the same
lineno = object()
repl = copy.replace(node, lineno=lineno)
# assert that there is no side-effect
self.assertEqual(node.lineno, 1)
# check the changes
self.assertEqual(repl.id, node.id)
self.assertEqual(repl.ctx, node.ctx)
self.assertEqual(repl.lineno, lineno)

_, _, state = node.__reduce__()
self.assertEqual(state['id'], 'x')
self.assertEqual(state['ctx'], node.ctx)
self.assertEqual(state['lineno'], 1)

_, _, state = repl.__reduce__()
self.assertEqual(state['id'], 'x')
self.assertEqual(state['ctx'], node.ctx)
self.assertEqual(state['lineno'], lineno)

def test_replace_accept_known_custom_class_fields(self):
class MyNode(ast.AST):
_fields = ('name', 'data',)
__annotations__ = {'name': str, 'data': object}
__match_args__ = ('name', 'data',)

name, data = 'name', object()

node = MyNode(name, data)
self.assertIs(node.name, name)
self.assertIs(node.data, data)
# check shallow copy
repl = copy.replace(node)
# assert that there is no side-effect
self.assertIs(node.name, name)
self.assertIs(node.data, data)
# check the shallow copy
self.assertIs(repl.name, name)
self.assertIs(repl.data, data)

node = MyNode(name, data)
repl_data = object()
# replace custom but known field
repl = copy.replace(node, data=repl_data)
# assert that there is no side-effect
self.assertIs(node.name, name)
self.assertIs(node.data, data)
# check the changes
self.assertIs(repl.name, node.name)
self.assertIs(repl.data, repl_data)

def test_replace_reject_known_custom_class_attributes(self):
class MyNode(ast.AST):
x = 0
y = 1
_attributes = ('x', 'y',)

node = MyNode()
self.assertEqual(node.x, 0)
self.assertEqual(node.y, 1)

y = object()
# Currently, we emit a warning (and thus the changes are committed)
picnixz marked this conversation as resolved.
Show resolved Hide resolved
# and only support hard-coded attributes 'lineno', 'col_offset',
# 'end_lineno', and 'end_col_offset'.
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)
# assert that there is no side-effect
self.assertEqual(node.x, 0)
self.assertEqual(node.y, 1)
# check the changes ('repl' will not be available in 3.15+)
self.assertEqual(repl.x, 0)
self.assertEqual(repl.y, y)

def test_replace_ignore_known_custom_instance_fields(self):
node = ast.parse('x').body[0].value
node.extra = extra = object() # add instance 'extra' field
context = node.ctx

# assert initial values
self.assertIs(node.id, 'x')
self.assertIs(node.ctx, context)
self.assertIs(node.extra, extra)
# shallow copy, but drops extra fields
repl = copy.replace(node)
# assert that there is no side-effect
self.assertIs(node.id, 'x')
self.assertIs(node.ctx, context)
self.assertIs(node.extra, extra)
# verify that the 'extra' field is not kept
self.assertIs(repl.id, 'x')
self.assertIs(repl.ctx, context)
self.assertRaises(AttributeError, getattr, repl, 'extra')

# change known native field
repl = copy.replace(node, id='y')
# assert that there is no side-effect
self.assertIs(node.id, 'x')
self.assertIs(node.ctx, context)
self.assertIs(node.extra, extra)
# verify that the 'extra' field is not kept
self.assertIs(repl.id, 'y')
self.assertIs(repl.ctx, context)
self.assertRaises(AttributeError, getattr, repl, 'extra')

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
context = node.ctx

# explicit rejection of known instance fields
self.assertTrue(hasattr(node, 'extra'))
msg = "Name.__init__ got an unexpected keyword argument 'extra'."
with self.assertWarnsRegex(DeprecationWarning, re.escape(msg)):
repl = copy.replace(node, extra=1)
# assert that there is no side-effect
self.assertIs(node.id, 'x')
self.assertIs(node.ctx, context)
self.assertIs(node.extra, extra)
# check the changes ('repl' will not be available in 3.15+)
self.assertIs(repl.id, 'x')
self.assertIs(repl.ctx, context)
self.assertIs(repl.extra, 1)

def test_replace_reject_unknown_instance_fields(self):
node = ast.parse('x').body[0].value
context = node.ctx

# explicit rejection of unknown extra fields
self.assertRaises(AttributeError, getattr, node, 'unknown')
msg = "Name.__init__ got an unexpected keyword argument 'unknown'."
with self.assertWarnsRegex(DeprecationWarning, re.escape(msg)):
repl = copy.replace(node, unknown=1)
# assert that there is no side-effect
self.assertIs(node.id, 'x')
self.assertIs(node.ctx, context)
self.assertRaises(AttributeError, getattr, node, 'unknown')
# check the changes ('repl' will not be available in 3.15+)
self.assertIs(repl.id, 'x')
self.assertIs(repl.ctx, context)
self.assertIs(repl.unknown, 1)

class ASTHelpers_Test(unittest.TestCase):
maxDiff = None
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add support for :func:`copy.replace` to AST nodes. Patch by Bénédikt Tran.
81 changes: 81 additions & 0 deletions Parser/asdl_c.py
Original file line number Diff line number Diff line change
Expand Up @@ -1132,13 +1132,93 @@ def visitModule(self, mod):
return result;
}

/* copy.replace() support (shallow copy) */
static PyObject *
ast_type_replace(PyObject *self, PyObject *args, PyObject *kwargs) {
if (!_PyArg_NoPositional("__replace__", args)) {
return NULL;
}

struct ast_state *state = get_ast_state();
if (state == NULL) {
return NULL;
}

PyObject *result = NULL;
// known AST class fields
PyObject *fields = NULL;
// current instance dictionary
PyObject *__dict__ = NULL;
picnixz marked this conversation as resolved.
Show resolved Hide resolved
// 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) {
goto cleanup;
}
if (PyObject_GetOptionalAttr(self, state->__dict__, &__dict__) < 0) {
goto cleanup;
}
if (!(empty_tuple = PyTuple_New(0))) {
goto cleanup;
}
if (!(payload = PyDict_New())) {
goto cleanup;
}
if (fields) {
Py_ssize_t numfields = PySequence_Size(fields);
if (numfields == -1) {
goto cleanup;
}
for (Py_ssize_t i = 0; i < numfields; i++) {
if (!(key = PySequence_GetItem(fields, i))) {
picnixz marked this conversation as resolved.
Show resolved Hide resolved
picnixz marked this conversation as resolved.
Show resolved Hide resolved
goto cleanup;
}
if (PyDict_GetItemRef(__dict__, key, &value) < 0) {
picnixz marked this conversation as resolved.
Show resolved Hide resolved
goto cleanup;
}
if (!value) {
break;
}
picnixz marked this conversation as resolved.
Show resolved Hide resolved
if (PyDict_SetItem(payload, key, value) < 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);
goto cleanup;
}
picnixz marked this conversation as resolved.
Show resolved Hide resolved
cleanup:
Py_XDECREF(value);
Py_XDECREF(key);
Py_XDECREF(payload);
Py_XDECREF(empty_tuple);
Py_XDECREF(__dict__);
Py_XDECREF(fields);
return result;
}

static PyMemberDef ast_type_members[] = {
{"__dictoffset__", Py_T_PYSSIZET, offsetof(AST_object, dict), Py_READONLY},
{NULL} /* Sentinel */
};

static PyMethodDef ast_type_methods[] = {
{"__reduce__", ast_type_reduce, METH_NOARGS, NULL},
{"__replace__", _PyCFunction_CAST(ast_type_replace), METH_VARARGS | METH_KEYWORDS,
PyDoc_STR("__replace__($self, /, **fields)\\n--\\n\\n"
"Return a copy of the AST node with new values "
"for the specified fields.")},
{NULL}
};

Expand Down Expand Up @@ -1773,6 +1853,7 @@ def generate_module_def(mod, metadata, f, internal_h):
#include "pycore_ceval.h" // _Py_EnterRecursiveCall
#include "pycore_lock.h" // _PyOnceFlag
#include "pycore_interp.h" // _PyInterpreterState.ast
#include "pycore_modsupport.h" // _PyArg_NoPositional()
#include "pycore_pystate.h" // _PyInterpreterState_GET()
#include "pycore_unionobject.h" // _Py_union_type_or
#include "structmember.h"
Expand Down
Loading
Loading