Skip to content

Commit 79a1852

Browse files
committed
Move some tests around to prevent exposure of the private API.
1 parent ab9e3b5 commit 79a1852

4 files changed

Lines changed: 45 additions & 35 deletions

File tree

Include/cpython/pystate.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -300,9 +300,6 @@ PyAPI_FUNC(void) PyInterpreterWeakRef_Close(PyInterpreterWeakRef wref);
300300
ref = 0; \
301301
} while (0)
302302

303-
// Exports for '_testcapi' shared extension
304-
PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_Refcount(PyInterpreterState *interp);
305-
PyAPI_FUNC(int) _PyInterpreterState_Incref(PyInterpreterState *interp);
306303

307304
PyAPI_FUNC(int) PyThreadState_Ensure(PyInterpreterRef interp_ref);
308305

Include/internal/pycore_pystate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,10 @@ _Py_RecursionLimit_GetMargin(PyThreadState *tstate)
328328
return Py_ARITHMETIC_RIGHT_SHIFT(intptr_t, here_addr - (intptr_t)_tstate->c_stack_soft_limit, PYOS_STACK_MARGIN_SHIFT);
329329
}
330330

331+
// Exports for '_testinternalcapi' shared extension
332+
PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_Refcount(PyInterpreterState *interp);
333+
PyAPI_FUNC(int) _PyInterpreterState_Incref(PyInterpreterState *interp);
334+
331335
#ifdef __cplusplus
332336
}
333337
#endif

Modules/_testcapimodule.c

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,36 +2556,6 @@ get_strong_ref(void)
25562556
return ref;
25572557
}
25582558

2559-
static PyObject *
2560-
test_interp_refcount(PyObject *self, PyObject *unused)
2561-
{
2562-
PyInterpreterState *interp = PyInterpreterState_Get();
2563-
PyInterpreterRef ref1;
2564-
PyInterpreterRef ref2;
2565-
2566-
// Reference counts are technically 0 by default
2567-
assert(_PyInterpreterState_Refcount(interp) == 0);
2568-
ref1 = get_strong_ref();
2569-
assert(_PyInterpreterState_Refcount(interp) == 1);
2570-
ref2 = get_strong_ref();
2571-
assert(_PyInterpreterState_Refcount(interp) == 2);
2572-
PyInterpreterRef_Close(ref1);
2573-
assert(_PyInterpreterState_Refcount(interp) == 1);
2574-
PyInterpreterRef_Close(ref2);
2575-
assert(_PyInterpreterState_Refcount(interp) == 0);
2576-
2577-
ref1 = get_strong_ref();
2578-
ref2 = PyInterpreterRef_Dup(ref1);
2579-
assert(_PyInterpreterState_Refcount(interp) == 2);
2580-
assert(PyInterpreterRef_AsInterpreter(ref1) == interp);
2581-
assert(PyInterpreterRef_AsInterpreter(ref2) == interp);
2582-
PyInterpreterRef_Close(ref1);
2583-
PyInterpreterRef_Close(ref2);
2584-
assert(_PyInterpreterState_Refcount(interp) == 0);
2585-
2586-
Py_RETURN_NONE;
2587-
}
2588-
25892559
static PyObject *
25902560
test_interp_weak_ref(PyObject *self, PyObject *unused)
25912561
{
@@ -2600,7 +2570,6 @@ test_interp_weak_ref(PyObject *self, PyObject *unused)
26002570
int res = PyInterpreterWeakRef_AsStrong(wref, &ref);
26012571
assert(res == 0);
26022572
assert(PyInterpreterRef_AsInterpreter(ref) == interp);
2603-
assert(_PyInterpreterState_Refcount(interp) == 1);
26042573
PyInterpreterWeakRef_Close(wref);
26052574
PyInterpreterRef_Close(ref);
26062575

@@ -2735,7 +2704,6 @@ static PyMethodDef TestMethods[] = {
27352704
{"test_atexit", test_atexit, METH_NOARGS},
27362705
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL},
27372706
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
2738-
{"test_interp_refcount", test_interp_refcount, METH_NOARGS},
27392707
{"test_interp_weak_ref", test_interp_weak_ref, METH_NOARGS},
27402708
{"test_interp_ensure", test_interp_ensure, METH_NOARGS},
27412709
{NULL, NULL} /* sentinel */

Modules/_testinternalcapi.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2342,6 +2342,46 @@ incref_decref_delayed(PyObject *self, PyObject *op)
23422342
Py_RETURN_NONE;
23432343
}
23442344

2345+
static PyInterpreterRef
2346+
get_strong_ref(void)
2347+
{
2348+
PyInterpreterRef ref;
2349+
if (PyInterpreterRef_Get(&ref) < 0) {
2350+
Py_FatalError("strong reference should not have failed");
2351+
}
2352+
return ref;
2353+
}
2354+
2355+
static PyObject *
2356+
test_interp_refcount(PyObject *self, PyObject *unused)
2357+
{
2358+
PyInterpreterState *interp = PyInterpreterState_Get();
2359+
PyInterpreterRef ref1;
2360+
PyInterpreterRef ref2;
2361+
2362+
// Reference counts are technically 0 by default
2363+
assert(_PyInterpreterState_Refcount(interp) == 0);
2364+
ref1 = get_strong_ref();
2365+
assert(_PyInterpreterState_Refcount(interp) == 1);
2366+
ref2 = get_strong_ref();
2367+
assert(_PyInterpreterState_Refcount(interp) == 2);
2368+
PyInterpreterRef_Close(ref1);
2369+
assert(_PyInterpreterState_Refcount(interp) == 1);
2370+
PyInterpreterRef_Close(ref2);
2371+
assert(_PyInterpreterState_Refcount(interp) == 0);
2372+
2373+
ref1 = get_strong_ref();
2374+
ref2 = PyInterpreterRef_Dup(ref1);
2375+
assert(_PyInterpreterState_Refcount(interp) == 2);
2376+
assert(PyInterpreterRef_AsInterpreter(ref1) == interp);
2377+
assert(PyInterpreterRef_AsInterpreter(ref2) == interp);
2378+
PyInterpreterRef_Close(ref1);
2379+
PyInterpreterRef_Close(ref2);
2380+
assert(_PyInterpreterState_Refcount(interp) == 0);
2381+
2382+
Py_RETURN_NONE;
2383+
}
2384+
23452385
static PyMethodDef module_functions[] = {
23462386
{"get_configs", get_configs, METH_NOARGS},
23472387
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -2444,6 +2484,7 @@ static PyMethodDef module_functions[] = {
24442484
{"is_static_immortal", is_static_immortal, METH_O},
24452485
{"incref_decref_delayed", incref_decref_delayed, METH_O},
24462486
GET_NEXT_DICT_KEYS_VERSION_METHODDEF
2487+
{"test_interp_refcount", test_interp_refcount, METH_NOARGS},
24472488
{NULL, NULL} /* sentinel */
24482489
};
24492490

0 commit comments

Comments
 (0)