Remove iter* utilities from py3compat and refactor uses
authorEli Bendersky <eliben@gmail.com>
Tue, 16 Aug 2022 13:03:20 +0000 (06:03 -0700)
committerEli Bendersky <eliben@gmail.com>
Tue, 16 Aug 2022 13:03:20 +0000 (06:03 -0700)
12 files changed:
elftools/common/py3compat.py
elftools/dwarf/callframe.py
elftools/dwarf/die.py
elftools/dwarf/dwarf_expr.py
elftools/dwarf/enums.py
elftools/dwarf/namelut.py
elftools/elf/descriptions.py
examples/dwarf_location_info.py
examples/dwarf_range_lists.py
scripts/readelf.py
test/test_dwarf_lineprogram.py
test/test_py3compat.py

index 336164ebd4a709ca041ac4336f5b8276a061baa6..9b508ab348f6911cbc881eb4493a862a65e92afe 100644 (file)
@@ -78,21 +78,3 @@ else:
 
     def path_to_posix(s):
         return posixpath.join(*os.path.split(s))
 
     def path_to_posix(s):
         return posixpath.join(*os.path.split(s))
-
-
-def iterkeys(d):
-    """Return an iterator over the keys of a dictionary."""
-    return getattr(d, 'keys' if PY3 else 'iterkeys')()
-
-def itervalues(d):
-    """Return an iterator over the values of a dictionary."""
-    return getattr(d, 'values' if PY3 else 'itervalues')()
-
-def iteritems(d):
-    """Return an iterator over the items of a dictionary."""
-    return getattr(d, 'items' if PY3 else 'iteritems')()
-
-try:
-    from collections.abc import Mapping  # python >= 3.3
-except ImportError:
-    from collections import Mapping  # python < 3.3
index 8b3ec5c787e410d9f9bb6cc539754fc2710b4912..436b11700fd0d84502bceef2d5af912027aa4c16 100644 (file)
@@ -9,7 +9,7 @@
 import copy
 from collections import namedtuple
 from ..common.utils import (struct_parse, dwarf_assert, preserve_stream_pos)
 import copy
 from collections import namedtuple
 from ..common.utils import (struct_parse, dwarf_assert, preserve_stream_pos)
-from ..common.py3compat import iterbytes, iterkeys
+from ..common.py3compat import iterbytes
 from ..construct import Struct, Switch
 from .enums import DW_EH_encoding_flags
 from .structs import DWARFStructs
 from ..construct import Struct, Switch
 from .enums import DW_EH_encoding_flags
 from .structs import DWARFStructs
@@ -719,6 +719,6 @@ _PRIMARY_ARG_MASK = 0b00111111
 # for DW_CFA_* instructions, and mapping their values to names. Since all
 # names were imported from constants with `import *`, we look in globals()
 _OPCODE_NAME_MAP = {}
 # for DW_CFA_* instructions, and mapping their values to names. Since all
 # names were imported from constants with `import *`, we look in globals()
 _OPCODE_NAME_MAP = {}
-for name in list(iterkeys(globals())):
+for name in list(globals().keys()):
     if name.startswith('DW_CFA'):
         _OPCODE_NAME_MAP[globals()[name]] = name
     if name.startswith('DW_CFA'):
         _OPCODE_NAME_MAP[globals()[name]] = name
index b26f8a1d206f4919d92a70cfc1d0641f950847bd..6d9a764e953075a39c3076e8c44b6f8e573c76f2 100755 (executable)
@@ -10,7 +10,7 @@ from collections import namedtuple, OrderedDict
 import os
 
 from ..common.exceptions import DWARFError
 import os
 
 from ..common.exceptions import DWARFError
-from ..common.py3compat import bytes2str, iteritems
+from ..common.py3compat import bytes2str
 from ..common.utils import struct_parse, preserve_stream_pos
 from .enums import DW_FORM_raw2name
 from .dwarf_util import _resolve_via_offset_table, _get_base_offset
 from ..common.utils import struct_parse, preserve_stream_pos
 from .enums import DW_FORM_raw2name
 from .dwarf_util import _resolve_via_offset_table, _get_base_offset
