Skip to content

Commit 2120e8b

Browse files
Merge pull request #92 from benoit-pierre/protocol/rq_fixes
improve Python 3 support
2 parents 36cfe94 + 2729e5e commit 2120e8b

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
- fix unclosed file in Xauth implementation
1010
- fix support for `Window.set_wm_transient_for`
1111
- fix support for `Drawable.put_image` / `Drawable.get_image`
12+
- use ASCII for decoding strings in Python 3 (same as Python 2)
13+
- fix Python 3 warnings about `array.tostring()` (deprecated)
1214

1315
** Misc
1416

Xlib/protocol/rq.py

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,24 @@
2727
import types
2828

2929
# Python 2/3 compatibility.
30-
from six import binary_type, byte2int, indexbytes, iterbytes
30+
from six import PY3, binary_type, byte2int, indexbytes, iterbytes
3131

3232
# Xlib modules
3333
from .. import X
3434
from ..support import lock
3535

3636

37+
def decode_string(bs):
38+
return bs.decode('ascii')
39+
40+
if PY3:
41+
def encode_array(a):
42+
return a.tobytes()
43+
else:
44+
def encode_array(a):
45+
return a.tostring()
46+
47+
3748
class BadDataError(Exception): pass
3849

3950
# These are struct codes, we know their byte sizes
@@ -424,17 +435,14 @@ def pack_value(self, val):
424435

425436
def parse_binary_value(self, data, display, length, format):
426437
if length is None:
427-
return data.decode(), b''
438+
return decode_string(data), b''
428439

429440
if self.pad:
430441
slen = length + ((4 - length % 4) % 4)
431442
else:
432443
slen = length
433444

434-
if sys.version_info < (3, 0):
435-
data_str = data[:length]
436-
else:
437-
data_str = data[:length].decode()
445+
data_str = decode_string(data[:length])
438446

439447
return data_str, data[slen:]
440448

@@ -552,8 +560,8 @@ def pack_value(self, val):
552560
if self.type.structcode and len(self.type.structcode) == 1:
553561
if self.type.check_value is not None:
554562
val = [self.type.check_value(v) for v in val]
555-
data = array(struct_to_array_codes[self.type.structcode],
556-
val).tostring()
563+
a = array(struct_to_array_codes[self.type.structcode], val)
564+
data = encode_array(a)
557565
else:
558566
data = []
559567
for v in val:
@@ -685,7 +693,8 @@ def pack_value(self, value):
685693
val = list(val)
686694

687695
size = fmt // 8
688-
data = array(array_unsigned_codes[size], val).tostring()
696+
a = array(array_unsigned_codes[size], val)
697+
data = encode_array(a)
689698
dlen = len(val)
690699

691700
dl = len(data)
@@ -806,7 +815,7 @@ def pack_value(self, value):
806815
for i in range(len(v), keycodes):
807816
a.append(X.NoSymbol)
808817

809-
return a.tostring(), len(value), keycodes
818+
return encode_array(a), len(value), keycodes
810819

811820

812821
class ModifierMapping(ValueField):
@@ -837,7 +846,7 @@ def pack_value(self, value):
837846
for i in range(len(v), keycodes):
838847
a.append(0)
839848

840-
return a.tostring(), len(value), keycodes
849+
return encode_array(a), len(value), keycodes
841850

842851
class EventField(ValueField):
843852
structcode = None
@@ -903,7 +912,7 @@ def pack_value(self, val):
903912

904913
def parse_binary(self, data, display):
905914
slen = byte2int(data) + 1
906-
return data[1:slen].decode(), data[slen:]
915+
return decode_string(data[1:slen]), data[slen:]
907916

908917
Str = StrClass()
909918

0 commit comments

Comments
 (0)