Repository navigation
bpo-30374: Fixed several bugs in win_add2path.py #1645
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 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
* Discard the DEFAULT "%PATH%" value. * Delete wrong `os.path.isdir()`. * Add absolute path instead of `%APPDATA%...` if the type of PATH is `REG_SZ`. * Broadcast a `WM_SETTINGCHANG` message after the change of env vars. * Add codes to deal with possible errors.
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,43 +11,67 @@ | |
| import site | ||
| import os | ||
| import winreg | ||
| import ctypes | ||
|
|
||
| HKCU = winreg.HKEY_CURRENT_USER | ||
| ENV = "Environment" | ||
| PATH = "PATH" | ||
| DEFAULT = "%PATH%" | ||
|
|
||
| def modify(): | ||
| pythonpath = os.path.dirname(os.path.normpath(sys.executable)) | ||
| scripts = os.path.join(pythonpath, "Scripts") | ||
| appdata = os.environ["APPDATA"] | ||
| if hasattr(site, "USER_SITE"): | ||
| usersite = site.USER_SITE.replace(appdata, "%APPDATA%") | ||
| userpath = os.path.dirname(usersite) | ||
| userscripts = os.path.join(userpath, "Scripts") | ||
| else: | ||
| userscripts = None | ||
|
|
||
| with winreg.CreateKey(HKCU, ENV) as key: | ||
| try: | ||
| envpath = winreg.QueryValueEx(key, PATH)[0] | ||
| except OSError: | ||
| envpath = DEFAULT | ||
| envpath, dtype = winreg.QueryValueEx(key, PATH) | ||
| except FileNotFoundError: | ||
| envpath, dtype = "", winreg.REG_EXPAND_SZ | ||
| pass | ||
|
Member
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. Unnecessary |
||
| except: | ||
| raise OSError("Failed to load PATH value") | ||
|
|
||
| if hasattr(site, "USER_SITE") and dtype == winreg.REG_EXPAND_SZ: | ||
|
Member
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. This |
||
| usersite = site.USER_SITE.replace(appdata, "%APPDATA%") | ||
| userpath = os.path.dirname(usersite) | ||
| userscripts = os.path.join(userpath, "Scripts") | ||
| elif dtype == winreg.REG_SZ: | ||
| userpath = site.USER_SITE | ||
| userscripts = os.path.join(userpath, "Scripts") | ||
| else: | ||
| userscripts = None | ||
|
|
||
| paths = [envpath] | ||
| paths = [] | ||
| for path in (pythonpath, scripts, userscripts): | ||
| if path and path not in envpath and os.path.isdir(path): | ||
| if path and path not in envpath: | ||
| paths.append(path) | ||
|
|
||
| envpath = os.pathsep.join(paths) | ||
| winreg.SetValueEx(key, PATH, 0, winreg.REG_EXPAND_SZ, envpath) | ||
| if envpath == "": | ||
| envpath = os.pathsep.join(paths) | ||
| else: | ||
| envpath = os.pathsep.join([envpath] + paths) | ||
| winreg.SetValueEx(key, PATH, 0, dtype, envpath) | ||
| return paths, envpath | ||
|
|
||
| def refresh_environment_variables(): | ||
| HWND_BROADCAST = 0xFFFF | ||
| WM_SETTINGCHANGE = 0x001A | ||
| SMTO_ABORTIFHUNG = 0x0002 | ||
| if not ctypes.windll.user32.SendMessageTimeoutW( | ||
| HWND_BROADCAST, | ||
| WM_SETTINGCHANGE, | ||
| 0, | ||
| ENV, | ||
| SMTO_ABORTIFHUNG, | ||
| 1000): | ||
| raise ctypes.WinError() | ||
|
|
||
| def main(): | ||
| paths, envpath = modify() | ||
| if len(paths) > 1: | ||
| refresh_environment_variables() | ||
| print("Path(s) added:") | ||
| print('\n'.join(paths[1:])) | ||
| print('\n'.join(paths)) | ||
| else: | ||
| print("No path was added") | ||
| print("\nPATH is now:\n%s\n" % envpath) | ||
|
|
||
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.
Put in alphabetical order