From 4a3f7b821ef360487c343d7cdc38fba6d1fdbdc4 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Sun, 11 Nov 2018 16:45:43 +0200 Subject: [PATCH 1/5] bpo-34864: warn if "Prefer tabs when opening documents" set to "Always" --- Lib/idlelib/macosx.py | 34 ++++++++++++++++++++++++++++++---- Lib/idlelib/outwin.py | 2 +- Lib/idlelib/pyshell.py | 25 +++++++++++++++++++++---- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Lib/idlelib/macosx.py b/Lib/idlelib/macosx.py index d3ae224100cff8..d8788bd56ee08c 100644 --- a/Lib/idlelib/macosx.py +++ b/Lib/idlelib/macosx.py @@ -1,6 +1,8 @@ """ A number of functions that enhance IDLE on Mac OSX. """ +from os.path import expanduser +import plistlib from sys import platform # Used in _init_tk_type, changed by test. import tkinter @@ -79,14 +81,38 @@ def tkVersionWarning(root): patchlevel = root.tk.call('info', 'patchlevel') if patchlevel not in ('8.5.7', '8.5.9'): return False - return (r"WARNING: The version of Tcl/Tk ({0}) in use may" - r" be unstable.\n" - r"Visit http://www.python.org/download/mac/tcltk/" - r" for current information.".format(patchlevel)) + return ("WARNING: The version of Tcl/Tk in use ({0}) may" + " be unstable.\n" + "Visit http://www.python.org/download/mac/tcltk/" + " for current information.".format(patchlevel)) else: return False +def readSystemPrefereces(): + """ + Fetch the macOS system preferences. + """ + plist_path = expanduser('~/Library/Preferences/.GlobalPreferences.plist') + with open(plist_path, 'rb') as plist_file: + return plistlib.load(plist_file) + + +def preferTabsPreferenceWarning(): + """ + Warn if "Prefer tabs when opening documents" is set to "Always". + """ + prefs = readSystemPrefereces() + if prefs['AppleWindowTabbingMode'] == 'always': + return ( + 'WARNING: The system preferece "Prefer tabs when opening documents"' + ' is set to "Always". This will cause various problems with IDLE.' + ' For the best expereince, change this setting before running IDLE' + ' (via System Preferences -> Dock).' + ) + return None + + ## Fix the menu and related functions. def addOpenEventSupport(root, flist): diff --git a/Lib/idlelib/outwin.py b/Lib/idlelib/outwin.py index 4af9f1afaed518..e962142498dfd8 100644 --- a/Lib/idlelib/outwin.py +++ b/Lib/idlelib/outwin.py @@ -109,7 +109,7 @@ def write(self, s, tags=(), mark="insert"): Return: Length of text inserted. """ - if isinstance(s, (bytes, bytes)): + if isinstance(s, bytes): s = s.decode(iomenu.encoding, "replace") self.text.insert(mark, s, tags) self.text.see(mark) diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index 47eef4399ce6b3..f831deb4e4d0db 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -38,6 +38,7 @@ import re import socket import subprocess +from textwrap import TextWrapper import threading import time import tokenize @@ -1273,6 +1274,14 @@ def showprompt(self): self.set_line_and_column() self.io.reset_undo() + def show_warning(self, msg): + width = self.interp.tkconsole.width + wrapper = TextWrapper(width=width, tabsize=8, expand_tabs=True) + wrapped_msg = '\n'.join(wrapper.wrap(msg)) + if not wrapped_msg.endswith('\n'): + wrapped_msg += '\n' + self.per.bottom.insert("iomark linestart", wrapped_msg, "stderr") + def resetoutput(self): source = self.text.get("iomark", "end-1c") if self.history: @@ -1541,12 +1550,20 @@ def main(): shell.interp.execfile(script) elif shell: # If there is a shell window and no cmd or script in progress, - # check for problematic OS X Tk versions and print a warning - # message in the IDLE shell window; this is less intrusive - # than always opening a separate window. + # check for problematic issues and print warning message(s) in + # the IDLE shell window; this is less intrusive than always + # opening a separate window. + + # Warn if using a problematic OS X Tk version. tkversionwarning = macosx.tkVersionWarning(root) if tkversionwarning: - shell.interp.runcommand("print('%s')" % tkversionwarning) + shell.show_warning(tkversionwarning) + + # Warn if the "Prefer tabs when opening documents" system + # prference is set to "Always". + preferTabsPreferenceWarning = macosx.preferTabsPreferenceWarning() + if preferTabsPreferenceWarning: + shell.show_warning(preferTabsPreferenceWarning) while flist.inversedict: # keep IDLE running while files are open. root.mainloop() From 283dc54d5ae464d7b6ac7846a66e0fb281033f58 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Sun, 11 Nov 2018 17:14:22 +0200 Subject: [PATCH 2/5] add NEWS entry --- Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst diff --git a/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst b/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst new file mode 100644 index 00000000000000..6558c989f809ed --- /dev/null +++ b/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst @@ -0,0 +1 @@ +On macOS, warn if "Prefer tabs when opening documents" is set to "Always". From deb0ca911d1ee21dc88721e304b8138dc75a7b6c Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Sun, 11 Nov 2018 23:37:26 +0200 Subject: [PATCH 3/5] address code review comments --- Lib/idlelib/macosx.py | 23 +++++++++++++---------- Lib/idlelib/pyshell.py | 8 ++++---- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/Lib/idlelib/macosx.py b/Lib/idlelib/macosx.py index d8788bd56ee08c..9ab99674529c24 100644 --- a/Lib/idlelib/macosx.py +++ b/Lib/idlelib/macosx.py @@ -81,7 +81,7 @@ def tkVersionWarning(root): patchlevel = root.tk.call('info', 'patchlevel') if patchlevel not in ('8.5.7', '8.5.9'): return False - return ("WARNING: The version of Tcl/Tk in use ({0}) may" + return ("WARNING: The version of Tcl/Tk ({0}) in use may" " be unstable.\n" "Visit http://www.python.org/download/mac/tcltk/" " for current information.".format(patchlevel)) @@ -89,26 +89,29 @@ def tkVersionWarning(root): return False -def readSystemPrefereces(): +def readSystemPreferences(): """ Fetch the macOS system preferences. """ plist_path = expanduser('~/Library/Preferences/.GlobalPreferences.plist') - with open(plist_path, 'rb') as plist_file: - return plistlib.load(plist_file) + try: + with open(plist_path, 'rb') as plist_file: + return plistlib.load(plist_file) + except OSError: + return None def preferTabsPreferenceWarning(): """ Warn if "Prefer tabs when opening documents" is set to "Always". """ - prefs = readSystemPrefereces() - if prefs['AppleWindowTabbingMode'] == 'always': + prefs = readSystemPreferences() + if prefs and prefs.get('AppleWindowTabbingMode') == 'always': return ( - 'WARNING: The system preferece "Prefer tabs when opening documents"' - ' is set to "Always". This will cause various problems with IDLE.' - ' For the best expereince, change this setting before running IDLE' - ' (via System Preferences -> Dock).' + 'WARNING: The system preference "Prefer tabs when opening' + ' documents" is set to "Always". This will cause various problems' + ' with IDLE. For the best experience, change this setting before' + ' running IDLE (via System Preferences -> Dock).' ) return None diff --git a/Lib/idlelib/pyshell.py b/Lib/idlelib/pyshell.py index f831deb4e4d0db..81a97ef6d6bcc5 100755 --- a/Lib/idlelib/pyshell.py +++ b/Lib/idlelib/pyshell.py @@ -1560,10 +1560,10 @@ def main(): shell.show_warning(tkversionwarning) # Warn if the "Prefer tabs when opening documents" system - # prference is set to "Always". - preferTabsPreferenceWarning = macosx.preferTabsPreferenceWarning() - if preferTabsPreferenceWarning: - shell.show_warning(preferTabsPreferenceWarning) + # preference is set to "Always". + prefer_tabs_preference_warning = macosx.preferTabsPreferenceWarning() + if prefer_tabs_preference_warning: + shell.show_warning(prefer_tabs_preference_warning) while flist.inversedict: # keep IDLE running while files are open. root.mainloop() From 81975d8f10fa52dfcfefd59c3f34d6aff4c73421 Mon Sep 17 00:00:00 2001 From: Tal Einat Date: Mon, 12 Nov 2018 08:24:10 +0200 Subject: [PATCH 4/5] address second code review comments --- Lib/idlelib/macosx.py | 8 +++++++- .../next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst | 3 ++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/Lib/idlelib/macosx.py b/Lib/idlelib/macosx.py index 9ab99674529c24..bbf9c6555bfd0a 100644 --- a/Lib/idlelib/macosx.py +++ b/Lib/idlelib/macosx.py @@ -93,6 +93,9 @@ def readSystemPreferences(): """ Fetch the macOS system preferences. """ + if platform != 'darwin': + return None + plist_path = expanduser('~/Library/Preferences/.GlobalPreferences.plist') try: with open(plist_path, 'rb') as plist_file: @@ -105,12 +108,15 @@ def preferTabsPreferenceWarning(): """ Warn if "Prefer tabs when opening documents" is set to "Always". """ + if platform != 'darwin': + return None + prefs = readSystemPreferences() if prefs and prefs.get('AppleWindowTabbingMode') == 'always': return ( 'WARNING: The system preference "Prefer tabs when opening' ' documents" is set to "Always". This will cause various problems' - ' with IDLE. For the best experience, change this setting before' + ' with IDLE. For the best experience, change this setting when' ' running IDLE (via System Preferences -> Dock).' ) return None diff --git a/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst b/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst index 6558c989f809ed..8d2b61d599ff56 100644 --- a/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst +++ b/Misc/NEWS.d/next/IDLE/2018-11-11-17-13-50.bpo-34864.cw0PvO.rst @@ -1 +1,2 @@ -On macOS, warn if "Prefer tabs when opening documents" is set to "Always". +On macOS, warn if the system preference "Prefer tabs when opening documents" +is set to "Always". \ No newline at end of file From a523b54e8f5997fb5966958cbfbbfafdea78e139 Mon Sep 17 00:00:00 2001 From: Terry Jan Reedy Date: Thu, 6 Dec 2018 23:10:04 -0500 Subject: [PATCH 5/5] Add entry for idlelib/NEWS.txt. --- Lib/idlelib/NEWS.txt | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/idlelib/NEWS.txt b/Lib/idlelib/NEWS.txt index 9a16ece3bbe3f7..6e11cab586008f 100644 --- a/Lib/idlelib/NEWS.txt +++ b/Lib/idlelib/NEWS.txt @@ -3,6 +3,12 @@ Released on 2019-10-20? ====================================== +bpo-34864: When starting IDLE on MacOS, warn if the system setting +"Prefer tabs when opening documents" is "Always". As previous +documented for this issue, running IDLE with this setting causes +problems. If the setting is changed while IDLE is running, +there will be no warning until IDLE is restarted. + bpo-35213: Where appropriate, use 'macOS' in idlelib. bpo-34864: Document two IDLE on MacOS issues. The System Preferences