Skip to content

Commit 531d3c8

Browse files
committed
Use C stackrefs to make use of _PyObject_GetMethodStackRef safe.
1 parent 722f51a commit 531d3c8

File tree

3 files changed

+82
-27
lines changed

3 files changed

+82
-27
lines changed

Python/bytecodes.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2328,17 +2328,22 @@ dummy_func(
23282328
PyObject *name = GETITEM(FRAME_CO_NAMES, oparg >> 1);
23292329
if (oparg & 1) {
23302330
/* Designed to work in tandem with CALL, pushes two values. */
2331-
_PyStackRef method = PyStackRef_NULL;
2332-
int is_meth = _PyObject_GetMethodStackRef(tstate, PyStackRef_AsPyObjectBorrow(owner), name, &method);
2331+
_PyCStackRef method;
2332+
_PyThreadState_PushCStackRef(tstate, &method);
2333+
int is_meth = _PyObject_GetMethodStackRef(tstate, PyStackRef_AsPyObjectBorrow(owner), name, &method.ref);
23332334
if (is_meth) {
23342335
/* We can bypass temporary bound method object.
23352336
meth is unbound method and obj is self.
23362337
meth | self | arg1 | ... | argN
23372338
*/
2338-
assert(!PyStackRef_IsNull(method)); // No errors on this branch
2339+
assert(!PyStackRef_IsNull(method.ref)); // No errors on this branch
23392340
self_or_null[0] = owner; // Transfer ownership
23402341
DEAD(owner);
2341-
attr = method;
2342+
attr = method.ref;
2343+
#ifdef Py_GIL_DISABLED
2344+
method_ref.ref = PyStackRef_NULL;
2345+
_PyThreadState_PopCStackRef(tstate, &method_ref);
2346+
#endif
23422347
}
23432348
else {
23442349
/* meth is not an unbound method (but a regular attr, or
@@ -2347,10 +2352,14 @@ dummy_func(
23472352
CALL that it's not a method call.
23482353
meth | NULL | arg1 | ... | argN
23492354
*/
2350-
attr = method;
2351-
self_or_null[0] = PyStackRef_NULL;
23522355
PyStackRef_CLOSE(owner);
2353-
ERROR_IF(PyStackRef_IsNull(method));
2356+
self_or_null[0] = PyStackRef_NULL;
2357+
attr = method.ref;
2358+
#ifdef Py_GIL_DISABLED
2359+
method_ref.ref = PyStackRef_NULL;
2360+
_PyThreadState_PopCStackRef(tstate, &method_ref);
2361+
#endif
2362+
ERROR_IF(PyStackRef_IsNull(attr));
23542363
}
23552364
}
23562365
else {

Python/executor_cases.c.h

Lines changed: 33 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Python/generated_cases.c.h

Lines changed: 33 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)