From e44e2bd54a25d12f935a321b4f86ed0a41dd80ea Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 16 Aug 2022 06:31:35 -0700 Subject: [PATCH] Remove int2byte and byte2int utilities from py3compat --- elftools/common/py3compat.py | 10 ---------- elftools/common/utils.py | 3 +-- scripts/readelf.py | 12 ++++++------ test/test_utils.py | 3 +-- 4 files changed, 8 insertions(+), 20 deletions(-) diff --git a/elftools/common/py3compat.py b/elftools/common/py3compat.py index 681dc66..7894896 100644 --- a/elftools/common/py3compat.py +++ b/elftools/common/py3compat.py @@ -24,13 +24,3 @@ if PY3: def bytes2str(b): return b.decode('latin-1') def str2bytes(s): return s.encode('latin-1') - def int2byte(i): return bytes((i,)) - def byte2int(b): return b - -else: - def bytes2str(b): return b - def str2bytes(s): return s - int2byte = chr - byte2int = ord - def iterbytes(b): - return iter(b) diff --git a/elftools/common/utils.py b/elftools/common/utils.py index 97c7a03..2355ed4 100644 --- a/elftools/common/utils.py +++ b/elftools/common/utils.py @@ -8,7 +8,6 @@ #------------------------------------------------------------------------------- from contextlib import contextmanager from .exceptions import ELFParseError, ELFError, DWARFError -from .py3compat import int2byte from ..construct import ConstructError, ULInt8 import os @@ -25,7 +24,7 @@ def bytelist2string(bytelist): """ Convert a list of byte values (e.g. [0x10 0x20 0x00]) to a bytes object (e.g. b'\x10\x20\x00'). """ - return b''.join(int2byte(b) for b in bytelist) + return b''.join(bytes((b,)) for b in bytelist) def struct_parse(struct, stream, stream_pos=None): diff --git a/scripts/readelf.py b/scripts/readelf.py index e4641f8..cd9d384 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -27,7 +27,7 @@ sys.path.insert(0, '.') from elftools import __version__ from elftools.common.exceptions import ELFError from elftools.common.py3compat import ( - byte2int, bytes2str, str2bytes) + bytes2str, str2bytes) from elftools.common.utils import iterbytes from elftools.elf.elffile import ELFFile from elftools.elf.dynamic import DynamicSection, DynamicSegment @@ -122,7 +122,7 @@ class ReadElf(object): """ self._emitline('ELF Header:') self._emit(' Magic: ') - self._emit(' '.join('%2.2x' % byte2int(b) + self._emit(' '.join('%2.2x' % b for b in self.elffile.e_ident_raw)) self._emitline(' ') header = self.elffile.header @@ -801,7 +801,7 @@ class ReadElf(object): self._emit(' %s ' % self._format_hex(addr, fieldsize=8)) for i in range(16): if i < linebytes: - self._emit('%2.2x' % byte2int(data[dataptr + i])) + self._emit('%2.2x' % data[dataptr + i]) else: self._emit(' ') if i % 4 == 3: @@ -809,7 +809,7 @@ class ReadElf(object): for i in range(linebytes): c = data[dataptr + i : dataptr + i + 1] - if byte2int(c[0]) >= 32 and byte2int(c[0]) < 0x7f: + if c[0] >= 32 and c[0] < 0x7f: self._emit(bytes2str(c)) else: self._emit(bytes2str(b'.')) @@ -844,14 +844,14 @@ class ReadElf(object): while dataptr < len(data): while ( dataptr < len(data) and - not (32 <= byte2int(data[dataptr]) <= 127)): + not (32 <= data[dataptr] <= 127)): dataptr += 1 if dataptr >= len(data): break endptr = dataptr - while endptr < len(data) and byte2int(data[endptr]) != 0: + while endptr < len(data) and data[endptr] != 0: endptr += 1 found = True diff --git a/test/test_utils.py b/test/test_utils.py index 52b8391..abb53dc 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -8,14 +8,13 @@ import unittest from io import BytesIO from random import randint -from elftools.common.py3compat import int2byte from elftools.common.utils import (parse_cstring_from_stream, merge_dicts, preserve_stream_pos) class Test_parse_cstring_from_stream(unittest.TestCase): def _make_random_bytes(self, n): - return b''.join(int2byte(randint(32, 127)) for i in range(n)) + return b''.join(bytes((randint(32, 127),)) for i in range(n)) def test_small1(self): sio = BytesIO(b'abcdefgh\x0012345') -- 2.30.2