Skip to content

Commit 42b2c9d

Browse files
gh-120108: Fix deepcopying of AST trees with .parent attributes (#120114)
1 parent ead6765 commit 42b2c9d

File tree

4 files changed

+105
-44
lines changed

4 files changed

+105
-44
lines changed

Lib/test/test_ast.py

Lines changed: 75 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ast
22
import builtins
3+
import copy
34
import dis
45
import enum
56
import os
@@ -20,7 +21,7 @@
2021
from test.support.ast_helper import ASTTestMixin
2122

2223
def to_tuple(t):
23-
if t is None or isinstance(t, (str, int, complex)) or t is Ellipsis:
24+
if t is None or isinstance(t, (str, int, complex, float, bytes)) or t is Ellipsis:
2425
return t
2526
elif isinstance(t, list):
2627
return [to_tuple(e) for e in t]
@@ -775,15 +776,6 @@ def test_no_fields(self):
775776
x = ast.Sub()
776777
self.assertEqual(x._fields, ())
777778

778-
def test_pickling(self):
779-
import pickle
780-
781-
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
782-
for ast in (compile(i, "?", "exec", 0x400) for i in exec_tests):
783-
with self.subTest(ast=ast, protocol=protocol):
784-
ast2 = pickle.loads(pickle.dumps(ast, protocol))
785-
self.assertEqual(to_tuple(ast2), to_tuple(ast))
786-
787779
def test_invalid_sum(self):
788780
pos = dict(lineno=2, col_offset=3)
789781
m = ast.Module([ast.Expr(ast.expr(**pos), **pos)], [])
@@ -1135,6 +1127,79 @@ def test_none_checks(self) -> None:
11351127
self.assert_none_check(node, attr, source)
11361128

11371129

1130+
class CopyTests(unittest.TestCase):
1131+
"""Test copying and pickling AST nodes."""
1132+
1133+
def test_pickling(self):
1134+
import pickle
1135+
1136+
for protocol in range(pickle.HIGHEST_PROTOCOL + 1):
1137+
for code in exec_tests:
1138+
with self.subTest(code=code, protocol=protocol):
1139+
tree = compile(code, "?", "exec", 0x400)
1140+
ast2 = pickle.loads(pickle.dumps(tree, protocol))
1141+
self.assertEqual(to_tuple(ast2), to_tuple(tree))
1142+
1143+
def test_copy_with_parents(self):
1144+
# gh-120108
1145+
code = """
1146+
('',)
1147+
while i < n:
1148+
if ch == '':
1149+
ch = format[i]
1150+
if ch == '':
1151+
if freplace is None:
1152+
'' % getattr(object)
1153+
elif ch == '':
1154+
if zreplace is None:
1155+
if hasattr:
1156+
offset = object.utcoffset()
1157+
if offset is not None:
1158+
if offset.days < 0:
1159+
offset = -offset
1160+
h = divmod(timedelta(hours=0))
1161+
if u:
1162+
zreplace = '' % (sign,)
1163+
elif s:
1164+
zreplace = '' % (sign,)
1165+
else:
1166+
zreplace = '' % (sign,)
1167+
elif ch == '':
1168+
if Zreplace is None:
1169+
Zreplace = ''
1170+
if hasattr(object):
1171+
s = object.tzname()
1172+
if s is not None:
1173+
Zreplace = s.replace('')
1174+
newformat.append(Zreplace)
1175+
else:
1176+
push('')
1177+
else:
1178+
push(ch)
1179+
1180+
"""
1181+
tree = ast.parse(textwrap.dedent(code))
1182+
for node in ast.walk(tree):
1183+
for child in ast.iter_child_nodes(node):
1184+
child.parent = node
1185+
try:
1186+
with support.infinite_recursion(200):
1187+
tree2 = copy.deepcopy(tree)
1188+
finally:
1189+
# Singletons like ast.Load() are shared; make sure we don't
1190+
# leave them mutated after this test.
1191+
for node in ast.walk(tree):
1192+
if hasattr(node, "parent"):
1193+
del node.parent
1194+
1195+
for node in ast.walk(tree2):
1196+
for child in ast.iter_child_nodes(node):
1197+
if hasattr(child, "parent") and not isinstance(child, (
1198+
ast.expr_context, ast.boolop, ast.unaryop, ast.cmpop, ast.operator,
1199+
)):
1200+
self.assertEqual(to_tuple(child.parent), to_tuple(node))
1201+
1202+
11381203
class ASTHelpers_Test(unittest.TestCase):
11391204
maxDiff = None
11401205

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix calling :func:`copy.deepcopy` on :mod:`ast` trees that have been
2+
modified to have references to parent nodes. Patch by Jelle Zijlstra.

Parser/asdl_c.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1064,17 +1064,22 @@ def visitModule(self, mod):
10641064
return NULL;
10651065
}
10661066
1067-
PyObject *dict = NULL, *fields = NULL, *remaining_fields = NULL,
1068-
*remaining_dict = NULL, *positional_args = NULL;
1067+
PyObject *dict = NULL, *fields = NULL, *positional_args = NULL;
10691068
if (PyObject_GetOptionalAttr(self, state->__dict__, &dict) < 0) {
10701069
return NULL;
10711070
}
10721071
PyObject *result = NULL;
10731072
if (dict) {
1074-
// Serialize the fields as positional args if possible, because if we
1075-
// serialize them as a dict, during unpickling they are set only *after*
1076-
// the object is constructed, which will now trigger a DeprecationWarning
1077-
// if the AST type has required fields.
1073+
// Unpickling (or copying) works as follows:
1074+
// - Construct the object with only positional arguments
1075+
// - Set the fields from the dict
1076+
// We have two constraints:
1077+
// - We must set all the required fields in the initial constructor call,
1078+
// or the unpickling or deepcopying of the object will trigger DeprecationWarnings.
1079+
// - We must not include child nodes in the positional args, because
1080+
// that may trigger runaway recursion during copying (gh-120108).
1081+
// To satisfy both constraints, we set all the fields to None in the
1082+
// initial list of positional args, and then set the fields from the dict.
10781083
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) {
10791084
goto cleanup;
10801085
}
@@ -1084,11 +1089,6 @@ def visitModule(self, mod):
10841089
Py_DECREF(dict);
10851090
goto cleanup;
10861091
}
1087-
remaining_dict = PyDict_Copy(dict);
1088-
Py_DECREF(dict);
1089-
if (!remaining_dict) {
1090-
goto cleanup;
1091-
}
10921092
positional_args = PyList_New(0);
10931093
if (!positional_args) {
10941094
goto cleanup;
@@ -1099,15 +1099,15 @@ def visitModule(self, mod):
10991099
goto cleanup;
11001100
}
11011101
PyObject *value;
1102-
int rc = PyDict_Pop(remaining_dict, name, &value);
1102+
int rc = PyDict_GetItemRef(dict, name, &value);
11031103
Py_DECREF(name);
11041104
if (rc < 0) {
11051105
goto cleanup;
11061106
}
11071107
if (!value) {
11081108
break;
11091109
}
1110-
rc = PyList_Append(positional_args, value);
1110+
rc = PyList_Append(positional_args, Py_None);
11111111
Py_DECREF(value);
11121112
if (rc < 0) {
11131113
goto cleanup;
@@ -1117,8 +1117,7 @@ def visitModule(self, mod):
11171117
if (!args_tuple) {
11181118
goto cleanup;
11191119
}
1120-
result = Py_BuildValue("ONO", Py_TYPE(self), args_tuple,
1121-
remaining_dict);
1120+
result = Py_BuildValue("ONN", Py_TYPE(self), args_tuple, dict);
11221121
}
11231122
else {
11241123
result = Py_BuildValue("O()N", Py_TYPE(self), dict);
@@ -1129,8 +1128,6 @@ def visitModule(self, mod):
11291128
}
11301129
cleanup:
11311130
Py_XDECREF(fields);
1132-
Py_XDECREF(remaining_fields);
1133-
Py_XDECREF(remaining_dict);
11341131
Py_XDECREF(positional_args);
11351132
return result;
11361133
}

Python/Python-ast.c

Lines changed: 14 additions & 17 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)