@@ -2364,19 +2364,45 @@ Internal only. Return a non-zero integer that uniquely identifies the main threa
23642364of 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
23732398Set the name of the current thread.
23742399[clinic start generated code]*/
23752400
23762401static 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
0 commit comments