Align Settings to PyConfig

This commit is contained in:
Jeong YunWon
2024-04-29 23:29:34 +09:00
parent cdfcd8a4c9
commit 24f57dde2f
7 changed files with 105 additions and 78 deletions

View File

@@ -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.

View File

@@ -103,8 +103,8 @@ fn cpy_compile_code<'a>(
fn bench_rustpy_code(group: &mut BenchmarkGroup<WallTime>, 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() {

View File

@@ -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") {

View File

@@ -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]

View File

@@ -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");
}
}

View File

@@ -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<PyObjectRef> {
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,

View File

@@ -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<u32>,
// 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<String>,
/// -Xfoo[=bar]
pub xoptions: Vec<(String, Option<String>)>,
/// -Wfoo
pub warnoptions: Vec<String>,
/// -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<String>)>,
/// -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<String>,
/// Environment PYTHONPATH and RUSTPYTHONPATH:
// /* --- Path configuration inputs ------------ */
// int pathconfig_warnings;
// wchar_t *program_name;
/// Environment PYTHONPATH (and RUSTPYTHONPATH)
pub path_list: Vec<String>,
/// sys.argv
pub argv: Vec<String>,
// wchar_t *home;
// wchar_t *platlibdir;
/// -d command line switch
pub debug: bool,
/// PYTHONHASHSEED=x
pub hash_seed: Option<u32>,
/// -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<OsString>,
#[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,