pyo3/tests/test_proc_macro_hygiene.rs

138 lines
3.1 KiB
Rust
Raw Normal View History

2021-08-16 14:32:11 +00:00
#![no_implicit_prelude]
2021-09-14 12:07:23 +00:00
#[derive(::pyo3::FromPyObject)]
struct Derive1(i32); // newtype case
2021-09-14 12:07:23 +00:00
#[derive(::pyo3::FromPyObject)]
#[allow(dead_code)]
struct Derive2(i32, i32); // tuple case
2021-09-14 12:07:23 +00:00
#[derive(::pyo3::FromPyObject)]
#[allow(dead_code)]
struct Derive3 {
f: i32,
g: i32,
} // struct case
2021-09-14 12:07:23 +00:00
#[derive(::pyo3::FromPyObject)]
#[allow(dead_code)]
enum Derive4 {
A(i32),
B { f: i32 },
} // enum case
2021-09-13 20:58:15 +00:00
#[::pyo3::pyclass]
2021-08-16 14:32:11 +00:00
#[derive(::std::clone::Clone)]
pub struct Foo;
2021-09-13 20:58:15 +00:00
#[::pyo3::pyclass]
2021-08-16 14:32:11 +00:00
pub struct Foo2;
2021-09-13 20:58:15 +00:00
#[::pyo3::pyclass(
2021-08-16 14:32:11 +00:00
name = "ActuallyBar",
freelist = 8,
weakref,
unsendable,
gc,
subclass,
2021-08-17 13:19:19 +00:00
extends = ::pyo3::types::PyAny,
2021-08-16 14:32:11 +00:00
module = "Spam"
)]
pub struct Bar {
#[pyo3(get, set)]
a: u8,
#[pyo3(get, set)]
b: Foo,
#[pyo3(get, set)]
c: ::std::option::Option<::pyo3::Py<Foo2>>,
}
2021-09-13 20:58:15 +00:00
#[::pyo3::pymethods]
impl Bar {
#[args(x = "1", "*", _z = "2")]
fn test(&self, _y: &Bar, _z: i32) {}
#[staticmethod]
fn staticmethod() {}
#[classmethod]
fn clsmethod(_: &::pyo3::types::PyType) {}
#[call]
#[args(args = "*", kwds = "**")]
fn __call__(
&self,
_args: &::pyo3::types::PyTuple,
_kwds: ::std::option::Option<&::pyo3::types::PyDict>,
) -> ::pyo3::PyResult<i32> {
::std::panic!("unimplemented isn't hygienic before 1.50")
}
#[new]
fn new(a: u8) -> Self {
Bar {
a,
b: Foo,
c: ::std::option::Option::None,
}
}
#[getter]
fn get(&self) -> i32 {
0
}
#[setter]
fn set(&self, _v: i32) {}
#[classattr]
fn class_attr() -> i32 {
0
}
}
2021-09-13 20:58:15 +00:00
#[::pyo3::pyproto]
2021-08-16 14:32:11 +00:00
impl ::pyo3::class::gc::PyGCProtocol for Bar {
fn __traverse__(
&self,
visit: ::pyo3::class::gc::PyVisit,
) -> ::std::result::Result<(), ::pyo3::class::gc::PyTraverseError> {
if let ::std::option::Option::Some(obj) = &self.c {
visit.call(obj)?
}
::std::result::Result::Ok(())
}
fn __clear__(&mut self) {
self.c = ::std::option::Option::None;
}
}
#[cfg(not(Py_LIMITED_API))]
2021-09-13 20:58:15 +00:00
#[::pyo3::pyproto]
impl ::pyo3::class::PyBufferProtocol for Bar {
fn bf_getbuffer(
_s: ::pyo3::PyRefMut<Self>,
_v: *mut ::pyo3::ffi::Py_buffer,
_f: ::std::os::raw::c_int,
) -> ::pyo3::PyResult<()> {
::std::panic!("unimplemented isn't hygienic before 1.50")
}
fn bf_releasebuffer(_s: ::pyo3::PyRefMut<Self>, _v: *mut ::pyo3::ffi::Py_buffer) {}
}
2021-09-13 20:58:15 +00:00
#[::pyo3::pyfunction]
fn do_something(x: i32) -> ::pyo3::PyResult<i32> {
::std::result::Result::Ok(x)
}
2021-09-13 20:58:15 +00:00
#[::pyo3::pymodule]
fn my_module(_py: ::pyo3::Python, m: &::pyo3::types::PyModule) -> ::pyo3::PyResult<()> {
m.add_function(::pyo3::wrap_pyfunction!(do_something, m)?)?;
::std::result::Result::Ok(())
}
#[test]
fn invoke_wrap_pyfunction() {
::pyo3::Python::with_gil(|py| {
let func = ::pyo3::wrap_pyfunction!(do_something)(py).unwrap();
::pyo3::py_run!(py, func, r#"func(5)"#);
});
}
::pyo3::create_exception!(mymodule, CustomError, ::pyo3::exceptions::PyException);
::pyo3::import_exception!(socket, gaierror);