Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit fb51e65

Browse files
committed
Issue #27239: idlelib.macosx.isXyzTk functions initialize as needed.
1 parent 47791df commit fb51e65

4 files changed

Lines changed: 84 additions & 10 deletions

File tree

Lib/idlelib/idle_test/htest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ def _wrapper(parent): # htest #
6666
'''
6767

6868
from importlib import import_module
69-
from idlelib.macosx import _initializeTkVariantTests
69+
from idlelib.macosx import _init_tk_type
7070
import tkinter as tk
7171

7272
AboutDialog_spec = {
@@ -337,7 +337,7 @@ def run(*tests):
337337
root = tk.Tk()
338338
root.title('IDLE htest')
339339
root.resizable(0, 0)
340-
_initializeTkVariantTests(root)
340+
_init_tk_type(root)
341341

342342
# a scrollable Label like constant width text widget.
343343
frameLabel = tk.Frame(root, padx=10)

Lib/idlelib/idle_test/test_configdialog.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,12 @@
88
requires('gui')
99
from tkinter import Tk
1010
import unittest
11-
from idlelib import macosx
1211

1312
class ConfigDialogTest(unittest.TestCase):
1413

1514
@classmethod
1615
def setUpClass(cls):
1716
cls.root = Tk()
18-
macosx._initializeTkVariantTests(cls.root)
1917

2018
@classmethod
2119
def tearDownClass(cls):
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''Test idlelib.macosx.py
2+
'''
3+
from idlelib import macosx
4+
from test.support import requires
5+
import sys
6+
import tkinter as tk
7+
import unittest
8+
import unittest.mock as mock
9+
10+
MAC = sys.platform == 'darwin'
11+
mactypes = {'carbon', 'cocoa', 'xquartz'}
12+
nontypes = {'other'}
13+
alltypes = mactypes | nontypes
14+
15+
16+
class InitTktypeTest(unittest.TestCase):
17+
"Test _init_tk_type."
18+
19+
@classmethod
20+
def setUpClass(cls):
21+
requires('gui')
22+
cls.root = tk.Tk()
23+
24+
@classmethod
25+
def tearDownClass(cls):
26+
cls.root.update_idletasks()
27+
cls.root.destroy()
28+
del cls.root
29+
30+
def test_init_sets_tktype(self):
31+
"Test that _init_tk_type sets _tk_type according to platform."
32+
for root in (None, self.root):
33+
with self.subTest(root=root):
34+
macosx._tk_type == None
35+
macosx._init_tk_type(root)
36+
self.assertIn(macosx._tk_type,
37+
mactypes if MAC else nontypes)
38+
39+
40+
class IsTypeTkTest(unittest.TestCase):
41+
"Test each of the four isTypeTk predecates."
42+
isfuncs = ((macosx.isAquaTk, ('carbon', 'cocoa')),
43+
(macosx.isCarbonTk, ('carbon')),
44+
(macosx.isCocoaTk, ('cocoa')),
45+
(macosx.isXQuartz, ('xquartz')),
46+
)
47+
48+
@mock.patch('idlelib.macosx._init_tk_type')
49+
def test_is_calls_init(self, mockinit):
50+
"Test that each isTypeTk calls _init_tk_type when _tk_type is None."
51+
macosx._tk_type = None
52+
for func, whentrue in self.isfuncs:
53+
with self.subTest(func=func):
54+
func()
55+
self.assertTrue(mockinit.called)
56+
mockinit.reset_mock()
57+
58+
def test_isfuncs(self):
59+
"Test that each isTypeTk return correct bool."
60+
for func, whentrue in self.isfuncs:
61+
for tktype in alltypes:
62+
with self.subTest(func=func, whentrue=whentrue, tktype=tktype):
63+
macosx._tk_type = tktype
64+
(self.assertTrue if tktype in whentrue else self.assertFalse)\
65+
(func())
66+
67+
68+
if __name__ == '__main__':
69+
unittest.main(verbosity=2)

Lib/idlelib/macosx.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@
77

88
_tk_type = None
99

10-
def _initializeTkVariantTests(root):
10+
def _init_tk_type(idleroot=None):
1111
"""
1212
Initializes OS X Tk variant values for
1313
isAquaTk(), isCarbonTk(), isCocoaTk(), and isXQuartz().
1414
"""
1515
global _tk_type
1616
if sys.platform == 'darwin':
17+
root = idleroot or tkinter.Tk()
1718
ws = root.tk.call('tk', 'windowingsystem')
1819
if 'x11' in ws:
1920
_tk_type = "xquartz"
@@ -23,36 +24,42 @@ def _initializeTkVariantTests(root):
2324
_tk_type = "cocoa"
2425
else:
2526
_tk_type = "carbon"
27+
if not idleroot:
28+
root.destroy
2629
else:
2730
_tk_type = "other"
2831

2932
def isAquaTk():
3033
"""
3134
Returns True if IDLE is using a native OS X Tk (Cocoa or Carbon).
3235
"""
33-
assert _tk_type is not None
36+
if not _tk_type:
37+
_init_tk_type()
3438
return _tk_type == "cocoa" or _tk_type == "carbon"
3539

3640
def isCarbonTk():
3741
"""
3842
Returns True if IDLE is using a Carbon Aqua Tk (instead of the
3943
newer Cocoa Aqua Tk).
4044
"""
41-
assert _tk_type is not None
45+
if not _tk_type:
46+
_init_tk_type()
4247
return _tk_type == "carbon"
4348

4449
def isCocoaTk():
4550
"""
4651
Returns True if IDLE is using a Cocoa Aqua Tk.
4752
"""
48-
assert _tk_type is not None
53+
if not _tk_type:
54+
_init_tk_type()
4955
return _tk_type == "cocoa"
5056

5157
def isXQuartz():
5258
"""
5359
Returns True if IDLE is using an OS X X11 Tk.
5460
"""
55-
assert _tk_type is not None
61+
if not _tk_type:
62+
_init_tk_type()
5663
return _tk_type == "xquartz"
5764

5865
def tkVersionWarning(root):
@@ -232,7 +239,7 @@ def setupApp(root, flist):
232239
isAquaTk(), isCarbonTk(), isCocoaTk(), isXQuartz() functions which
233240
are initialized here as well.
234241
"""
235-
_initializeTkVariantTests(root)
242+
_init_tk_type(root)
236243
if isAquaTk():
237244
hideTkConsole(root)
238245
overrideRootMenu(root, flist)

0 commit comments

Comments
 (0)