6
6
7
7
# STDLIB
8
8
import re
9
+ import struct
9
10
import sys
10
- from struct import pack as _struct_pack
11
- from struct import unpack as _struct_unpack
12
11
13
12
# THIRD-PARTY
14
13
import numpy as np
57
56
_zero_byte = b"\0 "
58
57
59
58
60
- struct_unpack = _struct_unpack
61
- struct_pack = _struct_pack
62
-
63
-
64
59
if sys .byteorder == "little" :
65
60
66
61
def _ensure_bigendian (x ):
@@ -154,7 +149,7 @@ def bool_to_bitarray(value):
154
149
if bit_no != 7 :
155
150
bytes .append (byte )
156
151
157
- return struct_pack (f"{ len (bytes )} B" , * bytes )
152
+ return struct . pack (f"{ len (bytes )} B" , * bytes )
158
153
159
154
160
155
class Converter :
@@ -182,11 +177,11 @@ def __init__(self, field, config=None, pos=None):
182
177
183
178
@staticmethod
184
179
def _parse_length (read ):
185
- return struct_unpack (">I" , read (4 ))[0 ]
180
+ return struct . unpack (">I" , read (4 ))[0 ]
186
181
187
182
@staticmethod
188
183
def _write_length (length ):
189
- return struct_pack (">I" , int (length ))
184
+ return struct . pack (">I" , int (length ))
190
185
191
186
def supports_empty_values (self , config ):
192
187
"""
@@ -379,7 +374,7 @@ def _binparse_var(self, read):
379
374
return read (length ).decode ("ascii" ), False
380
375
381
376
def _binparse_fixed (self , read ):
382
- s = struct_unpack (self ._struct_format , read (self .arraysize ))[0 ]
377
+ s = struct . unpack (self ._struct_format , read (self .arraysize ))[0 ]
383
378
end = s .find (_zero_byte )
384
379
s = s .decode ("ascii" )
385
380
if end != - 1 :
@@ -404,7 +399,7 @@ def _binoutput_fixed(self, value, mask):
404
399
value = value .encode ("ascii" )
405
400
except ValueError :
406
401
vo_raise (E24 , (value , self .field_name ))
407
- return struct_pack (self ._struct_format , value )
402
+ return struct . pack (self ._struct_format , value )
408
403
409
404
410
405
class UnicodeChar (Converter ):
@@ -453,7 +448,7 @@ def _binparse_var(self, read):
453
448
return read (length * 2 ).decode ("utf_16_be" ), False
454
449
455
450
def _binparse_fixed (self , read ):
456
- s = struct_unpack (self ._struct_format , read (self .arraysize * 2 ))[0 ]
451
+ s = struct . unpack (self ._struct_format , read (self .arraysize * 2 ))[0 ]
457
452
s = s .decode ("utf_16_be" )
458
453
end = s .find ("\0 " )
459
454
if end != - 1 :
@@ -469,7 +464,7 @@ def _binoutput_var(self, value, mask):
469
464
def _binoutput_fixed (self , value , mask ):
470
465
if mask :
471
466
value = ""
472
- return struct_pack (self ._struct_format , value .encode ("utf_16_be" ))
467
+ return struct . pack (self ._struct_format , value .encode ("utf_16_be" ))
473
468
474
469
475
470
class Array (Converter ):
0 commit comments