2021-08-16 14:32:11 +00:00
|
|
|
#![no_implicit_prelude]
|
|
|
|
|
2021-08-28 14:02:43 +00:00
|
|
|
#[derive(::pyo3::prelude::FromPyObject)]
|
|
|
|
struct Derive1(i32); // newtype case
|
|
|
|
|
|
|
|
#[derive(::pyo3::prelude::FromPyObject)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct Derive2(i32, i32); // tuple case
|
|
|
|
|
|
|
|
#[derive(::pyo3::prelude::FromPyObject)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
struct Derive3 {
|
|
|
|
f: i32,
|
|
|
|
g: i32,
|
|
|
|
} // struct case
|
|
|
|
|
|
|
|
#[derive(::pyo3::prelude::FromPyObject)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
enum Derive4 {
|
|
|
|
A(i32),
|
|
|
|
B { f: i32 },
|
|
|
|
} // enum case
|
|
|
|
|
2021-08-16 14:32:11 +00:00
|
|
|
#[::pyo3::proc_macro::pyclass]
|
|
|
|
#[derive(::std::clone::Clone)]
|
|
|
|
pub struct Foo;
|
|
|
|
|
|
|
|
#[::pyo3::proc_macro::pyclass]
|
|
|
|
pub struct Foo2;
|
|
|
|
|
|
|
|
#[::pyo3::proc_macro::pyclass(
|
|
|
|
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-08-28 14:40:30 +00:00
|
|
|
#[::pyo3::proc_macro::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> {
|
2021-08-29 06:30:05 +00:00
|
|
|
::std::panic!("unimplemented isn't hygienic before 1.50")
|
2021-08-28 14:40:30 +00:00
|
|
|
}
|
|
|
|
#[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-08-16 14:32:11 +00:00
|
|
|
#[::pyo3::proc_macro::pyproto]
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
2021-08-27 22:22:09 +00:00
|
|
|
|
2021-08-28 14:40:30 +00:00
|
|
|
#[cfg(not(Py_LIMITED_API))]
|
|
|
|
#[::pyo3::proc_macro::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<()> {
|
2021-08-29 06:30:05 +00:00
|
|
|
::std::panic!("unimplemented isn't hygienic before 1.50")
|
2021-08-28 14:40:30 +00:00
|
|
|
}
|
|
|
|
fn bf_releasebuffer(_s: ::pyo3::PyRefMut<Self>, _v: *mut ::pyo3::ffi::Py_buffer) {}
|
|
|
|
}
|
|
|
|
|
2021-08-27 22:22:09 +00:00
|
|
|
#[::pyo3::proc_macro::pyfunction]
|
|
|
|
fn do_something(x: i32) -> ::pyo3::PyResult<i32> {
|
|
|
|
::std::result::Result::Ok(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[::pyo3::proc_macro::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)"#);
|
|
|
|
});
|
|
|
|
}
|
2021-08-28 13:45:49 +00:00
|
|
|
|
|
|
|
::pyo3::create_exception!(mymodule, CustomError, ::pyo3::exceptions::PyException);
|
|
|
|
::pyo3::import_exception!(socket, gaierror);
|