Issue #18604: Consolidated checks for GUI availability. · pythoncapi/cpython@ceced6b · GitHub
Skip to content

Commit ceced6b

Browse files
committed
Issue python#18604: Consolidated checks for GUI availability.
test_support._is_gui_available is now defined the same way on every platform, and now includes the Windows-specific check that had been in the Windows version of _is_gui_available and the OSX-specific check that was in tkinter.test.support.check_tk_availability. Also, every platform checks whether Tk can be instantiated (if the platform-specific checks passed).
1 parent 3d5c9e2 commit ceced6b

6 files changed

Lines changed: 64 additions & 73 deletions

File tree

Lib/test/support/__init__.py

Lines changed: 55 additions & 12 deletions

Lib/test/test_idle.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
11
import unittest
22
from test import support
3-
from test.support import import_module, use_resources
3+
from test.support import import_module
44

55
# Skip test if _thread or _tkinter wasn't built or idlelib was deleted.
66
import_module('threading') # imported by PyShell, imports _thread
77
tk = import_module('tkinter') # imports _tkinter
88
idletest = import_module('idlelib.idle_test')
99

10-
# If buildbot improperly sets gui resource (#18365, #18441), remove it
11-
# so requires('gui') tests are skipped while non-gui tests still run.
12-
# If there is a problem with Macs, see #18441, msg 193805
13-
if use_resources and 'gui' in use_resources:
14-
try:
15-
root = tk.Tk()
16-
root.destroy()
17-
del root
18-
except tk.TclError:
19-
while 'gui' in use_resources:
20-
use_resources.remove('gui')
21-
2210
# Without test_main present, regrtest.runtest_inner (line1219) calls
2311
# unittest.TestLoader().loadTestsFromModule(this_module) which calls
2412
# load_tests() if it finds it. (Unittest.main does the same.)

Lib/test/test_tk.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
support.import_fresh_module('tkinter')
77

88
# Skip test if tk cannot be initialized.
9-
from tkinter.test.support import check_tk_availability
10-
check_tk_availability()
9+
support.requires('gui')
1110

1211
from tkinter.test import runtktests
1312

Lib/test/test_ttk_guionly.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
support.import_fresh_module('tkinter')
1010

1111
# Skip test if tk cannot be initialized.
12-
from tkinter.test.support import check_tk_availability
13-
check_tk_availability()
12+
support.requires('gui')
1413

1514
from _tkinter import TclError
1615
from tkinter import ttk

Lib/tkinter/test/support.py

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,10 @@
11
import sys
22
import tkinter
33
import unittest
4-
5-
_tk_unavailable = None
6-
7-
def check_tk_availability():
8-
"""Check that Tk is installed and available."""
9-
global _tk_unavailable
10-
11-
if _tk_unavailable is None:
12-
_tk_unavailable = False
13-
if sys.platform == 'darwin':
14-
# The Aqua Tk implementations on OS X can abort the process if
15-
# being called in an environment where a window server connection
16-
# cannot be made, for instance when invoked by a buildbot or ssh
17-
# process not running under the same user id as the current console
18-
# user. To avoid that, raise an exception if the window manager
19-
# connection is not available.
20-
from ctypes import cdll, c_int, pointer, Structure
21-
from ctypes.util import find_library
22-
23-
app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
24-
25-
if app_services.CGMainDisplayID() == 0:
26-
_tk_unavailable = "cannot run without OS X window manager"
27-
else:
28-
class ProcessSerialNumber(Structure):
29-
_fields_ = [("highLongOfPSN", c_int),
30-
("lowLongOfPSN", c_int)]
31-
psn = ProcessSerialNumber()
32-
psn_p = pointer(psn)
33-
if ( (app_services.GetCurrentProcess(psn_p) < 0) or
34-
(app_services.SetFrontProcess(psn_p) < 0) ):
35-
_tk_unavailable = "cannot run without OS X gui process"
36-
else: # not OS X
37-
import tkinter
38-
try:
39-
tkinter.Button()
40-
except tkinter.TclError as msg:
41-
# assuming tk is not available
42-
_tk_unavailable = "tk not available: %s" % msg
43-
44-
if _tk_unavailable:
45-
raise unittest.SkipTest(_tk_unavailable)
46-
return
4+
from test.support import requires
475

486
def get_tk_root():
49-
check_tk_availability() # raise exception if tk unavailable
7+
requires('gui') # raise exception if tk unavailable
508
try:
519
root = tkinter._default_root
5210
except AttributeError:

Misc/NEWS

Lines changed: 4 additions & 0 deletions

0 commit comments

Comments
 (0)