-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals #125616
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 3 commits
1772804
0130a66
124aaa0
c8a8eb4
d16cfad
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 |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Allow ``FrameLocalsProxy`` to delete and pop if the key is not a fast variable. |
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -158,16 +158,16 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) | |||||||||||||
| _PyStackRef *fast = _PyFrame_GetLocalsArray(frame->f_frame); | ||||||||||||||
| PyCodeObject *co = _PyFrame_GetCode(frame->f_frame); | ||||||||||||||
|
|
||||||||||||||
| if (value == NULL) { | ||||||||||||||
| PyErr_SetString(PyExc_TypeError, "cannot remove variables from FrameLocalsProxy"); | ||||||||||||||
| return -1; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| int i = framelocalsproxy_getkeyindex(frame, key, false); | ||||||||||||||
| if (i == -2) { | ||||||||||||||
| return -1; | ||||||||||||||
| } | ||||||||||||||
| if (i >= 0) { | ||||||||||||||
| if (value == NULL) { | ||||||||||||||
| PyErr_SetString(PyExc_KeyError, "cannot remove local variables from FrameLocalsProxy"); | ||||||||||||||
| return -1; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| _Py_Executors_InvalidateDependency(PyInterpreterState_Get(), co, 1); | ||||||||||||||
|
|
||||||||||||||
| _PyLocals_Kind kind = _PyLocals_GetKind(co->co_localspluskinds, i); | ||||||||||||||
|
|
@@ -202,6 +202,10 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) | |||||||||||||
| PyObject *extra = frame->f_extra_locals; | ||||||||||||||
|
|
||||||||||||||
| if (extra == NULL) { | ||||||||||||||
| if (value == NULL) { | ||||||||||||||
| PyErr_Format(PyExc_KeyError, "'%R'", key); | ||||||||||||||
| return -1; | ||||||||||||||
| } | ||||||||||||||
| extra = PyDict_New(); | ||||||||||||||
| if (extra == NULL) { | ||||||||||||||
| return -1; | ||||||||||||||
|
|
@@ -211,7 +215,11 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) | |||||||||||||
|
|
||||||||||||||
| assert(PyDict_Check(extra)); | ||||||||||||||
|
|
||||||||||||||
| return PyDict_SetItem(extra, key, value); | ||||||||||||||
| if (value == NULL) { | ||||||||||||||
| return PyDict_DelItem(extra, key); | ||||||||||||||
| } else { | ||||||||||||||
| return PyDict_SetItem(extra, key, value); | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| static int | ||||||||||||||
|
|
@@ -676,6 +684,60 @@ framelocalsproxy_setdefault(PyObject* self, PyObject *const *args, Py_ssize_t na | |||||||||||||
| return result; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| static PyObject* | ||||||||||||||
| framelocalsproxy_pop(PyObject* self, PyObject *const *args, Py_ssize_t nargs) | ||||||||||||||
| { | ||||||||||||||
| if (nargs < 1 || nargs > 2) { | ||||||||||||||
| PyErr_SetString(PyExc_TypeError, "pop expected 1 or 2 arguments"); | ||||||||||||||
| return NULL; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| PyObject *key = args[0]; | ||||||||||||||
| PyObject *default_value = NULL; | ||||||||||||||
|
|
||||||||||||||
| if (nargs == 2) { | ||||||||||||||
| default_value = args[1]; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame; | ||||||||||||||
|
|
||||||||||||||
| int i = framelocalsproxy_getkeyindex(frame, key, false); | ||||||||||||||
| if (i == -2) { | ||||||||||||||
| return NULL; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| if (i >= 0) { | ||||||||||||||
| PyErr_SetString(PyExc_KeyError, "cannot remove local variables from FrameLocalsProxy"); | ||||||||||||||
| return NULL; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| PyObject* result = NULL; | ||||||||||||||
gaogaotiantian marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| if (frame->f_extra_locals == NULL) { | ||||||||||||||
| if (default_value != NULL) { | ||||||||||||||
| return Py_XNewRef(default_value); | ||||||||||||||
| } else { | ||||||||||||||
| PyErr_Format(PyExc_KeyError, "'%R'", key); | ||||||||||||||
|
||||||||||||||
| PyErr_Format(PyExc_KeyError, "'%R'", key); | |
| PyObject *repr = PyObject_Repr(key); | |
| if (repr == NULL) { | |
| return NULL; | |
| } | |
| PyErr_SetObject(PyExc_KeyError, repr); |
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 just realized I could've used _PyErr_SetKeyError(key) like dictobject.c. I'll switch to that.
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.
Oh, that sounds better. I wasn't aware of that, TIL!
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.
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.