Struct cpython::PyModule [−][src]
pub struct PyModule(_);
Represents a Python module object.
Methods
impl PyModule[src]
impl PyModulepub fn new(py: Python, name: &str) -> PyResult<PyModule>[src]
pub fn new(py: Python, name: &str) -> PyResult<PyModule>Create a new module object with the __name__ attribute set to name.
pub fn import(py: Python, name: &str) -> PyResult<PyModule>[src]
pub fn import(py: Python, name: &str) -> PyResult<PyModule>Import the Python module with the specified name.
pub fn dict(&self, py: Python) -> PyDict[src]
pub fn dict(&self, py: Python) -> PyDictReturn the dictionary object that implements module's namespace;
this object is the same as the __dict__ attribute of the module object.
pub fn name<'a>(&'a self, py: Python) -> PyResult<&'a str>[src]
pub fn name<'a>(&'a self, py: Python) -> PyResult<&'a str>Gets the module name.
May fail if the module does not have a __name__ attribute.
pub fn filename<'a>(&'a self, py: Python) -> PyResult<&'a str>[src]
pub fn filename<'a>(&'a self, py: Python) -> PyResult<&'a str>Gets the module filename.
May fail if the module does not have a __file__ attribute.
pub fn get(&self, py: Python, name: &str) -> PyResult<PyObject>[src]
pub fn get(&self, py: Python, name: &str) -> PyResult<PyObject>Gets a member from the module.
This is equivalent to the Python expression: getattr(module, name)
pub fn call<A>(
&self,
py: Python,
name: &str,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>, [src]
pub fn call<A>(
&self,
py: Python,
name: &str,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject> where
A: ToPyObject<ObjectType = PyTuple>, Calls a function in the module.
This is equivalent to the Python expression: getattr(module, name)(*args, **kwargs)
args should be a value that, when converted to Python, results in a tuple.
For this purpose, you can use:
cpython::NoArgswhen calling a method without any arguments- otherwise, a Rust tuple with 1 or more elements
Example
use cpython::NoArgs; let sys = py.import("sys").unwrap(); // Call function without arguments: let encoding = sys.call(py, "getdefaultencoding", NoArgs, None).unwrap(); // Call function with a single argument: sys.call(py, "setrecursionlimit", (1000,), None).unwrap();
pub fn add<V>(&self, py: Python, name: &str, value: V) -> PyResult<()> where
V: ToPyObject, [src]
pub fn add<V>(&self, py: Python, name: &str, value: V) -> PyResult<()> where
V: ToPyObject, Adds a member to the module.
This is a convenience function which can be used from the module's initialization function.
pub fn add_class<'p, T>(&self, py: Python<'p>) -> PyResult<()> where
T: PythonObjectFromPyClassMacro, [src]
pub fn add_class<'p, T>(&self, py: Python<'p>) -> PyResult<()> where
T: PythonObjectFromPyClassMacro, Adds a new extension type to the module.
This is a convenience function that initializes the py_class!(),
sets new_type.__module__ to this module's name,
and adds the type to this module.
Trait Implementations
impl ToPyObject for PyModule[src]
impl ToPyObject for PyModuleIdentity conversion: allows using existing PyObject instances where
T: ToPyObject is expected.
type ObjectType = PyModule
fn to_py_object(&self, py: Python) -> PyModule[src]
fn to_py_object(&self, py: Python) -> PyModuleConverts self into a Python object.
fn into_py_object(self, _py: Python) -> PyModule[src]
fn into_py_object(self, _py: Python) -> PyModuleConverts self into a Python object. Read more
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R where
F: FnOnce(*mut PyObject) -> R, [src]
fn with_borrowed_ptr<F, R>(&self, _py: Python, f: F) -> R where
F: FnOnce(*mut PyObject) -> R, Converts self into a Python object and calls the specified closure on the native FFI pointer underlying the Python object. Read more
impl<'source> FromPyObject<'source> for PyModule[src]
impl<'source> FromPyObject<'source> for PyModulefn extract(py: Python, obj: &'source PyObject) -> PyResult<PyModule>[src]
fn extract(py: Python, obj: &'source PyObject) -> PyResult<PyModule>Extracts Self from the source PyObject.
impl<'source> FromPyObject<'source> for &'source PyModule[src]
impl<'source> FromPyObject<'source> for &'source PyModulefn extract(py: Python, obj: &'source PyObject) -> PyResult<&'source PyModule>[src]
fn extract(py: Python, obj: &'source PyObject) -> PyResult<&'source PyModule>Extracts Self from the source PyObject.
impl PythonObject for PyModule[src]
impl PythonObject for PyModulefn as_object(&self) -> &PyObject[src]
fn as_object(&self) -> &PyObjectCasts the Python object to PyObject.
fn into_object(self) -> PyObject[src]
fn into_object(self) -> PyObjectCasts the Python object to PyObject.
unsafe fn unchecked_downcast_from(obj: PyObject) -> Self[src]
unsafe fn unchecked_downcast_from(obj: PyObject) -> SelfUnchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
ⓘImportant traits for &'a mut Runsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self[src]
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a SelfUnchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
impl PythonObjectWithCheckedDowncast for PyModule[src]
impl PythonObjectWithCheckedDowncast for PyModulefn downcast_from<'p>(
py: Python<'p>,
obj: PyObject
) -> Result<PyModule, PythonObjectDowncastError<'p>>[src]
fn downcast_from<'p>(
py: Python<'p>,
obj: PyObject
) -> Result<PyModule, PythonObjectDowncastError<'p>>Cast from PyObject to a concrete Python object type.
fn downcast_borrow_from<'a, 'p>(
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>[src]
fn downcast_borrow_from<'a, 'p>(
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>Cast from PyObject to a concrete Python object type.
impl PythonObjectWithTypeObject for PyModule[src]
impl PythonObjectWithTypeObject for PyModulefn type_object(py: Python) -> PyType[src]
fn type_object(py: Python) -> PyTypeRetrieves the type object for this Python object type.