~bennabiy/+junk/python-xlib

« back to all changes in this revision

Viewing changes to Xlib/protocol/rq.py

  • Committer: Package Import Robot
  • Author(s): Andrew Shadura, Ramkumar Ramachandra, Andrew Shadura
  • Date: 2015-08-13 08:14:19 UTC
  • mfrom: (6.1.2 sid)
  • Revision ID: package-import@ubuntu.com-20150813081419-hdefinnghp2iydkx
Tags: 0.14+20091101-3
[ Ramkumar Ramachandra ]
* Remove useless debugging output (Closes: #565996)

[ Andrew Shadura ]
* Switch to 3.0 (quilt) format.
* Rename patches.
* Use debhelper 9 in its short form.
* Use pybuild.
* Bump Standards-Version.
* Don't build or install PostScript documentation and info files.
* Use system-provided texi2html instead of a shipped version
  (Closes: #795057).
* Update debian/copyright (Closes: #795057).
* Don't install Makefile or texi2html with the documentation.
* Set executable bit for examples.

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
#    along with this program; if not, write to the Free Software
17
17
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18
18
 
 
19
import types
19
20
 
20
21
# Standard modules
21
22
import sys
22
23
import traceback
23
24
import struct
24
 
import string
25
25
from array import array
26
26
import types
27
 
import new
28
27
 
29
28
# Xlib modules
30
29
from Xlib import X
31
30
from Xlib.support import lock
32
31
 
33
32
 
 
33
_PY3 = sys.version[0] >= '3'
 
34
 
 
35
# in Python 3, bytes are an actual array; in python 2, bytes are still
 
36
# string-like, so in order to get an array element we need to call ord()
 
37
if _PY3:
 
38
    def _bytes_item(x):
 
39
        return x
 
40
else:
 
41
    def _bytes_item(x):
 
42
        return ord(x)
 
43
 
34
44
class BadDataError(Exception): pass
35
45
 
36
46
# These are struct codes, we know their byte sizes
52
62
for c in 'bhil':
53
63
    size = array(c).itemsize
54
64
 
55
 
    array_unsigned_codes[size] = string.upper(c)
 
65
    array_unsigned_codes[size] = c.upper()
56
66
    try:
57
67
        struct_to_array_codes[signed_codes[size]] = c
58
 
        struct_to_array_codes[unsigned_codes[size]] = string.upper(c)
 
68
        struct_to_array_codes[unsigned_codes[size]] = c.upper()
59
69
    except KeyError:
60
70
        pass
61
71
 
135
145
class Pad(Field):
136
146
    def __init__(self, size):
137
147
        self.size = size
138
 
        self.value = '\0' * size
 
148
        self.value = b'\0' * size
139
149
        self.structcode = '%dx' % size
140
150
        self.structvalues = 0
141
151
 
190
200
    structvalues = 1
191
201
 
192
202
    def calc_length(self, length):
193
 
        return length / 4
 
203
        return length // 4
194
204
 
195
205
class ReplyLength(TotalLengthField):
196
206
    structcode = 'L'
197
207
    structvalues = 1
198
208
 
199
209
    def calc_length(self, length):
200
 
        return (length - 32) / 4
 
210
        return (length - 32) // 4
201
211
 
202
212
 
203
213
class LengthOf(LengthField):
280
290
        self.codes = codes
281
291
 
282
292
    def check_value(self, value):
283
 
        if type(value) is types.InstanceType:
 
293
        try:
284
294
            return getattr(value, self.cast_function)()
285
 
        else:
 
295
        except AttributeError:
286
296
            return value
287
297
 
288
298
    def parse_value(self, value, display):
380
390
    def pack_value(self, val):
381
391
        slen = len(val)
382
392
 
 
393
        if _PY3 and type(val) is str:
 
394
            val = val.encode('UTF-8')
 
395
 
383
396
        if self.pad:
384
 
            return val + '\0' * ((4 - slen % 4) % 4), slen, None
 
397
            return val + b'\0' * ((4 - slen % 4) % 4), slen, None
385
398
        else:
386
399
            return val, slen, None
387
400
 
388
401
    def parse_binary_value(self, data, display, length, format):
389
402
        if length is None:
390
 
            return str(data), ''
 
403
            try:
 
404
                return data.decode('UTF-8'), b''
 
405
            except UnicodeDecodeError:
 
406
                return data, b''
391
407
 
392
408
        if self.pad:
393
409
            slen = length + ((4 - length % 4) % 4)
394
410
        else:
395
411
            slen = length
396
412
 
397
 
        return str(data[:length]), buffer(data, slen)
 
413
        s = data[:length]
 
414
        try:
 
415
            s = s.decode('UTF-8')
 
416
        except UnicodeDecodeError:
 
417
            pass  # return as bytes
 
418
        return s, data[slen:]
398
419
 
399
420
 
400
421
class String16(ValueField):
406
427
 
407
428
    def pack_value(self, val):
408
429
        # Convert 8-byte string into 16-byte list
409
 
        if type(val) is types.StringType:
410
 
            val = map(lambda c: ord(c), val)
 
430
        if type(val) is str:
 
431
            val = list(map(lambda c: ord(c), val))
411
432
 
412
433
        slen = len(val)
413
434
 
414
435
        if self.pad:
415
 
            pad = '\0\0' * (slen % 2)
 
436
            pad = b'\0\0' * (slen % 2)
416
437
        else:
417
 
            pad = ''
 
438
            pad = b''
418
439
 
419
 
        return apply(struct.pack, ('>' + 'H' * slen, ) + tuple(val)) + pad, slen, None
 
440
        return struct.pack(*('>' + 'H' * slen, ) + tuple(val)) + pad, slen, None
420
441
 
421
442
    def parse_binary_value(self, data, display, length, format):
422
443
        if length == 'odd':
423
 
            length = len(data) / 2 - 1
 
444
            length = len(data) // 2 - 1
424
445
        elif length == 'even':
425
 
            length = len(data) / 2
 
446
            length = len(data) // 2
426
447
 
427
448
        if self.pad:
428
449
            slen = length + (length % 2)
429
450
        else:
430
451
            slen = length
431
452
 
432
 
        return struct.unpack('>' + 'H' * length, data[:length * 2]), buffer(data, slen * 2)
 
453
        return struct.unpack('>' + 'H' * length, data[:length * 2]), data[slen * 2:]
433
454
 
434
455
 
435
456
 
473
494
 
474
495
                    pos = pos + slen
475
496
 
476
 
                data = buffer(data, pos)
 
497
                data = data[pos:]
477
498
 
478
499
        else:
479
500
            ret = [None] * int(length)
498
519
 
499
520
                    pos = pos + slen
500
521
 
501
 
                data = buffer(data, pos)
 
522
                data = data[pos:]
502
523
 
503
524
        if self.pad:
504
 
            data = buffer(data, len(data) % 4)
 
525
            data = data[len(data) % 4:]
505
526
 
506
527
        return ret, data
507
528
 
515
536
            for v in val:
516
537
                data.append(self.type.pack_value(v))
517
538
 
518
 
            data = string.join(data, '')
 
539
            data = b''.join(data)
519
540
 
520
541
        if self.pad:
521
542
            dlen = len(data)
522
 
            data = data + '\0' * ((4 - dlen % 4) % 4)
 
543
            data = data + b'\0' * ((4 - dlen % 4) % 4)
523
544
 
524
545
        return data, len(val), None
525
546
 
562
583
            if self.type.parse_value is not None:
563
584
                v = self.type.parse_value(v, display)
564
585
 
565
 
            return v, buffer(data, slen)
 
586
            return v, data[slen:]
566
587
 
567
588
    def parse_value(self, val, display):
568
589
        if self.type.parse_value is None:
581
602
        if self.type.structcode is None:
582
603
            return val
583
604
 
584
 
        if type(val) is types.TupleType:
 
605
        if type(val) is tuple:
585
606
            return val
586
607
 
587
 
        if type(val) is types.DictType:
 
608
        if type(val) is dict:
588
609
            data = val
589
610
        elif isinstance(val, DictWrapper):
590
611
            data = val._data
604
625
 
605
626
    def parse_binary_value(self, data, display, length, format):
606
627
        if length is None:
607
 
            length = len(data) / (format / 8)
 
628
            length = len(data) // (format // 8)
608
629
        else:
609
630
            length = int(length)
610
631
 
611
632
        if format == 0:
612
633
            ret = None
 
634
            return ret, data
613
635
 
614
636
        elif format == 8:
615
 
            ret = (8, str(data[:length]))
616
 
            data = buffer(data, length + ((4 - length % 4) % 4))
 
637
            ret = (8, data[:length])
 
638
            data = data[length + ((4 - length % 4) % 4):]
617
639
 
618
640
        elif format == 16:
619
 
            ret = (16, array(array_unsigned_codes[2], str(data[:2 * length])))
620
 
            data = buffer(data, 2 * (length + length % 2))
 
641
            ret = (16, array(array_unsigned_codes[2], data[:2 * length]))
 
642
            data = data[2 * (length + length % 2):]
621
643
 
622
644
        elif format == 32:
623
 
            ret = (32, array(array_unsigned_codes[4], str(data[:4 * length])))
624
 
            data = buffer(data, 4 * length)
 
645
            ret = (32, array(array_unsigned_codes[4], data[:4 * length]))
 
646
            data = data[4 * length:]
 
647
 
 
648
        if type(ret[1]) is bytes:
 
649
            try:
 
650
                ret = (ret[0], ret[1].decode('UTF-8'))
 
651
            except UnicodeDecodeError:
 
652
                pass  # return as bytes
625
653
 
626
654
        return ret, data
627
655
 
631
659
        if fmt not in (8, 16, 32):
632
660
            raise BadDataError('Invalid property data format %d' % fmt)
633
661
 
634
 
        if type(val) is types.StringType:
635
 
            size = fmt / 8
 
662
        if _PY3 and type(val) is str:
 
663
            val = val.encode('UTF-8')
 
664
 
 
665
        if type(val) is bytes:
 
666
            size = fmt // 8
636
667
            vlen = len(val)
637
668
            if vlen % size:
638
669
                vlen = vlen - vlen % size
640
671
            else:
641
672
                data = val
642
673
 
643
 
            dlen = vlen / size
 
674
            dlen = vlen // size
644
675
 
645
676
        else:
646
 
            if type(val) is types.TupleType:
 
677
            if type(val) is tuple:
647
678
                val = list(val)
648
679
 
649
 
            size = fmt / 8
 
680
            size = fmt // 8
650
681
            data =  array(array_unsigned_codes[size], val).tostring()
651
682
            dlen = len(val)
652
683
 
653
684
        dl = len(data)
654
 
        data = data + '\0' * ((4 - dl % 4) % 4)
 
685
        data = data + b'\0' * ((4 - dl % 4) % 4)
655
686
 
656
687
        return data, dlen, fmt
657
688
 
663
694
 
664
695
    def parse_binary_value(self, data, display, length, format):
665
696
        return PropertyData.parse_binary_value(self, data, display,
666
 
                                               self.size / (format / 8), format)
 
697
                                               self.size // (format // 8), format)
667
698
 
668
699
    def pack_value(self, value):
669
700
        data, dlen, fmt = PropertyData.pack_value(self, value)
694
725
 
695
726
    def pack_value(self, arg, keys):
696
727
        mask = 0
697
 
        data = ''
 
728
        data = b''
698
729
 
699
730
        if arg == self.default:
700
731
            arg = keys
701
732
 
702
733
        for field, flag in self.fields:
703
 
            if arg.has_key(field.name):
 
734
            if field.name in arg:
704
735
                mask = mask | flag
705
736
 
706
737
                val = arg[field.name]
708
739
                    val = field.check_value(val)
709
740
 
710
741
                d = struct.pack('=' + field.structcode, val)
711
 
                data = data + d + '\0' * (4 - len(d))
 
742
                data = data + d + b'\0' * (4 - len(d))
712
743
 
713
744
        return struct.pack(self.maskcode, mask) + data, None, None
714
745
 
716
747
        r = {}
717
748
 
718
749
        mask = int(struct.unpack(self.maskcode, data[:self.maskcodelen])[0])
719
 
        data = buffer(data, self.maskcodelen)
 
750
        data = data[self.maskcodelen:]
720
751
 
721
752
        for field, flag in self.fields:
722
753
            if mask & flag:
733
764
                    vals, d = field.parse_binary_value(data[:4], display, None, None)
734
765
 
735
766
                r[field.name] = vals
736
 
                data = buffer(data, 4)
 
767
                data = data[4:]
737
768
 
738
769
        return DictWrapper(r), data
739
770
 
747
778
        else:
748
779
            dlen = 4 * length * format
749
780
 
750
 
        a = array(array_unsigned_codes[4], str(data[:dlen]))
 
781
        a = array(array_unsigned_codes[4], data[:dlen])
751
782
 
752
783
        ret = []
753
784
        for i in range(0, len(a), format):
754
785
            ret.append(a[i : i + format])
755
786
 
756
 
        return ret, buffer(data, dlen)
 
787
        return ret, data[dlen:]
757
788
 
758
789
    def pack_value(self, value):
759
790
        keycodes = 0
775
806
    structcode = None
776
807
 
777
808
    def parse_binary_value(self, data, display, length, format):
778
 
        a = array(array_unsigned_codes[1], str(data[:8 * format]))
 
809
        a = array(array_unsigned_codes[1], data[:8 * format])
779
810
 
780
811
        ret = []
781
812
        for i in range(0, 8):
782
813
            ret.append(a[i * format : (i + 1) * format])
783
814
 
784
 
        return ret, buffer(data, 8 * format)
 
815
        return ret, data[8 * format:]
785
816
 
786
817
    def pack_value(self, value):
787
818
        if len(value) != 8:
811
842
        return value._binary, None, None
812
843
 
813
844
    def parse_binary_value(self, data, display, length, format):
814
 
        import event
815
 
 
816
 
        estruct = display.event_classes.get(ord(data[0]) & 0x7f, event.AnyEvent)
817
 
 
818
 
        return estruct(display = display, binarydata = data[:32]), buffer(data, 32)
 
845
        from Xlib.protocol import event
 
846
 
 
847
        estruct = display.event_classes.get(_bytes_item(data[0]) & 0x7f, event.AnyEvent)
 
848
 
 
849
        return estruct(display = display, binarydata = data[:32]), data[32:]
819
850
 
820
851
 
821
852
#
856
887
    structcode = None
857
888
 
858
889
    def pack_value(self, val):
859
 
        return chr(len(val)) + val
 
890
        if type(val) is not bytes:
 
891
            val = val.encode('UTF-8')
 
892
        if _PY3:
 
893
            val = bytes([len(val)]) + val
 
894
        else:
 
895
            val = chr(len(val)) + val
 
896
        return val
860
897
 
861
898
    def parse_binary(self, data, display):
862
 
        slen = ord(data[0]) + 1
863
 
        return data[1:slen], buffer(data, slen)
 
899
        slen = _bytes_item(data[0]) + 1
 
900
        s = data[1:slen]
 
901
        try:
 
902
            s = s.decode('UTF-8')
 
903
        except UnicodeDecodeError:
 
904
            pass  # return as bytes
 
905
        return s, data[slen:]
864
906
 
865
907
Str = StrClass()
866
908
 
979
1021
        # static fields.  First argument is the structcode, the
980
1022
        # remaining are values.
981
1023
 
 
1024
 
982
1025
        pack_args = ['"%s"' % self.static_codes]
983
1026
 
984
1027
        i = 0
1028
1071
 
1029
1072
                    if f.check_value is not None:
1030
1073
                        code = code + ('  %s = self.static_fields[%d].check_value(%s)\n'
1031
 
                                       % (string.join(a, ', '), i, f.name))
 
1074
                                       % (', '.join(a), i, f.name))
1032
1075
                    else:
1033
 
                        code = code + '  %s = %s\n' % (string.join(a, ', '), f.name)
 
1076
                        code = code + '  %s = %s\n' % (', '.join(a), f.name)
1034
1077
 
1035
1078
                    pack_args = pack_args + a
1036
1079
 
1043
1086
 
1044
1087
            i = i + 1
1045
1088
 
1046
 
 
1047
1089
        # Construct call to struct.pack
1048
 
        pack = 'struct.pack(%s)' % string.join(pack_args, ', ')
 
1090
        pack = 'struct.pack(%s)' % ', '.join(pack_args)
1049
1091
 
1050
1092
        # If there are any varfields, we append the packed strings to build
1051
1093
        # the resulting binary value
1052
1094
        if self.var_fields:
1053
 
            code = code + '  return %s + %s\n' % (pack, string.join(joins, ' + '))
 
1095
            code = code + '  return %s + %s\n' % (pack, ' + '.join(joins))
1054
1096
 
1055
1097
        # If there's only static fields, return the packed value
1056
1098
        else:
1070
1112
            args.append('**_keyword_args')
1071
1113
 
1072
1114
        # Add function header
1073
 
        code = 'def to_binary(self, %s):\n' % string.join(args, ', ') + code
 
1115
        code = 'def to_binary(self, %s):\n' % ', '.join(args) + code
1074
1116
 
1075
1117
        # self._pack_code = code
1076
1118
 
1088
1130
        # memory leak isn't that serious.  Besides, Python 2.0 has
1089
1131
        # real garbage collect.
1090
1132
 
1091
 
        exec code
1092
 
        self.to_binary = new.instancemethod(to_binary, self, self.__class__)
 
1133
        g = globals().copy()
 
1134
        exec(code, g)
 
1135
        self.to_binary = types.MethodType(g['to_binary'], self)
1093
1136
 
1094
1137
        # Finally call it manually
1095
 
        return apply(self.to_binary, varargs, keys)
 
1138
        return self.to_binary(*varargs, **keys)
1096
1139
 
1097
1140
 
1098
1141
    def pack_value(self, value):
1103
1146
 
1104
1147
        """
1105
1148
 
1106
 
        if type(value) is types.TupleType:
1107
 
            return apply(self.to_binary, value, {})
1108
 
        elif type(value) is types.DictionaryType:
1109
 
            return apply(self.to_binary, (), value)
 
1149
        if type(value) is tuple:
 
1150
            return self.to_binary(*value, **{})
 
1151
        elif type(value) is dict:
 
1152
            return self.to_binary(*(), **value)
1110
1153
        elif isinstance(value, DictWrapper):
1111
 
            return apply(self.to_binary, (), value._data)
 
1154
            return self.to_binary(*(), **value._data)
1112
1155
        else:
1113
1156
            raise BadDataError('%s is not a tuple or a list' % (value))
1114
1157
 
1173
1216
 
1174
1217
        # Finally, compile function as for to_binary.
1175
1218
 
1176
 
        exec code
1177
 
        self.parse_value = new.instancemethod(parse_value, self, self.__class__)
 
1219
        g = globals().copy()
 
1220
        exec(code, g)
 
1221
        self.parse_value = types.MethodType(g['parse_value'], self)
1178
1222
 
1179
1223
        # Call it manually
1180
1224
        return self.parse_value(val, display, rawdict)
1249
1293
            fno = fno + 1
1250
1294
            vno = vno + f.structvalues
1251
1295
 
1252
 
        code = code + '  data = buffer(data, %d)\n' % self.static_size
 
1296
        code = code + '  data = data[%d:]\n' % self.static_size
1253
1297
 
1254
1298
        # Call parse_binary_value for each var_field, passing the
1255
1299
        # length and format values from the unpacked val.
1273
1317
 
1274
1318
        # Finally, compile function as for to_binary.
1275
1319
 
1276
 
        exec code
1277
 
        self.parse_binary = new.instancemethod(parse_binary, self, self.__class__)
 
1320
        g = globals().copy()
 
1321
        exec(code, g)
 
1322
        self.parse_binary = types.MethodType(g['parse_binary'], self)
1278
1323
 
1279
1324
        # Call it manually
1280
1325
        return self.parse_binary(data, display, rawdict)
1286
1331
                              String8('string', pad = 0) )
1287
1332
 
1288
1333
    def pack_value(self, value):
1289
 
        data = ''
 
1334
        data = b''
1290
1335
        args = {}
1291
1336
 
1292
1337
        for v in value:
1293
1338
            # Let values be simple strings, meaning a delta of 0
1294
 
            if type(v) is types.StringType:
 
1339
            if _PY3 and type(v) is str:
 
1340
                v = v.encode('UTF-8')
 
1341
 
 
1342
            if type(v) is bytes:
1295
1343
                v = (0, v)
1296
1344
 
1297
1345
            # A tuple, it should be (delta, string)
1298
1346
            # Encode it as one or more textitems
1299
1347
 
1300
 
            if type(v) in (types.TupleType, types.DictType) or \
 
1348
            if type(v) in (tuple, dict) or \
1301
1349
               isinstance(v, DictWrapper):
1302
1350
 
1303
 
                if type(v) is types.TupleType:
1304
 
                    delta, str = v
 
1351
                if type(v) is tuple:
 
1352
                    delta, s = v
1305
1353
                else:
1306
1354
                    delta = v['delta']
1307
 
                    str = v['string']
 
1355
                    s = v['string']
1308
1356
 
1309
 
                while delta or str:
 
1357
                while delta or s:
1310
1358
                    args['delta'] = delta
1311
 
                    args['string'] = str[:254]
 
1359
                    args['string'] = s[:254]
1312
1360
 
1313
 
                    data = data + apply(self.string_textitem.to_binary, (), args)
 
1361
                    data = data + self.string_textitem.to_binary(*(), **args)
1314
1362
 
1315
1363
                    delta = 0
1316
 
                    str = str[254:]
 
1364
                    s = s[254:]
1317
1365
 
1318
1366
            # Else an integer, i.e. a font change
1319
1367
            else:
1320
1368
                # Use fontable cast function if instance
1321
 
                if type(v) is types.InstanceType:
 
1369
                if hasattr(v, '__fontable__'):
1322
1370
                    v = v.__fontable__()
1323
1371
 
1324
1372
                data = data + struct.pack('>BL', 255, v)
1325
1373
 
1326
1374
        # Pad out to four byte length
1327
1375
        dlen = len(data)
1328
 
        return data + '\0' * ((4 - dlen % 4) % 4), None, None
 
1376
        return data + b'\0' * ((4 - dlen % 4) % 4), None, None
1329
1377
 
1330
1378
    def parse_binary_value(self, data, display, length, format):
1331
1379
        values = []
1334
1382
                break
1335
1383
 
1336
1384
            # font change
1337
 
            if ord(data[0]) == 255:
1338
 
                values.append(struct.unpack('>L', str(data[1:5]))[0])
1339
 
                data = buffer(data, 5)
 
1385
            if _bytes_item(data[0]) == 255:
 
1386
                values.append(struct.unpack('>L', data[1:5])[0])
 
1387
                data = data[5:]
1340
1388
 
1341
1389
            # skip null strings
1342
 
            elif ord(data[0]) == 0 and ord(data[1]) == 0:
1343
 
                data = buffer(data, 2)
 
1390
            elif _bytes_item(data[0]) == 0 and _bytes_item(data[1]) == 0:
 
1391
                data = data[2:]
1344
1392
 
1345
1393
            # string with delta
1346
1394
            else:
1347
1395
                v, data = self.string_textitem.parse_binary(data, display)
1348
1396
                values.append(v)
1349
1397
 
1350
 
        return values, ''
 
1398
        return values, b''
1351
1399
 
1352
1400
 
1353
1401
 
1358
1406
 
1359
1407
 
1360
1408
 
1361
 
class GetAttrData:
 
1409
class GetAttrData(object):
1362
1410
    def __getattr__(self, attr):
1363
1411
        try:
1364
1412
            if self._data:
1393
1441
    def __repr__(self):
1394
1442
        return '%s(%s)' % (self.__class__, repr(self._data))
1395
1443
 
1396
 
    def __cmp__(self, other):
 
1444
    def __eq__(self, other):
1397
1445
        if isinstance(other, DictWrapper):
1398
 
            return cmp(self._data, other._data)
 
1446
            return self._data == other._data
1399
1447
        else:
1400
 
            return cmp(self._data, other)
 
1448
            return self._data == other
1401
1449
 
 
1450
    def __ne__(self, other):
 
1451
        return not self.__eq__(other)
1402
1452
 
1403
1453
class Request:
1404
1454
    def __init__(self, display, onerror = None, *args, **keys):
1405
1455
        self._errorhandler = onerror
1406
 
        self._binary = apply(self._request.to_binary, args, keys)
 
1456
        self._binary = self._request.to_binary(*args, **keys)
1407
1457
        self._serial = None
1408
1458
        display.send_request(self, onerror is not None)
1409
1459
 
1416
1466
class ReplyRequest(GetAttrData):
1417
1467
    def __init__(self, display, defer = 0, *args, **keys):
1418
1468
        self._display = display
1419
 
        self._binary = apply(self._request.to_binary, args, keys)
 
1469
        self._binary = self._request.to_binary(*args, **keys)
1420
1470
        self._serial = None
1421
1471
        self._data = None
1422
1472
        self._error = None
1478
1528
 
1479
1529
            keys['sequence_number'] = 0
1480
1530
 
1481
 
            self._binary = apply(self._fields.to_binary, (), keys)
 
1531
            self._binary = self._fields.to_binary(*(), **keys)
1482
1532
 
1483
1533
            keys['send_event'] = 0
1484
1534
            self._data = keys
1492
1542
                val = val | 0x80
1493
1543
            kwlist.append('%s = %s' % (kw, repr(val)))
1494
1544
 
1495
 
        kws = string.join(kwlist, ', ')
 
1545
        kws = ', '.join(kwlist)
1496
1546
        return '%s(%s)' % (self.__class__, kws)
1497
1547
 
1498
 
    def __cmp__(self, other):
 
1548
    def __eq__(self, other):
1499
1549
        if isinstance(other, Event):
1500
 
            return cmp(self._data, other._data)
 
1550
            return self._data == other._data
1501
1551
        else:
1502
1552
            return cmp(self._data, other)
1503
1553
 
1504
 
 
1505
1554
def call_error_handler(handler, error, request):
1506
1555
    try:
1507
1556
        return handler(error, request)