From 8a15f4824e971d12e44312be8b4037d762bab5ca Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Tue, 16 Aug 2022 06:15:04 -0700 Subject: [PATCH] Move utilities from py3compat to utils --- elftools/common/py3compat.py | 22 ---------------------- elftools/common/utils.py | 13 +++++++++++++ elftools/dwarf/callframe.py | 4 ++-- elftools/elf/descriptions.py | 2 +- elftools/elf/notes.py | 4 ++-- examples/elf_notes.py | 2 +- scripts/readelf.py | 5 +++-- 7 files changed, 22 insertions(+), 30 deletions(-) diff --git a/elftools/common/py3compat.py b/elftools/common/py3compat.py index 9b508ab..0086132 100644 --- a/elftools/common/py3compat.py +++ b/elftools/common/py3compat.py @@ -27,26 +27,12 @@ if PY3: # and strings are different types and bytes hold numeric values when # iterated over. - def bytes2hex(b, sep=''): - if not sep: - return b.hex() - return sep.join(map('{:02x}'.format, b)) 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 - def iterbytes(b): - """Return an iterator over the elements of a bytes object. - - For example, for b'abc' yields b'a', b'b' and then b'c'. - """ - for i in range(len(b)): - yield b[i:i+1] - - ifilter = filter - maxint = sys.maxsize def path_to_posix(s): @@ -59,12 +45,6 @@ else: StringIO = BytesIO = cStringIO.StringIO - def bytes2hex(b, sep=''): - res = b.encode('hex') - if not sep: - return res - return sep.join(res[i:i+2] for i in range(0, len(res), 2)) - def bytes2str(b): return b def str2bytes(s): return s int2byte = chr @@ -72,8 +52,6 @@ else: def iterbytes(b): return iter(b) - from itertools import ifilter - maxint = sys.maxint def path_to_posix(s): diff --git a/elftools/common/utils.py b/elftools/common/utils.py index eb51d95..97c7a03 100644 --- a/elftools/common/utils.py +++ b/elftools/common/utils.py @@ -121,6 +121,19 @@ def save_dwarf_section(section, filename): file.write(data) stream.seek(pos, os.SEEK_SET) +def iterbytes(b): + """Return an iterator over the elements of a bytes object. + + For example, for b'abc' yields b'a', b'b' and then b'c'. + """ + for i in range(len(b)): + yield b[i:i+1] + +def bytes2hex(b, sep=''): + if not sep: + return b.hex() + return sep.join(map('{:02x}'.format, b)) + #------------------------- PRIVATE ------------------------- def _assert_with_exception(cond, msg, exception_type): diff --git a/elftools/dwarf/callframe.py b/elftools/dwarf/callframe.py index 436b117..cad8ce2 100644 --- a/elftools/dwarf/callframe.py +++ b/elftools/dwarf/callframe.py @@ -8,8 +8,8 @@ #------------------------------------------------------------------------------- import copy from collections import namedtuple -from ..common.utils import (struct_parse, dwarf_assert, preserve_stream_pos) -from ..common.py3compat import iterbytes +from ..common.utils import ( + struct_parse, dwarf_assert, preserve_stream_pos, iterbytes) from ..construct import Struct, Switch from .enums import DW_EH_encoding_flags from .structs import DWARFStructs diff --git a/elftools/elf/descriptions.py b/elftools/elf/descriptions.py index e91032d..bea5fbc 100644 --- a/elftools/elf/descriptions.py +++ b/elftools/elf/descriptions.py @@ -13,7 +13,7 @@ from .enums import ( ENUM_RELOC_TYPE_MIPS, ENUM_ATTR_TAG_ARM, ENUM_DT_FLAGS, ENUM_DT_FLAGS_1) from .constants import ( P_FLAGS, RH_FLAGS, SH_FLAGS, SUNW_SYMINFO_FLAGS, VER_FLAGS) -from ..common.py3compat import bytes2hex +from ..common.utils import bytes2hex def describe_ei_class(x): diff --git a/elftools/elf/notes.py b/elftools/elf/notes.py index 1389536..60648c6 100644 --- a/elftools/elf/notes.py +++ b/elftools/elf/notes.py @@ -6,8 +6,8 @@ # Eli Bendersky (eliben@gmail.com) # This code is in the public domain #------------------------------------------------------------------------------- -from ..common.py3compat import bytes2hex, bytes2str -from ..common.utils import struct_parse, roundup +from ..common.py3compat import bytes2str +from ..common.utils import struct_parse, bytes2hex, roundup from ..construct import CString diff --git a/examples/elf_notes.py b/examples/elf_notes.py index 1be56e0..21f7270 100644 --- a/examples/elf_notes.py +++ b/examples/elf_notes.py @@ -16,7 +16,7 @@ sys.path[0:0] = ['.', '..'] from elftools.elf.elffile import ELFFile from elftools.elf.sections import NoteSection -from elftools.common.py3compat import bytes2hex +from elftools.common.utils import bytes2hex def process_file(filename): diff --git a/scripts/readelf.py b/scripts/readelf.py index 7af011d..e4641f8 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -27,7 +27,8 @@ sys.path.insert(0, '.') from elftools import __version__ from elftools.common.exceptions import ELFError from elftools.common.py3compat import ( - ifilter, byte2int, bytes2str, str2bytes, iterbytes) + byte2int, bytes2str, str2bytes) +from elftools.common.utils import iterbytes from elftools.elf.elffile import ELFFile from elftools.elf.dynamic import DynamicSection, DynamicSegment from elftools.elf.enums import ENUM_D_TAG @@ -1406,7 +1407,7 @@ class ReadElf(object): # registers are sorted by their number, and the register matching # ra_regnum is always listed last with a special heading. decoded_table = entry.get_decoded() - reg_order = sorted(ifilter( + reg_order = sorted(filter( lambda r: r != ra_regnum, decoded_table.reg_order)) if len(decoded_table.reg_order): -- 2.30.2