MCPcopy Index your code
hub / github.com/RustPython/RustPython / _is_gui_available

Function _is_gui_available

Lib/test/support/__init__.py:225–294  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

223
224# Check whether a gui is actually available
225def _is_gui_available():
226 if hasattr(_is_gui_available, 'result'):
227 return _is_gui_available.result
228 import platform
229 reason = None
230 if sys.platform.startswith('win') and platform.win32_is_iot():
231 reason = "gui is not available on Windows IoT Core"
232 elif sys.platform.startswith('win'):
233 # if Python is running as a service (such as the buildbot service),
234 # gui interaction may be disallowed
235 import ctypes
236 import ctypes.wintypes
237 UOI_FLAGS = 1
238 WSF_VISIBLE = 0x0001
239 class USEROBJECTFLAGS(ctypes.Structure):
240 _fields_ = [("fInherit", ctypes.wintypes.BOOL),
241 ("fReserved", ctypes.wintypes.BOOL),
242 ("dwFlags", ctypes.wintypes.DWORD)]
243 dll = ctypes.windll.user32
244 h = dll.GetProcessWindowStation()
245 if not h:
246 raise ctypes.WinError()
247 uof = USEROBJECTFLAGS()
248 needed = ctypes.wintypes.DWORD()
249 res = dll.GetUserObjectInformationW(h,
250 UOI_FLAGS,
251 ctypes.byref(uof),
252 ctypes.sizeof(uof),
253 ctypes.byref(needed))
254 if not res:
255 raise ctypes.WinError()
256 if not bool(uof.dwFlags & WSF_VISIBLE):
257 reason = "gui not available (WSF_VISIBLE flag not set)"
258 elif sys.platform == 'darwin':
259 # The Aqua Tk implementations on OS X can abort the process if
260 # being called in an environment where a window server connection
261 # cannot be made, for instance when invoked by a buildbot or ssh
262 # process not running under the same user id as the current console
263 # user. To avoid that, raise an exception if the window manager
264 # connection is not available.
265 import subprocess
266 try:
267 rc = subprocess.run(["launchctl", "managername"],
268 capture_output=True, check=True)
269 managername = rc.stdout.decode("utf-8").strip()
270 except subprocess.CalledProcessError:
271 reason = "unable to detect macOS launchd job manager"
272 else:
273 if managername != "Aqua":
274 reason = f"{managername=} -- can only run in a macOS GUI session"
275
276 # check on every platform whether tkinter can actually do anything
277 if not reason:
278 try:
279 from tkinter import Tk
280 root = Tk()
281 root.withdraw()
282 root.update()

Callers 2

requiresFunction · 0.85
requires_resourceFunction · 0.85

Calls 13

destroyMethod · 0.95
TkClass · 0.90
hasattrFunction · 0.85
USEROBJECTFLAGSClass · 0.85
strFunction · 0.85
lenFunction · 0.85
startswithMethod · 0.45
sizeofMethod · 0.45
runMethod · 0.45
stripMethod · 0.45
decodeMethod · 0.45
updateMethod · 0.45

Tested by

no test coverage detected