Skip to content

Commit c2bffcd

Browse files
committed
Use the new APIs in the tests.
1 parent 16d79de commit c2bffcd

2 files changed

Lines changed: 42 additions & 28 deletions

File tree

Modules/_testcapimodule.c

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2550,37 +2550,46 @@ static PyObject *
25502550
test_interp_refcount(PyObject *self, PyObject *unused)
25512551
{
25522552
PyInterpreterState *interp = PyInterpreterState_Get();
2553+
PyInterpreterRef ref1;
2554+
PyInterpreterRef ref2;
25532555

25542556
// Reference counts are technically 0 by default
25552557
assert(_PyInterpreterState_Refcount(interp) == 0);
2556-
PyInterpreterState *held = PyInterpreterState_Hold();
2558+
ref1 = PyInterpreterRef_Get();
25572559
assert(_PyInterpreterState_Refcount(interp) == 1);
2558-
held = PyInterpreterState_Hold();
2560+
ref2 = PyInterpreterRef_Get();
25592561
assert(_PyInterpreterState_Refcount(interp) == 2);
2560-
PyInterpreterState_Release(held);
2562+
PyInterpreterRef_Close(ref1);
25612563
assert(_PyInterpreterState_Refcount(interp) == 1);
2562-
PyInterpreterState_Release(held);
2564+
PyInterpreterRef_Close(ref2);
2565+
assert(_PyInterpreterState_Refcount(interp) == 0);
2566+
2567+
ref1 = PyInterpreterRef_Get();
2568+
ref2 = PyInterpreterRef_Dup(ref1);
2569+
assert(_PyInterpreterState_Refcount(interp) == 2);
2570+
assert(PyInterpreterRef_AsInterpreter(ref1) == interp);
2571+
assert(PyInterpreterRef_AsInterpreter(ref2) == interp);
2572+
PyInterpreterRef_Close(ref1);
2573+
PyInterpreterRef_Close(ref2);
25632574
assert(_PyInterpreterState_Refcount(interp) == 0);
25642575

25652576
Py_RETURN_NONE;
25662577
}
25672578

25682579
static PyObject *
2569-
test_interp_lookup(PyObject *self, PyObject *unused)
2580+
test_interp_weak_ref(PyObject *self, PyObject *unused)
25702581
{
25712582
PyInterpreterState *interp = PyInterpreterState_Get();
2583+
PyInterpreterWeakRef wref = PyInterpreterWeakRef_Get();
25722584
assert(_PyInterpreterState_Refcount(interp) == 0);
2573-
int64_t interp_id = PyInterpreterState_GetID(interp);
2574-
PyInterpreterState *ref = PyInterpreterState_Lookup(interp_id);
2575-
assert(ref == interp);
2585+
2586+
PyInterpreterRef ref;
2587+
int res = PyInterpreterWeakRef_AsStrong(wref, &ref);
2588+
assert(res == 0);
2589+
assert(PyInterpreterRef_AsInterpreter(ref) == interp);
25762590
assert(_PyInterpreterState_Refcount(interp) == 1);
2577-
PyInterpreterState_Release(ref);
2578-
assert(PyInterpreterState_Lookup(10000) == NULL);
2579-
Py_BEGIN_ALLOW_THREADS;
2580-
ref = PyInterpreterState_Lookup(interp_id);
2581-
assert(ref == interp);
2582-
PyInterpreterState_Release(ref);
2583-
Py_END_ALLOW_THREADS;
2591+
PyInterpreterWeakRef_Close(wref);
2592+
PyInterpreterRef_Close(ref);
25842593

25852594
Py_RETURN_NONE;
25862595
}
@@ -2589,20 +2598,20 @@ static PyObject *
25892598
test_interp_ensure(PyObject *self, PyObject *unused)
25902599
{
25912600
PyInterpreterState *interp = PyInterpreterState_Get();
2601+
PyInterpreterRef ref = PyInterpreterRef_Get();
25922602
PyThreadState *save_tstate = PyThreadState_Swap(NULL);
25932603
PyThreadState *tstate = Py_NewInterpreter();
2604+
PyInterpreterRef sub_ref = PyInterpreterRef_Get();
25942605
PyInterpreterState *subinterp = PyThreadState_GetInterpreter(tstate);
25952606

25962607
for (int i = 0; i < 10; ++i) {
2597-
_PyInterpreterState_Incref(interp);
2598-
int res = PyThreadState_Ensure(interp);
2608+
int res = PyThreadState_Ensure(sub_ref);
25992609
assert(res == 0);
26002610
assert(PyInterpreterState_Get() == interp);
26012611
}
26022612

26032613
for (int i = 0; i < 10; ++i) {
2604-
_PyInterpreterState_Incref(subinterp);
2605-
int res = PyThreadState_Ensure(subinterp);
2614+
int res = PyThreadState_Ensure(sub_ref);
26062615
assert(res == 0);
26072616
assert(PyInterpreterState_Get() == subinterp);
26082617
}
@@ -2611,6 +2620,9 @@ test_interp_ensure(PyObject *self, PyObject *unused)
26112620
PyThreadState_Release();
26122621
}
26132622

