Skip to content

Add command line parameter -P #4611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Lib/test/test_support.py
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,6 @@ def check_options(self, args, func, expected=None):
self.assertEqual(proc.stdout.rstrip(), repr(expected))
self.assertEqual(proc.returncode, 0)

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_args_from_interpreter_flags(self):
# Test test.support.args_from_interpreter_flags()
for opts in (
Expand Down
23 changes: 22 additions & 1 deletion extra_tests/snippets/stdlib_sys.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import sys
import os
import subprocess

from testutils import assert_raises

Expand Down Expand Up @@ -105,4 +107,23 @@ def recursive_call(n):
sys.set_int_max_str_digits(1)

sys.set_int_max_str_digits(1000)
assert sys.get_int_max_str_digits() == 1000
assert sys.get_int_max_str_digits() == 1000

# Test the PYTHONSAFEPATH environment variable
code = "import sys; print(sys.flags.safe_path)"
env = dict(os.environ)
env.pop('PYTHONSAFEPATH', None)
args = (sys.executable, '-P', '-c', code)

proc = subprocess.run(
args, stdout=subprocess.PIPE,
universal_newlines=True, env=env)
assert proc.stdout.rstrip() == 'True', proc
assert proc.returncode == 0, proc

env['PYTHONSAFEPATH'] = '1'
proc = subprocess.run(
args, stdout=subprocess.PIPE,
universal_newlines=True, env=env)
assert proc.stdout.rstrip() == 'True'
assert proc.returncode == 0, proc
11 changes: 11 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ fn parse_arguments<'a>(app: App<'a, '_>) -> ArgMatches<'a> {
.short("B")
.help("don't write .pyc files on import"),
)
.arg(
Arg::with_name("safe-path")
.short("P")
.help("don’t prepend a potentially unsafe path to sys.path"),
)
.arg(
Arg::with_name("ignore-environment")
.short("E")
Expand Down Expand Up @@ -237,6 +242,12 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) {
};
}

if matches.is_present("safe-path")
|| (!ignore_environment && env::var_os("PYTHONSAFEPATH").is_some())
{
settings.safe_path = true;
}

settings.check_hash_based_pycs = matches
.value_of("check-hash-based-pycs")
.unwrap_or("default")
Expand Down
2 changes: 1 addition & 1 deletion vm/src/stdlib/sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -754,7 +754,7 @@ mod sys {
dev_mode: settings.dev_mode,
utf8_mode: settings.utf8_mode,
int_max_str_digits: settings.int_max_str_digits,
safe_path: false,
safe_path: settings.safe_path,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modified safe_path to be set from parameter

warn_default_encoding: settings.warn_default_encoding as u8,
}
}
Expand Down
4 changes: 4 additions & 0 deletions vm/src/vm/setting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ pub struct Settings {
/// -B
pub dont_write_bytecode: bool,

/// -P
pub safe_path: bool,

/// -b
pub bytes_warning: u64,

Expand Down Expand Up @@ -108,6 +111,7 @@ impl Default for Settings {
verbose: 0,
quiet: false,
dont_write_bytecode: false,
safe_path: false,
bytes_warning: 0,
xopts: vec![],
isolated: false,
Expand Down