File: test_various.rs

package info (click to toggle)
rust-pyo3 0.22.6-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 3,420 kB
  • sloc: makefile: 58; python: 39; sh: 1
file content (208 lines) | stat: -rw-r--r-- 5,223 bytes parent folder | download
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
#![cfg(feature = "macros")]

use pyo3::prelude::*;
use pyo3::py_run;
use pyo3::types::PyTuple;

use std::fmt;

#[path = "../src/tests/common.rs"]
mod common;

#[pyclass]
struct MutRefArg {
    n: i32,
}

#[pymethods]
impl MutRefArg {
    fn get(&self) -> i32 {
        self.n
    }
    fn set_other(&self, mut other: PyRefMut<'_, MutRefArg>) {
        other.n = 100;
    }
}

#[test]
fn mut_ref_arg() {
    Python::with_gil(|py| {
        let inst1 = Py::new(py, MutRefArg { n: 0 }).unwrap();
        let inst2 = Py::new(py, MutRefArg { n: 0 }).unwrap();

        py_run!(py, inst1 inst2, "inst1.set_other(inst2)");
        let inst2 = inst2.bind(py).borrow();
        assert_eq!(inst2.n, 100);
    });
}

#[pyclass]
struct PyUsize {
    #[pyo3(get)]
    pub value: usize,
}

#[pyfunction]
fn get_zero() -> PyUsize {
    PyUsize { value: 0 }
}

#[test]
/// Checks that we can use return a custom class in arbitrary function and use those functions
/// both in rust and python
fn return_custom_class() {
    Python::with_gil(|py| {
        // Using from rust
        assert_eq!(get_zero().value, 0);

        // Using from python
        let get_zero = wrap_pyfunction_bound!(get_zero)(py).unwrap();
        py_assert!(py, get_zero, "get_zero().value == 0");
    });
}

#[test]
fn intopytuple_primitive() {
    Python::with_gil(|py| {
        let tup = (1, 2, "foo");
        py_assert!(py, tup, "tup == (1, 2, 'foo')");
        py_assert!(py, tup, "tup[0] == 1");
        py_assert!(py, tup, "tup[1] == 2");
        py_assert!(py, tup, "tup[2] == 'foo'");
    });
}

#[pyclass]
struct SimplePyClass {}

#[test]
fn intopytuple_pyclass() {
    Python::with_gil(|py| {
        let tup = (
            Py::new(py, SimplePyClass {}).unwrap(),
            Py::new(py, SimplePyClass {}).unwrap(),
        );
        py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'");
        py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[1]).__name__");
        py_assert!(py, tup, "tup[0] != tup[1]");
    });
}

#[test]
fn pytuple_primitive_iter() {
    Python::with_gil(|py| {
        let tup = PyTuple::new_bound(py, [1u32, 2, 3].iter());
        py_assert!(py, tup, "tup == (1, 2, 3)");
    });
}

#[test]
fn pytuple_pyclass_iter() {
    Python::with_gil(|py| {
        let tup = PyTuple::new_bound(
            py,
            [
                Py::new(py, SimplePyClass {}).unwrap(),
                Py::new(py, SimplePyClass {}).unwrap(),
            ]
            .iter(),
        );
        py_assert!(py, tup, "type(tup[0]).__name__ == 'SimplePyClass'");
        py_assert!(py, tup, "type(tup[0]).__name__ == type(tup[0]).__name__");
        py_assert!(py, tup, "tup[0] != tup[1]");
    });
}

#[test]
#[cfg(any(Py_3_9, not(Py_LIMITED_API)))]
fn test_pickle() {
    use pyo3::types::PyDict;

    #[pyclass(dict, module = "test_module")]
    struct PickleSupport {}

    #[pymethods]
    impl PickleSupport {
        #[new]
        fn new() -> PickleSupport {
            PickleSupport {}
        }

        pub fn __reduce__<'py>(
            slf: &Bound<'py, Self>,
            py: Python<'py>,
        ) -> PyResult<(PyObject, Bound<'py, PyTuple>, PyObject)> {
            let cls = slf.to_object(py).getattr(py, "__class__")?;
            let dict = slf.to_object(py).getattr(py, "__dict__")?;
            Ok((cls, PyTuple::empty_bound(py), dict))
        }
    }

    fn add_module(module: Bound<'_, PyModule>) -> PyResult<()> {
        PyModule::import_bound(module.py(), "sys")?
            .dict()
            .get_item("modules")
            .unwrap()
            .unwrap()
            .downcast::<PyDict>()?
            .set_item(module.name()?, module)
    }

    Python::with_gil(|py| {
        let module = PyModule::new_bound(py, "test_module").unwrap();
        module.add_class::<PickleSupport>().unwrap();
        add_module(module).unwrap();
        let inst = Py::new(py, PickleSupport {}).unwrap();
        py_run!(
            py,
            inst,
            r#"
        inst.a = 1
        assert inst.__dict__ == {'a': 1}

        import pickle
        inst2 = pickle.loads(pickle.dumps(inst))

        assert inst2.__dict__ == {'a': 1}
    "#
        );
    });
}

/// Testing https://github.com/PyO3/pyo3/issues/1106. A result type that
/// implements `From<MyError> for PyErr` should be automatically converted
/// when using `#[pyfunction]`.
///
/// This only makes sure that valid `Result` types do work. For an invalid
/// enum type, see `ui/invalid_result_conversion.py`.
#[derive(Debug)]
struct MyError {
    pub descr: &'static str,
}

impl fmt::Display for MyError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "My error message: {}", self.descr)
    }
}

/// Important for the automatic conversion to `PyErr`.
impl From<MyError> for PyErr {
    fn from(err: MyError) -> pyo3::PyErr {
        pyo3::exceptions::PyOSError::new_err(err.to_string())
    }
}

#[pyfunction]
fn result_conversion_function() -> Result<(), MyError> {
    Err(MyError {
        descr: "something went wrong",
    })
}

#[test]
fn test_result_conversion() {
    Python::with_gil(|py| {
        wrap_pyfunction_bound!(result_conversion_function)(py).unwrap();
    });
}