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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/// A convenient macro to execute a Python code snippet, with some local variables set.
///
/// # Panics
///
/// This macro internally calls [`Python::run`](crate::Python::run) and panics
/// if it returns `Err`, after printing the error to stdout.
///
/// If you need to handle failures, please use [`Python::run`](crate::marker::Python::run) instead.
///
/// # Examples
/// ```
/// use pyo3::{prelude::*, py_run, types::PyList};
///
/// # fn main() -> PyResult<()> {
/// Python::attach(|py| {
/// let list = PyList::new(py, &[1, 2, 3])?;
/// py_run!(py, list, "assert list == [1, 2, 3]");
/// # Ok(())
/// })
/// # }
/// ```
///
/// You can use this macro to test pyfunctions or pyclasses quickly.
///
/// ```
/// use pyo3::{prelude::*, py_run};
///
/// #[pyclass]
/// #[derive(Debug)]
/// struct Time {
/// hour: u32,
/// minute: u32,
/// second: u32,
/// }
///
/// #[pymethods]
/// impl Time {
/// fn repl_japanese(&self) -> String {
/// format!("{}時{}分{}秒", self.hour, self.minute, self.second)
/// }
/// #[getter]
/// fn hour(&self) -> u32 {
/// self.hour
/// }
/// fn as_tuple(&self) -> (u32, u32, u32) {
/// (self.hour, self.minute, self.second)
/// }
/// }
///
/// Python::attach(|py| {
/// let time = Py::new(py, Time {hour: 8, minute: 43, second: 16}).unwrap();
/// let time_as_tuple = (8, 43, 16);
/// py_run!(py, time time_as_tuple, r#"
/// assert time.hour == 8
/// assert time.repl_japanese() == "8時43分16秒"
/// assert time.as_tuple() == time_as_tuple
/// "#);
/// });
/// ```
///
/// If you need to prepare the `locals` dict by yourself, you can pass it as `*locals`.
///
/// ```
/// use pyo3::prelude::*;
/// use pyo3::types::IntoPyDict;
///
/// #[pyclass]
/// struct MyClass;
///
/// #[pymethods]
/// impl MyClass {
/// #[new]
/// fn new() -> Self {
/// MyClass {}
/// }
/// }
///
/// # fn main() -> PyResult<()> {
/// Python::attach(|py| {
/// let locals = [("C", py.get_type::<MyClass>())].into_py_dict(py)?;
/// pyo3::py_run!(py, *locals, "c = C()");
/// # Ok(())
/// })
/// # }
/// ```
/// Internal implementation of the `py_run!` macro.
///
/// FIXME: this currently unconditionally allocates a `CString`. We should consider making this not so:
/// - Maybe require users to pass `&CStr` / `CString`?
/// - Maybe adjust the `unindent` code to produce `&Cstr` / `Cstring`?
/// Wraps a Rust function annotated with [`#[pyfunction]`](macro@crate::pyfunction).
///
/// This can be used with [`PyModule::add_function`](crate::types::PyModuleMethods::add_function) to
/// add free functions to a [`PyModule`](crate::types::PyModule) - see its documentation for more
/// information.
///
/// # Examples
/// ```
/// use pyo3::prelude::*;
/// #[pyfunction]
/// fn add(x: i32, y: i32) -> i32 {
/// x + y
/// }
///
/// # fn main() -> PyResult<()> {
/// Python::attach(|py| {
/// let example = PyModule::from_code(
/// py,
/// c"from collections.abc import Callable
/// def add_two_and_three(add: 'Callable[[int, int], int]') -> int:
/// return add(2, 3)",
/// c"example.py",
/// c"",
/// )?;
///
/// // `add_two_and_three` is a Python function defined in the code above
/// let add_two_and_three = example.getattr("add_two_and_three")?;
///
/// // `add` is a Python function defined by the `#[pyfunction]` macro
/// let add = wrap_pyfunction!(add, py)?;
///
/// let result = add_two_and_three.call1((add,))?.extract::<i32>()?;
///
/// assert_eq!(result, 5);
///
/// # Ok(())
/// })
/// # }
/// ```
/// Returns a function that takes a [`Python`](crate::Python) instance and returns a
/// Python module.
///
/// Use this together with [`#[pymodule]`](crate::pymodule) and
/// [`PyModule::add_wrapped`](crate::types::PyModuleMethods::add_wrapped).
/// Add the module to the initialization table in order to make embedded Python code to use it.
/// Module name is the argument.
///
/// Use it before [`Python::initialize`](crate::marker::Python::initialize) and
/// leave feature `auto-initialize` off