Skip to content
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

gh-59705: Implement _thread.set_name() on Windows #128675

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jan 9, 2025

Implement set_name() with SetThreadDescription() and _get_name() with GetThreadDescription(). If SetThreadDescription() or GetThreadDescription() is not available in kernelbase.dll, delete the method when the _thread module is imported.

Truncate the thread name to an arbitrary limit of 64 characters.

set_name() raises ValueError if the name contains an embedded null character.

Implement set_name() with SetThreadDescription() and _get_name() with
GetThreadDescription(). If SetThreadDescription() or
GetThreadDescription() is not available in kernelbase.dll, delete the
method when the _thread module is imported.

Truncate the thread name to an arbitrary limit of 64 characters.

set_name() raises ValueError if the name contains an embedded null
character.

Co-authored-by: Eryk Sun <[email protected]>
@vstinner
Copy link
Member Author

vstinner commented Jan 9, 2025

Code based on @eryksun's code: #59705 (comment). Differences with his code:

  • I only look for functions in kernelbase.dll, not in api-ms-win-core-processthreads-l1-1-3.dll (kernel32).
  • set_name() doesn't ignore E_NOTIMPL. That's fine since the threading module ignores any OSError. If Python is run on Windows older than 10.0.14393 (1607), set_name() failure would just be ignored silently.
  • I used the PyUnicode API to convert to/from wchar_t*.

Truncate the thread name to an arbitrary limit of 64 characters.

I copied this limit from @eryksun's code. Maybe it can be extended to 32766 characters?

set_name() raises ValueError if the name contains an embedded null character.

If it's a blocker issue, I can write the code differently to truncate to the first null character instead.

@vstinner
Copy link
Member Author

vstinner commented Jan 9, 2025

cc @serhiy-storchaka

@vstinner
Copy link
Member Author

vstinner commented Jan 9, 2025

set_name() raises ValueError if the name contains an embedded null character.

I misunderstood the code, in fact, the name is truncated at the first null character (ValueError is not raised): same behavior than Linux/macOS/FreeBSD/etc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant