Skip to content

Commit 9ebe879

Browse files
taleinatterryjreedy
authored andcommitted
bpo-34864: warn if "Prefer tabs when opening documents" set to "Always" (python#10464)
* bpo-34864: warn if "Prefer tabs when opening documents" set to "Always" * add NEWS entry * address code review comments * address second code review comments * Add entry for idlelib/NEWS.txt.
1 parent 16501b7 commit 9ebe879

File tree

5 files changed

+69
-9
lines changed

5 files changed

+69
-9
lines changed

Lib/idlelib/NEWS.txt

+6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ Released on 2019-10-20?
33
======================================
44

55

6+
bpo-34864: When starting IDLE on MacOS, warn if the system setting
7+
"Prefer tabs when opening documents" is "Always". As previous
8+
documented for this issue, running IDLE with this setting causes
9+
problems. If the setting is changed while IDLE is running,
10+
there will be no warning until IDLE is restarted.
11+
612
bpo-35213: Where appropriate, use 'macOS' in idlelib.
713

814
bpo-34864: Document two IDLE on MacOS issues. The System Preferences

Lib/idlelib/macosx.py

+39-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""
22
A number of functions that enhance IDLE on macOS.
33
"""
4+
from os.path import expanduser
5+
import plistlib
46
from sys import platform # Used in _init_tk_type, changed by test.
57

68
import tkinter
@@ -79,14 +81,47 @@ def tkVersionWarning(root):
7981
patchlevel = root.tk.call('info', 'patchlevel')
8082
if patchlevel not in ('8.5.7', '8.5.9'):
8183
return False
82-
return (r"WARNING: The version of Tcl/Tk ({0}) in use may"
83-
r" be unstable.\n"
84-
r"Visit http://www.python.org/download/mac/tcltk/"
85-
r" for current information.".format(patchlevel))
84+
return ("WARNING: The version of Tcl/Tk ({0}) in use may"
85+
" be unstable.\n"
86+
"Visit http://www.python.org/download/mac/tcltk/"
87+
" for current information.".format(patchlevel))
8688
else:
8789
return False
8890

8991

92+
def readSystemPreferences():
93+
"""
94+
Fetch the macOS system preferences.
95+
"""
96+
if platform != 'darwin':
97+
return None
98+
99+
plist_path = expanduser('~/Library/Preferences/.GlobalPreferences.plist')
100+
try:
101+
with open(plist_path, 'rb') as plist_file:
102+
return plistlib.load(plist_file)
103+
except OSError:
104+
return None
105+
106+
107+
def preferTabsPreferenceWarning():
108+
"""
109+
Warn if "Prefer tabs when opening documents" is set to "Always".
110+
"""
111+
if platform != 'darwin':
112+
return None
113+
114+
prefs = readSystemPreferences()
115+
if prefs and prefs.get('AppleWindowTabbingMode') == 'always':
116+
return (
117+
'WARNING: The system preference "Prefer tabs when opening'
118+
' documents" is set to "Always". This will cause various problems'
119+
' with IDLE. For the best experience, change this setting when'
120+
' running IDLE (via System Preferences -> Dock).'
121+
)
122+
return None
123+
124+
90125
## Fix the menu and related functions.
91126

92127
def addOpenEventSupport(root, flist):

Lib/idlelib/outwin.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def write(self, s, tags=(), mark="insert"):
109109
Return:
110110
Length of text inserted.
111111
"""
112-
if isinstance(s, (bytes, bytes)):
112+
if isinstance(s, bytes):
113113
s = s.decode(iomenu.encoding, "replace")
114114
self.text.insert(mark, s, tags)
115115
self.text.see(mark)

Lib/idlelib/pyshell.py

+21-4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import re
3939
import socket
4040
import subprocess
41+
from textwrap import TextWrapper
4142
import threading
4243
import time
4344
import tokenize
@@ -1273,6 +1274,14 @@ def showprompt(self):
12731274
self.set_line_and_column()
12741275
self.io.reset_undo()
12751276

1277+
def show_warning(self, msg):
1278+
width = self.interp.tkconsole.width
1279+
wrapper = TextWrapper(width=width, tabsize=8, expand_tabs=True)
1280+
wrapped_msg = '\n'.join(wrapper.wrap(msg))
1281+
if not wrapped_msg.endswith('\n'):
1282+
wrapped_msg += '\n'
1283+
self.per.bottom.insert("iomark linestart", wrapped_msg, "stderr")
1284+
12761285
def resetoutput(self):
12771286
source = self.text.get("iomark", "end-1c")
12781287
if self.history:
@@ -1541,12 +1550,20 @@ def main():
15411550
shell.interp.execfile(script)
15421551
elif shell:
15431552
# If there is a shell window and no cmd or script in progress,
1544-
# check for problematic OS X Tk versions and print a warning
1545-
# message in the IDLE shell window; this is less intrusive
1546-
# than always opening a separate window.
1553+
# check for problematic issues and print warning message(s) in
1554+
# the IDLE shell window; this is less intrusive than always
1555+
# opening a separate window.
1556+
1557+
# Warn if using a problematic OS X Tk version.
15471558
tkversionwarning = macosx.tkVersionWarning(root)
15481559
if tkversionwarning:
1549-
shell.interp.runcommand("print('%s')" % tkversionwarning)
1560+
shell.show_warning(tkversionwarning)
1561+
1562+
# Warn if the "Prefer tabs when opening documents" system
1563+
# preference is set to "Always".
1564+
prefer_tabs_preference_warning = macosx.preferTabsPreferenceWarning()
1565+
if prefer_tabs_preference_warning:
1566+
shell.show_warning(prefer_tabs_preference_warning)
15501567

15511568
while flist.inversedict: # keep IDLE running while files are open.
15521569
root.mainloop()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
On macOS, warn if the system preference "Prefer tabs when opening documents"
2+
is set to "Always".

0 commit comments

Comments
 (0)