From 24f57dde2f7b32ea95254bcb871ef0981c969bf3 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Mon, 29 Apr 2024 23:29:34 +0900 Subject: [PATCH] Align Settings to PyConfig --- benches/execution.rs | 4 +- benches/microbenchmarks.rs | 4 +- src/settings.rs | 18 ++--- vm/src/stdlib/imp.rs | 2 +- vm/src/stdlib/signal.rs | 2 +- vm/src/stdlib/sys.rs | 12 ++-- vm/src/vm/setting.rs | 141 ++++++++++++++++++++++--------------- 7 files changed, 105 insertions(+), 78 deletions(-) diff --git a/benches/execution.rs b/benches/execution.rs index de81cd080..8f09aca80 100644 --- a/benches/execution.rs +++ b/benches/execution.rs @@ -24,8 +24,8 @@ fn bench_rustpy_code(b: &mut Bencher, name: &str, source: &str) { // NOTE: Take long time. let mut settings = Settings::default(); settings.path_list.push("Lib/".to_string()); - settings.dont_write_bytecode = true; - settings.no_user_site = true; + settings.write_bytecode = false; + settings.user_site_directory = false; Interpreter::without_stdlib(settings).enter(|vm| { // Note: bench_cpython is both compiling and executing the code. // As such we compile the code in the benchmark loop as well. diff --git a/benches/microbenchmarks.rs b/benches/microbenchmarks.rs index befdc63fd..8575ae50a 100644 --- a/benches/microbenchmarks.rs +++ b/benches/microbenchmarks.rs @@ -103,8 +103,8 @@ fn cpy_compile_code<'a>( fn bench_rustpy_code(group: &mut BenchmarkGroup, bench: &MicroBenchmark) { let mut settings = Settings::default(); settings.path_list.push("Lib/".to_string()); - settings.dont_write_bytecode = true; - settings.no_user_site = true; + settings.write_bytecode = false; + settings.user_site_directory = false; Interpreter::with_init(settings, |vm| { for (name, init) in rustpython_stdlib::get_module_inits() { diff --git a/src/settings.rs b/src/settings.rs index d5132a06d..72c5f0680 100644 --- a/src/settings.rs +++ b/src/settings.rs @@ -179,7 +179,7 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { && !matches.is_present("m") && (!matches.is_present("script") || matches.is_present("inspect")); settings.bytes_warning = matches.occurrences_of("bytes-warning"); - settings.no_site = matches.is_present("no-site"); + settings.import_site = !matches.is_present("no-site"); let ignore_environment = settings.ignore_environment || settings.isolated; @@ -220,7 +220,7 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { || matches.is_present("isolate") || (!ignore_environment && env::var_os("PYTHONNOUSERSITE").is_some()) { - settings.no_user_site = true; + settings.user_site_directory = false; } if matches.is_present("quiet") { @@ -230,7 +230,7 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { if matches.is_present("dont-write-bytecode") || (!ignore_environment && env::var_os("PYTHONDONTWRITEBYTECODE").is_some()) { - settings.dont_write_bytecode = true; + settings.write_bytecode = false; } if !ignore_environment && env::var_os("PYTHONINTMAXSTRDIGITS").is_some() { settings.int_max_str_digits = match env::var("PYTHONINTMAXSTRDIGITS").unwrap().parse() { @@ -251,12 +251,12 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { matches .value_of("check-hash-based-pycs") .unwrap_or("default") - .clone_into(&mut settings.check_hash_based_pycs); + .clone_into(&mut settings.check_hash_pycs_mode); let mut dev_mode = false; let mut warn_default_encoding = false; if let Some(xopts) = matches.values_of("implementation-option") { - settings.xopts.extend(xopts.map(|s| { + settings.xoptions.extend(xopts.map(|s| { let mut parts = s.splitn(2, '='); let name = parts.next().unwrap().to_owned(); let value = parts.next().map(ToOwned::to_owned); @@ -267,7 +267,7 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { warn_default_encoding = true } if name == "no_sig_int" { - settings.no_sig_int = true; + settings.install_signal_handlers = false; } if name == "int_max_str_digits" { settings.int_max_str_digits = match value.as_ref().unwrap().parse() { @@ -290,7 +290,7 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { } if dev_mode { - settings.warnopts.push("default".to_owned()) + settings.warnoptions.push("default".to_owned()) } if settings.bytes_warning > 0 { let warn = if settings.bytes_warning > 1 { @@ -298,10 +298,10 @@ fn settings_from(matches: &ArgMatches) -> (Settings, RunMode) { } else { "default::BytesWarning" }; - settings.warnopts.push(warn.to_owned()); + settings.warnoptions.push(warn.to_owned()); } if let Some(warnings) = matches.values_of("warning-control") { - settings.warnopts.extend(warnings.map(ToOwned::to_owned)); + settings.warnoptions.extend(warnings.map(ToOwned::to_owned)); } let (mode, argv) = if let Some(mut cmd) = matches.values_of("c") { diff --git a/vm/src/stdlib/imp.rs b/vm/src/stdlib/imp.rs index f59f8e4e6..ab711bccc 100644 --- a/vm/src/stdlib/imp.rs +++ b/vm/src/stdlib/imp.rs @@ -88,7 +88,7 @@ mod _imp { #[pyattr] fn check_hash_based_pycs(vm: &VirtualMachine) -> PyStrRef { vm.ctx - .new_str(vm.state.settings.check_hash_based_pycs.clone()) + .new_str(vm.state.settings.check_hash_pycs_mode.clone()) } #[pyfunction] diff --git a/vm/src/stdlib/signal.rs b/vm/src/stdlib/signal.rs index 2f421ae07..289321222 100644 --- a/vm/src/stdlib/signal.rs +++ b/vm/src/stdlib/signal.rs @@ -113,7 +113,7 @@ pub(crate) mod _signal { let int_handler = module .get_attr("default_int_handler", vm) .expect("_signal does not have this attr?"); - if !vm.state.settings.no_sig_int { + if vm.state.settings.install_signal_handlers { signal(libc::SIGINT, int_handler, vm).expect("Failed to set sigint handler"); } } diff --git a/vm/src/stdlib/sys.rs b/vm/src/stdlib/sys.rs index f6d7a122a..55e9c12b7 100644 --- a/vm/src/stdlib/sys.rs +++ b/vm/src/stdlib/sys.rs @@ -162,7 +162,7 @@ mod sys { #[pyattr] fn dont_write_bytecode(vm: &VirtualMachine) -> bool { - vm.state.settings.dont_write_bytecode + !vm.state.settings.write_bytecode } #[pyattr] @@ -278,7 +278,7 @@ mod sys { fn _xoptions(vm: &VirtualMachine) -> PyDictRef { let ctx = &vm.ctx; let xopts = ctx.new_dict(); - for (key, value) in &vm.state.settings.xopts { + for (key, value) in &vm.state.settings.xoptions { let value = value.as_ref().map_or_else( || ctx.new_bool(true).into(), |s| ctx.new_str(s.clone()).into(), @@ -292,7 +292,7 @@ mod sys { fn warnoptions(vm: &VirtualMachine) -> Vec { vm.state .settings - .warnopts + .warnoptions .iter() .map(|s| vm.ctx.new_str(s.clone()).into()) .collect() @@ -744,9 +744,9 @@ mod sys { inspect: settings.inspect as u8, interactive: settings.interactive as u8, optimize: settings.optimize, - dont_write_bytecode: settings.dont_write_bytecode as u8, - no_user_site: settings.no_user_site as u8, - no_site: settings.no_site as u8, + dont_write_bytecode: (!settings.write_bytecode) as u8, + no_user_site: (!settings.user_site_directory) as u8, + no_site: (!settings.import_site) as u8, ignore_environment: settings.ignore_environment as u8, verbose: settings.verbose, bytes_warning: settings.bytes_warning, diff --git a/vm/src/vm/setting.rs b/vm/src/vm/setting.rs index a30c75560..62360aace 100644 --- a/vm/src/vm/setting.rs +++ b/vm/src/vm/setting.rs @@ -2,10 +2,54 @@ use std::ffi::OsString; /// Struct containing all kind of settings for the python vm. +/// Mostly `PyConfig` in CPython. #[non_exhaustive] pub struct Settings { - /// -d command line switch - pub debug: bool, + /// -I + pub isolated: bool, + + // int use_environment + /// -Xdev + pub dev_mode: bool, + + /// Not set SIGINT handler(i.e. for embedded mode) + pub install_signal_handlers: bool, + + /// PYTHONHASHSEED=x + /// None means use_hash_seed = 0 in CPython + pub hash_seed: Option, + + // int faulthandler; + // int tracemalloc; + // int perf_profiling; + // int import_time; + // int code_debug_ranges; + // int show_ref_count; + // int dump_refs; + // wchar_t *dump_refs_file; + // int malloc_stats; + // wchar_t *filesystem_encoding; + // wchar_t *filesystem_errors; + // wchar_t *pycache_prefix; + // int parse_argv; + // PyWideStringList orig_argv; + /// sys.argv + pub argv: Vec, + + /// -Xfoo[=bar] + pub xoptions: Vec<(String, Option)>, + + /// -Wfoo + pub warnoptions: Vec, + + /// -S + pub import_site: bool, + + /// -b + pub bytes_warning: u64, + + /// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING + pub warn_default_encoding: bool, /// -i pub inspect: bool, @@ -13,20 +57,10 @@ pub struct Settings { /// -i, with no script pub interactive: bool, - /// -O optimization switch counter - pub optimize: u8, - - /// Not set SIGINT handler(i.e. for embedded mode) - pub no_sig_int: bool, - - /// -s - pub no_user_site: bool, - - /// -S - pub no_site: bool, - - /// -E - pub ignore_environment: bool, + // int optimization_level; + // int parser_debug; + /// -B + pub write_bytecode: bool, /// verbosity level (-v switch) pub verbose: u8, @@ -34,54 +68,47 @@ pub struct Settings { /// -q pub quiet: bool, - /// -B - pub dont_write_bytecode: bool, + /// -s + pub user_site_directory: bool, + // int configure_c_stdio; + /// -u, PYTHONUNBUFFERED=x + // TODO: use this; can TextIOWrapper even work with a non-buffered? + pub buffered_stdio: bool, + + // wchar_t *stdio_encoding; + pub utf8_mode: u8, + // wchar_t *stdio_errors; + /// --check-hash-based-pycs + pub check_hash_pycs_mode: String, + + // int use_frozen_modules; /// -P pub safe_path: bool, - /// -b - pub bytes_warning: u64, - - /// -Xfoo[=bar] - pub xopts: Vec<(String, Option)>, - /// -X int_max_str_digits pub int_max_str_digits: i64, - /// -I - pub isolated: bool, - - /// -Xdev - pub dev_mode: bool, - - /// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING - pub warn_default_encoding: bool, - - /// -Wfoo - pub warnopts: Vec, - - /// Environment PYTHONPATH and RUSTPYTHONPATH: + // /* --- Path configuration inputs ------------ */ + // int pathconfig_warnings; + // wchar_t *program_name; + /// Environment PYTHONPATH (and RUSTPYTHONPATH) pub path_list: Vec, - /// sys.argv - pub argv: Vec, + // wchar_t *home; + // wchar_t *platlibdir; + /// -d command line switch + pub debug: bool, - /// PYTHONHASHSEED=x - pub hash_seed: Option, + /// -O optimization switch counter + pub optimize: u8, - /// -u, PYTHONUNBUFFERED=x - // TODO: use this; can TextIOWrapper even work with a non-buffered? - pub stdio_unbuffered: bool, - - /// --check-hash-based-pycs - pub check_hash_based_pycs: String, + /// -E + pub ignore_environment: bool, /// false for wasm. Not a command-line option pub allow_external_library: bool, - pub utf8_mode: u8, - #[cfg(feature = "flame-it")] pub profile_output: Option, #[cfg(feature = "flame-it")] @@ -103,25 +130,25 @@ impl Default for Settings { inspect: false, interactive: false, optimize: 0, - no_sig_int: false, - no_user_site: false, - no_site: false, + install_signal_handlers: true, + user_site_directory: true, + import_site: true, ignore_environment: false, verbose: 0, quiet: false, - dont_write_bytecode: false, + write_bytecode: true, safe_path: false, bytes_warning: 0, - xopts: vec![], + xoptions: vec![], isolated: false, dev_mode: false, warn_default_encoding: false, - warnopts: vec![], + warnoptions: vec![], path_list: vec![], argv: vec![], hash_seed: None, - stdio_unbuffered: false, - check_hash_based_pycs: "default".to_owned(), + buffered_stdio: true, + check_hash_pycs_mode: "default".to_owned(), allow_external_library: cfg!(feature = "importlib"), utf8_mode: 1, int_max_str_digits: -1,