[PATCH 1 of 2 RFC] osutil: add a ctypes fallback for isgui() on Mac OS X

Dan Villiom Podlaski Christiansen danchr at gmail.com
Wed Mar 23 16:24:29 CDT 2011


# HG changeset patch
# User Dan Villiom Podlaski Christiansen <danchr at gmail.com>
# Date 1300914284 -3600
# Node ID ca4aae97f719b657182e7a426cb4d62ada9bc7a7
# Parent  42d56d28ff5ca7ebcac921236320896ab0a0f685
osutil: add a ctypes fallback for isgui() on Mac OS X

diff --git a/mercurial/pure/osutil.py b/mercurial/pure/osutil.py
--- a/mercurial/pure/osutil.py
+++ b/mercurial/pure/osutil.py
@@ -7,6 +7,7 @@
 
 import os
 import stat as statmod
+import sys
 
 posixfile = open
 
@@ -57,3 +58,31 @@ def listdir(path, stat=False, skip=None)
             result.append((fn, _mode_to_kind(st.st_mode)))
     return result
 
+if sys.platform == 'darwin':
+    def isgui():
+        '''Is a CoreGraphics session available?'''
+        try:
+            import ctypes
+            ctypes.cdll
+        except ImportError:
+            # ctypes is unavailable; assume a safe default
+            return True
+
+        def framework(name):
+            return '/System/Library/Frameworks/%s.framework/%s' % (name, name)
+
+        cfoundation = ctypes.cdll.LoadLibrary(framework('CoreFoundation'))
+        cfoundation.CFRelease.restype = None
+
+        appservices = ctypes.cdll.LoadLibrary(framework('ApplicationServices'))
+        appservices.CGSessionCopyCurrentDictionary.restype = ctypes.c_void_p
+
+        # actual code begins here
+
+        p = appservices.CGSessionCopyCurrentDictionary()
+
+        if p:
+            cfoundation.CFRelease(ctypes.c_void_p(p))
+            return True
+        else:
+            return False
diff --git a/mercurial/util.py b/mercurial/util.py
--- a/mercurial/util.py
+++ b/mercurial/util.py
@@ -773,12 +773,9 @@ def gui():
         if 'SSH_CONNECTION' in os.environ:
             # handle SSH access to a box where the user is logged in
             return False
-        elif getattr(osutil, 'isgui', None):
+        else:
             # check if a CoreGraphics session is available
             return osutil.isgui()
-        else:
-            # pure build; use a safe default
-            return True
     else:
         return os.name == "nt" or os.environ.get("DISPLAY")
 


More information about the Mercurial-devel mailing list