Added a function to PyModule to load a module from a string of Python

This commit is contained in:
Kevin Phillips 2018-05-16 10:56:58 +02:00
parent b3c92f2be7
commit 1b1121e558
1 changed files with 28 additions and 1 deletions

View File

@ -8,7 +8,7 @@ use std::ffi::{CStr, CString};
use ffi;
use typeob::{PyTypeInfo, initialize_type};
use conversion::{ToPyObject, IntoPyTuple};
use conversion::{ToPyObject, IntoPyTuple, FromPyObject};
use object::PyObject;
use python::{Python, ToPyPointer, IntoPyDictPointer};
use objects::{PyObjectRef, PyDict, PyType, exc};
@ -41,6 +41,33 @@ impl PyModule {
}
}
/// Loads the python code specified into a new module
/// 'code' is the raw Python you want to load into the module
/// 'file_name' is the file name to associate with the module
/// (this is used when Python reports errors, for example)
/// 'module_name' is the name to give the module
pub fn from_code<'p>(py: Python<'p>, code: &str, file_name: &str, module_name: &str) -> PyResult<&'p PyModule> {
let data = CString::new(code)?;
let filename = CString::new(file_name)?;
let module = CString::new(module_name)?;
unsafe {
let cptr = ffi::Py_CompileString(data.as_ptr(), filename.as_ptr(), ffi::Py_file_input);
if cptr.is_null() {
return Err(PyErr::fetch(py));
}
let mptr = ffi::PyImport_ExecCodeModuleEx(module.as_ptr(), cptr, filename.as_ptr());
if mptr.is_null() {
return Err(PyErr::fetch(py));
}
<&PyModule as FromPyObject>::extract(py.from_owned_ptr_or_err(mptr)?)
}
}
/// Return the dictionary object that implements module's namespace;
/// this object is the same as the `__dict__` attribute of the module object.
pub fn dict(&self) -> &PyDict {