@@ -71,17 +71,14 @@ get_thread_state_by_cls(PyTypeObject *cls)
7171    return  get_thread_state (module );
7272}
7373
74+ 
7475#ifdef  MS_WINDOWS 
7576typedef  HRESULT  (WINAPI  * PF_GET_THREAD_DESCRIPTION )(HANDLE , PCWSTR * );
7677typedef  HRESULT  (WINAPI  * PF_SET_THREAD_DESCRIPTION )(HANDLE , PCWSTR );
7778static  PF_GET_THREAD_DESCRIPTION  pGetThreadDescription  =  NULL ;
7879static  PF_SET_THREAD_DESCRIPTION  pSetThreadDescription  =  NULL ;
7980#endif 
8081
81- #if  defined(HAVE_PTHREAD_SETNAME_NP ) ||  defined(HAVE_PTHREAD_SET_NAME_NP )
82- static  int  _set_thread_name (const  char  * name );
83- #endif 
84- 
8582
8683/*[clinic input] 
8784module _thread 
@@ -2579,12 +2576,24 @@ _thread.set_name
25792576Set the name of the current thread. 
25802577[clinic start generated code]*/ 
25812578
2582- 
2583- #ifndef  MS_WINDOWS 
2584- // Helper to set the thread name using platform-specific APIs (POSIX only) 
25852579static  int 
2586- _set_thread_name ( const  char  * name )
2580+ _set_thread_name_encoded ( PyObject   * name_obj ,  const  char  * encoding ,  int   * perrno )
25872581{
2582+     PyObject  * name_encoded  =  PyUnicode_AsEncodedString (name_obj , encoding , "replace" );
2583+     if  (name_encoded  ==  NULL ) {
2584+         return  -1 ;
2585+     }
2586+ #ifdef  _PYTHREAD_NAME_MAXLEN 
2587+     if  (PyBytes_GET_SIZE (name_encoded ) >  _PYTHREAD_NAME_MAXLEN ) {
2588+         PyObject  * truncated  =  PyBytes_FromStringAndSize (PyBytes_AS_STRING (name_encoded ), _PYTHREAD_NAME_MAXLEN );
2589+         if  (truncated  ==  NULL ) {
2590+             Py_DECREF (name_encoded );
2591+             return  -1 ;
2592+         }
2593+         Py_SETREF (name_encoded , truncated );
2594+     }
2595+ #endif 
2596+     const  char  * name  =  PyBytes_AS_STRING (name_encoded );
25882597    int  rc ;
25892598#ifdef  __APPLE__ 
25902599    rc  =  pthread_setname_np (name );
@@ -2599,66 +2608,32 @@ _set_thread_name(const char *name)
25992608    rc  =  0 ; /* pthread_set_name_np() returns void */ 
26002609    pthread_set_name_np (thread , name );
26012610#endif 
2611+     Py_DECREF (name_encoded );
2612+     if  (perrno ) {
2613+         * perrno  =  rc ;
2614+     }
26022615    return  rc ;
26032616}
2604- #endif  // !MS_WINDOWS 
2605- 
26062617
26072618static  PyObject  * 
26082619_thread_set_name_impl (PyObject  * module , PyObject  * name_obj )
26092620/*[clinic end generated code: output=402b0c68e0c0daed input=7e7acd98261be82f]*/ 
26102621{
26112622#ifndef  MS_WINDOWS 
2612-     // POSIX and non-Windows platforms 
26132623#ifdef  __sun 
26142624    const  char  * encoding  =  "utf-8" ;
26152625#else 
26162626    PyInterpreterState  * interp  =  _PyInterpreterState_GET ();
26172627    const  char  * encoding  =  interp -> unicode .fs_codec .encoding ;
26182628#endif 
2619-     PyObject  * name_encoded ;
2620-     int  rc ;
2621- 
2622-     name_encoded  =  PyUnicode_AsEncodedString (name_obj , encoding , "replace" );
2623-     if  (name_encoded  ==  NULL ) {
2624-         return  NULL ;
2625-     }
2626- #ifdef  _PYTHREAD_NAME_MAXLEN 
2627-     if  (PyBytes_GET_SIZE (name_encoded ) >  _PYTHREAD_NAME_MAXLEN ) {
2628-         PyObject  * truncated  =  PyBytes_FromStringAndSize (PyBytes_AS_STRING (name_encoded ), _PYTHREAD_NAME_MAXLEN );
2629-         if  (truncated  ==  NULL ) {
2630-             Py_DECREF (name_encoded );
2631-             return  NULL ;
2632-         }
2633-         Py_SETREF (name_encoded , truncated );
2634-     }
2635- #endif 
2636-     const  char  * name  =  PyBytes_AS_STRING (name_encoded );
2637-     rc  =  _set_thread_name (name );
2638-     Py_DECREF (name_encoded );
2639- 
2640-     // Fallback: If EINVAL, try ASCII encoding with "replace" 
2629+     int  rc  =  _set_thread_name_encoded (name_obj , encoding , NULL );
26412630    if  (rc  ==  EINVAL ) {
2642-         name_encoded  =  PyUnicode_AsEncodedString (name_obj , "ascii" , "replace" );
2643-         if  (name_encoded  ==  NULL ) {
2644-             return  NULL ;
2645-         }
2646- #ifdef  _PYTHREAD_NAME_MAXLEN 
2647-         if  (PyBytes_GET_SIZE (name_encoded ) >  _PYTHREAD_NAME_MAXLEN ) {
2648-             PyObject  * truncated  =  PyBytes_FromStringAndSize (PyBytes_AS_STRING (name_encoded ), _PYTHREAD_NAME_MAXLEN );
2649-             if  (truncated  ==  NULL ) {
2650-                 Py_DECREF (name_encoded );
2651-                 return  NULL ;
2652-             }
2653-             Py_SETREF (name_encoded , truncated );
2631+         int  rc2  =  _set_thread_name_encoded (name_obj , "ascii" , NULL );
2632+         if  (rc2  !=  0 ) {
2633+             errno  =  rc2 ;
2634+             return  PyErr_SetFromErrno (PyExc_OSError );
26542635        }
2655- #endif 
2656-         name  =  PyBytes_AS_STRING (name_encoded );
2657-         rc  =  _set_thread_name (name );
2658-         Py_DECREF (name_encoded );
2659-     }
2660- 
2661-     if  (rc ) {
2636+     } else  if  (rc  !=  0 ) {
26622637        errno  =  rc ;
26632638        return  PyErr_SetFromErrno (PyExc_OSError );
26642639    }
0 commit comments