GeezerVison is showing github.com — gh-152433: Windows: change ``winreg`` to conditional import for UWP compatibility by thexai · Pull Request #154918 · python/cpython
Conversation
thexai
commented
Jul 30, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
In current code, winreg module is not build because code guards not include MS_WINDOWS_APP:
Instead of add MS_WINDOWS_APP condition, guards removed when is:
#if defined(MS_WINDOWS_DESKTOP) || defined(MS_WINDOWS_SYSTEM) || defined(MS_WINDOWS_GAMES)
(as includes all Windows families)
Or replaced with MS_WINDOWS when code is for all platforms.
The clinic's code also regenerated, resulting in the removal of many redundant conditions.
Note that Windows Registry is not accessible from UWP Apps (or Games) but for consistency is necessary build module as is imported in many places at Python initialization. The same logic was already being applied with MS_WINDOWS_GAMES build.
Windows Registry is not accessible from UWP Apps (or Games), since module is not build, changed the import to conditional to able initialize Python in UWP without winreg module.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Out of interest, what would it look like to make those imports conditional? They ought to be anyway, and I suspect we'll end up with better overall behaviour if they handle this as "no registry access exists" rather than "registry accesses always fail".
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
thexai
commented
Jul 30, 2026
•
edited
Loading
Uh oh!
There was an error while loading. Please reload this page.
|
I don't really know how it would be done because it requires modifying a lot of Python code and introduce helper functions or constants to identify UWP / GAMES builds as "another platform". This is beyond the scope of this PR.
In addition to this, I want to avoid modifying the behavior of MS_WINDOWS_GAMES builds because I have no easy way to test it (nor any particular interest).
This PR allows compile the current code for UWP and is actually a simplification because it removes 185 lines of code. Without this PR, the resulting UWP builds won't work because they try to import a module that doesn't exist --> hard crash.
With this PR, you can use the rest of the Python code in UWP, and even if a Python script includes import winreg lines and the the script handles the exceptions, return values, etc. nothing bad happens.
Previously, Windows Registry was available for "Windows Phone" in the MS_WINDOWS_APP partition, and most of the Win32 Registry API is still defined. This is why compile winreg.c code for MS_WINDOWS_APP/GAMES is possible and doesn't produce errors. I'm not actually sure if accessing the Registry outside of classic Desktop environment is still useful on other types of devices (Surface or ARM64 devices).
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
Having had a quick look, it seems like Lib\importlib\_bootstrap_external.py is the only one that needs handling. getpath.py and the rest of the standard library uses are protected (apart from one in IDLE, which I assume you don't care about).
Some of the test suite also assumes the module will be there, and those ought to be handled either at the winreg import or the command importing the module that requires it.
There's no need to modify anything conditionally on the build, it's just going to be something like:
try:
import winreg
except ImportError:
winreg = None
...
if winreg:
# do the thing requiring the registry
else:
# fallback behaviour if needed
For _bootstrap_external.py I think it's just an entire block that's already optional for non-Windows, so that can become for non-winreg, and it should be good.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Having had a quick look, it seems like Lib\importlib_bootstrap_external.py is the only one that needs handling.
try:
import winreg
except ImportError:
winreg = None
Tested this change on UWP build and works fine and seems is enough. I'm going to update the PR with this.
|
chris-eibl reacted with thumbs up emoji
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
thexai
changed the title
gh-152433: Windows: Allow build winreg module for UWP
gh-152433: Windows: change winreg to conditional import for UWP compatibility
Jul 31, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@zooba's suggestion works, so approving.
This is sitting for a while now. If nobody objects, I'm gonna merge end of next week at the latest.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
thexai reacted with thumbs up emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|