1- # $Id: rq.py,v 1.8 2000-12-22 13:23:34 petli Exp $
1+ # $Id: rq.py,v 1.9 2000-12-29 16:17:02 petli Exp $
22#
33# Xlib.protocol.rq -- structure primitives for request, events and errors
44#
3333
3434class BadDataError (Exception ): pass
3535
36- unsigned_codes = { 1 : 'b' ,
37- 2 : 'h' ,
38- 4 : 'l'
39- }
4036
41- unsigned_codes = { 1 : 'B' ,
42- 2 : 'H' ,
43- 4 : 'L'
44- }
37+ # Thanks to buggy behaviour of struct and array on 64-bit architectures,
38+ # we have to probe which format codes to use for 8, 16 and 32-bit values
4539
40+ signed_codes = { }
41+ unsigned_codes = { }
42+
43+ for c in 'bhil' :
44+ signed_codes [struct .calcsize ('=' + c )] = c
45+ unsigned_codes [struct .calcsize ('=' + c )] = string .upper (c )
46+
47+ sb_code = signed_codes [1 ]
48+ ub_code = unsigned_codes [1 ]
49+ sw_code = signed_codes [2 ]
50+ uw_code = unsigned_codes [2 ]
51+ sl_code = signed_codes [4 ]
52+ ul_code = unsigned_codes [4 ]
4653
4754class Field :
4855 """Field objects represent the data fields of a Struct.
@@ -152,7 +159,7 @@ def get_binary_value(self, keys):
152159
153160
154161class Opcode (ConstantField ):
155- structcode = 'B'
162+ structcode = ub_code
156163 structvalues = 1
157164
158165
@@ -278,27 +285,27 @@ def pack_value(self, value):
278285 return val
279286
280287class Int8 (ValueField ):
281- structcode = 'b'
288+ structcode = sb_code
282289 structvalues = 1
283290
284291class Int16 (ValueField ):
285- structcode = 'h'
292+ structcode = sw_code
286293 structvalues = 1
287294
288295class Int32 (ValueField ):
289- structcode = 'l'
296+ structcode = sl_code
290297 structvalues = 1
291298
292299class Card8 (ValueField ):
293- structcode = 'B'
300+ structcode = ub_code
294301 structvalues = 1
295302
296303class Card16 (ValueField ):
297- structcode = 'H'
304+ structcode = uw_code
298305 structvalues = 1
299306
300307class Card32 (ValueField ):
301- structcode = 'L'
308+ structcode = ul_code
302309 structvalues = 1
303310
304311
@@ -361,7 +368,7 @@ class Cursor(Resource):
361368
362369class Bool (ValueField ):
363370 structvalues = 1
364- structcode = 'B'
371+ structcode = ub_code
365372
366373 def check_value (self , value ):
367374 return not not value
@@ -441,15 +448,15 @@ def pack_value(self, val):
441448 else :
442449 pad = ''
443450
444- return apply (struct .pack , ('>' + 'H' * slen , ) + tuple (val )) + pad , slen
451+ return apply (struct .pack , ('>' + uw_code * slen , ) + tuple (val )) + pad , slen
445452
446453 def parse_binary_value (self , data , display , length , format ):
447454 if self .pad :
448455 slen = length + (length % 2 )
449456 else :
450457 slen = length
451458
452- return struct .unpack ('>' + 'H' * length , data [:length ]), data [slen :]
459+ return struct .unpack ('>' + uw_code * length , data [:length ]), data [slen :]
453460
454461
455462
@@ -596,11 +603,11 @@ def parse_binary_value(self, data, display, length, format):
596603 data = data [length + ((4 - length % 4 ) % 4 ):]
597604
598605 elif format == 16 :
599- ret = (16 , array .array ('H' , data [:2 * length ]))
606+ ret = (16 , array .array (uw_code , data [:2 * length ]))
600607 data = data [2 * (length + length % 2 ):]
601608
602609 elif format == 32 :
603- ret = (32 , array .array ('L' , data [:4 * length ]))
610+ ret = (32 , array .array (ul_code , data [:4 * length ]))
604611 data = data [4 * length :]
605612
606613 return ret , data
@@ -725,7 +732,7 @@ def parse_binary_value(self, data, display, length, format):
725732 else :
726733 dlen = length * format
727734
728- a = array .array ('L' , data [:dlen ])
735+ a = array .array (ul_code , data [:dlen ])
729736
730737 ret = []
731738 for i in range (0 , len (a ), format ):
@@ -738,7 +745,7 @@ def pack_value(self, value):
738745 for v in value :
739746 keycodes = max (keycodes , len (v ))
740747
741- a = array .array ('L' )
748+ a = array .array (ul_code )
742749
743750 for v in value :
744751 for k in v :
@@ -753,7 +760,7 @@ class ModifierMapping(ValueField):
753760 structcode = None
754761
755762 def parse_binary_value (self , data , display , length , format ):
756- a = array .array ('B' , data [:8 * format ])
763+ a = array .array (ub_code , data [:8 * format ])
757764
758765 ret = []
759766 for i in range (0 , 8 ):
@@ -769,7 +776,7 @@ def pack_value(self, value):
769776 for v in value :
770777 keycodes = max (keycodes , len (v ))
771778
772- a = array .array ('B' )
779+ a = array .array (ub_code )
773780
774781 for v in value :
775782 for k in v :
@@ -802,12 +809,12 @@ def __init__(self, code):
802809 def parse_value (self , value , display ):
803810 return value
804811
805- Card8Obj = ScalarObj ('B' )
806- Card16Obj = ScalarObj ('H' )
807- Card32Obj = ScalarObj ('L' )
812+ Card8Obj = ScalarObj (ub_code )
813+ Card16Obj = ScalarObj (uw_code )
814+ Card32Obj = ScalarObj (ul_code )
808815
809816class ResourceObj :
810- structcode = 'L'
817+ structcode = ul_code
811818 structvalues = 1
812819
813820 def __init__ (self , class_name ):
0 commit comments