@@ -210,7 +210,7 @@ class DIE(object):
     def __repr__(self):
         s = 'DIE %s, size=%s, has_children=%s\n' % (
             self.tag, self.size, self.has_children)
     def __repr__(self):
         s = 'DIE %s, size=%s, has_children=%s\n' % (
             self.tag, self.size, self.has_children)
-        for attrname, attrval in iteritems(self.attributes):
+        for attrname, attrval in self.attributes.items():
             s += '    |%-18s:  %s\n' % (attrname, attrval)
         return s
 
             s += '    |%-18s:  %s\n' % (attrname, attrval)
         return s
 
index 714ef4e4fd1955eb05f350b8e14e70704431f72e..899a5f74aa224412e0b1b3265658cd086733750a 100644 (file)
@@ -8,7 +8,7 @@
 #-------------------------------------------------------------------------------
 from collections import namedtuple
 
 #-------------------------------------------------------------------------------
 from collections import namedtuple
 
-from ..common.py3compat import BytesIO, iteritems
+from ..common.py3compat import BytesIO
 from ..common.utils import struct_parse, bytelist2string, read_blob
 
 
 from ..common.utils import struct_parse, bytelist2string, read_blob
 
 
@@ -109,7 +109,7 @@ _generate_dynamic_values(DW_OP_name2opcode, 'DW_OP_reg', 0, 31, 0x50)
 _generate_dynamic_values(DW_OP_name2opcode, 'DW_OP_breg', 0, 31, 0x70)
 
 # opcode -> name mapping
 _generate_dynamic_values(DW_OP_name2opcode, 'DW_OP_breg', 0, 31, 0x70)
 
 # opcode -> name mapping
-DW_OP_opcode2name = dict((v, k) for k, v in iteritems(DW_OP_name2opcode))
+DW_OP_opcode2name = dict((v, k) for k, v in DW_OP_name2opcode.items())
 
 
 # Each parsed DWARF expression is returned as this type with its numeric opcode,
 
 
 # Each parsed DWARF expression is returned as this type with its numeric opcode,
index cf7a00b34972e01593e30796de23cf5e0f1bc181..1239ddc83f8114de6c7a679d1249d5d6b35ecd26 100644 (file)
@@ -7,7 +7,6 @@
 # This code is in the public domain
 #-------------------------------------------------------------------------------
 from ..construct import Pass
 # This code is in the public domain
 #-------------------------------------------------------------------------------
 from ..construct import Pass
