rustpython_vm/vm/
setting.rs

1#[cfg(feature = "flame-it")]
2use std::ffi::OsString;
3
4/// Struct containing all kind of settings for the python vm.
5/// Mostly `PyConfig` in CPython.
6#[non_exhaustive]
7pub struct Settings {
8    /// -I
9    pub isolated: bool,
10
11    // int use_environment
12    /// -Xdev
13    pub dev_mode: bool,
14
15    /// Not set SIGINT handler(i.e. for embedded mode)
16    pub install_signal_handlers: bool,
17
18    /// PYTHONHASHSEED=x
19    /// None means use_hash_seed = 0 in CPython
20    pub hash_seed: Option<u32>,
21
22    // int faulthandler;
23    // int tracemalloc;
24    // int perf_profiling;
25    // int import_time;
26    // int code_debug_ranges;
27    // int show_ref_count;
28    // int dump_refs;
29    // wchar_t *dump_refs_file;
30    // int malloc_stats;
31    // wchar_t *filesystem_encoding;
32    // wchar_t *filesystem_errors;
33    // wchar_t *pycache_prefix;
34    // int parse_argv;
35    // PyWideStringList orig_argv;
36    /// sys.argv
37    pub argv: Vec<String>,
38
39    /// -Xfoo[=bar]
40    pub xoptions: Vec<(String, Option<String>)>,
41
42    /// -Wfoo
43    pub warnoptions: Vec<String>,
44
45    /// -S
46    pub import_site: bool,
47
48    /// -b
49    pub bytes_warning: u64,
50
51    /// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING
52    pub warn_default_encoding: bool,
53
54    /// -i
55    pub inspect: bool,
56
57    /// -i, with no script
58    pub interactive: bool,
59
60    // int optimization_level;
61    // int parser_debug;
62    /// -B
63    pub write_bytecode: bool,
64
65    /// verbosity level (-v switch)
66    pub verbose: u8,
67
68    /// -q
69    pub quiet: bool,
70
71    /// -s
72    pub user_site_directory: bool,
73
74    // int configure_c_stdio;
75    /// -u, PYTHONUNBUFFERED=x
76    // TODO: use this; can TextIOWrapper even work with a non-buffered?
77    pub buffered_stdio: bool,
78
79    // wchar_t *stdio_encoding;
80    pub utf8_mode: u8,
81    // wchar_t *stdio_errors;
82    /// --check-hash-based-pycs
83    pub check_hash_pycs_mode: String,
84
85    // int use_frozen_modules;
86    /// -P
87    pub safe_path: bool,
88
89    /// -X int_max_str_digits
90    pub int_max_str_digits: i64,
91
92    // /* --- Path configuration inputs ------------ */
93    // int pathconfig_warnings;
94    // wchar_t *program_name;
95    /// Environment PYTHONPATH (and RUSTPYTHONPATH)
96    pub path_list: Vec<String>,
97
98    // wchar_t *home;
99    // wchar_t *platlibdir;
100    /// -d command line switch
101    pub debug: bool,
102
103    /// -O optimization switch counter
104    pub optimize: u8,
105
106    /// -E
107    pub ignore_environment: bool,
108
109    /// false for wasm. Not a command-line option
110    pub allow_external_library: bool,
111
112    #[cfg(feature = "flame-it")]
113    pub profile_output: Option<OsString>,
114    #[cfg(feature = "flame-it")]
115    pub profile_format: Option<String>,
116}
117
118impl Settings {
119    pub fn with_path(mut self, path: String) -> Self {
120        self.path_list.push(path);
121        self
122    }
123}
124
125/// Sensible default settings.
126impl Default for Settings {
127    fn default() -> Self {
128        Settings {
129            debug: false,
130            inspect: false,
131            interactive: false,
132            optimize: 0,
133            install_signal_handlers: true,
134            user_site_directory: true,
135            import_site: true,
136            ignore_environment: false,
137            verbose: 0,
138            quiet: false,
139            write_bytecode: true,
140            safe_path: false,
141            bytes_warning: 0,
142            xoptions: vec![],
143            isolated: false,
144            dev_mode: false,
145            warn_default_encoding: false,
146            warnoptions: vec![],
147            path_list: vec![],
148            argv: vec![],
149            hash_seed: None,
150            buffered_stdio: true,
151            check_hash_pycs_mode: "default".to_owned(),
152            allow_external_library: cfg!(feature = "importlib"),
153            utf8_mode: 1,
154            int_max_str_digits: 4300,
155            #[cfg(feature = "flame-it")]
156            profile_output: None,
157            #[cfg(feature = "flame-it")]
158            profile_format: None,
159        }
160    }
161}