Skip to content

Commit 0868c15

Browse files
committed
Add a test for PyThreadState_SetDaemon().
1 parent 7b9ac59 commit 0868c15

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

Include/cpython/pystate.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,5 @@ PyAPI_FUNC(void) PyInterpreterState_Release(PyInterpreterState *interp);
280280

281281
// Export for '_testcapi' shared extension
282282
PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_Refcount(PyInterpreterState *interp);
283+
284+
PyAPI_FUNC(int) PyThreadState_SetDaemon(int daemon);

Programs/_testembed.c

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,6 +2341,45 @@ test_get_incomplete_frame(void)
23412341
return result;
23422342
}
23432343

2344+
static void
2345+
non_daemon_native(void *arg)
2346+
{
2347+
PyEvent *event = (PyEvent *)arg;
2348+
PyThreadState *tstate = PyThreadState_New(PyInterpreterState_Main());
2349+
PyThreadState_Swap(tstate);
2350+
int res = PyThreadState_SetDaemon(0);
2351+
assert(res == 1);
2352+
_PyEvent_Notify(event);
2353+
const char *code = "import time\n"
2354+
"time.sleep(0.2)\n"
2355+
"def fib(n):\n"
2356+
" if n <= 1:\n"
2357+
" return n\n"
2358+
" else:\n"
2359+
" return fib(n - 1) + fib(n - 2)\n"
2360+
"fib(10)";
2361+
res = PyRun_SimpleString(code);
2362+
assert(res == 0);
2363+
PyThreadState_Clear(tstate);
2364+
PyThreadState_Swap(NULL);
2365+
PyThreadState_Delete(tstate);
2366+
}
2367+
2368+
static int
2369+
test_non_daemon_native_thread(void)
2370+
{
2371+
_testembed_Py_InitializeFromConfig();
2372+
PyThread_handle_t handle;
2373+
PyThread_ident_t ident;
2374+
PyEvent event;
2375+
if (PyThread_start_joinable_thread(non_daemon_native, &event,
2376+
&ident, &handle) < 0) {
2377+
return -1;
2378+
}
2379+
PyEvent_Wait(&event);
2380+
Py_Finalize();
2381+
return 0;
2382+
}
23442383

23452384
/* *********************************************************
23462385
* List of test cases and the function that implements it.
@@ -2431,6 +2470,7 @@ static struct TestCase TestCases[] = {
24312470
{"test_frozenmain", test_frozenmain},
24322471
#endif
24332472
{"test_get_incomplete_frame", test_get_incomplete_frame},
2473+
{"test_non_daemon_native_thread", test_non_daemon_native_thread},
24342474

24352475
{NULL, NULL}
24362476
};

0 commit comments

Comments
 (0)