Skip to content

Commit b4a94dc

Browse files
committed
protocol: Python 3 support
1 parent 8be5c0c commit b4a94dc

2 files changed

Lines changed: 30 additions & 14 deletions

File tree

Xlib/protocol/display.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import errno
2626
import socket
2727

28+
# Python 2/3 compatibility.
29+
from six import PY3, byte2int, indexbytes
30+
2831
# Xlib modules
2932
from .. import error
3033
from ..ext import ge
@@ -35,6 +38,15 @@
3538
from . import rq
3639
from . import event
3740

41+
if PY3:
42+
def buffer(object, offset=None, size=None):
43+
if offset is None:
44+
offset = 0
45+
if size is None:
46+
size = len(object)-offset
47+
return memoryview(object)[offset:offset+size]
48+
49+
3850
class Display(object):
3951
resource_classes = {}
4052
extension_major_opcodes = {}
@@ -84,8 +96,8 @@ def __init__(self, display = None):
8496
# Data used by the send-and-recieve loop
8597
self.sent_requests = []
8698
self.recv_packet_len = 0
87-
self.data_send = ''
88-
self.data_recv = ''
99+
self.data_send = b''
100+
self.data_recv = b''
89101
self.data_sent_bytes = 0
90102

91103
# Resource ID structures
@@ -557,7 +569,7 @@ def send_and_recv(self, flush = None, event = None, request = None, recv = None)
557569
self.close_internal('server')
558570
raise self.socket_error
559571

560-
self.data_recv = self.data_recv + bytes_recv
572+
self.data_recv = bytes(self.data_recv) + bytes_recv
561573
gotreq = self.parse_response(request)
562574

563575
# Otherwise return, allowing the calling thread to figure
@@ -642,7 +654,7 @@ def parse_response(self, request):
642654
while 1:
643655
if self.data_recv:
644656
# Check the first byte to find out what kind of response it is
645-
rtype = ord(self.data_recv[0])
657+
rtype = byte2int(self.data_recv)
646658

647659
# Are we're waiting for additional data for the current packet?
648660
if self.recv_packet_len:
@@ -681,7 +693,7 @@ def parse_response(self, request):
681693

682694
def parse_error_response(self, request):
683695
# Code is second byte
684-
code = ord(self.data_recv[1])
696+
code = indexbytes(self.data_recv, 1)
685697

686698
# Fetch error class
687699
estruct = self.error_classes.get(code, error.XError)

Xlib/protocol/rq.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@
2323
from array import array
2424
import types
2525

26+
# Python 2/3 compatibility.
27+
from six import byte2int, indexbytes, string_types
28+
2629
# Xlib modules
2730
from .. import X
2831
from ..support import lock
@@ -654,14 +657,15 @@ def pack_value(self, value):
654657
if fmt not in (8, 16, 32):
655658
raise BadDataError('Invalid property data format {0}'.format(fmt))
656659

657-
if type(val) in [bytes, str]:
660+
if isinstance(val, string_types):
661+
val = val.encode()
658662
size = fmt // 8
659663
vlen = len(val)
660664
if vlen % size:
661665
vlen = vlen - vlen % size
662-
data = val[:vlen].encode()
666+
data = val[:vlen]
663667
else:
664-
data = val.encode()
668+
data = val
665669

666670
dlen = vlen // size
667671

@@ -836,10 +840,10 @@ def pack_value(self, value):
836840
def parse_binary_value(self, data, display, length, format):
837841
from . import event
838842

839-
estruct = display.event_classes.get(_to_ord(data[0]) & 0x7f, event.AnyEvent)
843+
estruct = display.event_classes.get(byte2int(data) & 0x7f, event.AnyEvent)
840844
if type(estruct) == dict:
841845
# this etype refers to a set of sub-events with individual subcodes
842-
estruct = estruct[_to_ord(data[1])]
846+
estruct = estruct[indexbytes(data, 1)]
843847

844848
return estruct(display = display, binarydata = data[:32]), data[32:]
845849

@@ -887,7 +891,7 @@ def pack_value(self, val):
887891
return (chr(len(val)) + val).encode()
888892

889893
def parse_binary(self, data, display):
890-
slen = _to_ord(data[0]) + 1
894+
slen = byte2int(data) + 1
891895
return data[1:slen].decode(), data[slen:]
892896

893897
Str = StrClass()
@@ -1239,12 +1243,12 @@ def parse_binary_value(self, data, display, length, format):
12391243
break
12401244

12411245
# font change
1242-
if _to_ord(data[0]) == 255:
1243-
values.append(struct.unpack('>L', data[1:5].decode().encode())[0])
1246+
if byte2int(data) == 255:
1247+
values.append(struct.unpack('>L', bytes(data[1:5]))[0])
12441248
data = data[5:]
12451249

12461250
# skip null strings
1247-
elif _to_ord(data[0]) == 0 and _to_ord(data[1]) == 0:
1251+
elif byte2int(data) == 0 and indexbytes(data, 1) == 0:
12481252
data = data[2:]
12491253

12501254
# string with delta

0 commit comments

Comments
 (0)