pyembed/lib.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5/*!
6Control an embedded Python interpreter.
7
8The `pyembed` crate contains functionality for controlling an embedded
9Python interpreter running in the current process.
10
11`pyembed` provides additional functionality over what is covered by the official
12[Embedding Python in Another Application](https://docs.python.org/3/extending/embedding.html)
13docs and provided by the [CPython C API](https://docs.python.org/3/c-api/).
14For example, `pyembed` can utilize a custom Python *meta path importer* that
15can import Python module bytecode from memory using 0-copy.
16
17This crate was initially designed for and is maintained as part of the
18[PyOxidizer](https://github.com/indygreg/PyOxidizer) project. However,
19the crate is generic and can be used outside the PyOxidizer project.
20
21The most important types in this crate are
22[OxidizedPythonInterpreterConfig] and [MainPythonInterpreter]. An
23[OxidizedPythonInterpreterConfig] defines how a Python interpreter is to
24behave. A [MainPythonInterpreter] creates and manages that interpreter and
25serves as a high-level interface for running code in the interpreter.
26
27# Dependencies
28
29Under the hood, `pyembed` makes direct use of the `pyo3` crate for
30low-level Python FFI bindings as well as higher-level interfacing.
31
32**It is an explicit goal of this crate to rely on as few external dependencies
33as possible.** This is because we want to minimize bloat in produced binaries.
34
35# Features
36
37The optional `allocator-jemalloc` feature controls support for using
38[jemalloc](http://jemalloc.net/) as Python's memory allocator. Use of Jemalloc
39from Python is a run-time configuration option controlled by the
40[OxidizedPythonInterpreterConfig] type and having `jemalloc` compiled into the
41binary does not mean it is being used!
42
43The optional `allocator-mimalloc` feature controls support for using
44[mimalloc](https://github.com/microsoft/mimalloc) as Python's memory allocator.
45The feature behaves similarly to `jemalloc`, which is documented above.
46
47The optional `allocator-snmalloc` feature controls support for using
48[snmalloc](https://github.com/microsoft/snmalloc) as Python's memory allocator.
49The feature behaves similarly to `jemalloc`, which is documented above.
50
51The optional `serialization` feature controls whether configuration types
52(such as [OxidizedPythonInterpreterConfig]) implement `Serialize` and
53`Deserialize`.
54*/
55
56#[allow(unused)]
57mod config;
58mod conversion;
59mod error;
60mod interpreter;
61mod interpreter_config;
62mod osutils;
63mod pyalloc;
64pub mod technotes;
65#[cfg(test)]
66mod test;
67
68#[allow(unused_imports)]
69pub use {
70 crate::{
71 config::{
72 ExtensionModule, OxidizedPythonInterpreterConfig,
73 ResolvedOxidizedPythonInterpreterConfig,
74 },
75 error::NewInterpreterError,
76 interpreter::MainPythonInterpreter,
77 pyalloc::PythonMemoryAllocator,
78 },
79 oxidized_importer::{PackedResourcesSource, PythonResourcesState},
80 python_packaging::{
81 interpreter::{
82 Allocator, BytesWarning, CheckHashPycsMode, CoerceCLocale, MemoryAllocatorBackend,
83 MultiprocessingStartMethod, PythonInterpreterConfig, PythonInterpreterProfile,
84 TerminfoResolution,
85 },
86 resource::BytecodeOptimizationLevel,
87 },
88};