-
-
Notifications
You must be signed in to change notification settings - Fork 32.5k
gh-105858: Improve AST node constructors #105880
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
b1b73d9
gh-105858: Improve AST node constructors
JelleZijlstra d417cf3
Merge remote-tracking branch 'upstream/main' into astcons
JelleZijlstra c74740a
Fix subclasses
JelleZijlstra 05d5569
Fix most deprecationwarnings in test_ast
JelleZijlstra f4b14ac
Merge remote-tracking branch 'upstream/main' into astcons
JelleZijlstra 0efcdf3
Fix renamed function
JelleZijlstra f941696
Fix pickling; fix a refleak
JelleZijlstra 4c42748
Fix other refleak
JelleZijlstra 1a9b872
Merge branch 'main' into astcons
JelleZijlstra db16cc7
Merge branch 'main' into astcons
JelleZijlstra 6bf677b
Update Parser/asdl_c.py
JelleZijlstra a2442b6
fix merge
JelleZijlstra 5a1d37a
Expand NEWS
JelleZijlstra c4627a1
Merge remote-tracking branch 'upstream/main' into astcons
JelleZijlstra 1f85438
docs
JelleZijlstra ff0cc67
Apply suggestions from code review
JelleZijlstra 54cf55f
Merge remote-tracking branch 'upstream/main' into astcons
JelleZijlstra 4bb0656
Reword Whats New note (based on suggestion by Alex Waygood)
JelleZijlstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fix pickling; fix a refleak
- Loading branch information
commit f941696deb85116c8333efcc9f2b5189fd78d812
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1020,6 +1020,8 @@ def visitModule(self, mod): | |
} | ||
} | ||
} | ||
Py_DECREF(remaining_list); | ||
Py_DECREF(field_types); | ||
} | ||
cleanup: | ||
Py_XDECREF(fields); | ||
|
@@ -1041,14 +1043,75 @@ def visitModule(self, mod): | |
return NULL; | ||
} | ||
|
||
PyObject *dict; | ||
PyObject *dict = NULL, *fields = NULL, *remaining_fields = NULL, | ||
*positional_args = NULL; | ||
if (PyObject_GetOptionalAttr(self, state->__dict__, &dict) < 0) { | ||
return NULL; | ||
} | ||
PyObject *result = NULL; | ||
if (dict) { | ||
return Py_BuildValue("O()N", Py_TYPE(self), dict); | ||
// Serialize the fields as positional args if possible, because if we | ||
// serialize them as a dict, during unpickling they are set only *after* | ||
// the object is constructed, which will now trigger a DeprecationWarning | ||
// if the AST type has required fields. | ||
PyObject *fields = NULL; | ||
if (PyObject_GetOptionalAttr((PyObject*)Py_TYPE(self), state->_fields, &fields) < 0) { | ||
goto cleanup; | ||
} | ||
if (fields) { | ||
Py_ssize_t numfields = PySequence_Size(fields); | ||
if (numfields == -1) { | ||
Py_DECREF(dict); | ||
goto cleanup; | ||
} | ||
PyObject *remaining_dict = PyDict_Copy(dict); | ||
Py_DECREF(dict); | ||
if (!remaining_dict) { | ||
goto cleanup; | ||
} | ||
PyObject *positional_args = PyList_New(0); | ||
if (!positional_args) { | ||
goto cleanup; | ||
} | ||
for (Py_ssize_t i = 0; i < numfields; i++) { | ||
PyObject *name = PySequence_GetItem(fields, i); | ||
if (!name) { | ||
goto cleanup; | ||
} | ||
PyObject *value = PyDict_GetItemWithError(remaining_dict, name); | ||
if (!value) { | ||
if (PyErr_Occurred()) { | ||
goto cleanup; | ||
} | ||
break; | ||
} | ||
if (PyList_Append(positional_args, value) < 0) { | ||
goto cleanup; | ||
} | ||
if (PyDict_DelItem(remaining_dict, name) < 0) { | ||
goto cleanup; | ||
} | ||
Comment on lines
+1081
to
+1092
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There are leaks of |
||
Py_DECREF(name); | ||
} | ||
PyObject *args_tuple = PyList_AsTuple(positional_args); | ||
if (!args_tuple) { | ||
goto cleanup; | ||
} | ||
result = Py_BuildValue("ONN", Py_TYPE(self), args_tuple, | ||
remaining_dict); | ||
} | ||
else { | ||
result = Py_BuildValue("O()N", Py_TYPE(self), dict); | ||
} | ||
} | ||
else { | ||
result = Py_BuildValue("O()", Py_TYPE(self)); | ||
} | ||
return Py_BuildValue("O()", Py_TYPE(self)); | ||
cleanup: | ||
Py_XDECREF(fields); | ||
Py_XDECREF(remaining_fields); | ||
Py_XDECREF(positional_args); | ||
return result; | ||
} | ||
|
||
static PyMemberDef ast_type_members[] = { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do AST types even need their own custom reduce function? Shouldn't
object.__reduce__
(which IIRC uses__new__
to safely create an empty instance, then updates its__dict__
) work fine for AST objects as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
__new__
call will trigger DeprecationWarnings. If we don't have our own reducer, unpickling will essentially donode = ast.FunctionDef(); node.__dict__.update(...)
, and the first line raises warnings because we don't set the requiredname
field.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why would the
__new__
call raise deprecation warnings? Aren't the deprecation warnings in__init__
, not__new__
? That's whyobject.reduce()
uses__new__
, so that it can bypass whatever is happening in custom__init__
methods (otherwise lots of custom classes would fail to unpickle by default). Soobject.reduce()
is not equivalent tonode = ast.FunctionDef(); node.__dict__.update(...)
-- it's more likenode = ast.FunctionDef.__new__(ast.FunctionDef); node.__dict__.update(...)
, which is a critical difference. So it seems to me like using the default reducer should work fine here, and not require all this extra work. But I could definitely be missing something; there's probably some reason these nodes had a custom reduce() in the first place.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see what you mean. I tried commenting out the
__reduce__
definition, and lots of tests fail with:I don't entirely understand what
_reduce_ex
is doing there, but it picksast.AST
as the base, which fails.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting. Well, I might have just nerd-sniped myself into digging into this more later to understand what's happening, but I don't think it should block the PR; your added code to use positional args works fine.