Skip to content

Commit 9f6a8ab

Browse files
committed
Add tests
1 parent 63b5d52 commit 9f6a8ab

6 files changed

Lines changed: 91 additions & 19 deletions

File tree

Lib/test/test_threading.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,6 +2103,26 @@ def test__all__(self):
21032103
support.check__all__(self, threading, ('threading', '_thread'),
21042104
extra=extra, not_exported=not_exported)
21052105

2106+
def test_set_name(self):
2107+
try:
2108+
get_name = _thread._get_name
2109+
set_name = _thread.set_name
2110+
except AttributeError:
2111+
self.skipTest("need thread._get_name() and thread.set_name()")
2112+
2113+
work_name = None
2114+
def work():
2115+
nonlocal work_name
2116+
work_name = get_name()
2117+
2118+
# name not too long to fit into Linux 15 bytes limit
2119+
name = "CustomName"
2120+
2121+
thread = threading.Thread(target=work, name=name)
2122+
thread.start()
2123+
thread.join()
2124+
self.assertEqual(work_name, name)
2125+
21062126

21072127
class InterruptMainTests(unittest.TestCase):
21082128
def check_interrupt_main_with_signal_handler(self, signum):

Modules/_threadmodule.c

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2364,19 +2364,45 @@ Internal only. Return a non-zero integer that uniquely identifies the main threa
23642364
of the main interpreter.");
23652365

23662366

2367+
#ifdef HAVE_PTHREAD_SETNAME_NP
2368+
/*[clinic input]
2369+
_thread._get_name
2370+
2371+
Get the name of the current thread.
2372+
[clinic start generated code]*/
2373+
2374+
static PyObject *
2375+
_thread__get_name_impl(PyObject *module)
2376+
/*[clinic end generated code: output=20026e7ee3da3dd7 input=35cec676833d04c8]*/
2377+
{
2378+
char name[17];
2379+
size_t size = Py_ARRAY_LENGTH(name) - 1;
2380+
#ifdef __APPLE__
2381+
pthread_getname_np(name, size);
2382+
#else
2383+
pthread_t thread = pthread_self();
2384+
pthread_getname_np(thread, name, size);
2385+
#endif
2386+
name[size] = 0;
2387+
return PyUnicode_DecodeFSDefault(name);
2388+
}
2389+
#endif // HAVE_PTHREAD_SETNAME_NP
2390+
2391+
23672392
#ifdef HAVE_PTHREAD_SETNAME_NP
23682393
/*[clinic input]
23692394
_thread.set_name
23702395
2371-
name: str
2396+
name as name_obj: object(converter="PyUnicode_FSConverter")
23722397
23732398
Set the name of the current thread.
23742399
[clinic start generated code]*/
23752400

23762401
static PyObject *
2377-
_thread_set_name_impl(PyObject *module, const char *name)
2378-
/*[clinic end generated code: output=f051ce549bcd6b8e input=7e29bbdbfb046a04]*/
2402+
_thread_set_name_impl(PyObject *module, PyObject *name_obj)
2403+
/*[clinic end generated code: output=402b0c68e0c0daed input=a0459bd64f771808]*/
23792404
{
2405+
const char *name = PyBytes_AS_STRING(name_obj);
23802406
#ifdef __APPLE__
23812407
pthread_setname_np(name);
23822408
#else
@@ -2428,6 +2454,7 @@ static PyMethodDef thread_methods[] = {
24282454
{"_get_main_thread_ident", thread__get_main_thread_ident,
24292455
METH_NOARGS, thread__get_main_thread_ident_doc},
24302456
_THREAD_SET_NAME_METHODDEF
2457+
_THREAD__GET_NAME_METHODDEF
24312458
{NULL, NULL} /* sentinel */
24322459
};
24332460

Modules/clinic/_threadmodule.c.h

Lines changed: 31 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5107,7 +5107,7 @@ AC_CHECK_FUNCS([ \
51075107
posix_spawn_file_actions_addclosefrom_np \
51085108
pread preadv preadv2 process_vm_readv \
51095109
pthread_cond_timedwait_relative_np pthread_condattr_setclock pthread_init \
5110-
pthread_kill pthread_setname_np \
5110+
pthread_kill pthread_getname_np pthread_setname_np \
51115111
ptsname ptsname_r pwrite pwritev pwritev2 readlink readlinkat readv realpath renameat \
51125112
rtpSpawn sched_get_priority_max sched_rr_get_interval sched_setaffinity \
51135113
sched_setparam sched_setscheduler sem_clockwait sem_getvalue sem_open \

pyconfig.h.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@
981981
/* Define to 1 if you have the `pthread_getcpuclockid' function. */
982982
#undef HAVE_PTHREAD_GETCPUCLOCKID
983983

984+
/* Define to 1 if you have the `pthread_getname_np' function. */
985+
#undef HAVE_PTHREAD_GETNAME_NP
986+
984987
/* Define to 1 if you have the <pthread.h> header file. */
985988
#undef HAVE_PTHREAD_H
986989

0 commit comments

Comments
 (0)