|
27 | 27 | import types |
28 | 28 |
|
29 | 29 | # Python 2/3 compatibility. |
30 | | -from six import binary_type, byte2int, indexbytes, iterbytes |
| 30 | +from six import PY3, binary_type, byte2int, indexbytes, iterbytes |
31 | 31 |
|
32 | 32 | # Xlib modules |
33 | 33 | from .. import X |
34 | 34 | from ..support import lock |
35 | 35 |
|
36 | 36 |
|
| 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 | + |
37 | 48 | class BadDataError(Exception): pass |
38 | 49 |
|
39 | 50 | # These are struct codes, we know their byte sizes |
@@ -424,17 +435,14 @@ def pack_value(self, val): |
424 | 435 |
|
425 | 436 | def parse_binary_value(self, data, display, length, format): |
426 | 437 | if length is None: |
427 | | - return data.decode(), b'' |
| 438 | + return decode_string(data), b'' |
428 | 439 |
|
429 | 440 | if self.pad: |
430 | 441 | slen = length + ((4 - length % 4) % 4) |
431 | 442 | else: |
432 | 443 | slen = length |
433 | 444 |
|
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]) |
438 | 446 |
|
439 | 447 | return data_str, data[slen:] |
440 | 448 |
|
@@ -552,8 +560,8 @@ def pack_value(self, val): |
552 | 560 | if self.type.structcode and len(self.type.structcode) == 1: |
553 | 561 | if self.type.check_value is not None: |
554 | 562 | 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) |
557 | 565 | else: |
558 | 566 | data = [] |
559 | 567 | for v in val: |
@@ -685,7 +693,8 @@ def pack_value(self, value): |
685 | 693 | val = list(val) |
686 | 694 |
|
687 | 695 | 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) |
689 | 698 | dlen = len(val) |
690 | 699 |
|
691 | 700 | dl = len(data) |
@@ -806,7 +815,7 @@ def pack_value(self, value): |
806 | 815 | for i in range(len(v), keycodes): |
807 | 816 | a.append(X.NoSymbol) |
808 | 817 |
|
809 | | - return a.tostring(), len(value), keycodes |
| 818 | + return encode_array(a), len(value), keycodes |
810 | 819 |
|
811 | 820 |
|
812 | 821 | class ModifierMapping(ValueField): |
@@ -837,7 +846,7 @@ def pack_value(self, value): |
837 | 846 | for i in range(len(v), keycodes): |
838 | 847 | a.append(0) |
839 | 848 |
|
840 | | - return a.tostring(), len(value), keycodes |
| 849 | + return encode_array(a), len(value), keycodes |
841 | 850 |
|
842 | 851 | class EventField(ValueField): |
843 | 852 | structcode = None |
@@ -903,7 +912,7 @@ def pack_value(self, val): |
903 | 912 |
|
904 | 913 | def parse_binary(self, data, display): |
905 | 914 | slen = byte2int(data) + 1 |
906 | | - return data[1:slen].decode(), data[slen:] |
| 915 | + return decode_string(data[1:slen]), data[slen:] |
907 | 916 |
|
908 | 917 | Str = StrClass() |
909 | 918 |
|
|
0 commit comments