|
| 1 | +#![allow(non_snake_case)] |
| 2 | +use pyo3::class::PyMethodDefType; |
| 3 | +use pyo3::prelude::*; |
| 4 | +use pyo3::py_run; |
| 5 | + |
| 6 | +mod common; |
| 7 | + |
| 8 | +// Tests for PyClassDefaultImpls |
| 9 | +#[pyclass] |
| 10 | +struct TestDefaultImpl; |
| 11 | + |
| 12 | +// generated using `Cargo expand` |
| 13 | +// equivalent to |
| 14 | +// ``` |
| 15 | +// impl TestDefaultImpl { |
| 16 | +// #[staticmethod] |
| 17 | +// fn default() {} |
| 18 | +// #[staticmethod] |
| 19 | +// fn overriden() -> bool { false } |
| 20 | +// } |
| 21 | +// ``` |
| 22 | +impl TestDefaultImpl { |
| 23 | + fn __pyo3__default() {} |
| 24 | + fn __pyo3__overriden() -> bool { |
| 25 | + false |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl pyo3::class::impl_::PyClassDefaultImpls<TestDefaultImpl> |
| 30 | + for pyo3::class::impl_::PyClassImplCollector<TestDefaultImpl> |
| 31 | +{ |
| 32 | + fn py_class_default_impls(self) -> &'static [PyMethodDefType] { |
| 33 | + static METHODS: &[::pyo3::class::methods::PyMethodDefType] = &[ |
| 34 | + ::pyo3::class::PyMethodDefType::Static( |
| 35 | + ::pyo3::class::methods::PyMethodDef::noargs( |
| 36 | + "default\u{0}", |
| 37 | + ::pyo3::class::methods::PyCFunction({ |
| 38 | + unsafe extern "C" fn __wrap( |
| 39 | + _slf: *mut ::pyo3::ffi::PyObject, |
| 40 | + _args: *mut ::pyo3::ffi::PyObject, |
| 41 | + ) -> *mut ::pyo3::ffi::PyObject { |
| 42 | + ::pyo3::callback::handle_panic(|_py| { |
| 43 | + ::pyo3::callback::convert(_py, TestDefaultImpl::__pyo3__default()) |
| 44 | + }) |
| 45 | + } |
| 46 | + __wrap |
| 47 | + }), |
| 48 | + "\u{0}", |
| 49 | + ) |
| 50 | + .flags(::pyo3::ffi::METH_STATIC), |
| 51 | + ), |
| 52 | + ::pyo3::class::PyMethodDefType::Static( |
| 53 | + ::pyo3::class::methods::PyMethodDef::noargs( |
| 54 | + "overriden\u{0}", |
| 55 | + ::pyo3::class::methods::PyCFunction({ |
| 56 | + unsafe extern "C" fn __wrap( |
| 57 | + _slf: *mut ::pyo3::ffi::PyObject, |
| 58 | + _args: *mut ::pyo3::ffi::PyObject, |
| 59 | + ) -> *mut ::pyo3::ffi::PyObject { |
| 60 | + ::pyo3::callback::handle_panic(|_py| { |
| 61 | + ::pyo3::callback::convert(_py, TestDefaultImpl::__pyo3__overriden()) |
| 62 | + }) |
| 63 | + } |
| 64 | + __wrap |
| 65 | + }), |
| 66 | + "\u{0}", |
| 67 | + ) |
| 68 | + .flags(::pyo3::ffi::METH_STATIC), |
| 69 | + ), |
| 70 | + ]; |
| 71 | + METHODS |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +#[pymethods] |
| 76 | +impl TestDefaultImpl { |
| 77 | + #[staticmethod] |
| 78 | + fn overriden() -> bool { |
| 79 | + true |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[test] |
| 84 | +fn test_default_impl_exists() { |
| 85 | + Python::with_gil(|py| { |
| 86 | + let test_object = Py::new(py, TestDefaultImpl).unwrap(); |
| 87 | + py_run!(py, test_object, "test_object.default()"); |
| 88 | + }) |
| 89 | +} |
| 90 | + |
| 91 | +#[test] |
| 92 | +fn test_default_impl_is_overriden() { |
| 93 | + Python::with_gil(|py| { |
| 94 | + let test_object = Py::new(py, TestDefaultImpl).unwrap(); |
| 95 | + py_assert!(py, test_object, "test_object.overriden() == True"); |
| 96 | + }) |
| 97 | +} |
| 98 | + |
| 99 | +#[pyclass] |
| 100 | +struct OverrideMagicMethod; |
| 101 | + |
| 102 | +// generated using `Cargo expand` |
| 103 | +// equivalent to |
| 104 | +// ``` |
| 105 | +// impl OverrideMagicMethod { |
| 106 | +// fn __str__(&self) -> &'static str { |
| 107 | +// "default" |
| 108 | +// } |
| 109 | +// } |
| 110 | +// ``` |
| 111 | +impl OverrideMagicMethod { |
| 112 | + fn __pyo3__str__(&self) -> &'static str { |
| 113 | + "default" |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +impl ::pyo3::class::impl_::PyClassDefaultSlots<OverrideMagicMethod> |
| 118 | + for ::pyo3::class::impl_::PyClassImplCollector<OverrideMagicMethod> |
| 119 | +{ |
| 120 | + fn py_class_default_slots(self) -> &'static [::pyo3::ffi::PyType_Slot] { |
| 121 | + &[{ |
| 122 | + unsafe extern "C" fn __wrap( |
| 123 | + _raw_slf: *mut ::pyo3::ffi::PyObject, |
| 124 | + ) -> *mut ::pyo3::ffi::PyObject { |
| 125 | + let _slf = _raw_slf; |
| 126 | + ::pyo3::callback::handle_panic(|_py| { |
| 127 | + let _cell = _py |
| 128 | + .from_borrowed_ptr::<::pyo3::PyAny>(_slf) |
| 129 | + .downcast::<::pyo3::PyCell<OverrideMagicMethod>>()?; |
| 130 | + let _ref = _cell.try_borrow()?; |
| 131 | + let _slf = &_ref; |
| 132 | + ::pyo3::callback::convert(_py, OverrideMagicMethod::__pyo3__str__(_slf)) |
| 133 | + }) |
| 134 | + } |
| 135 | + ::pyo3::ffi::PyType_Slot { |
| 136 | + slot: ::pyo3::ffi::Py_tp_str, |
| 137 | + pfunc: __wrap as ::pyo3::ffi::reprfunc as _, |
| 138 | + } |
| 139 | + }] |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +#[pymethods] |
| 144 | +impl OverrideMagicMethod { |
| 145 | + fn __str__(&self) -> &str { |
| 146 | + "overriden" |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +#[test] |
| 151 | +fn test_override_magic_method() { |
| 152 | + Python::with_gil(|py| { |
| 153 | + let test_object = Py::new(py, OverrideMagicMethod).unwrap(); |
| 154 | + py_assert!(py, test_object, "str(test_object) == 'overriden'"); |
| 155 | + }) |
| 156 | +} |
0 commit comments