2021-04-17 21:22:06 +00:00
|
|
|
#![deny(deprecated)]
|
2024-03-15 10:25:27 +00:00
|
|
|
#![allow(dead_code)]
|
2021-04-17 21:22:06 +00:00
|
|
|
|
|
|
|
use pyo3::prelude::*;
|
2024-03-09 09:52:12 +00:00
|
|
|
use pyo3::types::{PyString, PyType};
|
2021-04-17 21:22:06 +00:00
|
|
|
|
2022-10-25 06:23:21 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct MyClass;
|
2021-04-17 21:22:06 +00:00
|
|
|
|
2023-10-05 22:59:03 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl MyClass {
|
|
|
|
#[__new__]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self
|
|
|
|
}
|
2024-03-09 09:52:12 +00:00
|
|
|
|
|
|
|
#[classmethod]
|
|
|
|
fn cls_method_gil_ref(_cls: &PyType) {}
|
|
|
|
|
|
|
|
#[classmethod]
|
|
|
|
fn cls_method_bound(_cls: &Bound<'_, PyType>) {}
|
|
|
|
|
|
|
|
fn method_gil_ref(_slf: &PyCell<Self>) {}
|
|
|
|
|
|
|
|
fn method_bound(_slf: &Bound<'_, Self>) {}
|
2024-03-20 22:35:08 +00:00
|
|
|
|
|
|
|
#[staticmethod]
|
|
|
|
fn static_method_gil_ref(_any: &PyAny) {}
|
2024-03-26 18:53:11 +00:00
|
|
|
|
|
|
|
#[setter]
|
|
|
|
fn set_foo_gil_ref(&self, #[pyo3(from_py_with = "extract_gil_ref")] _value: i32) {}
|
|
|
|
|
|
|
|
#[setter]
|
|
|
|
fn set_foo_bound(&self, #[pyo3(from_py_with = "extract_bound")] _value: i32) {}
|
2024-03-27 15:41:04 +00:00
|
|
|
|
|
|
|
#[setter]
|
|
|
|
fn set_bar_gil_ref(&self, _value: &PyAny) {}
|
|
|
|
|
|
|
|
#[setter]
|
|
|
|
fn set_bar_bound(&self, _value: &Bound<'_, PyAny>) {}
|
2024-05-04 07:39:40 +00:00
|
|
|
|
2024-05-10 10:34:58 +00:00
|
|
|
#[setter]
|
|
|
|
fn set_option(&self, _value: Option<i32>) {}
|
|
|
|
|
2024-05-04 07:39:40 +00:00
|
|
|
fn __eq__(&self, #[pyo3(from_py_with = "extract_gil_ref")] _other: i32) -> bool {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn __contains__(&self, #[pyo3(from_py_with = "extract_bound")] _value: i32) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2023-10-05 22:59:03 +00:00
|
|
|
}
|
|
|
|
|
2023-06-13 07:19:43 +00:00
|
|
|
fn main() {}
|
2024-03-08 00:28:11 +00:00
|
|
|
|
2024-03-09 09:52:12 +00:00
|
|
|
#[pyfunction]
|
|
|
|
#[pyo3(pass_module)]
|
|
|
|
fn pyfunction_with_module<'py>(module: &Bound<'py, PyModule>) -> PyResult<Bound<'py, PyString>> {
|
|
|
|
module.name()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
#[pyo3(pass_module)]
|
2024-05-09 07:58:44 +00:00
|
|
|
fn pyfunction_with_module_gil_ref(_module: &PyModule) -> PyResult<&str> {
|
|
|
|
todo!()
|
2024-03-09 09:52:12 +00:00
|
|
|
}
|
|
|
|
|
2024-03-08 00:28:11 +00:00
|
|
|
#[pyfunction]
|
|
|
|
fn double(x: usize) -> usize {
|
|
|
|
x * 2
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2024-05-09 07:58:44 +00:00
|
|
|
fn module_gil_ref(_m: &PyModule) -> PyResult<()> {
|
2024-03-08 00:28:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
2024-05-09 07:58:44 +00:00
|
|
|
fn module_gil_ref_with_explicit_py_arg(_py: Python<'_>, _m: &PyModule) -> PyResult<()> {
|
2024-03-08 00:28:11 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
|
|
|
fn module_bound(m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
|
m.add_function(wrap_pyfunction!(double, m)?)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
|
|
|
fn module_bound_with_explicit_py_arg(_py: Python<'_>, m: &Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
|
m.add_function(wrap_pyfunction!(double, m)?)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymodule]
|
|
|
|
fn module_bound_by_value(m: Bound<'_, PyModule>) -> PyResult<()> {
|
|
|
|
m.add_function(wrap_pyfunction!(double, &m)?)?;
|
|
|
|
Ok(())
|
|
|
|
}
|
2024-03-12 22:57:03 +00:00
|
|
|
|
2024-03-19 08:58:41 +00:00
|
|
|
fn extract_gil_ref(obj: &PyAny) -> PyResult<i32> {
|
|
|
|
obj.extract()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extract_bound(obj: &Bound<'_, PyAny>) -> PyResult<i32> {
|
|
|
|
obj.extract()
|
|
|
|
}
|
|
|
|
|
2024-05-10 10:34:58 +00:00
|
|
|
fn extract_options(obj: &Bound<'_, PyAny>) -> PyResult<Option<i32>> {
|
|
|
|
obj.extract()
|
|
|
|
}
|
|
|
|
|
2024-03-19 08:58:41 +00:00
|
|
|
#[pyfunction]
|
|
|
|
fn pyfunction_from_py_with(
|
|
|
|
#[pyo3(from_py_with = "extract_gil_ref")] _gil_ref: i32,
|
|
|
|
#[pyo3(from_py_with = "extract_bound")] _bound: i32,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-03-20 22:35:08 +00:00
|
|
|
#[pyfunction]
|
|
|
|
fn pyfunction_gil_ref(_any: &PyAny) {}
|
|
|
|
|
2024-03-21 07:24:40 +00:00
|
|
|
#[pyfunction]
|
2024-05-10 10:34:58 +00:00
|
|
|
#[pyo3(signature = (_any))]
|
2024-03-21 07:24:40 +00:00
|
|
|
fn pyfunction_option_gil_ref(_any: Option<&PyAny>) {}
|
|
|
|
|
2024-05-10 10:34:58 +00:00
|
|
|
#[pyfunction]
|
|
|
|
#[pyo3(signature = (_i, _any=None))]
|
|
|
|
fn pyfunction_option_1(_i: u32, _any: Option<i32>) {}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn pyfunction_option_2(_i: u32, _any: Option<i32>) {}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn pyfunction_option_3(_i: u32, _any: Option<i32>, _foo: Option<String>) {}
|
|
|
|
|
|
|
|
#[pyfunction]
|
|
|
|
fn pyfunction_option_4(
|
|
|
|
_i: u32,
|
|
|
|
#[pyo3(from_py_with = "extract_options")] _any: Option<i32>,
|
|
|
|
_foo: Option<String>,
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
|
2024-03-20 09:27:38 +00:00
|
|
|
#[derive(Debug, FromPyObject)]
|
|
|
|
pub struct Zap {
|
|
|
|
#[pyo3(item)]
|
|
|
|
name: String,
|
|
|
|
|
|
|
|
#[pyo3(from_py_with = "PyAny::len", item("my_object"))]
|
|
|
|
some_object_length: usize,
|
|
|
|
|
|
|
|
#[pyo3(from_py_with = "extract_bound")]
|
|
|
|
some_number: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, FromPyObject)]
|
|
|
|
pub struct ZapTuple(
|
|
|
|
String,
|
|
|
|
#[pyo3(from_py_with = "PyAny::len")] usize,
|
|
|
|
#[pyo3(from_py_with = "extract_bound")] i32,
|
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Debug, FromPyObject, PartialEq, Eq)]
|
|
|
|
pub enum ZapEnum {
|
|
|
|
Zip(#[pyo3(from_py_with = "extract_gil_ref")] i32),
|
|
|
|
Zap(String, #[pyo3(from_py_with = "extract_bound")] i32),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, FromPyObject, PartialEq, Eq)]
|
|
|
|
#[pyo3(transparent)]
|
|
|
|
pub struct TransparentFromPyWithGilRef {
|
|
|
|
#[pyo3(from_py_with = "extract_gil_ref")]
|
|
|
|
len: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, FromPyObject, PartialEq, Eq)]
|
|
|
|
#[pyo3(transparent)]
|
|
|
|
pub struct TransparentFromPyWithBound {
|
|
|
|
#[pyo3(from_py_with = "extract_bound")]
|
|
|
|
len: i32,
|
|
|
|
}
|
|
|
|
|
2024-03-12 22:57:03 +00:00
|
|
|
fn test_wrap_pyfunction(py: Python<'_>, m: &Bound<'_, PyModule>) {
|
|
|
|
// should lint
|
|
|
|
let _ = wrap_pyfunction!(double, py);
|
|
|
|
|
|
|
|
// should lint but currently does not
|
|
|
|
let _ = wrap_pyfunction!(double)(py);
|
|
|
|
|
|
|
|
// should not lint
|
|
|
|
let _ = wrap_pyfunction!(double, m);
|
|
|
|
let _ = wrap_pyfunction!(double)(m);
|
|
|
|
let _ = wrap_pyfunction!(double, m.as_gil_ref());
|
|
|
|
let _ = wrap_pyfunction!(double)(m.as_gil_ref());
|
|
|
|
let _ = wrap_pyfunction_bound!(double, py);
|
|
|
|
let _ = wrap_pyfunction_bound!(double)(py);
|
|
|
|
}
|