Skip to content

Commit fc7ef52

Browse files
committed
Use -P to enable safe path semantics instead of PYTHONSAFEPATH
1 parent 905af69 commit fc7ef52

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

python/private/python_bootstrap_template.txt

+19-17
Original file line numberDiff line numberDiff line change
@@ -376,12 +376,17 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
376376
shutil.rmtree(os.path.dirname(module_space), True)
377377
sys.exit(ret_code)
378378

379+
def SupportsSafePath():
380+
return sys.version_info[0] >= 3 and sys.version_info[1] >= 11
381+
379382
def _RunExecv(python_program, main_filename, args, env):
380383
# type: (str, str, list[str], dict[str, str]) -> ...
381384
"""Executes the given Python file using the various environment settings."""
382385
os.environ.update(env)
383386
PrintVerbose("RunExecv: environ:", os.environ)
384387
argv = [python_program, main_filename] + args
388+
if SupportsSafePath():
389+
argv.insert(1, "-P")
385390
PrintVerbose("RunExecv: argv:", python_program, argv)
386391
os.execv(python_program, argv)
387392

@@ -410,25 +415,26 @@ relative_files = True
410415
PrintVerboseCoverage('Coverage entrypoint:', coverage_entrypoint)
411416
# First run the target Python file via coveragepy to create a .coverage
412417
# database file, from which we can later export lcov.
413-
ret_code = subprocess.call(
414-
[
415-
python_program,
416-
coverage_entrypoint,
417-
"run",
418-
"--rcfile=" + rcfile_name,
419-
"--append",
420-
"--branch",
421-
main_filename
422-
] + args,
423-
env=env,
424-
cwd=workspace
425-
)
418+
argv = [
419+
python_program,
420+
coverage_entrypoint,
421+
"run",
422+
"--rcfile=" + rcfile_name,
423+
"--append",
424+
"--branch",
425+
main_filename
426+
] + args
427+
if SupportsSafePath():
428+
argv.insert(1, "-P")
429+
ret_code = subprocess.call(argv, env=env, cwd=workspace)
426430
output_filename = os.path.join(os.environ['COVERAGE_DIR'], 'pylcov.dat')
427431

428432
PrintVerboseCoverage('Converting coveragepy database to lcov:', output_filename)
429433
# Run coveragepy again to convert its .coverage database file into lcov.
430434
# Under normal conditions running lcov outputs to stdout/stderr, which causes problems for `coverage`.
431435
params = [python_program, coverage_entrypoint, "lcov", "--rcfile=" + rcfile_name, "-o", output_filename, "--quiet"]
436+
if SupportsSafePath():
437+
params.insert(1, "-P")
432438
kparams = {"env": env, "cwd": workspace, "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
433439
if IsVerboseCoverage():
434440
# reconnect stdout/stderr to lcov generation. Should be useful for debugging `coverage` issues.
@@ -495,10 +501,6 @@ def Main():
495501
if runfiles_envkey:
496502
new_env[runfiles_envkey] = runfiles_envvalue
497503

498-
# Don't prepend a potentially unsafe path to sys.path
499-
# See: https://docs.python.org/3.11/using/cmdline.html#envvar-PYTHONSAFEPATH
500-
new_env['PYTHONSAFEPATH'] = '1'
501-
502504
main_filename = os.path.join(module_space, main_rel_path)
503505
main_filename = GetWindowsPathWithUNCPrefix(main_filename)
504506
assert os.path.exists(main_filename), \

0 commit comments

Comments
 (0)