1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
use libc::c_char;
use std::ffi::{CStr, CString};
use crate::conversion::ToPyObject;
use crate::err::{self, PyErr, PyResult};
use crate::ffi;
use crate::objectprotocol::ObjectProtocol;
use crate::objects::{exc, PyDict, PyObject, PyTuple};
use crate::py_class::PythonObjectFromPyClassMacro;
use crate::python::{PyDrop, Python, PythonObject};
pub struct PyModule(PyObject);
pyobject_newtype!(PyModule, PyModule_Check, PyModule_Type);
impl PyModule {
pub fn new(py: Python, name: &str) -> PyResult<PyModule> {
let name = CString::new(name).unwrap();
unsafe { err::result_cast_from_owned_ptr(py, ffi::PyModule_New(name.as_ptr())) }
}
pub fn import(py: Python, name: &str) -> PyResult<PyModule> {
let name = CString::new(name).unwrap();
unsafe { err::result_cast_from_owned_ptr(py, ffi::PyImport_ImportModule(name.as_ptr())) }
}
pub fn dict(&self, py: Python) -> PyDict {
unsafe {
let r = PyObject::from_borrowed_ptr(py, ffi::PyModule_GetDict(self.0.as_ptr()));
r.unchecked_cast_into::<PyDict>()
}
}
unsafe fn str_from_ptr<'a>(&'a self, py: Python, ptr: *const c_char) -> PyResult<&'a str> {
if ptr.is_null() {
Err(PyErr::fetch(py))
} else {
let slice = CStr::from_ptr(ptr).to_bytes();
match std::str::from_utf8(slice) {
Ok(s) => Ok(s),
Err(e) => Err(PyErr::from_instance(
py,
exc::UnicodeDecodeError::new_utf8(py, slice, e)?,
)),
}
}
}
pub fn name<'a>(&'a self, py: Python) -> PyResult<&'a str> {
unsafe { self.str_from_ptr(py, ffi::PyModule_GetName(self.0.as_ptr())) }
}
#[allow(deprecated)]
pub fn filename(&self, py: Python) -> PyResult<&str> {
unsafe { self.str_from_ptr(py, ffi::PyModule_GetFilename(self.0.as_ptr())) }
}
#[cfg(feature = "python3-sys")]
pub fn filename_object(&self, py: Python) -> PyResult<PyObject> {
let ptr = unsafe { ffi::PyModule_GetFilenameObject(self.0.as_ptr()) };
if ptr.is_null() {
Err(PyErr::fetch(py))
} else {
Ok(unsafe { PyObject::from_borrowed_ptr(py, ptr) })
}
}
pub fn get(&self, py: Python, name: &str) -> PyResult<PyObject> {
self.as_object().getattr(py, name)
}
pub fn call<A>(
&self,
py: Python,
name: &str,
args: A,
kwargs: Option<&PyDict>,
) -> PyResult<PyObject>
where
A: ToPyObject<ObjectType = PyTuple>,
{
self.as_object().getattr(py, name)?.call(py, args, kwargs)
}
pub fn add<V>(&self, py: Python, name: &str, value: V) -> PyResult<()>
where
V: ToPyObject,
{
self.as_object().setattr(py, name, value)
}
pub fn add_class<T>(&self, py: Python<'_>) -> PyResult<()>
where
T: PythonObjectFromPyClassMacro,
{
T::add_to_module(py, self)
}
}