-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-119180: Set the name of the param to __annotate__ to "format" #124730
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
Changes from 5 commits
02770d7
68324ef
2dae0d0
c51b8d0
90a35be
b6cbb87
c30a54d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -675,6 +675,34 @@ codegen_leave_annotations_scope(compiler *c, location loc, | |
| ADDOP_I(c, loc, BUILD_MAP, annotations_len); | ||
| ADDOP_IN_SCOPE(c, loc, RETURN_VALUE); | ||
| PyCodeObject *co = _PyCompile_OptimizeAndAssemble(c, 1); | ||
|
|
||
| // We want the parameter to __annotate__ to be named "format" in the | ||
| // signature shown by inspect.signature(), but we need to use a | ||
| // different name (.format) in the symtable so that if the name | ||
| // "format" appears in the annotations, it does not get clobbered | ||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // by this name. | ||
JelleZijlstra marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| // This code is essentially: | ||
| // co->co_localsplusnames = ("format", *co->co_localsplusnames[1:]) | ||
| const Py_ssize_t size = PyObject_Size(co->co_localsplusnames); | ||
| if (size == -1) { | ||
| return ERROR; | ||
| } | ||
| PyObject *new_names = PyTuple_New(size); | ||
|
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. This isn't a critique of your approach, but--I'm surprised you needed to go to all this effort. Why was it necessary to make a new tuple, write the new value for index 0, copy over the other values, and release the reference to the old tuple? I'm assuming the reference count of What am I missing? 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. While it is possible to mutate tuples in C code, it feels riskier. For example, maybe we'll make changes in the future that rely on tuples being immutable. 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. I assure you, this is a long-standing CPython idiom. We've relied on "if there's only one reference to an object, and you own it, you may modify the object however you like" for decades now. For fun I made a survey of CPython, literally examining every instance of (I'll append the list of such sites at the bottom of this comment.) Clearly these existing sites are optimizations; instead of destroying the old tuple and creating a fresh one, they're just reusing the existing tuple. They have a harder time of it because generally the tuple has been shown to the interpreter. In our case, we have a freshly compiled code object that hasn't been shown to the interpreter. So there's no chance anyone else has taken any references yet. If we did change CPython so this was no longer viable, the developer making that change would have to fix all the sites I listed below, which they would probably find the same way I did--looking for all places where people set things in tuples. I don't think modifying the tuple directly would trip up such a future developer. So, yeah, I really do think it'd be safe to modify the tuple in-place. Just to be totally safe, I'd check the reference count was 1 and raise if it wasn't. (It'd only happen if someone was hacking on I don't actually mind you doing it the hard way--we can ship it like this. It just seems needless. We have a longstanding idiom that lets us skip the laborious approach you took. But I'm not gonna fight you about it. Places where CPython modifies tuples in-place:
Five places in p.s. you should see the if-only-one-reference-modify-the-object shenanigans in the Unicode object! 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. See #127058 where @markshannon proposes to deprecate existing tuple-mutation shenanigans. That strengthens my conviction that we shouldn't introduce a new tuple mutation here. |
||
| if (new_names == NULL) { | ||
| return ERROR; | ||
| } | ||
| PyTuple_SET_ITEM(new_names, 0, Py_NewRef(&_Py_ID(format))); | ||
| for (int i = 1; i < size; i++) { | ||
| PyObject *item = PyTuple_GetItem(co->co_localsplusnames, i); | ||
| if (item == NULL) { | ||
| Py_DECREF(new_names); | ||
| return ERROR; | ||
| } | ||
| Py_INCREF(item); | ||
| PyTuple_SET_ITEM(new_names, i, item); | ||
| } | ||
| Py_SETREF(co->co_localsplusnames, new_names); | ||
|
|
||
| _PyCompile_ExitScope(c); | ||
| if (co == NULL) { | ||
| return ERROR; | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.