Struct cpython::PyModule

source ·
pub struct PyModule(/* private fields */);
Expand description

Represents a Python module object.

Implementations§

source§

impl PyModule

source

pub fn new(py: Python<'_>, name: &str) -> PyResult<PyModule>

Create a new module object with the __name__ attribute set to name.

source

pub fn import(py: Python<'_>, name: &str) -> PyResult<PyModule>

Import the Python module with the specified name.

source

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.

source

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.

source

pub fn filename(&self, py: Python<'_>) -> PyResult<&str>

Gets the module filename.

May fail if the module does not have a __file__ attribute.

source

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.

source

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)

source

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();
source

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.

source

pub fn add_class<T>(&self, py: Python<'_>) -> PyResult<()>

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§

source§

impl<'s> FromPyObject<'s> for &'s PyModule

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<&'s PyModule>

Extracts Self from the source PyObject.
source§

impl<'s> FromPyObject<'s> for PyModule

source§

fn extract(py: Python<'_>, obj: &'s PyObject) -> PyResult<PyModule>

Extracts Self from the source PyObject.
source§

impl PythonObject for PyModule

source§

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

Unchecked downcast from PyObject to Self. Undefined behavior if the input object does not have the expected type.

source§

fn as_object(&self) -> &PyObject

Casts the Python object to PyObject.
source§

fn into_object(self) -> PyObject

Casts the Python object to PyObject.
source§

impl PythonObjectWithCheckedDowncast for PyModule

source§

fn downcast_from<'p>( py: Python<'p>, obj: PyObject ) -> Result<PyModule, PythonObjectDowncastError<'p>>

Cast from PyObject to a concrete Python object type.
source§

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.
source§

impl PythonObjectWithTypeObject for PyModule

source§

fn type_object(py: Python<'_>) -> PyType

Retrieves the type object for this Python object type.
source§

impl ToPyObject for PyModule

Identity conversion: allows using existing PyObject instances where T: ToPyObject is expected.

§

type ObjectType = PyModule

source§

fn to_py_object(&self, py: Python<'_>) -> PyModule

Converts self into a Python object.
source§

fn into_py_object(self, _py: Python<'_>) -> PyModule

Converts self into a Python object. Read more
source§

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

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> PyClone for T
where T: PythonObject,

source§

fn clone_ref(&self, py: Python<'_>) -> T

source§

impl<T> PyDrop for T
where T: PythonObject,

source§

fn release_ref(self, _py: Python<'_>)

source§

impl<T> RefFromPyObject for T
where &'a T: for<'a> FromPyObject<'a>, T: ?Sized,

source§

fn with_extracted<F, R>( py: Python<'_>, obj: &PyObject, f: F ) -> Result<R, PyErr>
where F: FnOnce(&T) -> R,

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.