|
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 |
|
37 | 37 | def decode_string(bs): |
38 | 38 | return bs.decode('ascii') |
39 | 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 | + |
40 | 47 |
|
41 | 48 | class BadDataError(Exception): pass |
42 | 49 |
|
@@ -553,8 +560,8 @@ def pack_value(self, val): |
553 | 560 | if self.type.structcode and len(self.type.structcode) == 1: |
554 | 561 | if self.type.check_value is not None: |
555 | 562 | 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) |
558 | 565 | else: |
559 | 566 | data = [] |
560 | 567 | for v in val: |
@@ -686,7 +693,8 @@ def pack_value(self, value): |
686 | 693 | val = list(val) |
687 | 694 |
|
688 | 695 | 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) |
690 | 698 | dlen = len(val) |
691 | 699 |
|
692 | 700 | dl = len(data) |
@@ -807,7 +815,7 @@ def pack_value(self, value): |
807 | 815 | for i in range(len(v), keycodes): |
808 | 816 | a.append(X.NoSymbol) |
809 | 817 |
|
810 | | - return a.tostring(), len(value), keycodes |
| 818 | + return encode_array(a), len(value), keycodes |
811 | 819 |
|
812 | 820 |
|
813 | 821 | class ModifierMapping(ValueField): |
@@ -838,7 +846,7 @@ def pack_value(self, value): |
838 | 846 | for i in range(len(v), keycodes): |
839 | 847 | a.append(0) |
840 | 848 |
|
841 | | - return a.tostring(), len(value), keycodes |
| 849 | + return encode_array(a), len(value), keycodes |
842 | 850 |
|
843 | 851 | class EventField(ValueField): |
844 | 852 | structcode = None |
|
0 commit comments