Skip to content

Commit d4be55c

Browse files
naonusyouknowone
andauthored
Add command line parameter -P (#4611)
* Add command line parameter -P * Modify the value of safe_path to be set --------- Co-authored-by: Jeong YunWon <jeong@youknowone.org>
1 parent e2f7d5b commit d4be55c

File tree

5 files changed

+38
-4
lines changed

5 files changed

+38
-4
lines changed

Lib/test/test_support.py

-2
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,6 @@ def check_options(self, args, func, expected=None):
513513
self.assertEqual(proc.stdout.rstrip(), repr(expected))
514514
self.assertEqual(proc.returncode, 0)
515515

516-
# TODO: RUSTPYTHON
517-
@unittest.expectedFailure
518516
def test_args_from_interpreter_flags(self):
519517
# Test test.support.args_from_interpreter_flags()
520518
for opts in (

extra_tests/snippets/stdlib_sys.py

+22-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import sys
2+
import os
3+
import subprocess
24

35
from testutils import assert_raises
46

@@ -105,4 +107,23 @@ def recursive_call(n):
105107
sys.set_int_max_str_digits(1)
106108

107109
sys.set_int_max_str_digits(1000)
108-
assert sys.get_int_max_str_digits() == 1000
110+
assert sys.get_int_max_str_digits() == 1000
111+
112+
# Test the PYTHONSAFEPATH environment variable
113+
code = "import sys; print(sys.flags.safe_path)"
114+
env = dict(os.environ)
115+
env.pop('PYTHONSAFEPATH', None)
116+
args = (sys.executable, '-P', '-c', code)
117+
118+
proc = subprocess.run(
119+
args, stdout=subprocess.PIPE,
120+
universal_newlines=True, env=env)
121+
assert proc.stdout.rstrip() == 'True', proc
122+
assert proc.returncode == 0, proc
123+
124+
env['PYTHONSAFEPATH'] = '1'
125+
proc = subprocess.run(
126+
args, stdout=subprocess.PIPE,
127+
universal_newlines=True, env=env)
128+
assert proc.stdout.rstrip() == 'True'
129+
assert proc.returncode == 0, proc

src/settings.rs

+11
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
100100
.short("B")
101101
.help("don't write .pyc files on import"),
102102
)
103+
.arg(
104+
Arg::with_name("safe-path")
105+
.short("P")
106+
.help("don’t prepend a potentially unsafe path to sys.path"),
107+
)
103108
.arg(
104109
Arg::with_name("ignore-environment")
105110
.short("E")
@@ -237,6 +242,12 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) {
237242
};
238243
}
239244

245+
if matches.is_present("safe-path")
246+
|| (!ignore_environment && env::var_os("PYTHONSAFEPATH").is_some())
247+
{
248+
settings.safe_path = true;
249+
}
250+
240251
settings.check_hash_based_pycs = matches
241252
.value_of("check-hash-based-pycs")
242253
.unwrap_or("default")

vm/src/stdlib/sys.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -754,7 +754,7 @@ mod sys {
754754
dev_mode: settings.dev_mode,
755755
utf8_mode: settings.utf8_mode,
756756
int_max_str_digits: settings.int_max_str_digits,
757-
safe_path: false,
757+
safe_path: settings.safe_path,
758758
warn_default_encoding: settings.warn_default_encoding as u8,
759759
}
760760
}

vm/src/vm/setting.rs

+4
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ pub struct Settings {
3737
/// -B
3838
pub dont_write_bytecode: bool,
3939

40+
/// -P
41+
pub safe_path: bool,
42+
4043
/// -b
4144
pub bytes_warning: u64,
4245

@@ -108,6 +111,7 @@ impl Default for Settings {
108111
verbose: 0,
109112
quiet: false,
110113
dont_write_bytecode: false,
114+
safe_path: false,
111115
bytes_warning: 0,
112116
xopts: vec![],
113117
isolated: false,

0 commit comments

Comments
 (0)