Skip to content

Commit 2729e5e

Browse files
committed
protocol/rq: fix Python 3 warnings about array.tostring()
1 parent d303d1e commit 2729e5e

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- fix support for `Window.set_wm_transient_for`
1111
- fix support for `Drawable.put_image` / `Drawable.get_image`
1212
- use ASCII for decoding strings in Python 3 (same as Python 2)
13+
- fix Python 3 warnings about `array.tostring()` (deprecated)
1314

1415
** Misc
1516

Xlib/protocol/rq.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
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
@@ -37,6 +37,13 @@
3737
def decode_string(bs):
3838
return bs.decode('ascii')
3939

40+
if PY3:
41+
def encode_array(a):
42+
return a.tobytes()
43+
else:
44+
def encode_array(a):
45+
return a.tostring()
46+
4047

4148
class BadDataError(Exception): pass
4249

@@ -553,8 +560,8 @@ def pack_value(self, val):
553560
if self.type.structcode and len(self.type.structcode) == 1:
554561
if self.type.check_value is not None:
555562
val = [self.type.check_value(v) for v in val]
556-
data = array(struct_to_array_codes[self.type.structcode],
557-
val).tostring()
563+
a = array(struct_to_array_codes[self.type.structcode], val)
564+
data = encode_array(a)
558565
else:
559566
data = []
560567
for v in val:
@@ -686,7 +693,8 @@ def pack_value(self, value):
686693
val = list(val)
687694

688695
size = fmt // 8
689-
data = array(array_unsigned_codes[size], val).tostring()
696+
a = array(array_unsigned_codes[size], val)
697+
data = encode_array(a)
690698
dlen = len(val)
691699

692700
dl = len(data)
@@ -807,7 +815,7 @@ def pack_value(self, value):
807815
for i in range(len(v), keycodes):
808816
a.append(X.NoSymbol)
809817

810-
return a.tostring(), len(value), keycodes
818+
return encode_array(a), len(value), keycodes
811819

812820

813821
class ModifierMapping(ValueField):
@@ -838,7 +846,7 @@ def pack_value(self, value):
838846
for i in range(len(v), keycodes):
839847
a.append(0)
840848

841-
return a.tostring(), len(value), keycodes
849+
return encode_array(a), len(value), keycodes
842850

843851
class EventField(ValueField):
844852
structcode = None

0 commit comments

Comments
 (0)