Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit afbe5bf

Browse filesBrowse files
gh-74929: Make containment checks more efficient in FrameLocalsProxy (#118624)
Properly implement the `sq_contains` slot for frame locals proxy containment checks.
1 parent 757fd3e commit afbe5bf
Copy full SHA for afbe5bf

File tree

Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+19
-10
lines changed

‎Objects/frameobject.c

Copy file name to clipboardExpand all lines: Objects/frameobject.c
+19-10Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -488,29 +488,33 @@ framelocalsproxy_length(PyObject *self)
488488
return size;
489489
}
490490

491-
static PyObject*
491+
static int
492492
framelocalsproxy_contains(PyObject *self, PyObject *key)
493493
{
494494
PyFrameObject *frame = ((PyFrameLocalsProxyObject*)self)->frame;
495495

496496
if (PyUnicode_CheckExact(key)) {
497497
int i = framelocalsproxy_getkeyindex(frame, key, true);
498498
if (i >= 0) {
499-
Py_RETURN_TRUE;
499+
return 1;
500500
}
501501
}
502502

503503
PyObject *extra = ((PyFrameObject*)frame)->f_extra_locals;
504504
if (extra != NULL) {
505-
int result = PyDict_Contains(extra, key);
506-
if (result < 0) {
507-
return NULL;
508-
} else if (result > 0) {
509-
Py_RETURN_TRUE;
510-
}
505+
return PyDict_Contains(extra, key);
511506
}
512507

513-
Py_RETURN_FALSE;
508+
return 0;
509+
}
510+
511+
static PyObject* framelocalsproxy___contains__(PyObject *self, PyObject *key)
512+
{
513+
int result = framelocalsproxy_contains(self, key);
514+
if (result < 0) {
515+
return NULL;
516+
}
517+
return PyBool_FromLong(result);
514518
}
515519

516520
static PyObject*
@@ -604,14 +608,18 @@ static PyNumberMethods framelocalsproxy_as_number = {
604608
.nb_inplace_or = framelocalsproxy_inplace_or,
605609
};
606610

611+
static PySequenceMethods framelocalsproxy_as_sequence = {
612+
.sq_contains = framelocalsproxy_contains,
613+
};
614+
607615
static PyMappingMethods framelocalsproxy_as_mapping = {
608616
framelocalsproxy_length, // mp_length
609617
framelocalsproxy_getitem, // mp_subscript
610618
framelocalsproxy_setitem, // mp_ass_subscript
611619
};
612620

613621
static PyMethodDef framelocalsproxy_methods[] = {
614-
{"__contains__", framelocalsproxy_contains, METH_O | METH_COEXIST,
622+
{"__contains__", framelocalsproxy___contains__, METH_O | METH_COEXIST,
615623
NULL},
616624
{"__getitem__", framelocalsproxy_getitem, METH_O | METH_COEXIST,
617625
NULL},
@@ -639,6 +647,7 @@ PyTypeObject PyFrameLocalsProxy_Type = {
639647
.tp_dealloc = (destructor)framelocalsproxy_dealloc,
640648
.tp_repr = &framelocalsproxy_repr,
641649
.tp_as_number = &framelocalsproxy_as_number,
650+
.tp_as_sequence = &framelocalsproxy_as_sequence,
642651
.tp_as_mapping = &framelocalsproxy_as_mapping,
643652
.tp_getattro = PyObject_GenericGetAttr,
644653
.tp_setattro = PyObject_GenericSetAttr,

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.