From: Eli Bendersky Date: Tue, 16 Aug 2022 13:03:20 +0000 (-0700) Subject: Remove iter* utilities from py3compat and refactor uses X-Git-Tag: v0.30~31 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=a14c58ae7a6371164633ad151486a8c3f02aceda;p=pyelftools.git Remove iter* utilities from py3compat and refactor uses --- diff --git a/elftools/common/py3compat.py b/elftools/common/py3compat.py index 336164e..9b508ab 100644 --- a/elftools/common/py3compat.py +++ b/elftools/common/py3compat.py @@ -78,21 +78,3 @@ else: 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 diff --git a/elftools/dwarf/callframe.py b/elftools/dwarf/callframe.py index 8b3ec5c..436b117 100644 --- a/elftools/dwarf/callframe.py +++ b/elftools/dwarf/callframe.py @@ -9,7 +9,7 @@ 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 @@ -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 name in list(iterkeys(globals())): +for name in list(globals().keys()): if name.startswith('DW_CFA'): _OPCODE_NAME_MAP[globals()[name]] = name diff --git a/elftools/dwarf/die.py b/elftools/dwarf/die.py index b26f8a1..6d9a764 100755 --- a/elftools/dwarf/die.py +++ b/elftools/dwarf/die.py @@ -10,7 +10,7 @@ from collections import namedtuple, OrderedDict 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 @@ -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) - for attrname, attrval in iteritems(self.attributes): + for attrname, attrval in self.attributes.items(): s += ' |%-18s: %s\n' % (attrname, attrval) return s diff --git a/elftools/dwarf/dwarf_expr.py b/elftools/dwarf/dwarf_expr.py index 714ef4e..899a5f7 100644 --- a/elftools/dwarf/dwarf_expr.py +++ b/elftools/dwarf/dwarf_expr.py @@ -8,7 +8,7 @@ #------------------------------------------------------------------------------- from collections import namedtuple -from ..common.py3compat import BytesIO, iteritems +from ..common.py3compat import BytesIO 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 -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, diff --git a/elftools/dwarf/enums.py b/elftools/dwarf/enums.py index cf7a00b..1239ddc 100644 --- a/elftools/dwarf/enums.py +++ b/elftools/dwarf/enums.py @@ -7,7 +7,6 @@ # This code is in the public domain #------------------------------------------------------------------------------- from ..construct import Pass -from ..common.py3compat import iteritems ENUM_DW_TAG = dict( @@ -371,7 +370,7 @@ ENUM_DW_FORM = dict( ) # 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( diff --git a/elftools/dwarf/namelut.py b/elftools/dwarf/namelut.py index fd12aad..7999be8 100755 --- a/elftools/dwarf/namelut.py +++ b/elftools/dwarf/namelut.py @@ -9,8 +9,8 @@ import os import collections from collections import OrderedDict +from collections.abc import Mapping from ..common.utils import struct_parse -from ..common.py3compat import Mapping from bisect import bisect_right import math from ..construct import CString, Struct, If diff --git a/elftools/elf/descriptions.py b/elftools/elf/descriptions.py index 38c80b6..e91032d 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, iteritems +from ..common.py3compat import bytes2hex 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 = {} - 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 diff --git a/examples/dwarf_location_info.py b/examples/dwarf_location_info.py index 0ec9933..ec491aa 100644 --- a/examples/dwarf_location_info.py +++ b/examples/dwarf_location_info.py @@ -26,7 +26,6 @@ import sys # 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) @@ -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. - 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)) diff --git a/examples/dwarf_range_lists.py b/examples/dwarf_range_lists.py index 9157f3b..c9540d1 100644 --- a/examples/dwarf_range_lists.py +++ b/examples/dwarf_range_lists.py @@ -14,7 +14,6 @@ import sys # 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) @@ -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. - 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 diff --git a/scripts/readelf.py b/scripts/readelf.py index 2095c91..7af011d 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 ( - 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 @@ -1120,7 +1120,7 @@ class ReadElf(object): 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): diff --git a/test/test_dwarf_lineprogram.py b/test/test_dwarf_lineprogram.py index 2a0a19e..526aa7b 100644 --- a/test/test_dwarf_lineprogram.py +++ b/test/test_dwarf_lineprogram.py @@ -6,7 +6,7 @@ #------------------------------------------------------------------------------- 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 * @@ -40,7 +40,7 @@ class TestLineProgram(unittest.TestCase): """ 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): diff --git a/test/test_py3compat.py b/test/test_py3compat.py index 9c8fb13..69dcbc2 100644 --- a/test/test_py3compat.py +++ b/test/test_py3compat.py @@ -6,25 +6,5 @@ #------------------------------------------------------------------------------- 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()