2623+
PyInterpreterRef_Close(ref);
2624+
PyInterpreterRef_Close(sub_ref);
2625+
26142626
PyThreadState_Swap(save_tstate);
26152627
Py_RETURN_NONE;
26162628
}
@@ -2710,7 +2722,7 @@ static PyMethodDef TestMethods[] = {
27102722
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL},
27112723
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
27122724
{"test_interp_refcount", test_interp_refcount, METH_NOARGS},
2713-
{"test_interp_lookup", test_interp_lookup, METH_NOARGS},
2725+
{"test_interp_weak_ref", test_interp_weak_ref, METH_NOARGS},
27142726
{"test_interp_ensure", test_interp_ensure, METH_NOARGS},
27152727
{NULL, NULL} /* sentinel */
27162728
};

Programs/_testembed.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2323,40 +2323,42 @@ const char *THREAD_CODE = "import time\n"
23232323
"fib(10)";
23242324

23252325
typedef struct {
2326-
PyInterpreterState *interp;
2326+
PyInterpreterRef ref;
23272327
int done;
23282328
} ThreadData;
23292329

23302330
static void
23312331
do_tstate_ensure(void *arg)
23322332
{
23332333
ThreadData *data = (ThreadData *)arg;
2334-
int res = PyThreadState_Ensure(data->interp);
2334+
int res = PyThreadState_Ensure(data->ref);
23352335
assert(res == 0);
2336-
PyThreadState_Ensure(PyInterpreterState_Hold());
2337-
PyThreadState_Ensure(PyInterpreterState_Hold());
2336+
PyThreadState_Ensure(data->ref);
2337+
PyThreadState_Ensure(data->ref);
23382338
PyGILState_STATE gstate = PyGILState_Ensure();
2339-
PyThreadState_Ensure(PyInterpreterState_Hold());
2339+
PyThreadState_Ensure(data->ref);
23402340
res = PyRun_SimpleString(THREAD_CODE);
23412341
PyThreadState_Release();
23422342
PyGILState_Release(gstate);
23432343
PyThreadState_Release();
23442344
PyThreadState_Release();
23452345
assert(res == 0);
23462346
PyThreadState_Release();
2347+
PyInterpreterRef_Close(data->ref);
23472348
data->done = 1;
23482349
}
23492350

23502351
static int
23512352
test_thread_state_ensure(void)
23522353
{
2353-
_testembed_Py_InitializeFromConfig();
2354+
_testembed_initialize();
23542355
PyThread_handle_t handle;
23552356
PyThread_ident_t ident;
2356-
ThreadData data = { PyInterpreterState_Hold() };
2357+
PyInterpreterRef ref = PyInterpreterRef_Get();
2358+
ThreadData data = { ref };
23572359
if (PyThread_start_joinable_thread(do_tstate_ensure, &data,
23582360
&ident, &handle) < 0) {
2359-
PyInterpreterState_Release(data.interp);
2361+
PyInterpreterRef_Close(ref);
23602362
return -1;
23612363
}
23622364
Py_Finalize();

0 commit comments

Comments
 (0)