From 2bad848b85f3463edca652e0f73cc64c57961fbb Mon Sep 17 00:00:00 2001 From: Andreas Sandberg Date: Fri, 25 Jan 2019 11:46:30 +0000 Subject: [PATCH] python: Enforce absolute imports for Python 3 compatibility Change-Id: Ia88d7fd472f7aed9b97df81468211384981bf6c6 Signed-off-by: Andreas Sandberg Reviewed-on: https://gem5-review.googlesource.com/c/15983 Reviewed-by: Gabe Black Maintainer: Gabe Black --- src/arch/generic/BaseTLB.py | 2 +- src/python/importer.py | 3 +++ src/python/m5/SimObject.py | 1 + src/python/m5/__init__.py | 21 ++++++++++++--------- src/python/m5/core.py | 3 +++ src/python/m5/ext/__init__.py | 3 +++ src/python/m5/ext/pyfdt/pyfdt.py | 3 +++ src/python/m5/internal/params.py | 3 +++ src/python/m5/main.py | 22 +++++++++++----------- src/python/m5/objects/__init__.py | 3 +++ src/python/m5/options.py | 3 +++ src/python/m5/params.py | 8 ++++---- src/python/m5/proxy.py | 5 ++++- src/python/m5/simulate.py | 12 ++++++------ src/python/m5/trace.py | 3 +++ src/python/m5/util/__init__.py | 16 ++++++++-------- src/python/m5/util/code_formatter.py | 2 +- src/python/m5/util/dot_writer.py | 3 +++ src/python/m5/util/jobfile.py | 1 + src/python/m5/util/pybind.py | 3 +++ src/python/m5/util/smartdict.py | 6 ++++-- src/python/m5/util/sorteddict.py | 1 + src/python/m5/util/terminal.py | 1 + 23 files changed, 85 insertions(+), 43 deletions(-) diff --git a/src/arch/generic/BaseTLB.py b/src/arch/generic/BaseTLB.py index b98b99356..688117a66 100644 --- a/src/arch/generic/BaseTLB.py +++ b/src/arch/generic/BaseTLB.py @@ -29,7 +29,7 @@ # Ivan Pizarro from m5.params import * -from MemObject import MemObject +from m5.objects.MemObject import MemObject class BaseTLB(MemObject): type = 'BaseTLB' diff --git a/src/python/importer.py b/src/python/importer.py index 224ab3bd9..20d168e1c 100644 --- a/src/python/importer.py +++ b/src/python/importer.py @@ -26,6 +26,9 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + # Simple importer that allows python to import data from a dict of # code objects. The keys are the module path, and the items are the # filename and bytecode of the file. diff --git a/src/python/m5/SimObject.py b/src/python/m5/SimObject.py index 11330b4c3..5a869125e 100644 --- a/src/python/m5/SimObject.py +++ b/src/python/m5/SimObject.py @@ -44,6 +44,7 @@ # Andreas Sandberg from __future__ import print_function +from __future__ import absolute_import import sys from types import FunctionType, MethodType, ModuleType diff --git a/src/python/m5/__init__.py b/src/python/m5/__init__.py index 2730ea142..d97727cd8 100644 --- a/src/python/m5/__init__.py +++ b/src/python/m5/__init__.py @@ -26,6 +26,9 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + # Import useful subpackages of M5, but *only* when run as an m5 # script. This is mostly to keep backward compatibility with existing # scripts while allowing new SCons code to operate properly. @@ -42,14 +45,14 @@ except ImportError: in_gem5 = False if in_gem5: - import SimObject - import core - import objects - import params - import stats - import util + from . import SimObject + from . import core + from . import objects + from . import params + from . import stats + from . import util - from event import * - from main import main - from simulate import * + from .event import * + from .main import main + from .simulate import * diff --git a/src/python/m5/core.py b/src/python/m5/core.py index ab0ea57ea..4c94353aa 100644 --- a/src/python/m5/core.py +++ b/src/python/m5/core.py @@ -26,4 +26,7 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + from _m5.core import setOutputDir diff --git a/src/python/m5/ext/__init__.py b/src/python/m5/ext/__init__.py index a5bc8ef26..f566a3493 100644 --- a/src/python/m5/ext/__init__.py +++ b/src/python/m5/ext/__init__.py @@ -36,3 +36,6 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # Authors: Glenn Bergmans + +from __future__ import print_function +from __future__ import absolute_import diff --git a/src/python/m5/ext/pyfdt/pyfdt.py b/src/python/m5/ext/pyfdt/pyfdt.py index 3b3238e25..3c78b036c 100644 --- a/src/python/m5/ext/pyfdt/pyfdt.py +++ b/src/python/m5/ext/pyfdt/pyfdt.py @@ -19,6 +19,9 @@ Device Tree Blob Parser @author: Neil 'superna' Armstrong """ +from __future__ import print_function +from __future__ import absolute_import + import string import os import json diff --git a/src/python/m5/internal/params.py b/src/python/m5/internal/params.py index 400e78062..d06851ac8 100644 --- a/src/python/m5/internal/params.py +++ b/src/python/m5/internal/params.py @@ -38,6 +38,9 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + import inspect import _m5 diff --git a/src/python/m5/main.py b/src/python/m5/main.py index eeaa9470b..fae04e76c 100644 --- a/src/python/m5/main.py +++ b/src/python/m5/main.py @@ -54,8 +54,8 @@ brief_copyright=\ "gem5 is copyrighted software; use the --copyright option for details." def parse_options(): - import config - from options import OptionParser + from . import config + from .options import OptionParser options = OptionParser(usage=usage, version=version, description=brief_copyright) @@ -203,15 +203,15 @@ def _check_tracing(): def main(*args): import m5 - import core - import debug - import defines - import event - import info - import stats - import trace + from . import core + from . import debug + from . import defines + from . import event + from . import info + from . import stats + from . import trace - from util import inform, fatal, panic, isInteractive + from .util import inform, fatal, panic, isInteractive if len(args) == 0: options, arguments = parse_options() @@ -286,7 +286,7 @@ def main(*args): debug.help() if options.list_sim_objects: - import SimObject + from . import SimObject done = True print("SimObjects:") objects = list(SimObject.allClasses.keys()) diff --git a/src/python/m5/objects/__init__.py b/src/python/m5/objects/__init__.py index 302bb6f23..f80f6c5d1 100644 --- a/src/python/m5/objects/__init__.py +++ b/src/python/m5/objects/__init__.py @@ -26,6 +26,9 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + from m5.internal import params from m5.SimObject import * diff --git a/src/python/m5/options.py b/src/python/m5/options.py index 5b291b143..d34abfc41 100644 --- a/src/python/m5/options.py +++ b/src/python/m5/options.py @@ -26,6 +26,9 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + import optparse import sys diff --git a/src/python/m5/params.py b/src/python/m5/params.py index 1470765bb..72cc0b2ad 100644 --- a/src/python/m5/params.py +++ b/src/python/m5/params.py @@ -68,9 +68,9 @@ import sys import time import math -import proxy -import ticks -from util import * +from . import proxy +from . import ticks +from .util import * def isSimObject(*args, **kwargs): return SimObject.isSimObject(*args, **kwargs) @@ -2159,4 +2159,4 @@ __all__ = ['Param', 'VectorParam', 'MasterPort', 'SlavePort', 'VectorMasterPort', 'VectorSlavePort'] -import SimObject +from . import SimObject diff --git a/src/python/m5/proxy.py b/src/python/m5/proxy.py index 2a3250020..346ed928b 100644 --- a/src/python/m5/proxy.py +++ b/src/python/m5/proxy.py @@ -45,9 +45,12 @@ # ##################################################################### +from __future__ import print_function +from __future__ import absolute_import + import copy -import params +from . import params class BaseProxy(object): def __init__(self, search_self, search_up): diff --git a/src/python/m5/simulate.py b/src/python/m5/simulate.py index d72dee222..6f0232736 100644 --- a/src/python/m5/simulate.py +++ b/src/python/m5/simulate.py @@ -51,14 +51,14 @@ import _m5.drain import _m5.core from _m5.stats import updateEvents as updateStatEvents -import stats -import SimObject -import ticks -import objects +from . import stats +from . import SimObject +from . import ticks +from . import objects from m5.util.dot_writer import do_dot, do_dvfs_dot -from util import fatal -from util import attrdict +from .util import fatal +from .util import attrdict # define a MaxTick parameter, unsigned 64 bit MaxTick = 2**64 - 1 diff --git a/src/python/m5/trace.py b/src/python/m5/trace.py index ca98ebdc4..fe26325f1 100644 --- a/src/python/m5/trace.py +++ b/src/python/m5/trace.py @@ -26,5 +26,8 @@ # # Authors: Nathan Binkert +from __future__ import print_function +from __future__ import absolute_import + # Export native methods to Python from _m5.trace import output, ignore, disable, enable diff --git a/src/python/m5/util/__init__.py b/src/python/m5/util/__init__.py index 341e54fdb..f7493be22 100644 --- a/src/python/m5/util/__init__.py +++ b/src/python/m5/util/__init__.py @@ -45,14 +45,14 @@ import os import re import sys -import convert -import jobfile - -from attrdict import attrdict, multiattrdict, optiondict -from code_formatter import code_formatter -from multidict import multidict -from smartdict import SmartDict -from sorteddict import SortedDict +from . import convert +from . import jobfile + +from .attrdict import attrdict, multiattrdict, optiondict +from .code_formatter import code_formatter +from .multidict import multidict +from .smartdict import SmartDict +from .sorteddict import SortedDict # panic() should be called when something happens that should never # ever happen regardless of what the user does (i.e., an acutal m5 diff --git a/src/python/m5/util/code_formatter.py b/src/python/m5/util/code_formatter.py index 21bbcd7a8..8d48d0f76 100644 --- a/src/python/m5/util/code_formatter.py +++ b/src/python/m5/util/code_formatter.py @@ -279,7 +279,7 @@ class code_formatter(object): __all__ = [ "code_formatter" ] if __name__ == '__main__': - from code_formatter import code_formatter + from .code_formatter import code_formatter f = code_formatter() class Foo(dict): diff --git a/src/python/m5/util/dot_writer.py b/src/python/m5/util/dot_writer.py index f368faacb..730f0ede1 100644 --- a/src/python/m5/util/dot_writer.py +++ b/src/python/m5/util/dot_writer.py @@ -57,6 +57,9 @@ # ##################################################################### +from __future__ import print_function +from __future__ import absolute_import + import m5, os, re from m5.SimObject import isRoot, isSimObjectVector from m5.params import PortRef, isNullPointer diff --git a/src/python/m5/util/jobfile.py b/src/python/m5/util/jobfile.py index 45214a0b5..e19c62e02 100644 --- a/src/python/m5/util/jobfile.py +++ b/src/python/m5/util/jobfile.py @@ -27,6 +27,7 @@ # Authors: Nathan Binkert from __future__ import print_function +from __future__ import absolute_import import sys diff --git a/src/python/m5/util/pybind.py b/src/python/m5/util/pybind.py index f66654786..4b5e03d31 100644 --- a/src/python/m5/util/pybind.py +++ b/src/python/m5/util/pybind.py @@ -35,6 +35,9 @@ # # Authors: Andreas Sandberg +from __future__ import print_function +from __future__ import absolute_import + from abc import * class PyBindExport(object): diff --git a/src/python/m5/util/smartdict.py b/src/python/m5/util/smartdict.py index b8127b149..3cfe3294e 100644 --- a/src/python/m5/util/smartdict.py +++ b/src/python/m5/util/smartdict.py @@ -41,9 +41,11 @@ # rather than a normal value, and (c) coerce values written to the # dict to be strings. +from __future__ import print_function +from __future__ import absolute_import -from convert import * -from attrdict import attrdict +from .convert import * +from .attrdict import attrdict class Variable(str): """Intelligent proxy class for SmartDict. Variable will use the diff --git a/src/python/m5/util/sorteddict.py b/src/python/m5/util/sorteddict.py index 28c9c601d..25d6d3986 100644 --- a/src/python/m5/util/sorteddict.py +++ b/src/python/m5/util/sorteddict.py @@ -25,6 +25,7 @@ # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. from __future__ import print_function +from __future__ import absolute_import from bisect import bisect_left, bisect_right diff --git a/src/python/m5/util/terminal.py b/src/python/m5/util/terminal.py index 00f8b7061..fd4392e95 100644 --- a/src/python/m5/util/terminal.py +++ b/src/python/m5/util/terminal.py @@ -27,6 +27,7 @@ # Author: Steve Reinhardt from __future__ import print_function +from __future__ import absolute_import import sys -- 2.30.2