2021-03-02 22:34:25 +00:00
|
|
|
#![cfg(feature = "multiple-pymethods")]
|
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
|
|
|
use pyo3::types::PyType;
|
|
|
|
|
|
|
|
#[macro_use]
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2021-03-02 22:34:25 +00:00
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct PyClassWithMultiplePyMethods {}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl PyClassWithMultiplePyMethods {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl PyClassWithMultiplePyMethods {
|
2021-10-17 21:49:10 +00:00
|
|
|
fn __call__(&self) -> &'static str {
|
2021-03-02 22:34:25 +00:00
|
|
|
"call"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl PyClassWithMultiplePyMethods {
|
|
|
|
fn method(&self) -> &'static str {
|
|
|
|
"method"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl PyClassWithMultiplePyMethods {
|
|
|
|
#[classmethod]
|
2024-02-16 00:36:11 +00:00
|
|
|
fn classmethod(_ty: &Bound<'_, PyType>) -> &'static str {
|
2021-03-02 22:34:25 +00:00
|
|
|
"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| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let cls = py.get_type_bound::<PyClassWithMultiplePyMethods>();
|
2021-03-02 22:34:25 +00:00
|
|
|
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'");
|
|
|
|
})
|
|
|
|
}
|