Skip to content

Commit d303d1e

Browse files
committed
protocol/rq: fix string decoding when using Python 3
X11 text strings are encoded with the Host Portable Character Encoding, which means we should be using ASCII, the same as the default with Python 2, and not UTF-8.
1 parent 36cfe94 commit d303d1e

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
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)
1213

1314
** Misc
1415

Xlib/protocol/rq.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
from ..support import lock
3535

3636

37+
def decode_string(bs):
38+
return bs.decode('ascii')
39+
40+
3741
class BadDataError(Exception): pass
3842

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

425429
def parse_binary_value(self, data, display, length, format):
426430
if length is None:
427-
return data.decode(), b''
431+
return decode_string(data), b''
428432

429433
if self.pad:
430434
slen = length + ((4 - length % 4) % 4)
431435
else:
432436
slen = length
433437

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

439440
return data_str, data[slen:]
440441

@@ -903,7 +904,7 @@ def pack_value(self, val):
903904

904905
def parse_binary(self, data, display):
905906
slen = byte2int(data) + 1
906-
return data[1:slen].decode(), data[slen:]
907+
return decode_string(data[1:slen]), data[slen:]
907908

908909
Str = StrClass()
909910

0 commit comments

Comments
 (0)