From 39f0bcd9aff4851f91d851bd07f2078dc1891857 Mon Sep 17 00:00:00 2001 From: "Bobby R. Bruce" Date: Tue, 12 Sep 2023 14:51:59 -0700 Subject: [PATCH] python: Mimic Python 3's `-P` flag in gem5 Python 3's `-P` flag, when set, means `sys.path` is not prepended with potentially unsafe paths: https://docs.python.org/3/using/cmdline.html#cmdoption-P This patch allows gem5 to mimic this. This is necesssary when using `mypy.stubgen` as it expects the Python Interpreter to have the `-P` flag. Change-Id: I456c8001d3ee1e806190dc37142566d50d54cc90 --- src/python/m5/main.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/python/m5/main.py b/src/python/m5/main.py index e07e9562ab..3c418ba29b 100644 --- a/src/python/m5/main.py +++ b/src/python/m5/main.py @@ -193,6 +193,14 @@ def parse_options(): callback=collect_args, ) + option( + "-P", + action="store_true", + default=False, + help="Don't prepend the script directory to the system path. " + "Mimics Python 3's `-P` option.", + ) + option( "-s", action="store_true", @@ -601,7 +609,11 @@ def main(): sys.argv = ["-c"] + options.c[1] scope = {"__name__": "__m5_main__"} else: - sys.path = [os.path.dirname(sys.argv[0])] + sys.path + # If `-P` was used (`options.P == true`), don't prepend the script + # directory to the `sys.path`. This mimics Python 3's `-P` option + # (https://docs.python.org/3/using/cmdline.html#cmdoption-P). + if not options.P: + sys.path = [os.path.dirname(sys.argv[0])] + sys.path filename = sys.argv[0] filedata = open(filename, "r").read() filecode = compile(filedata, filename, "exec")