jenkins-bot has submitted this change and it was merged.
Change subject: Fix Win32 config.editor detection ......................................................................
Fix Win32 config.editor detection
Correctly extract the text editor from either .txt or .py extension registry values.
The winreg.EnumValue invocation was always failing as it used the wrong parameters.
The extracted text editor command may not work correctly if it used additional arguments other than "%1". See T102465.
Bug: T70315 Change-Id: I3143bb31ad33b1399ed09fd069b6dae3992926f3 --- M pywikibot/config2.py M pywikibot/editor.py 2 files changed, 56 insertions(+), 19 deletions(-)
Approvals: Merlijn van Deen: Looks good to me, approved jenkins-bot: Verified
diff --git a/pywikibot/config2.py b/pywikibot/config2.py index 559a9cf..f9dcab8 100644 --- a/pywikibot/config2.py +++ b/pywikibot/config2.py @@ -49,6 +49,12 @@
from warnings import warn
+if sys.platform == 'win32': + if sys.version_info[0] > 2: + import winreg + else: + import _winreg as winreg + # This frozen set should contain all imported modules/variables, so it must # occur directly after the imports. At that point globals() only contains the # names and some magic variables (like __name__) @@ -420,25 +426,6 @@ # editor will be used. editor = os.environ.get('EDITOR', None) # On Windows systems, this script tries to determine the default text editor. -if sys.platform == 'win32': - try: - if sys.version_info[0] > 2: - import winreg as _winreg - else: - import _winreg - _key1 = _winreg.OpenKey(_winreg.HKEY_CURRENT_USER, - 'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.txt\OpenWithProgids') - _progID = _winreg.EnumValue(_key1, 1)[0] - _key2 = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, - '%s\shell\open\command' % _progID) - _cmd = _winreg.QueryValueEx(_key2, None)[0] - _cmd = _cmd.replace('%1', '') - # Notepad is even worse than our Tkinter editor. - if not _cmd.lower().endswith('notepad.exe'): - editor = _cmd - except WindowsError: - # Catch any key lookup errors - pass
# Warning: DO NOT use an editor which doesn't support Unicode to edit pages! # You will BREAK non-ASCII symbols! @@ -847,6 +834,51 @@ if path.startswith(base_dir): return path[len(base_dir) + len(os.path.sep):] return path + + +def _win32_extension_command(extension): + """Get the command from the Win32 registry for an extension.""" + fileexts_key = r'Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts' + key_name = fileexts_key + r'.' + extension + r'\OpenWithProgids' + _winreg = winreg # exists for git blame only; do not use + try: + key1 = winreg.OpenKey(winreg.HKEY_CURRENT_USER, key_name) + _progID = winreg.EnumValue(key1, 0)[0] + _key2 = _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, + '%s\shell\open\command' % _progID) + _cmd = _winreg.QueryValueEx(_key2, None)[0] + # See T102465 for issues relating to using this value. + cmd = _cmd + if cmd.find('%1'): + cmd = cmd[:cmd.find('%1')] + # Remove any trailing characher, which should be a quote or space + # and then remove all whitespace. + return cmd[:-1].strip() + except WindowsError as e: + # Catch any key lookup errors + print('WARNING: Unable to find editor for files *.' + extension) + print(e) + + +def _detect_win32_editor(): + """Detect the best Win32 editor.""" + # Notepad is even worse than our Tkinter editor. + unusable_exes = ['notepad.exe', + 'py.exe', + 'pyw.exe', + 'python.exe', + 'pythonw.exe'] + + for ext in ['py', 'txt']: + editor = _win32_extension_command(ext) + if editor: + for unusable in unusable_exes: + if unusable in editor.lower(): + break + else: + return editor + + # System-level and User-level changes. # Store current variables and their types. _glv = dict((_key, _val) for _key, _val in globals().items() @@ -956,6 +988,10 @@ elif transliteration_target in ('None', 'none'): transliteration_target = None
+ +if sys.platform == 'win32' and editor is None: + editor = _detect_win32_editor() + if sys.platform == 'win32' and editor: # single character string literals from # https://docs.python.org/2/reference/lexical_analysis.html#string-literals diff --git a/pywikibot/editor.py b/pywikibot/editor.py index 7d0aa1b..f734bda 100644 --- a/pywikibot/editor.py +++ b/pywikibot/editor.py @@ -63,6 +63,7 @@ else: command = []
+ # See T102465 for problems relating to using config.editor unparsed. command = [config.editor] + command + [file_name] pywikibot.log(u'Running editor: %s' % TextEditor._concat(command)) return command
pywikibot-commits@lists.wikimedia.org