Skip to content

Commit ec57918

Browse files
committed
Add a test for pending calls.
1 parent 3edc3a8 commit ec57918

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Lib/test/test_capi/test_misc.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from test import support
2323
from test.support import MISSING_C_DOCSTRINGS
2424
from test.support import import_helper
25+
from test.support import script_helper
2526
from test.support import threading_helper
2627
from test.support import warnings_helper
2728
from test.support import requires_limited_api
@@ -1641,6 +1642,36 @@ def subthread():
16411642

16421643
self.assertEqual(actual, int(interpid))
16431644

1645+
@threading_helper.requires_working_threading()
1646+
def test_pending_call_creates_thread(self):
1647+
source = """
1648+
import _testcapi
1649+
import threading
1650+
import time
1651+
1652+
1653+
def output():
1654+
print(24)
1655+
time.sleep(1)
1656+
print(42)
1657+
1658+
1659+
def callback():
1660+
threading.Thread(target=output).start()
1661+
1662+
1663+
def create_pending_call():
1664+
time.sleep(1)
1665+
_testcapi.simple_pending_call(callback)
1666+
1667+
1668+
threading.Thread(target=create_pending_call).start()
1669+
"""
1670+
return_code, stdout, stderr = script_helper.assert_python_ok('-c', textwrap.dedent(source))
1671+
self.assertEqual(return_code, 0)
1672+
self.assertEqual(stdout, b"24\n42\n")
1673+
self.assertEqual(stderr, b"")
1674+
16441675

16451676
class SubinterpreterTest(unittest.TestCase):
16461677

Modules/_testcapimodule.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,6 +2546,16 @@ toggle_reftrace_printer(PyObject *ob, PyObject *arg)
25462546
Py_RETURN_NONE;
25472547
}
25482548

2549+
static PyObject *
2550+
simple_pending_call(PyObject *self, PyObject *callable)
2551+
{
2552+
if (Py_AddPendingCall(_pending_callback, Py_NewRef(callable)) < 0) {
2553+
return NULL;
2554+
}
2555+
2556+
Py_RETURN_NONE;
2557+
}
2558+
25492559
static PyMethodDef TestMethods[] = {
25502560
{"set_errno", set_errno, METH_VARARGS},
25512561
{"test_config", test_config, METH_NOARGS},
@@ -2640,6 +2650,7 @@ static PyMethodDef TestMethods[] = {
26402650
{"test_atexit", test_atexit, METH_NOARGS},
26412651
{"code_offset_to_line", _PyCFunction_CAST(code_offset_to_line), METH_FASTCALL},
26422652
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
2653+
{"simple_pending_call", simple_pending_call, METH_O},
26432654
{NULL, NULL} /* sentinel */
26442655
};
26452656

0 commit comments

Comments
 (0)