-from ..common.py3compat import iteritems
 
 
 ENUM_DW_TAG = dict(
 
 
 ENUM_DW_TAG = dict(
@@ -371,7 +370,7 @@ ENUM_DW_FORM = dict(
 )
 
 # Inverse mapping for ENUM_DW_FORM
 )
 
 # Inverse mapping for ENUM_DW_FORM
-DW_FORM_raw2name = dict((v, k) for k, v in iteritems(ENUM_DW_FORM))
+DW_FORM_raw2name = dict((v, k) for k, v in ENUM_DW_FORM.items())
 
 # See http://www.airs.com/blog/archives/460
 DW_EH_encoding_flags = dict(
 
 # See http://www.airs.com/blog/archives/460
 DW_EH_encoding_flags = dict(
index fd12aad332fad5a19f9edbba7f094cd54fd74aba..7999be8834b7484b694eb96b64ec3d694007aacd 100755 (executable)
@@ -9,8 +9,8 @@
 import os
 import collections
 from collections import OrderedDict
 import os
 import collections
 from collections import OrderedDict
+from collections.abc import Mapping
 from ..common.utils import struct_parse
 from ..common.utils import struct_parse
-from ..common.py3compat import Mapping
 from bisect import bisect_right
 import math
 from ..construct import CString, Struct, If
 from bisect import bisect_right
 import math
 from ..construct import CString, Struct, If
index 38c80b6e6ee5b418c39936b5d4a4fabe49270535..e91032d5f17c03d05a764584224540976447fb46 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)
     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, iteritems
+from ..common.py3compat import bytes2hex
 
 
 def describe_ei_class(x):
 
 
 def describe_ei_class(x):
@@ -660,7 +660,7 @@ def _reverse_dict(d, low_priority=()):
     not override any other entries of the same value.
     """
     out = {}
     not override any other entries of the same value.
     """
     out = {}
-    for k, v in iteritems(d):
+    for k, v in d.items():
         if v in out and k in low_priority:
             continue
         out[v] = k
         if v in out and k in low_priority:
             continue
         out[v] = k
index 0ec9933f5113b29dbda6c678768659cfdcb55dbc..ec491aa51ec69caa91b812ffd7477972588bc91c 100644 (file)
@@ -26,7 +26,6 @@ import sys
 # examples/ dir of the source distribution.
 sys.path[0:0] = ['.', '..']
 
 # examples/ dir of the source distribution.
 sys.path[0:0] = ['.', '..']
 
-from elftools.common.py3compat import itervalues
 from elftools.elf.elffile import ELFFile
 from elftools.dwarf.descriptions import (
     describe_DWARF_expr, set_global_machine_arch)
 from elftools.elf.elffile import ELFFile
 from elftools.dwarf.descriptions import (
     describe_DWARF_expr, set_global_machine_arch)
@@ -72,7 +71,7 @@ def process_file(filename):
                 # Go over all attributes of the DIE. Each attribute is an
                 # AttributeValue object (from elftools.dwarf.die), which we
                 # can examine.
                 # Go over all attributes of the DIE. Each attribute is an
                 # AttributeValue object (from elftools.dwarf.die), which we
                 # can examine.
-                for attr in itervalues(DIE.attributes):
+                for attr in DIE.attributes.values():
                     # Check if this attribute contains location information
                     if loc_parser.attribute_has_location(attr, CU['version']):
                         print('   DIE %s. attr %s.' % (DIE.tag, attr.name))
                     # Check if this attribute contains location information
                     if loc_parser.attribute_has_location(attr, CU['version']):
                         print('   DIE %s. attr %s.' % (DIE.tag, attr.name))
index 9157f3b920fcedca14fe9b51b637d7be3602f377..c9540d1388bd2c9a6ba427dbc9dfeab91e0dde0e 100644 (file)
@@ -14,7 +14,6 @@ import sys
 # examples/ dir of the source distribution.
 sys.path[0:0] = ['.', '..']
 
 # examples/ dir of the source distribution.
 sys.path[0:0] = ['.', '..']
 
-from elftools.common.py3compat import itervalues
 from elftools.elf.elffile import ELFFile
 from elftools.dwarf.descriptions import (
     describe_DWARF_expr, set_global_machine_arch)
 from elftools.elf.elffile import ELFFile
 from elftools.dwarf.descriptions import (
     describe_DWARF_expr, set_global_machine_arch)
@@ -55,7 +54,7 @@ def process_file(filename):
                 # Go over all attributes of the DIE. Each attribute is an
                 # AttributeValue object (from elftools.dwarf.die), which we
                 # can examine.
                 # Go over all attributes of the DIE. Each attribute is an
                 # AttributeValue object (from elftools.dwarf.die), which we
                 # can examine.
-                for attr in itervalues(DIE.attributes):
+                for attr in DIE.attributes.values():
                     if attribute_has_range_list(attr):
                         # This is a range list. Its value is an offset into
                         # the .debug_ranges section, so we can use the range
                     if attribute_has_range_list(attr):
                         # This is a range list. Its value is an offset into
                         # the .debug_ranges section, so we can use the range
index 2095c915d629635efdb34ffb33790ff2421ca840..7af011d71887eaa01b8ea1de14a85b8b15c73eb5 100755 (executable)
@@ -27,7 +27,7 @@ sys.path.insert(0, '.')
 from elftools import __version__
 from elftools.common.exceptions import ELFError
 from elftools.common.py3compat import (
 from elftools import __version__
 from elftools.common.exceptions import ELFError
 from elftools.common.py3compat import (
-        ifilter, byte2int, bytes2str, itervalues, str2bytes, iterbytes)
+        ifilter, byte2int, bytes2str, str2bytes, iterbytes)
 from elftools.elf.elffile import ELFFile
 from elftools.elf.dynamic import DynamicSection, DynamicSegment
 from elftools.elf.enums import ENUM_D_TAG
 from elftools.elf.elffile import ELFFile
 from elftools.elf.dynamic import DynamicSection, DynamicSegment
 from elftools.elf.enums import ENUM_D_TAG
@@ -1120,7 +1120,7 @@ class ReadElf(object):
                     die_depth -= 1
                     continue
 
                     die_depth -= 1
                     continue
 
-                for attr in itervalues(die.attributes):
+                for attr in die.attributes.values():
                     name = attr.name
                     # Unknown attribute values are passed-through as integers
                     if isinstance(name, int):
                     name = attr.name
                     # Unknown attribute values are passed-through as integers
                     if isinstance(name, int):
index 2a0a19e0dbafce5f8cab4a47097d936c5442f241..526aa7b528040fa226e66a0aacc8770ac7355f0c 100644 (file)
@@ -6,7 +6,7 @@
 #-------------------------------------------------------------------------------
 import unittest
 
 #-------------------------------------------------------------------------------
 import unittest
 
-from elftools.common.py3compat import BytesIO, iteritems
+from elftools.common.py3compat import BytesIO
 from elftools.dwarf.lineprogram import LineProgram, LineState, LineProgramEntry
 from elftools.dwarf.structs import DWARFStructs
 from elftools.dwarf.constants import *
 from elftools.dwarf.lineprogram import LineProgram, LineState, LineProgramEntry
 from elftools.dwarf.structs import DWARFStructs
 from elftools.dwarf.constants import *
@@ -40,7 +40,7 @@ class TestLineProgram(unittest.TestCase):
         """ Assert that the state attributes specified in kwargs have the given
             values (the rest are default).
         """
         """ Assert that the state attributes specified in kwargs have the given
             values (the rest are default).
         """
-        for k, v in iteritems(kwargs):
+        for k, v in kwargs.items():
             self.assertEqual(getattr(state, k), v)
 
     def test_spec_sample_59(self):
             self.assertEqual(getattr(state, k), v)
 
     def test_spec_sample_59(self):
index 9c8fb13681d7eb5f9b2f02560080d7d090793b2a..69dcbc21c28a6d4d9ab4c54230ad3a32dfbb08d0 100644 (file)
@@ -6,25 +6,5 @@
 #-------------------------------------------------------------------------------
 import unittest
 
 #-------------------------------------------------------------------------------
 import unittest
 
-from elftools.common.py3compat import (iterbytes, iterkeys, itervalues,
-                                       iteritems)
-
-
-class TestPy3Compat(unittest.TestCase):
-    def test_iterbytes(self):
-        bi = iterbytes(b'fo1')
-        self.assertEqual(next(bi), b'f')
-        self.assertEqual(next(bi), b'o')
-        self.assertEqual(next(bi), b'1')
-        with self.assertRaises(StopIteration):
-            next(bi)
-
-    def test_iterdict(self):
-        d = {1: 'foo', 2: 'bar'}
-        self.assertEqual(list(sorted(iterkeys(d))), [1, 2])
-        self.assertEqual(list(sorted(itervalues(d))), ['bar', 'foo'])
-        self.assertEqual(list(sorted(iteritems(d))), [(1, 'foo'), (2, 'bar')])
-
-
 if __name__ == '__main__':
     unittest.main()
 if __name__ == '__main__':
     unittest.main()