Skip to main content

rustpython_vm/vm/
setting.rs

1#[cfg(feature = "flame-it")]
2use std::ffi::OsString;
3
4/// Path configuration computed at runtime (like PyConfig path outputs)
5#[derive(Debug, Clone, Default)]
6pub struct Paths {
7    /// sys.executable
8    pub executable: String,
9    /// sys._base_executable (original interpreter in venv)
10    pub base_executable: String,
11    /// sys.prefix
12    pub prefix: String,
13    /// sys.base_prefix
14    pub base_prefix: String,
15    /// sys.exec_prefix
16    pub exec_prefix: String,
17    /// sys.base_exec_prefix
18    pub base_exec_prefix: String,
19    /// sys._stdlib_dir
20    pub stdlib_dir: Option<String>,
21    /// Computed module_search_paths (complete sys.path)
22    pub module_search_paths: Vec<String>,
23}
24
25/// Combined configuration: user settings + computed paths
26/// CPython directly exposes every fields under both of them.
27/// We separate them to maintain better ownership discipline.
28pub struct PyConfig {
29    pub settings: Settings,
30    pub paths: Paths,
31}
32
33impl PyConfig {
34    pub fn new(settings: Settings, paths: Paths) -> Self {
35        Self { settings, paths }
36    }
37}
38
39/// User-configurable settings for the python vm.
40#[non_exhaustive]
41pub struct Settings {
42    /// -I
43    pub isolated: bool,
44
45    // int use_environment
46    /// -Xdev
47    pub dev_mode: bool,
48
49    /// Not set SIGINT handler(i.e. for embedded mode)
50    pub install_signal_handlers: bool,
51
52    /// PYTHONHASHSEED=x
53    /// None means use_hash_seed = 0 in CPython
54    pub hash_seed: Option<u32>,
55
56    /// -X faulthandler, PYTHONFAULTHANDLER
57    pub faulthandler: bool,
58
59    // int tracemalloc;
60    // int perf_profiling;
61    // int import_time;
62    /// -X no_debug_ranges: disable column info in bytecode
63    pub code_debug_ranges: bool,
64    // int show_ref_count;
65    // int dump_refs;
66    // wchar_t *dump_refs_file;
67    // int malloc_stats;
68    // wchar_t *filesystem_encoding;
69    // wchar_t *filesystem_errors;
70    // wchar_t *pycache_prefix;
71    // int parse_argv;
72    // PyWideStringList orig_argv;
73    /// sys.argv
74    pub argv: Vec<String>,
75
76    // spell-checker:ignore Xfoo
77    /// -Xfoo[=bar]
78    pub xoptions: Vec<(String, Option<String>)>,
79
80    // spell-checker:ignore Wfoo
81    /// -Wfoo
82    pub warnoptions: Vec<String>,
83
84    /// -S
85    pub import_site: bool,
86
87    /// -b
88    pub bytes_warning: u64,
89
90    /// -X warn_default_encoding, PYTHONWARNDEFAULTENCODING
91    pub warn_default_encoding: bool,
92
93    /// -X thread_inherit_context, whether new threads inherit context from parent
94    pub thread_inherit_context: bool,
95
96    /// -X context_aware_warnings, whether warnings are context aware
97    pub context_aware_warnings: bool,
98
99    /// -i
100    pub inspect: bool,
101
102    /// -i, with no script
103    pub interactive: bool,
104
105    // int optimization_level;
106    // int parser_debug;
107    /// -B
108    pub write_bytecode: bool,
109
110    /// verbosity level (-v switch)
111    pub verbose: u8,
112
113    /// -q
114    pub quiet: bool,
115
116    /// -s
117    pub user_site_directory: bool,
118
119    // int configure_c_stdio;
120    /// -u, PYTHONUNBUFFERED=x
121    pub buffered_stdio: bool,
122
123    /// PYTHONIOENCODING - stdio encoding
124    pub stdio_encoding: Option<String>,
125    /// PYTHONIOENCODING - stdio error handler
126    pub stdio_errors: Option<String>,
127    pub utf8_mode: i8,
128    /// --check-hash-based-pycs
129    pub check_hash_pycs_mode: CheckHashPycsMode,
130
131    // int use_frozen_modules;
132    /// -P
133    pub safe_path: bool,
134
135    /// -X int_max_str_digits
136    pub int_max_str_digits: i64,
137
138    // /* --- Path configuration inputs ------------ */
139    // int pathconfig_warnings;
140    // wchar_t *program_name;
141    /// Environment PYTHONPATH (and RUSTPYTHONPATH)
142    pub path_list: Vec<String>,
143
144    // wchar_t *home;
145    // wchar_t *platlibdir;
146    /// -d command line switch
147    pub debug: u8,
148
149    /// -O optimization switch counter
150    pub optimize: u8,
151
152    /// -E
153    pub ignore_environment: bool,
154
155    /// false for wasm. Not a command-line option
156    pub allow_external_library: bool,
157
158    #[cfg(feature = "flame-it")]
159    pub profile_output: Option<OsString>,
160    #[cfg(feature = "flame-it")]
161    pub profile_format: Option<String>,
162}
163
164#[derive(Debug, Default, Copy, Clone, strum_macros::Display, strum_macros::EnumString)]
165#[strum(serialize_all = "lowercase")]
166pub enum CheckHashPycsMode {
167    #[default]
168    Default,
169    Always,
170    Never,
171}
172
173impl Settings {
174    pub fn with_path(mut self, path: String) -> Self {
175        self.path_list.push(path);
176        self
177    }
178}
179
180/// Sensible default settings.
181impl Default for Settings {
182    fn default() -> Self {
183        Self {
184            debug: 0,
185            inspect: false,
186            interactive: false,
187            optimize: 0,
188            install_signal_handlers: true,
189            user_site_directory: true,
190            import_site: true,
191            ignore_environment: false,
192            verbose: 0,
193            quiet: false,
194            write_bytecode: true,
195            safe_path: false,
196            bytes_warning: 0,
197            xoptions: vec![],
198            isolated: false,
199            dev_mode: false,
200            warn_default_encoding: false,
201            thread_inherit_context: false,
202            context_aware_warnings: false,
203            warnoptions: vec![],
204            path_list: vec![],
205            argv: vec![],
206            hash_seed: None,
207            faulthandler: false,
208            code_debug_ranges: true,
209            buffered_stdio: true,
210            check_hash_pycs_mode: CheckHashPycsMode::Default,
211            allow_external_library: cfg!(feature = "importlib"),
212            stdio_encoding: None,
213            stdio_errors: None,
214            utf8_mode: -1,
215            int_max_str_digits: 4300,
216            #[cfg(feature = "flame-it")]
217            profile_output: None,
218            #[cfg(feature = "flame-it")]
219            profile_format: None,
220        }
221    }
222}