Skip to content

Commit fe8db63

Browse files
gh-97527: IDLE: protect macosx Tk() call when no GUI (GH-97530)
Only call tkinter.tk and its follow-up code in _init_tk_type when requires('gui') does not raise. This function can be called as an unintended side-effect of calling other idlelib code as part of tests on macOS without a GUI enabled. (cherry picked from commit 9704f8d) Co-authored-by: Terry Jan Reedy <[email protected]>
1 parent c2916d2 commit fe8db63

File tree

1 file changed

+17
-13
lines changed

1 file changed

+17
-13
lines changed

Lib/idlelib/macosx.py

+17-13
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from os.path import expanduser
55
import plistlib
66
from sys import platform # Used in _init_tk_type, changed by test.
7+
from test.support import requires, ResourceDenied
78

89
import tkinter
910

@@ -14,23 +15,26 @@
1415
_tk_type = None
1516

1617
def _init_tk_type():
17-
"""
18-
Initializes OS X Tk variant values for
19-
isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
18+
""" Initialize _tk_type for isXyzTk functions.
2019
"""
2120
global _tk_type
2221
if platform == 'darwin':
23-
root = tkinter.Tk()
24-
ws = root.tk.call('tk', 'windowingsystem')
25-
if 'x11' in ws:
26-
_tk_type = "xquartz"
27-
elif 'aqua' not in ws:
28-
_tk_type = "other"
29-
elif 'AppKit' in root.tk.call('winfo', 'server', '.'):
30-
_tk_type = "cocoa"
22+
try:
23+
requires('gui')
24+
except ResourceDenied: # Possible when testing.
25+
_tk_type = "cocoa" # Newest and most common.
3126
else:
32-
_tk_type = "carbon"
33-
root.destroy()
27+
root = tkinter.Tk()
28+
ws = root.tk.call('tk', 'windowingsystem')
29+
if 'x11' in ws:
30+
_tk_type = "xquartz"
31+
elif 'aqua' not in ws:
32+
_tk_type = "other"
33+
elif 'AppKit' in root.tk.call('winfo', 'server', '.'):
34+
_tk_type = "cocoa"
35+
else:
36+
_tk_type = "carbon"
37+
root.destroy()
3438
else:
3539
_tk_type = "other"
3640

0 commit comments

Comments
 (0)