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

Fix Windows registry access mode when adding icons to file type associations #206

Merged
merged 6 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion menuinst/platforms/win_utils/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ def register_file_extension(extension, identifier, command, icon=None, mode="use
log.debug("Created registry entry for command '%s'", command)

if icon:
subkey = winreg.OpenKey(key, identifier)
subkey = winreg.OpenKey(key, identifier, access=winreg.KEY_SET_VALUE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome catch!

We don't need this anywhere else for other keys, right? Thinking of file type association and so on.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only other key we open is here:

with winreg.OpenKeyEx(
(
winreg.HKEY_LOCAL_MACHINE if mode == "system" else winreg.HKEY_CURRENT_USER # HKLM
), # HKCU
r"Software\Classes",
) as key:

This is done with write-only access as the default. However, we only write to subkeys and the documentation there is a little confusing:

The key identified by the key parameter must have been opened with KEY_SET_VALUE access.

However, when a subkey is not null, the source code suggests that they are always opened with KEY_SET_VALUE access: https://github.com/python/cpython/blob/6d419db10c84cacbb3862e2adc940342175bfce9/PC/winreg.c#L1787-L1796

My interpretation is that the reason the icon fails is that we don't explicitly pass a subkey value, but instead operate on the key object itself.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. So for now no action needed but let's keep an eye for reports in case we need to pass more access flags. Thanks for the research!

winreg.SetValueEx(subkey, "DefaultIcon", 0, winreg.REG_SZ, icon)
log.debug("Created registry entry for icon '%s'", icon)

Expand Down
19 changes: 19 additions & 0 deletions news/206-file-extensions-regkey-access
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
### Enhancements

* <news item>

### Bug fixes

* Fix Windows registry key access mode when adding icon file to file type association. (#191 via #206)

### Deprecations

* <news item>

### Docs

* <news item>

### Other

* <news item>
1 change: 1 addition & 0 deletions tests/data/jsons/file_types.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
],
"platforms": {
"win": {
"icon": "doesnotexistbutitsok.{{ ICON_EXT }}",
"command": [
"{{ PYTHON }}",
"-c",
Expand Down
Loading