4242
4343from Xlib import error , xauth
4444
45+
46+ SUPPORTED_PROTOCOLS = (None , 'tcp' , 'unix' )
47+
48+ # Darwin funky socket.
4549uname = platform .uname ()
4650if (uname [0 ] == 'Darwin' ) and ([int (x ) for x in uname [2 ].split ('.' )] >= [9 , 0 ]):
51+ SUPPORTED_PROTOCOLS += ('darwin' ,)
52+ DARWIN_DISPLAY_RE = re .compile (r'^/private/tmp/[-:a-zA-Z0-9._]*:(?P<dno>[0-9]+)(\.(?P<screen>[0-9]+))?$' )
4753
48- display_re = re .compile (r'^(?P<proto>)(?P<host>[-:a-zA-Z0-9._/]*):(?P<dno>[0-9]+)(\.(?P<screen>[0-9]+))?$' )
49-
50- else :
54+ DISPLAY_RE = re .compile (r'^((?P<proto>tcp|unix)/)?(?P<host>[-:a-zA-Z0-9._]*):(?P<dno>[0-9]+)(\.(?P<screen>[0-9]+))?$' )
5155
52- display_re = re .compile (r'^((?P<proto>tcp|unix)/)?(?P<host>[-:a-zA-Z0-9._]*):(?P<dno>[0-9]+)(\.(?P<screen>[0-9]+))?$' )
5356
5457def get_display (display ):
5558 # Use $DISPLAY if display isn't provided
5659 if display is None :
5760 display = os .environ .get ('DISPLAY' , '' )
5861
59- m = display_re .match (display )
60- if not m :
62+ re_list = [(DISPLAY_RE , {})]
63+
64+ if 'darwin' in SUPPORTED_PROTOCOLS :
65+ re_list .insert (0 , (DARWIN_DISPLAY_RE , {'protocol' : 'darwin' }))
66+
67+ for re , defaults in re_list :
68+ m = re .match (display )
69+ if m is not None :
70+ protocol , host , dno , screen = [
71+ m .groupdict ().get (field , defaults .get (field ))
72+ for field in ('proto' , 'host' , 'dno' , 'screen' )
73+ ]
74+ break
75+ else :
6176 raise error .DisplayNameError (display )
6277
63- name = display
64- protocol , host , dno , screen = m .group ('proto' , 'host' , 'dno' , 'screen' )
6578 if protocol == 'tcp' and not host :
6679 # Host is mandatory when protocol is TCP.
6780 raise error .DisplayNameError (display )
81+
6882 dno = int (dno )
6983 if screen :
7084 screen = int (screen )
7185 else :
7286 screen = 0
7387
74- return name , protocol , host , dno , screen
88+ return display , protocol , host , dno , screen
7589
7690
7791def _get_tcp_socket (host , dno ):
@@ -85,14 +99,14 @@ def _get_unix_socket(address):
8599 return s
86100
87101def get_socket (dname , protocol , host , dno ):
88- assert protocol in ( None , 'tcp' , 'unix' )
102+ assert protocol in SUPPORTED_PROTOCOLS
89103 try :
90104 # Darwin funky socket.
91- if uname [ 0 ] == 'Darwin' and host and host . startswith ( '/private/tmp/' ) :
105+ if protocol == 'darwin' :
92106 s = _get_unix_socket (dname )
93107
94108 # TCP socket, note the special case: `unix:0.0` is equivalent to `:0.0`.
95- elif (not protocol or protocol != 'unix' ) and host and host != 'unix' :
109+ elif (protocol is None or protocol != 'unix' ) and host and host != 'unix' :
96110 s = _get_tcp_socket (host , dno )
97111
98112 # Unix socket.
@@ -118,13 +132,14 @@ def get_socket(dname, protocol, host, dno):
118132 return s
119133
120134
121- def new_get_auth (sock , dname , host , dno ):
135+ def new_get_auth (sock , dname , protocol , host , dno ):
136+ assert protocol in SUPPORTED_PROTOCOLS
122137 # Translate socket address into the xauth domain
123- if ( uname [ 0 ] == 'Darwin' ) and host and host . startswith ( '/private/tmp/' ) :
138+ if protocol == 'darwin' :
124139 family = xauth .FamilyLocal
125140 addr = socket .gethostname ()
126141
127- elif host :
142+ elif protocol == 'tcp' :
128143 family = xauth .FamilyInternet
129144
130145 # Convert the prettyprinted IP number into 4-octet string.
0 commit comments