Move utilities from py3compat to utils
authorEli Bendersky <eliben@gmail.com>
Tue, 16 Aug 2022 13:15:04 +0000 (06:15 -0700)
committerEli Bendersky <eliben@gmail.com>
Tue, 16 Aug 2022 13:15:04 +0000 (06:15 -0700)
elftools/common/py3compat.py
elftools/common/utils.py
elftools/dwarf/callframe.py
elftools/elf/descriptions.py
elftools/elf/notes.py
examples/elf_notes.py
scripts/readelf.py

index 9b508ab348f6911cbc881eb4493a862a65e92afe..0086132c76f487804670e20785c833975391d66e 100644 (file)
@@ -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):
index eb51d959b5d3340c0985580a29aedb0f8c9285e1..97c7a039d27362d49f337c8a49f41f1817fccbea 100644 (file)
@@ -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):
index 436b11700fd0d84502bceef2d5af912027aa4c16..cad8ce2454fb6302c0ac1969da32648d46ac95f8 100644 (file)
@@ -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
index e91032d5f17c03d05a764584224540976447fb46..bea5fbc88e0ef2d233e0427172386e36dda7f1c9 100644 (file)
@@ -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):
index 13895364760f01f46a77489dc64a6d8d4630b5ca..60648c606ead191fb50f05c4ac50a2eeef8a403c 100644 (file)
@@ -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
 
 
index 1be56e06ffa200be8b7b2bb7c3c08b57c6ad554e..21f7270ca92079f6ddd5ed9d55922b48d5560a50 100644 (file)
@@ -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):
index 7af011d71887eaa01b8ea1de14a85b8b15c73eb5..e4641f86926b6b511cad9f0b4580af1acdff53fa 100755 (executable)
@@ -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):