Skip to content

Commit 37eec0b

Browse files
gh-61698: Use launchctl to detect macOS window manager in tests
1 parent c7e7bfc commit 37eec0b

File tree

1 file changed

+9
-15
lines changed

1 file changed

+9
-15
lines changed

Lib/test/support/__init__.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -251,22 +251,16 @@ class USEROBJECTFLAGS(ctypes.Structure):
251251
# process not running under the same user id as the current console
252252
# user. To avoid that, raise an exception if the window manager
253253
# connection is not available.
254-
from ctypes import cdll, c_int, pointer, Structure
255-
from ctypes.util import find_library
256-
257-
app_services = cdll.LoadLibrary(find_library("ApplicationServices"))
258-
259-
if app_services.CGMainDisplayID() == 0:
260-
reason = "gui tests cannot run without OS X window manager"
254+
import subprocess
255+
try:
256+
rc = subprocess.run(["launchctl", "managername"],
257+
capture_output=True, check=True)
258+
managername = rc.stdout.decode("utf-8").strip()
259+
except subprocess.CalledProcessError:
260+
reason = "unable to detect macOS launchd job manager"
261261
else:
262-
class ProcessSerialNumber(Structure):
263-
_fields_ = [("highLongOfPSN", c_int),
264-
("lowLongOfPSN", c_int)]
265-
psn = ProcessSerialNumber()
266-
psn_p = pointer(psn)
267-
if ( (app_services.GetCurrentProcess(psn_p) < 0) or
268-
(app_services.SetFrontProcess(psn_p) < 0) ):
269-
reason = "cannot run without OS X gui process"
262+
if managername != "Aqua":
263+
reason = f"{managername=} -- can only run in a macOS GUI session"
270264

271265
# check on every platform whether tkinter can actually do anything
272266
if not reason:

0 commit comments

Comments
 (0)