Skip to content

Commit 992fe6f

Browse files
Merge pull request #115 from benoit-pierre/fix_114
support/unix_connect: fix protocol handling
2 parents a2537eb + 3c8812e commit 992fe6f

File tree

3 files changed

+36
-21
lines changed

3 files changed

+36
-21
lines changed

Xlib/protocol/display.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def __init__(self, display = None):
8989

9090
self.socket = connect.get_socket(name, protocol, host, displayno)
9191

92-
auth_name, auth_data = connect.get_auth(self.socket,
93-
name, host, displayno)
92+
auth_name, auth_data = connect.get_auth(self.socket, name,
93+
protocol, host, displayno)
9494

9595
# Internal structures for communication, grouped
9696
# by their function and locks

Xlib/support/connect.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,16 @@ def get_socket(dname, protocol, host, dno):
8787
return mod.get_socket(dname, protocol, host, dno)
8888

8989

90-
def get_auth(sock, dname, host, dno):
91-
"""auth_name, auth_data = get_auth(sock, dname, host, dno)
90+
def get_auth(sock, dname, protocol, host, dno):
91+
"""auth_name, auth_data = get_auth(sock, dname, protocol, host, dno)
9292
9393
Return authentication data for the display on the other side of
94-
SOCK, which was opened with DNAME, HOST and DNO.
94+
SOCK, which was opened with DNAME, HOST and DNO, using PROTOCOL.
9595
9696
Return AUTH_NAME and AUTH_DATA, two strings to be used in the
9797
connection setup request.
9898
"""
9999

100100
modname = _auth_mods.get(platform, _default_auth_mod)
101101
mod = _relative_import(modname)
102-
return mod.get_auth(sock, dname, host, dno)
102+
return mod.get_auth(sock, dname, protocol, host, dno)

Xlib/support/unix_connect.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,36 +42,50 @@
4242

4343
from Xlib import error, xauth
4444

45+
46+
SUPPORTED_PROTOCOLS = (None, 'tcp', 'unix')
47+
48+
# Darwin funky socket.
4549
uname = platform.uname()
4650
if (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

5457
def 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

7791
def _get_tcp_socket(host, dno):
@@ -85,14 +99,14 @@ def _get_unix_socket(address):
8599
return s
86100

87101
def 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

Comments
 (0)