@@ -376,12 +376,17 @@ def ExecuteFile(python_program, main_filename, args, env, module_space,
376
376
shutil.rmtree(os.path.dirname(module_space), True)
377
377
sys.exit(ret_code)
378
378
379
+ def SupportsSafePath():
380
+ return sys.version_info[0] >= 3 and sys.version_info[1] >= 11
381
+
379
382
def _RunExecv(python_program, main_filename, args, env):
380
383
# type: (str, str, list[str], dict[str, str]) -> ...
381
384
"""Executes the given Python file using the various environment settings."""
382
385
os.environ.update(env)
383
386
PrintVerbose("RunExecv: environ:", os.environ)
384
387
argv = [python_program, main_filename] + args
388
+ if SupportsSafePath():
389
+ argv.insert(1, "-P")
385
390
PrintVerbose("RunExecv: argv:", python_program, argv)
386
391
os.execv(python_program, argv)
387
392
@@ -410,25 +415,26 @@ relative_files = True
410
415
PrintVerboseCoverage('Coverage entrypoint:', coverage_entrypoint)
411
416
# First run the target Python file via coveragepy to create a .coverage
412
417
# 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)
426
430
output_filename = os.path.join(os.environ['COVERAGE_DIR'], 'pylcov.dat')
427
431
428
432
PrintVerboseCoverage('Converting coveragepy database to lcov:', output_filename)
429
433
# Run coveragepy again to convert its .coverage database file into lcov.
430
434
# Under normal conditions running lcov outputs to stdout/stderr, which causes problems for `coverage`.
431
435
params = [python_program, coverage_entrypoint, "lcov", "--rcfile=" + rcfile_name, "-o", output_filename, "--quiet"]
436
+ if SupportsSafePath():
437
+ params.insert(1, "-P")
432
438
kparams = {"env": env, "cwd": workspace, "stdout": subprocess.DEVNULL, "stderr": subprocess.DEVNULL}
433
439
if IsVerboseCoverage():
434
440
# reconnect stdout/stderr to lcov generation. Should be useful for debugging `coverage` issues.
@@ -495,10 +501,6 @@ def Main():
495
501
if runfiles_envkey:
496
502
new_env[runfiles_envkey] = runfiles_envvalue
497
503
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
-
502
504
main_filename = os.path.join(module_space, main_rel_path)
503
505
main_filename = GetWindowsPathWithUNCPrefix(main_filename)
504
506
assert os.path.exists(main_filename), \
0 commit comments