-
-
Notifications
You must be signed in to change notification settings - Fork 257
Expand file tree
/
Copy pathconfig.rs
More file actions
708 lines (587 loc) · 25.3 KB
/
Copy pathconfig.rs
File metadata and controls
708 lines (587 loc) · 25.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
//! Data structures for configuring a Python interpreter.
use {
libc::c_ulong,
python3_sys as pyffi,
std::ffi::{CString, OsString},
std::path::PathBuf,
};
/// Defines Python code to run.
#[derive(Clone, Debug, PartialEq)]
pub enum PythonRunMode {
/// No-op.
None,
/// Run a Python REPL.
Repl,
/// Run a Python module as the main module.
Module { module: String },
/// Evaluate Python code from a string.
Eval { code: String },
/// Execute Python code in a file.
///
/// We define this as a CString because the underlying API wants
/// a char* and we want the constructor of this type to worry about
/// the type coercion.
File { path: PathBuf },
}
/// Defines `terminfo`` database resolution semantics.
#[derive(Clone, Debug)]
pub enum TerminfoResolution {
/// Resolve `terminfo` database using appropriate behavior for current OS.
Dynamic,
/// Do not attempt to resolve the `terminfo` database. Basically a no-op.
None,
/// Use a specified string as the `TERMINFO_DIRS` value.
Static(String),
}
/// Defines an extra extension module to load.
#[derive(Clone, Debug)]
pub struct ExtensionModule {
/// Name of the extension module.
pub name: CString,
/// Extension module initialization function.
pub init_func: unsafe extern "C" fn() -> *mut pyffi::PyObject,
}
/// Holds the configuration of an embedded Python interpreter.
///
/// Instances of this struct can be used to construct Python interpreters.
///
/// Each instance contains the total state to define the run-time behavior of
/// a Python interpreter.
#[derive(Clone, Debug)]
pub struct PythonConfig<'a> {
/// Name of encoding for stdio handles.
pub standard_io_encoding: Option<String>,
/// Name of encoding error mode for stdio handles.
pub standard_io_errors: Option<String>,
/// Python optimization level.
pub opt_level: i32,
/// Whether to load our custom frozen importlib bootstrap modules.
pub use_custom_importlib: bool,
/// Whether to load the filesystem-based sys.meta_path finder.
pub filesystem_importer: bool,
/// Filesystem paths to add to sys.path.
///
/// ``$ORIGIN`` will resolve to the directory of the application at
/// run-time.
pub sys_paths: Vec<String>,
/// Controls whether to detect comparing bytes/bytearray with str.
///
/// If 1, issues a warning. If 2 or greater, raises a BytesWarning
/// exception.
pub bytes_warning: i32,
/// Whether to load the site.py module at initialization time.
pub import_site: bool,
/// Whether to load a user-specific site module at initialization time.
pub import_user_site: bool,
/// Whether to ignore various PYTHON* environment variables.
pub ignore_python_env: bool,
/// Whether to enter interactive mode after executing a script or a command.
pub inspect: bool,
/// Whether to put interpreter in interactive mode.
pub interactive: bool,
/// Whether to enable isolated mode.
pub isolated: bool,
/// If set, set the Windows filesystem encoding to mbcs and the filesystem
/// error handler to replace.
pub legacy_windows_fs_encoding: bool,
/// Whether io.File instead of io.WindowsConsoleIO for sys.stdin, sys.stdout,
/// and sys.stderr.
pub legacy_windows_stdio: bool,
/// Whether to suppress writing of ``.pyc`` files when importing ``.py``
/// files from the filesystem. This is typically irrelevant since modules
/// are imported from memory.
pub write_bytecode: bool,
/// Whether stdout and stderr streams should be unbuffered.
pub unbuffered_stdio: bool,
/// Whether to enable parser debugging output.
pub parser_debug: bool,
/// Whether to enable quiet mode.
pub quiet: bool,
/// Whether to use the PYTHONHASHSEED environment variable to initialize the
/// hash seed.
pub use_hash_seed: bool,
/// Controls the level of the verbose mode for the interpreter.
pub verbose: i32,
/// Reference to packed resources data.
///
/// The referenced data contains Python module data. It likely comes from an
/// `include_bytes!(...)` of a file generated by PyOxidizer.
///
/// The format of the data is defined by the ``python-packed-resources``
/// crate. The data will be parsed as part of initializing the custom
/// meta path importer during interpreter initialization.
pub packed_resources: &'a [u8],
/// Extra extension modules to make available to the interpreter.
///
/// The values will effectively be passed to ``PyImport_ExtendInitTab()``.
pub extra_extension_modules: Vec<ExtensionModule>,
/// Whether to set sys.argvb with bytes versions of process arguments.
///
/// On Windows, bytes will be UTF-16. On POSIX, bytes will be raw char*
/// values passed to `int main()`.
pub argvb: bool,
/// Whether to set sys.frozen=True.
///
/// Setting this will enable Python to emulate "frozen" binaries, such as
/// those used by PyInstaller.
pub sys_frozen: bool,
/// Whether to set sys._MEIPASS to the directory of the executable.
///
/// Setting this will enable Python to emulate PyInstaller's behavior
/// of setting this attribute.
pub sys_meipass: bool,
/// Which memory allocator to use for the raw domain.
pub raw_allocator: PythonRawAllocator,
/// How to resolve the `terminfo` database.
pub terminfo_resolution: TerminfoResolution,
/// Environment variable holding the directory to write a loaded modules file.
///
/// If this value is set and the environment it refers to is set,
/// on interpreter shutdown, we will write a ``modules-<random>`` file to
/// the directory specified containing a ``\n`` delimited list of modules
/// loaded in ``sys.modules``.
pub write_modules_directory_env: Option<String>,
/// Defines what code to run by default.
///
pub run: PythonRunMode,
}
impl<'a> Default for PythonConfig<'a> {
/// Create a new instance using defaults.
#[allow(unused)]
fn default() -> Self {
PythonConfig {
standard_io_encoding: None,
standard_io_errors: None,
opt_level: 0,
use_custom_importlib: false,
filesystem_importer: false,
sys_paths: vec![],
bytes_warning: 0,
import_site: false,
import_user_site: false,
ignore_python_env: true,
inspect: false,
interactive: false,
isolated: false,
legacy_windows_fs_encoding: false,
legacy_windows_stdio: false,
write_bytecode: false,
unbuffered_stdio: false,
parser_debug: false,
quiet: false,
use_hash_seed: false,
verbose: 0,
packed_resources: &[],
extra_extension_modules: vec![],
argvb: false,
sys_frozen: false,
sys_meipass: false,
raw_allocator: PythonRawAllocator::default(),
terminfo_resolution: TerminfoResolution::Dynamic,
write_modules_directory_env: None,
run: PythonRunMode::None,
}
}
}
/// Defines the profile to use to configure a Python interpreter.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum PythonInterpreterProfile {
/// Python is isolated from the system.
///
/// See https://docs.python.org/3/c-api/init_config.html#isolated-configuration.
Isolated,
/// Python interpreter behaves like `python`.
///
/// See https://docs.python.org/3/c-api/init_config.html#python-configuration.
Python,
}
impl Default for PythonInterpreterProfile {
fn default() -> Self {
PythonInterpreterProfile::Isolated
}
}
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.allocator.
#[derive(Clone, Copy, Debug)]
pub enum Allocator {
NotSet = 0,
Default = 1,
Debug = 2,
Malloc = 3,
MallocDebug = 4,
PyMalloc = 5,
PyMallocDebug = 6,
}
/// Holds values for coerce_c_locale.
///
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.coerce_c_locale.
#[derive(Clone, Copy, Debug)]
pub enum CoerceCLocale {
LCCtype = 1,
C = 2,
}
/// Defines what to do when comparing bytes with str.
///
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.bytes_warning.
#[derive(Clone, Copy, Debug)]
pub enum BytesWarning {
None = 0,
Warn = 1,
Raise = 2,
}
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.check_hash_pycs_mode.
#[derive(Clone, Copy, Debug)]
pub enum CheckHashPYCsMode {
Always,
Never,
Default,
}
/// Optimization level for bytecode.
///
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.optimization_level.
#[derive(Clone, Copy, Debug)]
pub enum OptimizationLevel {
Zero = 0,
One = 1,
Two = 2,
}
/// Holds configuration of a Python interpreter.
///
/// This struct holds fields that are exposed by `PyPreConfig` and
/// `PyConfig` in the CPython API.
///
/// Other than the profile (which is used to initialize instances of
/// `PyPreConfig` and `PyConfig`), all fields are optional. Only fields
/// with `Some(T)` will be updated from the defaults.
#[derive(Clone, Debug, Default)]
pub struct PythonInterpreterConfig {
/// Profile to use to initialize pre-config and config state of interpreter.
pub profile: PythonInterpreterProfile,
// The following fields are from PyPreConfig or are shared with PyConfig.
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.allocator.
pub allocator: Option<Allocator>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.configure_locale.
pub configure_locale: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.coerce_c_locale.
pub coerce_c_locale: Option<CoerceCLocale>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.coerce_c_locale_warn.
pub coerce_c_locale_warn: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.dev_mode.
pub development_mode: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.isolated.
pub isolated: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.legacy_windows_fs_encoding.
pub legacy_windows_fs_encoding: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.parse_argv.
pub parse_argv: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.use_environment.
pub use_environment: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyPreConfig.utf8_mode.
pub utf8_mode: Option<bool>,
// The following fields are from PyConfig.
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.argv.
pub argv: Option<Vec<OsString>>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.base_exec_prefix.
pub base_exec_prefix: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.base_executable.
pub base_executable: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.base_prefix.
pub base_prefix: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.buffered_stdio.
pub buffered_stdio: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.bytes_warning.
pub bytes_warning: Option<BytesWarning>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.check_hash_pycs_mode.
pub check_hash_pycs_mode: Option<CheckHashPYCsMode>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.configure_c_stdio.
pub configure_c_stdio: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.dump_refs.
pub dump_refs: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.exec_prefix.
pub exec_prefix: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.executable.
pub executable: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.faulthandler.
pub fault_handler: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.filesystem_encoding.
pub filesystem_encoding: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.filesystem_errors.
pub filesystem_errors: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.hash_seed.
pub hash_seed: Option<c_ulong>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.home.
pub home: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.import_time.
pub import_time: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.inspect.
pub inspect: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.install_signal_handlers.
pub install_signal_handlers: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.interactive.
pub interactive: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.legacy_windows_stdio.
pub legacy_windows_stdio: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.malloc_stats.
pub malloc_stats: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.pythonpath_env.
pub python_path_env: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.module_search_paths.
pub module_search_paths: Option<Vec<PathBuf>>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.optimization_level.
pub optimization_level: Option<OptimizationLevel>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.parser_debug.
pub parser_debug: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.pathconfig_warnings.
pub pathconfig_warnings: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.prefix.
pub prefix: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.program_name.
pub program_name: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.pycache_prefix.
pub pycache_prefix: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.quiet.
pub quiet: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.run_command.
pub run_command: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.run_filename.
pub run_filename: Option<PathBuf>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.run_module.
pub run_module: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.show_alloc_count.
pub show_alloc_count: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.show_ref_count.
pub show_ref_count: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.site_import.
pub site_import: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.skip_source_first_line.
pub skip_first_source_line: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.stdio_encoding.
pub stdio_encoding: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.stdio_errors.
pub stdio_errors: Option<String>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.tracemalloc.
pub tracemalloc: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.user_site_directory.
pub user_site_directory: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.verbose.
pub verbose: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.warnoptions.
pub warn_options: Option<Vec<String>>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.write_bytecode.
pub write_bytecode: Option<bool>,
/// See https://docs.python.org/3/c-api/init_config.html#c.PyConfig.xoptions.
pub x_options: Option<Vec<String>>,
}
/// Defines a backend for a memory allocator.
#[derive(Clone, Copy, Debug)]
pub enum MemoryAllocatorBackend {
/// The default system allocator.
System,
/// Use jemalloc.
Jemalloc,
/// Use Rust's global allocator.
Rust,
}
/// Defines configuration for Python's raw allocator.
///
/// This allocator is what Python uses for all memory allocations.
///
/// See https://docs.python.org/3/c-api/memory.html for more.
#[derive(Clone, Copy, Debug)]
pub struct PythonRawAllocator {
/// Which allocator backend to use.
pub backend: MemoryAllocatorBackend,
/// Whether memory debugging should be enabled.
pub debug: bool,
}
impl PythonRawAllocator {
pub fn system() -> Self {
Self {
backend: MemoryAllocatorBackend::System,
..PythonRawAllocator::default()
}
}
pub fn jemalloc() -> Self {
Self {
backend: MemoryAllocatorBackend::Jemalloc,
..PythonRawAllocator::default()
}
}
pub fn rust() -> Self {
Self {
backend: MemoryAllocatorBackend::Rust,
..PythonRawAllocator::default()
}
}
}
impl Default for PythonRawAllocator {
fn default() -> Self {
Self {
backend: if cfg!(windows) {
MemoryAllocatorBackend::System
} else {
MemoryAllocatorBackend::Jemalloc
},
debug: false,
}
}
}
/// Configure a Python interpreter.
///
/// This type defines the configuration of a Python interpreter. It is used
/// to initialize a Python interpreter embedded in the current process.
///
/// The type contains a reference to a `PythonInterpreterConfig` instance,
/// which is an abstraction over the low-level C structs that Python uses during
/// interpreter initialization.
///
/// The `PythonInterpreterConfig` has a single non-optional field: `profile`.
/// This defines the defaults for various fields of the `PyPreConfig` and
/// `PyConfig` instances that are initialized as part of interpreter
/// initialization. See
/// https://docs.python.org/3/c-api/init_config.html#isolated-configuration for
/// more.
///
/// During interpreter initialization, we produce a `PyPreConfig` and
/// `PyConfig` derived from this type. Config settings are applied in
/// layers. First, we use the `PythonInterpreterConfig.profile` to derive
/// a default instance given a profile. Next, we override fields if the
/// `PythonInterpreterConfig` has `Some(T)` value set. Finally, we populate
/// some fields if they are missing but required for the given configuration.
/// For example, when in *isolated* mode, we set `program_name` and `home`
/// unless an explicit value was provided in the `PythonInterpreterConfig`.
///
/// Generally speaking, the `PythonInterpreterConfig` exists to hold
/// configuration that is defined in the CPython initialization and
/// configuration API and `OxidizedPythonInterpreterConfig` exists to
/// hold higher-level configuration for features specific to this crate.
#[derive(Clone, Debug)]
pub struct OxidizedPythonInterpreterConfig<'a> {
/// Low-level configuration of Python interpreter.
pub interpreter_config: PythonInterpreterConfig,
/// Allocator to use for Python's raw allocator.
pub raw_allocator: Option<PythonRawAllocator>,
/// Whether to install our custom meta path importer on interpreter init.
pub oxidized_importer: bool,
/// Whether to install the default `PathFinder` meta path finder.
pub filesystem_importer: bool,
/// Reference to packed resources data.
///
/// The referenced data contains Python module data. It likely comes from an
/// `include_bytes!(...)` of a file generated by PyOxidizer.
///
/// The format of the data is defined by the ``python-packed-resources``
/// crate. The data will be parsed as part of initializing the custom
/// meta path importer during interpreter initialization.
pub packed_resources: Option<&'a [u8]>,
/// Extra extension modules to make available to the interpreter.
///
/// The values will effectively be passed to ``PyImport_ExtendInitTab()``.
pub extra_extension_modules: Option<Vec<ExtensionModule>>,
/// Whether to set sys.argvb with bytes versions of process arguments.
///
/// On Windows, bytes will be UTF-16. On POSIX, bytes will be raw char*
/// values passed to `int main()`.
pub argvb: bool,
/// Whether to set sys.frozen=True.
///
/// Setting this will enable Python to emulate "frozen" binaries, such as
/// those used by PyInstaller.
pub sys_frozen: bool,
/// Whether to set sys._MEIPASS to the directory of the executable.
///
/// Setting this will enable Python to emulate PyInstaller's behavior
/// of setting this attribute.
pub sys_meipass: bool,
/// How to resolve the `terminfo` database.
pub terminfo_resolution: TerminfoResolution,
/// Environment variable holding the directory to write a loaded modules file.
///
/// If this value is set and the environment it refers to is set,
/// on interpreter shutdown, we will write a ``modules-<random>`` file to
/// the directory specified containing a ``\n`` delimited list of modules
/// loaded in ``sys.modules``.
pub write_modules_directory_env: Option<String>,
/// Defines what code to run by default.
///
pub run: PythonRunMode,
}
impl<'a> Default for OxidizedPythonInterpreterConfig<'a> {
fn default() -> Self {
Self {
interpreter_config: PythonInterpreterConfig {
profile: PythonInterpreterProfile::Python,
..PythonInterpreterConfig::default()
},
raw_allocator: None,
oxidized_importer: false,
filesystem_importer: true,
packed_resources: None,
extra_extension_modules: None,
argvb: false,
sys_frozen: false,
sys_meipass: false,
terminfo_resolution: TerminfoResolution::Dynamic,
write_modules_directory_env: None,
run: PythonRunMode::Repl,
}
}
}
impl<'a> From<PythonConfig<'a>> for OxidizedPythonInterpreterConfig<'a> {
fn from(config: PythonConfig<'a>) -> Self {
Self {
interpreter_config: PythonInterpreterConfig {
profile: if config.isolated {
PythonInterpreterProfile::Isolated
} else {
PythonInterpreterProfile::Python
},
stdio_encoding: config.standard_io_encoding,
stdio_errors: config.standard_io_errors,
optimization_level: Some(match config.opt_level {
0 => OptimizationLevel::Zero,
1 => OptimizationLevel::One,
2 => OptimizationLevel::Two,
_ => OptimizationLevel::Two,
}),
module_search_paths: if config.sys_paths.is_empty() {
None
} else {
Some(config.sys_paths.iter().map(PathBuf::from).collect::<_>())
},
bytes_warning: Some(match config.bytes_warning {
0 => BytesWarning::None,
1 => BytesWarning::Warn,
2 => BytesWarning::Raise,
_ => BytesWarning::Raise,
}),
site_import: Some(config.import_site),
user_site_directory: Some(config.import_user_site),
use_environment: Some(config.ignore_python_env),
inspect: Some(config.inspect),
interactive: Some(config.interactive),
legacy_windows_fs_encoding: Some(config.legacy_windows_stdio),
legacy_windows_stdio: Some(config.legacy_windows_stdio),
write_bytecode: Some(config.write_bytecode),
buffered_stdio: Some(config.unbuffered_stdio),
parser_debug: Some(config.parser_debug),
quiet: Some(config.quiet),
verbose: Some(config.verbose != 0),
..PythonInterpreterConfig::default()
},
raw_allocator: Some(config.raw_allocator),
oxidized_importer: config.use_custom_importlib,
filesystem_importer: config.filesystem_importer,
packed_resources: Some(config.packed_resources),
extra_extension_modules: Some(config.extra_extension_modules),
argvb: config.argvb,
sys_frozen: config.sys_frozen,
sys_meipass: config.sys_meipass,
terminfo_resolution: config.terminfo_resolution,
write_modules_directory_env: config.write_modules_directory_env,
run: config.run,
}
}
}