File: test_multiple_pymethods.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 (76 lines) | stat: -rw-r--r-- 1,662 bytes parent folder | download | duplicates (2)
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
#![cfg(feature = "multiple-pymethods")]

use pyo3::prelude::*;
use pyo3::types::PyType;

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

#[pyclass]
struct PyClassWithMultiplePyMethods {}

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

#[pymethods]
impl PyClassWithMultiplePyMethods {
    fn __call__(&self) -> &'static str {
        "call"
    }
}

#[pymethods]
impl PyClassWithMultiplePyMethods {
    fn method(&self) -> &'static str {
        "method"
    }
}

#[pymethods]
impl PyClassWithMultiplePyMethods {
    #[classmethod]
    fn classmethod(_ty: &Bound<'_, PyType>) -> &'static str {
        "classmethod"
    }
}

#[pymethods]
impl PyClassWithMultiplePyMethods {
    #[staticmethod]
    fn staticmethod() -> &'static str {
        "staticmethod"
    }
}

#[pymethods]
impl PyClassWithMultiplePyMethods {
    #[classattr]
    fn class_attribute() -> &'static str {
        "class_attribute"
    }
}

#[pymethods]
impl PyClassWithMultiplePyMethods {
    #[classattr]
    const CLASS_ATTRIBUTE: &'static str = "CLASS_ATTRIBUTE";
}

#[test]
fn test_class_with_multiple_pymethods() {
    Python::with_gil(|py| {
        let cls = py.get_type_bound::<PyClassWithMultiplePyMethods>();
        py_assert!(py, cls, "cls()() == 'call'");
        py_assert!(py, cls, "cls().method() == 'method'");
        py_assert!(py, cls, "cls.classmethod() == 'classmethod'");
        py_assert!(py, cls, "cls.staticmethod() == 'staticmethod'");
        py_assert!(py, cls, "cls.class_attribute == 'class_attribute'");
        py_assert!(py, cls, "cls.CLASS_ATTRIBUTE == 'CLASS_ATTRIBUTE'");
    })
}