-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
gh-125590: Allow FrameLocalsProxy to delete and pop keys from extra locals #125616
Merged
gaogaotiantian
merged 5 commits into
python:main
from
gaogaotiantian:allow-framelocalsproxy-pop-extra
Oct 21, 2024
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
1772804
Allow FrameLocalsProxy to delete and pop keys from extra locals
gaogaotiantian 0130a66
📜🤖 Added by blurb_it.
blurb-it[bot] 124aaa0
Remove blank line
gaogaotiantian c8a8eb4
Use RuntimeError and some internal functions
gaogaotiantian d16cfad
Use ValueError instead of RuntimeError
gaogaotiantian 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
There are no files selected for viewing
This file contains 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
1 change: 1 addition & 0 deletions
1
Misc/NEWS.d/next/Library/2024-10-16-20-32-40.gh-issue-125590.stHzOP.rst
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
Allow ``FrameLocalsProxy`` to delete and pop if the key is not a fast variable. |
This file contains 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 |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
#include "pycore_code.h" // CO_FAST_LOCAL, etc. | ||
#include "pycore_function.h" // _PyFunction_FromConstructor() | ||
#include "pycore_moduleobject.h" // _PyModule_GetDict() | ||
#include "pycore_modsupport.h" // _PyArg_CheckPositional() | ||
#include "pycore_object.h" // _PyObject_GC_UNTRACK() | ||
#include "pycore_opcode_metadata.h" // _PyOpcode_Deopt, _PyOpcode_Caches | ||
|
||
|
@@ -158,16 +159,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_RuntimeError, "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 +203,10 @@ framelocalsproxy_setitem(PyObject *self, PyObject *key, PyObject *value) | |
PyObject *extra = frame->f_extra_locals; | ||
|
||
if (extra == NULL) { | ||
if (value == NULL) { | ||
_PyErr_SetKeyError(key); | ||
return -1; | ||
} | ||
extra = PyDict_New(); | ||
if (extra == NULL) { | ||
return -1; | ||
|
@@ -211,7 +216,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 +685,59 @@ 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 (!_PyArg_CheckPositional("pop", nargs, 1, 2)) { | ||
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_RuntimeError, "cannot remove local variables from FrameLocalsProxy"); | ||
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. Ditto |
||
return NULL; | ||
} | ||
|
||
PyObject *result = NULL; | ||
|
||
if (frame->f_extra_locals == NULL) { | ||
if (default_value != NULL) { | ||
return Py_XNewRef(default_value); | ||
} else { | ||
_PyErr_SetKeyError(key); | ||
return NULL; | ||
} | ||
} | ||
|
||
if (PyDict_Pop(frame->f_extra_locals, key, &result) < 0) { | ||
return NULL; | ||
} | ||
|
||
if (result == NULL) { | ||
if (default_value != NULL) { | ||
return Py_XNewRef(default_value); | ||
} else { | ||
_PyErr_SetKeyError(key); | ||
return NULL; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
static PyObject* | ||
framelocalsproxy_copy(PyObject *self, PyObject *Py_UNUSED(ignored)) | ||
{ | ||
|
@@ -743,6 +805,8 @@ static PyMethodDef framelocalsproxy_methods[] = { | |
NULL}, | ||
{"get", _PyCFunction_CAST(framelocalsproxy_get), METH_FASTCALL, | ||
NULL}, | ||
{"pop", _PyCFunction_CAST(framelocalsproxy_pop), METH_FASTCALL, | ||
NULL}, | ||
{"setdefault", _PyCFunction_CAST(framelocalsproxy_setdefault), METH_FASTCALL, | ||
NULL}, | ||
{NULL, NULL} /* sentinel */ | ||
|
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.
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