From f213ee55cdc60a424baabea370587f7cb3af63b0 Mon Sep 17 00:00:00 2001 From: naon Date: Thu, 2 Mar 2023 19:32:03 +0900 Subject: [PATCH 1/5] Add command line parameter -P --- Lib/test/test_support.py | 2 +- src/settings.rs | 11 +++++++++++ vm/src/vm/setting.rs | 4 ++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 8158bee3029..a04097cabba 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -514,7 +514,7 @@ def check_options(self, args, func, expected=None): self.assertEqual(proc.returncode, 0) # TODO: RUSTPYTHON - @unittest.expectedFailure + # @unittest.expectedFailure def test_args_from_interpreter_flags(self): # Test test.support.args_from_interpreter_flags() for opts in ( diff --git a/src/settings.rs b/src/settings.rs index 6a47233353a..494d594de48 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -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") @@ -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") diff --git a/vm/src/vm/setting.rs b/vm/src/vm/setting.rs index 508c50da787..c4d1f85b2a6 100644 --- a/vm/src/vm/setting.rs +++ b/vm/src/vm/setting.rs @@ -37,6 +37,9 @@ pub struct Settings { /// -B pub dont_write_bytecode: bool, + /// -P + pub safe_path: bool, + /// -b pub bytes_warning: u64, @@ -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, From 62d09b438ecd6f39fdf2624df0ebf5b683f34e24 Mon Sep 17 00:00:00 2001 From: naon Date: Thu, 2 Mar 2023 22:58:04 +0900 Subject: [PATCH 2/5] Modify the value of safe_path to be set --- vm/src/stdlib/sys.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index 3a3824b3109..47f9376f17a 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -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, warn_default_encoding: settings.warn_default_encoding as u8, } } From 16218cfae44af4c2c7046354a914024e9286ac7f Mon Sep 17 00:00:00 2001 From: naon Date: Thu, 2 Mar 2023 23:52:13 +0900 Subject: [PATCH 3/5] Add test_pythonsafepath_env in test_cmd_line --- Lib/subprocess.py | 1 + Lib/test/test_cmd_line.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 9cadd1bf8e6..03df5527c48 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -309,6 +309,7 @@ def _args_from_interpreter_flags(): 'verbose': 'v', 'bytes_warning': 'b', 'quiet': 'q', + 'safe_path': 'P' # -O is handled in _optim_args_from_interpreter_flags() } args = _optim_args_from_interpreter_flags() diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 7e15bc8f885..e68541e642b 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -1008,6 +1008,25 @@ def test_sys_flags_not_set(self): PYTHONSAFEPATH="1", ) + # TODO: RUSTPYTHON + @unittest.expectedFailure + def test_pythonsafepath_env(self): + # 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', code) + + proc = subprocess.run(args, stdout=subprocess.PIPE, + universal_newlines=True, env=env) + self.assertEqual(proc.stdout.rstrip(), 'False') + self.assertEqual(proc.returncode, 0, proc) + + env['PYTHONSAFEPATH'] = '1' + proc = subprocess.run(args, stdout=subprocess.PIPE, + universal_newlines=True, env=env) + self.assertEqual(proc.stdout.rstrip(), 'True') + self.assertEqual(proc.returncode, 0, proc) class SyntaxErrorTests(unittest.TestCase): def check_string(self, code): From e170a998318b0694e8c48de7bbad957c38301d92 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 30 Aug 2023 18:07:30 +0900 Subject: [PATCH 4/5] Fix tests --- Lib/test/test_cmd_line.py | 20 -------------------- Lib/test/test_support.py | 2 -- extra_tests/snippets/stdlib_sys.py | 23 ++++++++++++++++++++++- 3 files changed, 22 insertions(+), 23 deletions(-) diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index e68541e642b..97cd362bfb4 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -1008,26 +1008,6 @@ def test_sys_flags_not_set(self): PYTHONSAFEPATH="1", ) - # TODO: RUSTPYTHON - @unittest.expectedFailure - def test_pythonsafepath_env(self): - # 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', code) - - proc = subprocess.run(args, stdout=subprocess.PIPE, - universal_newlines=True, env=env) - self.assertEqual(proc.stdout.rstrip(), 'False') - self.assertEqual(proc.returncode, 0, proc) - - env['PYTHONSAFEPATH'] = '1' - proc = subprocess.run(args, stdout=subprocess.PIPE, - universal_newlines=True, env=env) - self.assertEqual(proc.stdout.rstrip(), 'True') - self.assertEqual(proc.returncode, 0, proc) - class SyntaxErrorTests(unittest.TestCase): def check_string(self, code): proc = subprocess.run([sys.executable, "-"], input=code, diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index a04097cabba..6ad272697b0 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -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 ( diff --git a/extra_tests/snippets/stdlib_sys.py b/extra_tests/snippets/stdlib_sys.py index cb92fc64164..5d8859ac8e8 100644 --- a/extra_tests/snippets/stdlib_sys.py +++ b/extra_tests/snippets/stdlib_sys.py @@ -1,4 +1,6 @@ import sys +import os +import subprocess from testutils import assert_raises @@ -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 \ No newline at end of file +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 From 3ff1ab57993180acb310c6432a069adb766906a7 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Wed, 30 Aug 2023 18:27:51 +0900 Subject: [PATCH 5/5] x --- Lib/subprocess.py | 1 - Lib/test/test_cmd_line.py | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/subprocess.py b/Lib/subprocess.py index 03df5527c48..9cadd1bf8e6 100644 --- a/Lib/subprocess.py +++ b/Lib/subprocess.py @@ -309,7 +309,6 @@ def _args_from_interpreter_flags(): 'verbose': 'v', 'bytes_warning': 'b', 'quiet': 'q', - 'safe_path': 'P' # -O is handled in _optim_args_from_interpreter_flags() } args = _optim_args_from_interpreter_flags() diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 97cd362bfb4..7e15bc8f885 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -1008,6 +1008,7 @@ def test_sys_flags_not_set(self): PYTHONSAFEPATH="1", ) + class SyntaxErrorTests(unittest.TestCase): def check_string(self, code): proc = subprocess.run([sys.executable, "-"], input=code,