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
This commit is contained in:
Bobby R. Bruce
2023-09-12 14:51:59 -07:00
parent 1bebf6a3cc
commit 39f0bcd9af

View File

@@ -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")