From dd71c43bc81b528a805aae4d60805e70fddec667 Mon Sep 17 00:00:00 2001 From: Eli Bendersky Date: Mon, 8 Apr 2013 06:38:57 -0700 Subject: [PATCH] Setup sys.path properly this time (I'll update the hacking guide to explain what's been done) --- elftools/__init__.py | 1 - examples/dwarf_decode_address.py | 4 ++-- examples/dwarf_die_tree.py | 2 +- examples/dwarf_location_lists.py | 4 ++-- examples/dwarf_range_lists.py | 4 ++-- examples/elf_low_high_api.py | 2 +- examples/elf_relocations.py | 2 +- examples/elf_show_debug_sections.py | 2 +- examples/elfclass_address_size.py | 2 +- examples/examine_dwarf_info.py | 2 +- scripts/readelf.py | 4 ++-- test/run_all_unittests.py | 10 ++++------ test/run_examples_test.py | 4 ++-- test/run_readelf_tests.py | 6 ++---- test/test_arm_support.py | 2 +- test/test_callframe.py | 2 +- test/test_dwarf_expr.py | 2 +- test/test_dwarf_lineprogram.py | 2 +- test/test_dwarf_structs.py | 2 +- test/test_utils.py | 2 +- test/utils.py | 4 +++- 21 files changed, 31 insertions(+), 34 deletions(-) diff --git a/elftools/__init__.py b/elftools/__init__.py index 4bec384..90be2eb 100644 --- a/elftools/__init__.py +++ b/elftools/__init__.py @@ -6,4 +6,3 @@ #------------------------------------------------------------------------------- __version__ = '0.21' - diff --git a/examples/dwarf_decode_address.py b/examples/dwarf_decode_address.py index 831b4fc..4b9ecdc 100644 --- a/examples/dwarf_decode_address.py +++ b/examples/dwarf_decode_address.py @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import maxint, bytes2str from elftools.elf.elffile import ELFFile @@ -45,7 +45,7 @@ def process_file(filename, address): def decode_funcname(dwarfinfo, address): # Go over all DIEs in the DWARF information, looking for a subprogram # entry with an address range that includes the given address. Note that - # this simplifies things by disregarding subprograms that may have + # this simplifies things by disregarding subprograms that may have # split address ranges. for CU in dwarfinfo.iter_CUs(): for DIE in CU.iter_DIEs(): diff --git a/examples/dwarf_die_tree.py b/examples/dwarf_die_tree.py index 7eb07ff..66280e6 100644 --- a/examples/dwarf_die_tree.py +++ b/examples/dwarf_die_tree.py @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile diff --git a/examples/dwarf_location_lists.py b/examples/dwarf_location_lists.py index 6289618..308dbe6 100644 --- a/examples/dwarf_location_lists.py +++ b/examples/dwarf_location_lists.py @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import itervalues from elftools.elf.elffile import ELFFile @@ -86,7 +86,7 @@ def show_loclist(loclist, dwarfinfo, indent): else: d.append(str(loc_entity)) return '\n'.join(indent + s for s in d) - + def attribute_has_location_list(attr): """ Only some attributes can have location list values, if they have the diff --git a/examples/dwarf_range_lists.py b/examples/dwarf_range_lists.py index c310e58..63fd6c7 100644 --- a/examples/dwarf_range_lists.py +++ b/examples/dwarf_range_lists.py @@ -1,7 +1,7 @@ #------------------------------------------------------------------------------- # elftools example: dwarf_range_lists.py # -# Examine DIE entries which have range list values, and decode these range +# Examine DIE entries which have range list values, and decode these range # lists. # # Eli Bendersky (eliben@gmail.com) @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import itervalues from elftools.elf.elffile import ELFFile diff --git a/examples/elf_low_high_api.py b/examples/elf_low_high_api.py index 05d7564..b06b9e6 100644 --- a/examples/elf_low_high_api.py +++ b/examples/elf_low_high_api.py @@ -16,7 +16,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile diff --git a/examples/elf_relocations.py b/examples/elf_relocations.py index fd65541..540f477 100644 --- a/examples/elf_relocations.py +++ b/examples/elf_relocations.py @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile diff --git a/examples/elf_show_debug_sections.py b/examples/elf_show_debug_sections.py index 96e1dd9..8ffe8ad 100644 --- a/examples/elf_show_debug_sections.py +++ b/examples/elf_show_debug_sections.py @@ -14,7 +14,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile diff --git a/examples/elfclass_address_size.py b/examples/elfclass_address_size.py index d6b19d3..e6448a6 100644 --- a/examples/elfclass_address_size.py +++ b/examples/elfclass_address_size.py @@ -15,7 +15,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.elf.elffile import ELFFile diff --git a/examples/examine_dwarf_info.py b/examples/examine_dwarf_info.py index 35ce35b..45cdc67 100644 --- a/examples/examine_dwarf_info.py +++ b/examples/examine_dwarf_info.py @@ -14,7 +14,7 @@ import sys try: import elftools except ImportError: - sys.path.extend(['.', '..']) + sys.path[0:0] = ['.', '..'] from elftools.common.py3compat import bytes2str from elftools.elf.elffile import ELFFile diff --git a/scripts/readelf.py b/scripts/readelf.py index 64d0174..56b78e3 100755 --- a/scripts/readelf.py +++ b/scripts/readelf.py @@ -11,10 +11,10 @@ import os, sys from optparse import OptionParser import string -# If we're running from the root of the development directory, elftools should -# be loaded from there. +# For running from development directory. sys.path.insert(0, '.') + from elftools import __version__ from elftools.common.exceptions import ELFError from elftools.common.py3compat import ( diff --git a/test/run_all_unittests.py b/test/run_all_unittests.py index 1130251..f7291bc 100755 --- a/test/run_all_unittests.py +++ b/test/run_all_unittests.py @@ -16,11 +16,9 @@ except ImportError: if __name__ == '__main__': - try: + import os + if not os.path.isdir('test'): + print('!! Please execute from the root directory of pyelftools') + else: tests = unittest.TestLoader().discover('test', 'test*.py', 'test') unittest.TextTestRunner().run(tests) - except ImportError as err: - print(err) - print('!! Please execute from the root directory of pyelftools') - - diff --git a/test/run_examples_test.py b/test/run_examples_test.py index 10f8c1e..0ac5859 100755 --- a/test/run_examples_test.py +++ b/test/run_examples_test.py @@ -9,8 +9,8 @@ #------------------------------------------------------------------------------- import os, sys import logging - -from test.utils import run_exe, is_in_rootdir, dump_output_to_temp_files +from utils import setup_syspath; setup_syspath() +from utils import run_exe, is_in_rootdir, dump_output_to_temp_files # Create a global logger object diff --git a/test/run_readelf_tests.py b/test/run_readelf_tests.py index fc64dbc..0c542d8 100755 --- a/test/run_readelf_tests.py +++ b/test/run_readelf_tests.py @@ -13,7 +13,8 @@ from difflib import SequenceMatcher from optparse import OptionParser import logging import platform -from test.utils import run_exe, is_in_rootdir, dump_output_to_temp_files +from utils import setup_syspath; setup_syspath() +from utils import run_exe, is_in_rootdir, dump_output_to_temp_files # Create a global logger object @@ -202,6 +203,3 @@ def main(): if __name__ == '__main__': sys.exit(main()) - - - diff --git a/test/test_arm_support.py b/test/test_arm_support.py index fe35051..b2b0ab2 100644 --- a/test/test_arm_support.py +++ b/test/test_arm_support.py @@ -10,7 +10,7 @@ except ImportError: import unittest import os -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.elf.elffile import ELFFile class TestARMSupport(unittest.TestCase): diff --git a/test/test_callframe.py b/test/test_callframe.py index 546ce29..79317d6 100644 --- a/test/test_callframe.py +++ b/test/test_callframe.py @@ -9,7 +9,7 @@ try: except ImportError: import unittest -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.common.py3compat import BytesIO from elftools.dwarf.callframe import ( CallFrameInfo, CIE, FDE, instruction_name, CallFrameInstruction, diff --git a/test/test_dwarf_expr.py b/test/test_dwarf_expr.py index 0314804..22a250f 100644 --- a/test/test_dwarf_expr.py +++ b/test/test_dwarf_expr.py @@ -9,7 +9,7 @@ try: except ImportError: import unittest -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.dwarf.descriptions import ExprDumper, set_global_machine_arch from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_dwarf_lineprogram.py b/test/test_dwarf_lineprogram.py index 8d1b4ab..3a10932 100644 --- a/test/test_dwarf_lineprogram.py +++ b/test/test_dwarf_lineprogram.py @@ -9,7 +9,7 @@ try: except ImportError: import unittest -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.common.py3compat import BytesIO, iteritems from elftools.dwarf.lineprogram import LineProgram, LineState, LineProgramEntry from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_dwarf_structs.py b/test/test_dwarf_structs.py index 9ca575b..94f77c1 100644 --- a/test/test_dwarf_structs.py +++ b/test/test_dwarf_structs.py @@ -9,7 +9,7 @@ try: except ImportError: import unittest -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.dwarf.structs import DWARFStructs diff --git a/test/test_utils.py b/test/test_utils.py index 00e6137..314b6b5 100644 --- a/test/test_utils.py +++ b/test/test_utils.py @@ -10,7 +10,7 @@ except ImportError: import unittest from random import randint -from test.utils import setup_syspath; setup_syspath() +from utils import setup_syspath; setup_syspath() from elftools.common.py3compat import int2byte, BytesIO from elftools.common.utils import (parse_cstring_from_stream, preserve_stream_pos) diff --git a/test/utils.py b/test/utils.py index 57ae62c..7a3b83f 100644 --- a/test/utils.py +++ b/test/utils.py @@ -7,8 +7,9 @@ # This code is in the public domain #------------------------------------------------------------------------------- import os, sys, subprocess, tempfile -from elftools.common.py3compat import bytes2str +# This module should not import elftools before setup_syspath() is called! +# See the Hacking Guide in the documentation for more details. def setup_syspath(): """ Setup sys.path so that tests pick up local pyelftools before the @@ -28,6 +29,7 @@ def run_exe(exe_path, args): popen_cmd.insert(0, sys.executable) proc = subprocess.Popen(popen_cmd, stdout=subprocess.PIPE) proc_stdout = proc.communicate()[0] + from elftools.common.py3compat import bytes2str return proc.returncode, bytes2str(proc_stdout) -- 2.30.2