Struct cpython::PyModule [−][src]
Represents a Python module object.
Implementations
impl PyModule
[src]
pub fn new(py: Python<'_>, name: &str) -> PyResult<PyModule>
[src]
Create a new module object with the __name__
attribute set to name.
pub fn import(py: Python<'_>, name: &str) -> PyResult<PyModule>
[src]
Import the Python module with the specified name.
pub fn dict(&self, py: Python<'_>) -> PyDict
[src]
Return 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]
Gets the module name.
May fail if the module does not have a __name__
attribute.
pub fn filename(&self, py: Python<'_>) -> PyResult<&str>
[src]
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]
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]
&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::NoArgs
when 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]
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<T>(&self, py: Python<'_>) -> PyResult<()> where
T: PythonObjectFromPyClassMacro,
[src]
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<'s> FromPyObject<'s> for PyModule
[src]
impl<'s> FromPyObject<'s> for &'s PyModule
[src]
impl PythonObject for PyModule
[src]
fn as_object(&self) -> &PyObject
[src]
fn into_object(self) -> PyObject
[src]
unsafe fn unchecked_downcast_from(obj: PyObject) -> Self
[src]
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self
[src]
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
impl PythonObjectWithCheckedDowncast for PyModule
[src]
fn downcast_from<'p>(
py: Python<'p>,
obj: PyObject
) -> Result<PyModule, PythonObjectDowncastError<'p>>
[src]
py: Python<'p>,
obj: PyObject
) -> Result<PyModule, PythonObjectDowncastError<'p>>
fn downcast_borrow_from<'a, 'p>(
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>
[src]
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>
impl PythonObjectWithTypeObject for PyModule
[src]
fn type_object(py: Python<'_>) -> PyType
[src]
impl ToPyObject for PyModule
[src]
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.
type ObjectType = PyModule
fn to_py_object(&self, py: Python<'_>) -> PyModule
[src]
fn into_py_object(self, _py: Python<'_>) -> PyModule
[src]
fn with_borrowed_ptr<F, R>(&self, _py: Python<'_>, f: F) -> R where
F: FnOnce(*mut PyObject) -> R,
[src]
F: FnOnce(*mut PyObject) -> R,
Auto Trait Implementations
impl RefUnwindSafe for PyModule
[src]
impl Send for PyModule
[src]
impl Sync for PyModule
[src]
impl Unpin for PyModule
[src]
impl UnwindSafe for PyModule
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,