Struct cpython::PyModule[][src]

pub struct PyModule(_);

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]

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]

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]

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]

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]

impl PythonObjectWithTypeObject for PyModule[src]

impl ToPyObject for PyModule[src]

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

type ObjectType = PyModule

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]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.