/
pyinterp.rs
416 lines (354 loc) · 13.3 KB
/
pyinterp.rs
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
// 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/.
use libc::c_char;
use std::env;
use std::ffi::CString;
use std::path::PathBuf;
use std::ptr::null;
use cpython::{
NoArgs, ObjectProtocol, PyErr, PyModule, PyObject, PyResult, Python, PythonObject, ToPyObject,
};
use pyffi;
use crate::data::*;
use crate::pyalloc::{make_raw_memory_allocator, RawAllocator};
use crate::pymodules_module::PyInit__pymodules;
use crate::pystr::OwnedPyStr;
const PYMODULES_NAME: &'static [u8] = b"_pymodules\0";
/// Holds the configuration of an embedded Python interpreter.
pub struct PythonConfig {
/// Path to the current executable.
pub exe: PathBuf,
/// Name of the current program to tell to Python.
pub program_name: String,
/// 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,
/// Filesystem paths to add to sys.path.
///
/// ``.`` will resolve to the path of the application at run-time.
pub sys_paths: Vec<PathBuf>,
/// 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 suppress writing of ``.pyc`` files when importing ``.py``
/// files from the filesystem. This is typically irrelevant since modules
/// are imported from memory.
pub dont_write_bytecode: bool,
/// Whether stdout and stderr streams should be unbuffered.
pub unbuffered_stdio: bool,
}
impl PythonConfig {
/// Obtain the PythonConfig with the settings compiled into the binary.
pub fn default() -> PythonConfig {
PythonConfig {
exe: env::current_exe().unwrap(),
program_name: PROGRAM_NAME.to_string(),
standard_io_encoding: STANDARD_IO_ENCODING,
standard_io_errors: STANDARD_IO_ERRORS,
opt_level: OPT_LEVEL,
use_custom_importlib: true,
sys_paths: vec![],
import_site: !NO_SITE,
import_user_site: !NO_USER_SITE_DIRECTORY,
ignore_python_env: IGNORE_ENVIRONMENT,
dont_write_bytecode: DONT_WRITE_BYTECODE,
unbuffered_stdio: UNBUFFERED_STDIO,
}
}
}
fn make_custom_frozen_modules() -> [pyffi::_frozen; 3] {
[
pyffi::_frozen {
name: FROZEN_IMPORTLIB_NAME.as_ptr() as *const i8,
code: FROZEN_IMPORTLIB_DATA.as_ptr(),
size: FROZEN_IMPORTLIB_DATA.len() as i32,
},
pyffi::_frozen {
name: FROZEN_IMPORTLIB_EXTERNAL_NAME.as_ptr() as *const i8,
code: FROZEN_IMPORTLIB_EXTERNAL_DATA.as_ptr(),
size: FROZEN_IMPORTLIB_EXTERNAL_DATA.len() as i32,
},
pyffi::_frozen {
name: null(),
code: null(),
size: 0,
},
]
}
#[cfg(windows)]
extern "C" {
pub fn __acrt_iob_func(x: libc::uint32_t) -> *mut libc::FILE;
}
#[cfg(windows)]
fn stdin_to_file() -> *mut libc::FILE {
// The stdin symbol is made available by importing <stdio.h>. On Windows,
// stdin is defined in corecrt_wstdio.h as a `#define` that calls this
// internal CRT function. There's no exported symbol to use. So we
// emulate the behavior of the C code.
//
// Relying on an internal CRT symbol is probably wrong. But Microsoft
// typically keeps backwards compatibility for undocumented functions
// like this because people use them in the wild.
//
// An attempt was made to use fdopen(0) like we do on POSIX. However,
// this causes a crash. The Microsoft C Runtime is already bending over
// backwards to coerce its native HANDLEs into POSIX file descriptors.
// Even if there are other ways to coerce a FILE* from a HANDLE
// (_open_osfhandle() + _fdopen() might work), using the same function
// that <stdio.h> uses to obtain a FILE* seems like the least risky thing
// to do.
unsafe { __acrt_iob_func(0) }
}
#[cfg(unix)]
fn stdin_to_file() -> *mut libc::FILE {
unsafe { libc::fdopen(libc::STDIN_FILENO, &('r' as libc::c_char)) }
}
/// Represents an embedded Python interpreter.
///
/// Since the Python API has global state and methods of this mutate global
/// state, there should only be a single instance of this type at any time.
pub struct MainPythonInterpreter {
pub config: PythonConfig,
frozen_modules: [pyffi::_frozen; 3],
init_run: bool,
raw_allocator: Option<RawAllocator>,
}
impl MainPythonInterpreter {
/// Construct an instance from a config.
///
/// There are no significant side-effects from calling this.
pub fn new(config: PythonConfig) -> MainPythonInterpreter {
MainPythonInterpreter {
config,
frozen_modules: make_custom_frozen_modules(),
init_run: false,
raw_allocator: Some(make_raw_memory_allocator()),
}
}
/// Initialize the interpreter.
///
/// This mutates global state in the Python interpreter according to the
/// bound config and initializes the Python interpreter.
///
/// After this is called, the embedded Python interpreter is ready to
/// execute custom code.
///
/// If called more than once, is a no-op.
pub fn init(&mut self, py: Python) {
// TODO return Result<> and don't panic.
if self.init_run {
return;
}
let config = &self.config;
if let Some(raw_allocator) = &self.raw_allocator {
unsafe {
let ptr = &raw_allocator.allocator as *const _;
pyffi::PyMem_SetAllocator(
pyffi::PyMemAllocatorDomain::PYMEM_DOMAIN_RAW,
ptr as *mut _,
);
// TODO call this if memory debugging enabled.
//pyffi::PyMem_SetupDebugHooks();
}
}
if config.use_custom_importlib {
// Replace the frozen modules in the interpreter with our custom set
// that knows how to import from memory.
unsafe {
pyffi::PyImport_FrozenModules = self.frozen_modules.as_ptr();
}
// Register our _pymodules extension which exposes modules data.
unsafe {
// name char* needs to live as long as the interpreter is active.
pyffi::PyImport_AppendInittab(
PYMODULES_NAME.as_ptr() as *const i8,
Some(PyInit__pymodules),
);
}
}
let home = OwnedPyStr::from(config.exe.to_str().unwrap());
unsafe {
// Pointer needs to live for lifetime of interpreter.
pyffi::Py_SetPythonHome(home.into());
}
let program_name = OwnedPyStr::from(config.program_name.as_str());
unsafe {
// Pointer needs to live for lifetime of interpreter.
pyffi::Py_SetProgramName(program_name.into());
}
if let (Some(ref encoding), Some(ref errors)) =
(&config.standard_io_encoding, &config.standard_io_errors)
{
let cencoding = CString::new(encoding.clone()).unwrap();
let cerrors = CString::new(errors.clone()).unwrap();
let res = unsafe {
pyffi::Py_SetStandardStreamEncoding(
cencoding.as_ptr() as *const i8,
cerrors.as_ptr() as *const i8,
)
};
if res != 0 {
panic!("unable to set standard stream encoding");
}
}
/*
// TODO expand "." to the exe's path.
let paths: Vec<&str> = config.sys_paths.iter().map(|p| p.to_str().unwrap()).collect();
// TODO use ; on Windows.
// TODO OwnedPyStr::from("") appears to fail?
let paths = paths.join(":");
let path = OwnedPyStr::from(paths.as_str());
unsafe {
pyffi::Py_SetPath(path.into());
}
*/
unsafe {
pyffi::Py_DontWriteBytecodeFlag = match config.dont_write_bytecode {
true => 1,
false => 0,
};
}
unsafe {
pyffi::Py_IgnoreEnvironmentFlag = match config.ignore_python_env {
true => 1,
false => 0,
};
}
unsafe {
pyffi::Py_NoSiteFlag = match config.import_site {
true => 0,
false => 1,
};
}
unsafe {
pyffi::Py_NoUserSiteDirectory = match config.import_user_site {
true => 0,
false => 1,
};
}
unsafe {
pyffi::Py_OptimizeFlag = config.opt_level;
}
unsafe {
pyffi::Py_UnbufferedStdioFlag = match config.unbuffered_stdio {
true => 1,
false => 0,
};
}
/* Pre-initialization functions we could support:
*
* PyObject_SetArenaAllocator()
* PySys_AddWarnOption()
* PySys_AddXOption()
* PySys_ResetWarnOptions()
*/
// TODO support storing raw argv somewhere to work around
// Python coercing it to Unicode on POSIX.
unsafe {
pyffi::Py_Initialize();
}
self.init_run = true;
// As a convention, sys.frozen is set to indicate we are running from
// a self-contained application.
let frozen = b"_pymodules\0";
let res = py.True().with_borrowed_ptr(py, |py_true| unsafe {
pyffi::PySys_SetObject(frozen.as_ptr() as *const i8, py_true)
});
match res {
0 => (),
_ => panic!("unable to set sys.frozen"),
}
}
pub fn run(&mut self) {
let py = unsafe { Python::assume_gil_acquired() };
self.init(py);
py.eval("import re, sys; from black import main; main()", None, None)
.unwrap();
//py.eval("print(\"hello, world\")", None, None).unwrap();
//py.import("__main__").unwrap();
}
/// Runs a Python module as the __main__ module.
///
/// Returns the execution result of the module code.
pub fn run_module_as_main(&mut self, name: &str) -> PyResult<PyObject> {
let py = unsafe { Python::assume_gil_acquired() };
self.init(py);
// This is modeled after runpy.py:_run_module_as_main().
let main: PyModule = unsafe {
PyObject::from_owned_ptr(
py,
pyffi::PyImport_AddModule("__main__\0".as_ptr() as *const c_char),
)
.cast_into(py)?
};
let main_dict = main.dict(py);
let importlib_util = py.import("importlib.util")?;
let spec = importlib_util.call(py, "find_spec", (name,), None)?;
let loader = spec.getattr(py, "loader")?;
let code = loader.call_method(py, "get_code", (name,), None)?;
let origin = spec.getattr(py, "origin")?;
let cached = spec.getattr(py, "cached")?;
// TODO handle __package__.
main_dict.set_item(py, "__name__", "__main__")?;
main_dict.set_item(py, "__file__", origin)?;
main_dict.set_item(py, "__cached__", cached)?;
main_dict.set_item(py, "__doc__", py.None())?;
main_dict.set_item(py, "__loader__", loader)?;
main_dict.set_item(py, "__spec__", spec)?;
unsafe {
let globals = main_dict.as_object().as_ptr();
let res = pyffi::PyEval_EvalCode(code.as_ptr(), globals, globals);
if res.is_null() {
let err = PyErr::fetch(py);
err.print(py);
Err(PyErr::fetch(py))
} else {
Ok(PyObject::from_owned_ptr(py, res))
}
}
}
/// Start and run a Python REPL.
///
/// This emulates what CPython's main.c does.
pub fn run_repl(&mut self) -> PyResult<PyObject> {
let py = unsafe { Python::assume_gil_acquired() };
self.init(py);
unsafe {
pyffi::Py_InspectFlag = 0;
}
match py.import("readline") {
Ok(_) => (),
Err(_) => (),
};
let sys = py.import("sys")?;
match sys.get(py, "__interactivehook__") {
Ok(hook) => {
hook.call(py, NoArgs, None)?;
}
Err(_) => (),
};
let stdin_filename = "<stdin>";
let filename = CString::new(stdin_filename).expect("could not create CString");
let mut cf = pyffi::PyCompilerFlags { cf_flags: 0 };
// TODO use return value.
unsafe {
let stdin = stdin_to_file();
pyffi::PyRun_AnyFileExFlags(stdin, filename.as_ptr() as *const c_char, 0, &mut cf)
};
Ok(py.None())
}
}
impl Drop for MainPythonInterpreter {
fn drop(&mut self) {
let _ = unsafe { pyffi::Py_FinalizeEx() };
}
}