pub struct PyModule(/* private fields */);
Expand description
Represents a Python module object.
Implementations§
source§impl PyModule
impl PyModule
sourcepub fn new(py: Python<'_>, name: &str) -> PyResult<PyModule>
pub fn new(py: Python<'_>, name: &str) -> PyResult<PyModule>
Create a new module object with the __name__
attribute set to name.
sourcepub fn import(py: Python<'_>, name: &str) -> PyResult<PyModule>
pub fn import(py: Python<'_>, name: &str) -> PyResult<PyModule>
Import the Python module with the specified name.
sourcepub fn dict(&self, py: Python<'_>) -> PyDict
pub fn dict(&self, py: Python<'_>) -> PyDict
Return the dictionary object that implements module’s namespace;
this object is the same as the __dict__
attribute of the module object.
sourcepub fn name<'a>(&'a self, py: Python<'_>) -> PyResult<&'a str>
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.
sourcepub fn filename(&self, py: Python<'_>) -> PyResult<&str>
pub fn filename(&self, py: Python<'_>) -> PyResult<&str>
Gets the module filename.
May fail if the module does not have a __file__
attribute.
sourcepub fn filename_object(&self, py: Python<'_>) -> PyResult<PyObject>
pub fn filename_object(&self, py: Python<'_>) -> PyResult<PyObject>
Gets the module filename object.
May fail if the module does not have a __file__
attribute.
sourcepub fn get(&self, py: Python<'_>, name: &str) -> PyResult<PyObject>
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)
sourcepub fn call<A>(
&self,
py: Python<'_>,
name: &str,
args: A,
kwargs: Option<&PyDict>
) -> PyResult<PyObject>where
A: ToPyObject<ObjectType = PyTuple>,
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::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();
Trait Implementations§
source§impl<'s> FromPyObject<'s> for &'s PyModule
impl<'s> FromPyObject<'s> for &'s PyModule
source§impl<'s> FromPyObject<'s> for PyModule
impl<'s> FromPyObject<'s> for PyModule
source§impl PythonObject for PyModule
impl PythonObject for PyModule
source§unsafe fn unchecked_downcast_from(obj: PyObject) -> Self
unsafe fn unchecked_downcast_from(obj: PyObject) -> Self
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
source§unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self
unsafe fn unchecked_downcast_borrow_from<'a>(obj: &'a PyObject) -> &'a Self
Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.
source§fn into_object(self) -> PyObject
fn into_object(self) -> PyObject
source§impl PythonObjectWithCheckedDowncast for PyModule
impl PythonObjectWithCheckedDowncast for PyModule
source§fn downcast_from<'p>(
py: Python<'p>,
obj: PyObject
) -> Result<PyModule, PythonObjectDowncastError<'p>>
fn downcast_from<'p>( py: Python<'p>, obj: PyObject ) -> Result<PyModule, PythonObjectDowncastError<'p>>
source§fn downcast_borrow_from<'a, 'p>(
py: Python<'p>,
obj: &'a PyObject
) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>
fn downcast_borrow_from<'a, 'p>( py: Python<'p>, obj: &'a PyObject ) -> Result<&'a PyModule, PythonObjectDowncastError<'p>>
source§impl PythonObjectWithTypeObject for PyModule
impl PythonObjectWithTypeObject for PyModule
source§fn type_object(py: Python<'_>) -> PyType
fn type_object(py: Python<'_>) -> PyType
source§impl ToPyObject for PyModule
impl ToPyObject for PyModule
Identity conversion: allows using existing PyObject
instances where
T: ToPyObject
is expected.