-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Implement PEP 788 #133110
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
base: main
Are you sure you want to change the base?
Implement PEP 788 #133110
Changes from 1 commit
1828b9a
d0895f9
5c3ee8d
7b9ac59
0868c15
e9ea644
0ebdca4
40989de
d501f35
c5ec89c
4e1f599
3127a3f
82b5b9f
fda9886
f7723c0
62e9549
bc60630
9d8d526
54b0ce0
5955de6
92cf906
b0d0673
0a15beb
16d79de
c2bffcd
911c6b5
b84fa90
9ccf6bd
481caf5
71e1aec
d661578
4249c5d
71f2fd7
03ccb38
bca65fb
510ade1
3971408
6c4c52b
64920a8
05436f3
02bc2d7
03fa2af
b955d85
5236700
a277130
dac0c1a
ab9e3b5
79a1852
47957b8
771d7ed
0c3c1c7
531928e
08a8af6
02f93bc
d6c82bd
082fd69
61f70ae
fa961e9
ea1da77
6f19384
b702da2
40e7e68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2550,37 +2550,46 @@ static PyObject * | |
test_interp_refcount(PyObject *self, PyObject *unused) | ||
{ | ||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||
PyInterpreterRef ref1; | ||
PyInterpreterRef ref2; | ||
|
||
// Reference counts are technically 0 by default | ||
assert(_PyInterpreterState_Refcount(interp) == 0); | ||
PyInterpreterState *held = PyInterpreterState_Hold(); | ||
ref1 = PyInterpreterRef_Get(); | ||
assert(_PyInterpreterState_Refcount(interp) == 1); | ||
held = PyInterpreterState_Hold(); | ||
ref2 = PyInterpreterRef_Get(); | ||
assert(_PyInterpreterState_Refcount(interp) == 2); | ||
PyInterpreterState_Release(held); | ||
PyInterpreterRef_Close(ref1); | ||
assert(_PyInterpreterState_Refcount(interp) == 1); | ||
PyInterpreterState_Release(held); | ||
PyInterpreterRef_Close(ref2); | ||
assert(_PyInterpreterState_Refcount(interp) == 0); | ||
|
||
ref1 = PyInterpreterRef_Get(); | ||
ref2 = PyInterpreterRef_Dup(ref1); | ||
assert(_PyInterpreterState_Refcount(interp) == 2); | ||
assert(PyInterpreterRef_AsInterpreter(ref1) == interp); | ||
assert(PyInterpreterRef_AsInterpreter(ref2) == interp); | ||
PyInterpreterRef_Close(ref1); | ||
PyInterpreterRef_Close(ref2); | ||
assert(_PyInterpreterState_Refcount(interp) == 0); | ||
|
||
Py_RETURN_NONE; | ||
} | ||
|
||
static PyObject * | ||
test_interp_lookup(PyObject *self, PyObject *unused) | ||
test_interp_weak_ref(PyObject *self, PyObject *unused) | ||
{ | ||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||
PyInterpreterWeakRef wref = PyInterpreterWeakRef_Get(); | ||
assert(_PyInterpreterState_Refcount(interp) == 0); | ||
int64_t interp_id = PyInterpreterState_GetID(interp); | ||
PyInterpreterState *ref = PyInterpreterState_Lookup(interp_id); | ||
assert(ref == interp); | ||
|
||
PyInterpreterRef ref; | ||
int res = PyInterpreterWeakRef_AsStrong(wref, &ref); | ||
assert(res == 0); | ||
assert(PyInterpreterRef_AsInterpreter(ref) == interp); | ||
assert(_PyInterpreterState_Refcount(interp) == 1); | ||
PyInterpreterState_Release(ref); | ||
assert(PyInterpreterState_Lookup(10000) == NULL); | ||
Py_BEGIN_ALLOW_THREADS; | ||
ref = PyInterpreterState_Lookup(interp_id); | ||
assert(ref == interp); | ||
PyInterpreterState_Release(ref); | ||
Py_END_ALLOW_THREADS; | ||
PyInterpreterWeakRef_Close(wref); | ||
PyInterpreterRef_Close(ref); | ||
|
||
Py_RETURN_NONE; | ||
} | ||
|
@@ -2589,20 +2598,20 @@ static PyObject * | |
test_interp_ensure(PyObject *self, PyObject *unused) | ||
{ | ||
PyInterpreterState *interp = PyInterpreterState_Get(); | ||
PyInterpreterRef ref = PyInterpreterRef_Get(); | ||
PyThreadState *save_tstate = PyThreadState_Swap(NULL); | ||
PyThreadState *tstate = Py_NewInterpreter(); | ||
PyInterpreterRef sub_ref = PyInterpreterRef_Get(); | ||
PyInterpreterState *subinterp = PyThreadState_GetInterpreter(tstate); | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
_PyInterpreterState_Incref(interp); | ||
int res = PyThreadState_Ensure(interp); | ||
int res = PyThreadState_Ensure(sub_ref); | ||
assert(res == 0); | ||
assert(PyInterpreterState_Get() == interp); | ||
} | ||
|
||
for (int i = 0; i < 10; ++i) { | ||
_PyInterpreterState_Incref(subinterp); | ||
int res = PyThreadState_Ensure(subinterp); | ||
int res = PyThreadState_Ensure(sub_ref); | ||
assert(res == 0); | ||
assert(PyInterpreterState_Get() == subinterp); | ||
} | ||
|
@@ -2611,6 +2620,9 @@ test_interp_ensure(PyObject *self, PyObject *unused) | |
PyThreadState_Release(); | ||
} | ||
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. I suppose that PyThreadState_GetUnchecked() should be NULL at this point? Maybe add a check for that? 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. I redid this test. |
||
|
||
PyInterpreterRef_Close(ref); | ||
PyInterpreterRef_Close(sub_ref); | ||
|
||
PyThreadState_Swap(save_tstate); | ||
Py_RETURN_NONE; | ||
} | ||
|
@@ -2710,7 +2722,7 @@ static PyMethodDef TestMethods[] = { | |
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL}, | ||
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O}, | ||
{"test_interp_refcount", test_interp_refcount, METH_NOARGS}, | ||
{"test_interp_lookup", test_interp_lookup, METH_NOARGS}, | ||
{"test_interp_weak_ref", test_interp_weak_ref, METH_NOARGS}, | ||
{"test_interp_ensure", test_interp_ensure, METH_NOARGS}, | ||
{NULL, NULL} /* sentinel */ | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2323,40 +2323,42 @@ const char *THREAD_CODE = "import time\n" | |
"fib(10)"; | ||
|
||
typedef struct { | ||
PyInterpreterState *interp; | ||
PyInterpreterRef ref; | ||
int done; | ||
} ThreadData; | ||
|
||
static void | ||
do_tstate_ensure(void *arg) | ||
{ | ||
ThreadData *data = (ThreadData *)arg; | ||
int res = PyThreadState_Ensure(data->interp); | ||
int res = PyThreadState_Ensure(data->ref); | ||
assert(res == 0); | ||
PyThreadState_Ensure(PyInterpreterState_Hold()); | ||
PyThreadState_Ensure(PyInterpreterState_Hold()); | ||
PyThreadState_Ensure(data->ref); | ||
PyThreadState_Ensure(data->ref); | ||
PyGILState_STATE gstate = PyGILState_Ensure(); | ||
PyThreadState_Ensure(PyInterpreterState_Hold()); | ||
PyThreadState_Ensure(data->ref); | ||
res = PyRun_SimpleString(THREAD_CODE); | ||
PyThreadState_Release(); | ||
PyGILState_Release(gstate); | ||
PyThreadState_Release(); | ||
PyThreadState_Release(); | ||
assert(res == 0); | ||
PyThreadState_Release(); | ||
PyInterpreterRef_Close(data->ref); | ||
data->done = 1; | ||
} | ||
|
||
static int | ||
test_thread_state_ensure(void) | ||
{ | ||
_testembed_Py_InitializeFromConfig(); | ||
_testembed_initialize(); | ||
PyThread_handle_t handle; | ||
PyThread_ident_t ident; | ||
ThreadData data = { PyInterpreterState_Hold() }; | ||
PyInterpreterRef ref = PyInterpreterRef_Get(); | ||
ThreadData data = { ref }; | ||
if (PyThread_start_joinable_thread(do_tstate_ensure, &data, | ||
&ident, &handle) < 0) { | ||
PyInterpreterState_Release(data.interp); | ||
PyInterpreterRef_Close(ref); | ||
return -1; | ||
} | ||
Py_Finalize(); | ||
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. It may be safe to add a PyEvent to wait until the thread started before calling Py_Finalize(). 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. We hold a strong interpreter reference, we're implicitly waiting for it to start anyway. 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. Oh you're right, but I always forget the subtle semantics of interpreter strong references. Can you please add a comment explaining that the interpreter reference makes sure that Py_Finalize() doesn't complete until PyInterpreterRef_Close() is called? Or something like that. 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. Done |
||
|
Uh oh!
There was an error while loading. Please reload this page.