Skip to content

Commit d9ba9e8

Browse files
committed
revert
1 parent 1ab9ecd commit d9ba9e8

File tree

1 file changed

+38
-70
lines changed

1 file changed

+38
-70
lines changed

Modules/_threadmodule.c

Lines changed: 38 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -75,93 +75,61 @@ get_thread_state_by_cls(PyTypeObject *cls)
7575
return get_thread_state(module);
7676
}
7777

78+
// Helper to set the thread name using platform-specific APIs
7879
static int
79-
set_thread_name_with_encoding(PyObject *name_obj, const char *encoding)
80+
set_native_thread_name(const char *name)
8081
{
81-
PyObject *name_encoded = NULL;
82-
83-
#ifndef MS_WINDOWS
84-
/* Determine encoding to use. If encoding arg is NULL, use FS codec. */
85-
const char *enc = encoding;
86-
#ifdef __sun
87-
enc = "utf-8"; /* Solaris always uses UTF-8 */
88-
#else
89-
if (enc == NULL) {
90-
PyInterpreterState *interp = _PyInterpreterState_GET();
91-
enc = interp->unicode.fs_codec.encoding;
92-
}
93-
#endif
94-
95-
name_encoded = PyUnicode_AsEncodedString(name_obj, enc, "replace");
96-
if (name_encoded == NULL) {
97-
return -1; /* Python exception set */
98-
}
99-
100-
#ifdef _PYTHREAD_NAME_MAXLEN
101-
/* Truncate to _PYTHREAD_NAME_MAXLEN bytes if needed */
102-
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
103-
PyObject *truncated = PyBytes_FromStringAndSize(
104-
PyBytes_AS_STRING(name_encoded),
105-
_PYTHREAD_NAME_MAXLEN);
106-
if (truncated == NULL) {
107-
Py_DECREF(name_encoded);
108-
return -1; /* Python exception set */
109-
}
110-
Py_SETREF(name_encoded, truncated);
111-
}
112-
#endif
113-
114-
const char *name = PyBytes_AS_STRING(name_encoded);
115-
82+
int rc;
11683
#ifdef __APPLE__
117-
int rc = pthread_setname_np(name);
84+
rc = pthread_setname_np(name);
11885
#elif defined(__NetBSD__)
11986
pthread_t thread = pthread_self();
120-
int rc = pthread_setname_np(thread, "%s", (void *)name);
87+
rc = pthread_setname_np(thread, "%s", (void *)name);
12188
#elif defined(HAVE_PTHREAD_SETNAME_NP)
12289
pthread_t thread = pthread_self();
123-
int rc = pthread_setname_np(thread, name);
124-
#else
125-
/* pthread_set_name_np() (void) on some platforms */
90+
rc = pthread_setname_np(thread, name);
91+
#elif defined(HAVE_PTHREAD_SET_NAME_NP)
12692
pthread_t thread = pthread_self();
127-
int rc = 0;
12893
pthread_set_name_np(thread, name);
94+
rc = 0; /* pthread_set_name_np() returns void */
12995
#endif
96+
return rc;
97+
}
13098

131-
Py_DECREF(name_encoded);
132-
return rc; /* 0 on success, errno-style >0 on error */
133-
#else
134-
/* Windows: convert to wide string and call SetThreadDescription */
135-
assert(pSetThreadDescription != NULL);
136-
Py_ssize_t len;
137-
wchar_t *wname = PyUnicode_AsWideCharString(name_obj, &len);
138-
if (wname == NULL) {
139-
return -1; /* Python exception set */
99+
// Helper to encode and truncate thread name
100+
static PyObject *
101+
encode_thread_name(PyObject *name_obj, const char *encoding)
102+
{
103+
#ifdef __sun
104+
// Solaris always uses UTF-8
105+
encoding = "utf-8";
106+
#endif
107+
PyObject *name_encoded = PyUnicode_AsEncodedString(name_obj, encoding, "replace");
108+
if (name_encoded == NULL) {
109+
return NULL;
140110
}
141-
142-
/* Truncate if necessary (len is number of wchar_t characters) */
143111
#ifdef _PYTHREAD_NAME_MAXLEN
144-
if (len > _PYTHREAD_NAME_MAXLEN) {
145-
/* Ensure we null-terminate safely. Use maxlen as max characters allowed. */
146-
/* If the char at max-1 is a high surrogate, avoid chopping the surrogate pair. */
147-
Py_UCS4 ch = (Py_UCS4)wname[_PYTHREAD_NAME_MAXLEN - 1];
148-
if (Py_UNICODE_IS_HIGH_SURROGATE(ch) && _PYTHREAD_NAME_MAXLEN >= 2) {
149-
wname[_PYTHREAD_NAME_MAXLEN - 1] = L'\0';
150-
} else {
151-
wname[_PYTHREAD_NAME_MAXLEN] = L'\0';
152-
}
112+
if (PyBytes_GET_SIZE(name_encoded) > _PYTHREAD_NAME_MAXLEN) {
113+
PyObject *truncated = PyBytes_FromStringAndSize(PyBytes_AS_STRING(name_encoded), _PYTHREAD_NAME_MAXLEN);
114+
Py_DECREF(name_encoded);
115+
return truncated;
153116
}
154117
#endif
118+
return name_encoded;
119+
}
155120

156-
HRESULT hr = pSetThreadDescription(GetCurrentThread(), wname);
157-
PyMem_Free(wname);
158-
if (FAILED(hr)) {
159-
/* Convert to a Python exception and return -1 so caller propagates it */
160-
PyErr_SetFromWindowsErr((int)hr);
161-
return -1;
121+
// Helper to encode, set, and cleanup thread name in one step
122+
static int
123+
set_thread_name_with_encoding(PyObject *name_obj, const char *encoding)
124+
{
125+
PyObject *name_encoded = encode_thread_name(name_obj, encoding);
126+
if (name_encoded == NULL) {
127+
return -1; // error, exception set
162128
}
163-
return 0;
164-
#endif
129+
const char *name = PyBytes_AS_STRING(name_encoded);
130+
int rc = set_native_thread_name(name);
131+
Py_DECREF(name_encoded);
132+
return rc;
165133
}
166134

167135
#ifdef MS_WINDOWS

0 commit comments

Comments
 (0)