Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-133783: Fix __replace__ on AST nodes for optional attributes#133797

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
JelleZijlstra merged 4 commits intopython:mainfromJelleZijlstra:ast-replace
May 10, 2025
Merged
Show file tree
Hide file tree
Changes fromall 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
9 changes: 9 additions & 0 deletionsLib/test/test_ast/test_ast.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1315,6 +1315,15 @@ def test_replace_reject_missing_field(self):
self.assertIs(repl.id,'y')
self.assertIs(repl.ctx,context)

deftest_replace_accept_missing_field_with_default(self):
node=ast.FunctionDef(name="foo",args=ast.arguments())
self.assertIs(node.returns,None)
self.assertEqual(node.decorator_list, [])
node2=copy.replace(node,name="bar")
self.assertEqual(node2.name,"bar")
self.assertIs(node2.returns,None)
self.assertEqual(node2.decorator_list, [])

deftest_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
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
Fix bug with applying:func:`copy.replace` to:mod:`ast` objects. Attributes
that default to ``None`` were incorrectly treated as required for manually
created AST nodes.
26 changes: 26 additions & 0 deletionsParser/asdl_c.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1244,6 +1244,32 @@ def visitModule(self, mod):
Py_DECREF(unused);
}
}

// Discard fields from 'expecting' that default to None
PyObject *field_types = NULL;
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self),
&_Py_ID(_field_types),
&field_types) < 0)
{
Py_DECREF(expecting);
return -1;
}
if (field_types != NULL) {
Py_ssize_t pos = 0;
PyObject *field_name, *field_type;
while (PyDict_Next(field_types, &pos, &field_name, &field_type)) {
if (_PyUnion_Check(field_type)) {
// optional field
if (PySet_Discard(expecting, field_name) < 0) {
Py_DECREF(expecting);
Py_DECREF(field_types);
return -1;
}
}
}
Py_DECREF(field_types);
}

// Now 'expecting' contains the fields or attributes
// that would not be filled inside ast_type_replace().
Py_ssize_t m = PySet_GET_SIZE(expecting);
Expand Down
26 changes: 26 additions & 0 deletionsPython/Python-ast.c
View file
Open in desktop

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

Loading

[8]ページ先頭

©2009-2025 Movatter.jp