-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
gh-59705: Implement _thread.set_name() on Windows #128675
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
479c7fb
d588103
8f2ead7
f1c5232
ba09ba1
8148a96
0ff0567
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,6 +47,14 @@ get_thread_state(PyObject *module) | |
} | ||
|
||
|
||
#ifdef MS_WINDOWS | ||
typedef HRESULT (WINAPI *PF_GET_THREAD_DESCRIPTION)(HANDLE, PCWSTR*); | ||
typedef HRESULT (WINAPI *PF_SET_THREAD_DESCRIPTION)(HANDLE, PCWSTR); | ||
static PF_GET_THREAD_DESCRIPTION pGetThreadDescription = NULL; | ||
static PF_SET_THREAD_DESCRIPTION pSetThreadDescription = NULL; | ||
#endif | ||
|
||
|
||
/*[clinic input] | ||
module _thread | ||
[clinic start generated code]*/ | ||
|
@@ -2364,7 +2372,7 @@ Internal only. Return a non-zero integer that uniquely identifies the main threa | |
of the main interpreter."); | ||
|
||
|
||
#ifdef HAVE_PTHREAD_GETNAME_NP | ||
#if defined(HAVE_PTHREAD_GETNAME_NP) || defined(MS_WINDOWS) | ||
/*[clinic input] | ||
_thread._get_name | ||
|
||
|
@@ -2375,6 +2383,7 @@ static PyObject * | |
_thread__get_name_impl(PyObject *module) | ||
/*[clinic end generated code: output=20026e7ee3da3dd7 input=35cec676833d04c8]*/ | ||
{ | ||
#ifndef MS_WINDOWS | ||
// Linux and macOS are limited to respectively 16 and 64 bytes | ||
char name[100]; | ||
pthread_t thread = pthread_self(); | ||
|
@@ -2389,11 +2398,26 @@ _thread__get_name_impl(PyObject *module) | |
#else | ||
return PyUnicode_DecodeFSDefault(name); | ||
#endif | ||
#else | ||
// Windows implementation | ||
assert(pGetThreadDescription != NULL); | ||
|
||
wchar_t *name; | ||
HRESULT hr = pGetThreadDescription(GetCurrentThread(), &name); | ||
if (FAILED(hr)) { | ||
PyErr_SetFromWindowsErr(0); | ||
return NULL; | ||
} | ||
|
||
PyObject *name_obj = PyUnicode_FromWideChar(name, -1); | ||
LocalFree(name); | ||
return name_obj; | ||
#endif | ||
} | ||
#endif // HAVE_PTHREAD_GETNAME_NP | ||
|
||
|
||
#ifdef HAVE_PTHREAD_SETNAME_NP | ||
#if defined(HAVE_PTHREAD_SETNAME_NP) || defined(MS_WINDOWS) | ||
/*[clinic input] | ||
_thread.set_name | ||
|
||
|
@@ -2406,6 +2430,7 @@ static PyObject * | |
_thread_set_name_impl(PyObject *module, PyObject *name_obj) | ||
/*[clinic end generated code: output=402b0c68e0c0daed input=7e7acd98261be82f]*/ | ||
{ | ||
#ifndef MS_WINDOWS | ||
#ifdef __sun | ||
// Solaris always uses UTF-8 | ||
const char *encoding = "utf-8"; | ||
|
@@ -2451,6 +2476,35 @@ _thread_set_name_impl(PyObject *module, PyObject *name_obj) | |
return PyErr_SetFromErrno(PyExc_OSError); | ||
} | ||
Py_RETURN_NONE; | ||
#else | ||
// Windows implementation | ||
assert(pSetThreadDescription != NULL); | ||
|
||
Py_ssize_t len; | ||
wchar_t *name = PyUnicode_AsWideCharString(name_obj, &len); | ||
if (name == NULL) { | ||
return NULL; | ||
} | ||
|
||
if (len > PYTHREAD_NAME_MAXLEN) { | ||
// Truncate the name | ||
Py_UCS4 ch = name[PYTHREAD_NAME_MAXLEN-1]; | ||
if (Py_UNICODE_IS_HIGH_SURROGATE(ch)) { | ||
name[PYTHREAD_NAME_MAXLEN-1] = 0; | ||
} | ||
else { | ||
name[PYTHREAD_NAME_MAXLEN] = 0; | ||
} | ||
} | ||
|
||
HRESULT hr = pSetThreadDescription(GetCurrentThread(), name); | ||
PyMem_Free(name); | ||
if (FAILED(hr)) { | ||
PyErr_SetFromWindowsErr(0); | ||
return NULL; | ||
} | ||
Py_RETURN_NONE; | ||
#endif | ||
} | ||
#endif // HAVE_PTHREAD_SETNAME_NP | ||
|
||
|
@@ -2594,6 +2648,31 @@ thread_module_exec(PyObject *module) | |
} | ||
#endif | ||
|
||
#ifdef MS_WINDOWS | ||
HMODULE kernelbase = GetModuleHandleW(L"kernelbase.dll"); | ||
if (kernelbase != NULL) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this check fails, we likely want to report a system error since everything is broken, but at least we should call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oops, my code was wrong. Fixed. |
||
if (pGetThreadDescription == NULL) { | ||
pGetThreadDescription = (PF_GET_THREAD_DESCRIPTION)GetProcAddress( | ||
kernelbase, "GetThreadDescription"); | ||
} | ||
if (pSetThreadDescription == NULL) { | ||
pSetThreadDescription = (PF_SET_THREAD_DESCRIPTION)GetProcAddress( | ||
kernelbase, "SetThreadDescription"); | ||
} | ||
|
||
if (pGetThreadDescription == NULL) { | ||
if (PyObject_DelAttrString(module, "_get_name") < 0) { | ||
return -1; | ||
} | ||
} | ||
if (pSetThreadDescription == NULL) { | ||
if (PyObject_DelAttrString(module, "set_name") < 0) { | ||
return -1; | ||
} | ||
} | ||
} | ||
#endif | ||
|
||
return 0; | ||
} | ||
|
||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -753,4 +753,8 @@ Py_NO_ENABLE_SHARED to find out. Also support MS_NO_COREDLL for b/w compat */ | |
/* Define if libssl has X509_VERIFY_PARAM_set1_host and related function */ | ||
#define HAVE_X509_VERIFY_PARAM_SET1_HOST 1 | ||
|
||
// Truncate the thread name to 64 characters. The OS limit is 32766 wide | ||
// characters, but long names aren't of practical use. | ||
#define PYTHREAD_NAME_MAXLEN 64 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's no reason for us to artificially limit this at all, but I'd rather be much closer to the real limit than this. "Aren't of practical use" is a value judgement, not a technical limit. Also, there's no C API for this, so we probably don't need a public C constant for the limit. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed the limit to 32766 characters. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I can rename the macro use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I created #128945 to make the macro private. |
||
|
||
#endif /* !Py_CONFIG_H */ |
Uh oh!
There was an error while loading. Please reload this page.