Uh oh!
There was an error while loading. Please reload this page.
gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals - #125616
Conversation
gaogaotiantian
commented
Oct 16, 2024
@terryjreedy the only tests failed were Idle on MacOS-13, which is a bit weird. I can imagine changing |
Uh oh!
There was an error while loading. Please reload this page.
| if (default_value != NULL) { | ||
| return Py_XNewRef(default_value); | ||
| } else { | ||
| PyErr_Format(PyExc_KeyError, "'%R'", key); |
There was a problem hiding this comment.
A tiny optimization to consider (though, it's more complex--feel free to reject this):
| PyErr_Format(PyExc_KeyError, "'%R'", key); | |
| PyObject*repr=PyObject_Repr(key); | |
| if (repr==NULL) { | |
| returnNULL; | |
| } | |
| PyErr_SetObject(PyExc_KeyError, repr); |
There was a problem hiding this comment.
I just realized I could've used _PyErr_SetKeyError(key) like dictobject.c. I'll switch to that.
There was a problem hiding this comment.
Oh, that sounds better. I wasn't aware of that, TIL!
There was a problem hiding this comment.
So I switched to _PyErr_SetKeyError(key) when the key does not exist. However, for local variables cases, I think RuntimeError would fit better. One important reason was KeyError actually expects the "key" to be the explanation string, and giving a reason is a bit weird. Also in this case we have the key, we just can't remove it, so RuntimeError might be better.
Uh oh!
There was an error while loading. Please reload this page.
terryjreedy
commented
Oct 17, 2024
MacOS versions after 10 have various odd interactions with tkinter and python. Without a saved reference to the now-overwritten failing test, I cannot comment other than to note the following: The first call must result in the second, which I have no memory of. There is no direct test of either function. I'm glad you found better code that worked on major systems |
markshannon
commented
Oct 17, 2024
I think this needs backporting to 3.13, as that's the version mentioned in the issue |
markshannon
left a comment
There was a problem hiding this comment.
The exception types need changing from RuntimeError to KeyError.
Otherwise, looks good.
| } | ||
| if (i >= 0) { | ||
| if (value == NULL) { | ||
| PyErr_SetString(PyExc_RuntimeError, "cannot remove local variables from FrameLocalsProxy"); |
There was a problem hiding this comment.
I think this should be a KeyError.
Users will expect mapping types to raise a KeyError. https://docs.python.org/3/library/stdtypes.html#mapping-types-dict
| } | ||
| if (i >= 0) { | ||
| PyErr_SetString(PyExc_RuntimeError, "cannot remove local variables from FrameLocalsProxy"); |
When you're done making the requested changes, leave the comment: |
FTR, from this PR, the potential additions to PEP 667 would be: I think this is what we want. k=KeyError(name)
k.add_note("Cannot delete local variable") |
gaogaotiantian
commented
Oct 17, 2024
From the documentation, it explains
I think there's a distinctive difference here. The key is in the mapping, we can even read it, |
markshannon
commented
Oct 18, 2024
I appreciate I guess the question is do we want: try:
delf.f_locals["local_name"]
exceptKeyError:
...to handle the exception or not? If not, then I think
Other than that, the PR looks ready to merge. |
I think we need two separate exceptions because I don't know what they would do in the
|
I also agree For python/peps#3845, I've described this with prose rather than the equivalent Python code:
(and then linked back to this PR and the associated bug report from the "Implementation Notes" section) For the main docs, looking at https://docs.python.org/dev/reference/datamodel.html#frame-objects there's nothing that currently goes into that level of detail. I've filed a separate docs issue (#125731) suggesting adding a 4th subsection there that talks more about the write-through proxy behaviour. |
markshannon
commented
Oct 21, 2024
Ok, let's go with ValueError. |
Thanks @gaogaotiantian for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
Thanks @gaogaotiantian for the PR 🌮🎉.. I'm working now to backport this PR to: 3.13. |
…xtra locals (pythonGH-125616) (cherry picked from commit 5b7a872) Co-authored-by: Tian Gao <gaogaotiantian@hotmail.com>
GH-125797 is a backport of this pull request to the 3.13 branch. |
It's okay for us to pop/delete the keys that were added to
f_localsmanually, just ban the real fast variables.@ncoghlan not sure if there's any documentation that needs to be changed.
There are two types of exceptions that could be raised:
RuntimeErrorwhen users trying to remove a local variableKeyErrorwhen users trying to remove a key that does not exist