Skip to content

Commit 0faa95b

Browse files
committed
pyembed: consolidate functions to load state
We want the code in Python land to be as simple as possible. Plus this will make it easier to test, since we don't need to rely on having a Python interpreter.
1 parent 895ca6d commit 0faa95b

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

pyembed/src/importer.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -475,22 +475,7 @@ fn module_setup(
475475

476476
let mut importer_state = PythonImporterState::default();
477477

478-
// Populate our importer with entries from the interpreter then in-memory data
479-
// structures. The last write wins. That's why we load builtins and frozens before
480-
// data provided by us.
481-
if let Err(e) = importer_state.load_interpreter_builtin_modules() {
482-
return Err(PyErr::new::<ValueError, _>(py, e));
483-
}
484-
485-
if let Err(e) = importer_state.load_interpreter_frozen_modules() {
486-
return Err(PyErr::new::<ValueError, _>(py, e));
487-
}
488-
489-
if let Err(e) = importer_state.load_modules_data(state.py_modules_data) {
490-
return Err(PyErr::new::<ValueError, _>(py, e));
491-
}
492-
493-
if let Err(e) = importer_state.load_resources_data(state.py_resources_data) {
478+
if let Err(e) = importer_state.load(state.py_modules_data, state.py_resources_data) {
494479
return Err(PyErr::new::<ValueError, _>(py, e));
495480
}
496481

pyembed/src/python_resources.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,24 @@ impl Default for PythonImporterState {
5454
}
5555

5656
impl PythonImporterState {
57+
/// Load state from the environment and by parsing data structures.
58+
pub fn load(
59+
&mut self,
60+
modules_data: &'static [u8],
61+
resources_data: &'static [u8],
62+
) -> Result<(), &'static str> {
63+
// Last write wins. So start with core modules then move on to more
64+
// local data.
65+
self.load_interpreter_builtin_modules()?;
66+
self.load_interpreter_frozen_modules()?;
67+
self.load_modules_data(modules_data)?;
68+
self.load_resources_data(resources_data)?;
69+
70+
Ok(())
71+
}
72+
5773
/// Load `builtin` modules from the Python interpreter.
58-
pub fn load_interpreter_builtin_modules(&mut self) -> Result<(), &'static str> {
74+
fn load_interpreter_builtin_modules(&mut self) -> Result<(), &'static str> {
5975
for i in 0.. {
6076
let record = unsafe { pyffi::PyImport_Inittab.offset(i) };
6177

@@ -78,7 +94,7 @@ impl PythonImporterState {
7894
}
7995

8096
/// Load `frozen` modules from the Python interpreter.
81-
pub fn load_interpreter_frozen_modules(&mut self) -> Result<(), &'static str> {
97+
fn load_interpreter_frozen_modules(&mut self) -> Result<(), &'static str> {
8298
for i in 0.. {
8399
let record = unsafe { pyffi::PyImport_FrozenModules.offset(i) };
84100

@@ -101,7 +117,7 @@ impl PythonImporterState {
101117
}
102118

103119
/// Parse binary modules data and update current data structure.
104-
pub fn load_modules_data(&mut self, data: &'static [u8]) -> Result<(), &'static str> {
120+
fn load_modules_data(&mut self, data: &'static [u8]) -> Result<(), &'static str> {
105121
let mut reader = Cursor::new(data);
106122

107123
let count = reader
@@ -196,7 +212,7 @@ impl PythonImporterState {
196212
Ok(())
197213
}
198214

199-
pub fn load_resources_data(&mut self, data: &'static [u8]) -> Result<(), &'static str> {
215+
fn load_resources_data(&mut self, data: &'static [u8]) -> Result<(), &'static str> {
200216
let mut reader = Cursor::new(data);
201217

202218
let package_count = reader

0 commit comments

Comments
 (0)