summaryrefslogtreecommitdiffstats
path: root/Lib/test/support/__init__.py
diff options
context:
space:
mode:
authorZachary Ware <zachary.ware@gmail.com>2014-05-02 15:51:07 (GMT)
committerZachary Ware <zachary.ware@gmail.com>2014-05-02 15:51:07 (GMT)
commitceced6bfead67f81cafad47c950e0769e1ba6db0 (patch)
treec0de1b5ada19641b3bb8ce14ba84f8b5777b993a /Lib/test/support/__init__.py
parent3d5c9e2c6710ad9bd93651494b95c6fb1f81da89 (diff)
downloadcpython-ceced6bfead67f81cafad47c950e0769e1ba6db0.zip
cpython-ceced6bfead67f81cafad47c950e0769e1ba6db0.tar.gz
cpython-ceced6bfead67f81cafad47c950e0769e1ba6db0.tar.bz2
Issue #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).
Diffstat (limited to 'Lib/test/support/__init__.py')
-rw-r--r--Lib/test/support/__init__.py67
1 files changed, 55 insertions, 12 deletions
diff --git a/Lib/test/support/__init__.py b/Lib/test/support/__init__.py
index 9857f9d..d26281a 100644
--- a/Lib/test/support/__init__.py
+++ b/Lib/test/support/__init__.py
@@ -378,12 +378,16 @@ def forget(modname):
unlink(importlib.util.cache_from_source(source, debug_override=True))
unlink(importlib.util.cache_from_source(source, debug_override=False))
-# On some platforms, should not run gui test even if it is allowed
-# in `use_resources'.
-if sys.platform.startswith('win'):
- import ctypes
- import ctypes.wintypes
- def _is_gui_available():
+# Check whether a gui is actually available
+def _is_gui_available():
+ if hasattr(_is_gui_available, 'result'):
+ return _is_gui_available.result
+ reason = None
+ if sys.platform.startswith('win'):
+ # if Python is running as a service (such as the buildbot service),
+ # gui interaction may be disallowed
+ import ctypes
+ import ctypes.wintypes
UOI_FLAGS = 1
WSF_VISIBLE = 0x0001
class USEROBJECTFLAGS(ctypes.Structure):
@@ -403,10 +407,49 @@ if sys.platform.startswith('win'):
ctypes.byref(needed))
if not res:
raise ctypes.WinError()
- return bool(uof.dwFlags & WSF_VISIBLE)
-else:
- def _is_gui_available():
- return True
+ if not bool(uof.dwFlags & WSF_VISIBLE):
+ reason = "gui not available (WSF_VISIBLE flag not set)"
+ elif sys.platform == 'darwin':
+ # The Aqua Tk implementations on OS X can abort the process if
+ # being called in an environment where a window server connection
+ # cannot be made, for instance when invoked by a buildbot or ssh
+ # process not running under the same user id as the current console
+ # user. To avoid that, raise an exception if the window manager
+ # connection is not available.
+ from ctypes import cdll, c_int, pointer, Structure
+ from ctypes.util import find_library
+
+ app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
+
+ if app_services.CGMainDisplayID() == 0:
+ reason = "gui tests cannot run without OS X window manager"
+ else:
+ class ProcessSerialNumber(Structure):
+ _fields_ = [("highLongOfPSN", c_int),
+ ("lowLongOfPSN", c_int)]
+ psn = ProcessSerialNumber()
+ psn_p = pointer(psn)
+ if ( (app_services.GetCurrentProcess(psn_p) < 0) or
+ (app_services.SetFrontProcess(psn_p) < 0) ):
+ reason = "cannot run without OS X gui process"
+
+ # check on every platform whether tkinter can actually do anything
+ if not reason:
+ try:
+ from tkinter import Tk
+ root = Tk()
+ root.destroy()
+ except Exception as e:
+ err_string = str(e)
+ if len(err_string) > 50:
+ err_string = err_string[:50] + ' [...]'
+ reason = 'Tk unavailable due to {}: {}'.format(type(e).__name__,
+ err_string)
+
+ _is_gui_available.reason = reason
+ _is_gui_available.result = not reason
+
+ return _is_gui_available.result
def is_resource_enabled(resource):
"""Test whether a resource is enabled. Known resources are set by
@@ -421,7 +464,7 @@ def requires(resource, msg=None):
executing.
"""
if resource == 'gui' and not _is_gui_available():
- raise unittest.SkipTest("Cannot use the 'gui' resource")
+ raise ResourceDenied(_is_gui_available.reason)
# see if the caller's module is __main__ - if so, treat as if
# the resource was set
if sys._getframe(1).f_globals.get("__name__") == "__main__":
@@ -1589,7 +1632,7 @@ def _id(obj):
def requires_resource(resource):
if resource == 'gui' and not _is_gui_available():
- return unittest.skip("resource 'gui' is not available")
+ return unittest.skip(_is_gui_available.reason)
if is_resource_enabled(resource):
return _id
else: