Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
- fix unclosed file in Xauth implementation
- fix support for `Window.set_wm_transient_for`
- fix support for `Drawable.put_image` / `Drawable.get_image`
- use ASCII for decoding strings in Python 3 (same as Python 2)
- fix Python 3 warnings about `array.tostring()` (deprecated)

** Misc

Expand Down
33 changes: 21 additions & 12 deletions Xlib/protocol/rq.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,24 @@
import types

# Python 2/3 compatibility.
from six import binary_type, byte2int, indexbytes, iterbytes
from six import PY3, binary_type, byte2int, indexbytes, iterbytes

# Xlib modules
from .. import X
from ..support import lock


def decode_string(bs):
return bs.decode('ascii')

if PY3:
def encode_array(a):
return a.tobytes()
else:
def encode_array(a):
return a.tostring()


class BadDataError(Exception): pass

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

def parse_binary_value(self, data, display, length, format):
if length is None:
return data.decode(), b''
return decode_string(data), b''

if self.pad:
slen = length + ((4 - length % 4) % 4)
else:
slen = length

if sys.version_info < (3, 0):
data_str = data[:length]
else:
data_str = data[:length].decode()
data_str = decode_string(data[:length])

return data_str, data[slen:]

Expand Down Expand Up @@ -552,8 +560,8 @@ def pack_value(self, val):
if self.type.structcode and len(self.type.structcode) == 1:
if self.type.check_value is not None:
val = [self.type.check_value(v) for v in val]
data = array(struct_to_array_codes[self.type.structcode],
val).tostring()
a = array(struct_to_array_codes[self.type.structcode], val)
data = encode_array(a)
else:
data = []
for v in val:
Expand Down Expand Up @@ -685,7 +693,8 @@ def pack_value(self, value):
val = list(val)

size = fmt // 8
data = array(array_unsigned_codes[size], val).tostring()
a = array(array_unsigned_codes[size], val)
data = encode_array(a)
dlen = len(val)

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

return a.tostring(), len(value), keycodes
return encode_array(a), len(value), keycodes


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

return a.tostring(), len(value), keycodes
return encode_array(a), len(value), keycodes

class EventField(ValueField):
structcode = None
Expand Down Expand Up @@ -903,7 +912,7 @@ def pack_value(self, val):

def parse_binary(self, data, display):
slen = byte2int(data) + 1
return data[1:slen].decode(), data[slen:]
return decode_string(data[1:slen]), data[slen:]

Str = StrClass()

Expand Down