Skip to content

Commit 43eed25

Browse files
committed
construct: Remove Python 2 compatibility layer
Remove the compatibility layer `py3compat`. PS: There is eliben#548 to migrate from the interned version of `construct` back to "Construct 2.10+" now that it is maintained again. When that happens, this change will become moot. Until then restore some sanity back into our copy as handling Python type annotations for both Python 2 and 3 is a major pain. Signed-off-by: Philipp Hahn <[email protected]>
1 parent 940981a commit 43eed25

File tree

8 files changed

+15
-101
lines changed

8 files changed

+15
-101
lines changed

elftools/construct/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
... )
2323
>>> s.parse("\\x01\\x02\\x03")
2424
Container(a = 1, b = 515)
25-
>>> print s.parse("\\x01\\x02\\x03")
25+
>>> print(s.parse("\\x01\\x02\\x03"))
2626
Container:
2727
a = 1
2828
b = 515

elftools/construct/adapters.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
from io import BytesIO
2+
13
from .core import Adapter, AdaptationError, Pass
24
from .lib import int_to_bin, bin_to_int, swap_bytes
35
from .lib import FlagsContainer, HexString
4-
from .lib.py3compat import BytesIO, decodebytes
56

67

78
#===============================================================================

elftools/construct/core.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from io import BytesIO
12
from struct import Struct as Packer
23

3-
from .lib.py3compat import BytesIO, advance_iterator, bchr
44
from .lib import Container, ListContainer, LazyContainer
55

66

@@ -520,13 +520,13 @@ def _build(self, obj, stream, context):
520520
if self.subcon.conflags & self.FLAG_COPY_CONTEXT:
521521
for subobj in obj:
522522
if isinstance(obj, bytes):
523-
subobj = bchr(subobj)
523+
subobj = bytes((subobj,))
524524
self.subcon._build(subobj, stream, context.__copy__())
525525
cnt += 1
526526
else:
527527
for subobj in obj:
528528
if isinstance(obj, bytes):
529-
subobj = bchr(subobj)
529+
subobj = bytes((subobj,))
530530
self.subcon._build(subobj, stream, context)
531531
cnt += 1
532532
except ConstructError as ex:
@@ -587,7 +587,7 @@ def _build(self, obj, stream, context):
587587
break
588588
else:
589589
for subobj in obj:
590-
subobj = bchr(subobj)
590+
subobj = bytes((subobj,))
591591
self.subcon._build(subobj, stream, context.__copy__())
592592
if self.predicate(subobj, context):
593593
terminated = True
@@ -722,7 +722,7 @@ def _build(self, obj, stream, context):
722722
elif sc.name is None:
723723
subobj = None
724724
else:
725-
subobj = advance_iterator(objiter)
725+
subobj = next(objiter)
726726
context[sc.name] = subobj
727727
sc._build(subobj, stream, context)
728728

elftools/construct/lib/binary.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
from .py3compat import int2byte
2-
3-
41
def int_to_bin(number, width=32):
52
r"""
63
Convert an integer into its binary representation in a bytes object.
@@ -32,13 +29,6 @@ def int_to_bin(number, width=32):
3229
1: 1,
3330
48: 0, # '0'
3431
49: 1, # '1'
35-
36-
# The following are for Python 2, in which iteration over a bytes object
37-
# yields single-character bytes and not integers.
38-
'\x00': 0,
39-
'\x01': 1,
40-
'0': 0,
41-
'1': 1,
4232
}
4333

4434
def bin_to_int(bits, signed=False):
@@ -81,7 +71,7 @@ def swap_bytes(bits, bytesize=8):
8171
_char_to_bin = {}
8272
_bin_to_char = {}
8373
for i in range(256):
84-
ch = int2byte(i)
74+
ch = bytes((i,))
8575
bin = int_to_bin(i, 8)
8676
# Populate with for both keys i and ch, to support Python 2 & 3
8777
_char_to_bin[ch] = bin

elftools/construct/lib/container.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
Various containers.
33
"""
44

5+
from collections.abc import MutableMapping
56
from pprint import pformat
6-
from .py3compat import MutableMapping
7+
78

89
def recursion_lock(retval, lock_name = "__recursion_lock__"):
910
def decorator(func):

elftools/construct/lib/hex.py

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
from .py3compat import byte2int, int2byte, bytes2str
2-
3-
41
# Map an integer in the inclusive range 0-255 to its string byte representation
52
_printable = dict((i, ".") for i in range(256))
6-
_printable.update((i, bytes2str(int2byte(i))) for i in range(32, 128))
3+
_printable.update((i, chr(i)) for i in range(32, 128))
74

85

96
def hexdump(data, linesize):
@@ -18,8 +15,8 @@ def hexdump(data, linesize):
1815
fmt = fmt % (3 * linesize - 1,)
1916
for i in range(0, len(data), linesize):
2017
line = data[i : i + linesize]
21-
hextext = " ".join('%02x' % byte2int(b) for b in line)
22-
rawtext = "".join(_printable[byte2int(b)] for b in line)
18+
hextext = " ".join('%02x' % b for b in line)
19+
rawtext = "".join(_printable[b] for b in line)
2320
prettylines.append(fmt % (i, str(hextext), str(rawtext)))
2421
return prettylines
2522

elftools/construct/lib/py3compat.py

Lines changed: 0 additions & 74 deletions
This file was deleted.

elftools/construct/macros.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
from .lib.py3compat import int2byte
21
from .lib import (BitStreamReader, BitStreamWriter, encode_bin,
32
decode_bin)
43
from .core import (Struct, MetaField, StaticField, FormatField,
@@ -103,7 +102,7 @@ def Flag(name, truth = 1, falsehood = 0, default = False):
103102
"""
104103

105104
return SymmetricMapping(Field(name, 1),
106-
{True : int2byte(truth), False : int2byte(falsehood)},
105+
{True : bytes((truth,)), False : bytes((falsehood,))},
107106
default = default,
108107
)
109108

0 commit comments

Comments
 (0)