2018-05-02 18:49:40 +00:00
|
|
|
use pyo3::prelude::*;
|
2020-08-07 12:31:17 +00:00
|
|
|
use pyo3::types::PyType;
|
|
|
|
use pyo3::{py_run, PyClass};
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-06-15 19:21:12 +00:00
|
|
|
struct EmptyClass {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn empty_class() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<EmptyClass>();
|
|
|
|
// By default, don't allow creating instances from python.
|
2019-02-23 17:01:22 +00:00
|
|
|
assert!(typeobj.call((), None).is_err());
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'EmptyClass'");
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Line1
|
|
|
|
///Line2
|
|
|
|
/// Line3
|
|
|
|
// this is not doc string
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2020-02-03 08:01:30 +00:00
|
|
|
struct ClassWithDocs {
|
|
|
|
/// Property field
|
|
|
|
#[pyo3(get, set)]
|
|
|
|
value: i32,
|
|
|
|
|
|
|
|
/// Read-only property field
|
|
|
|
#[pyo3(get)]
|
|
|
|
readonly: i32,
|
|
|
|
|
|
|
|
/// Write-only property field
|
|
|
|
#[pyo3(set)]
|
|
|
|
writeonly: i32,
|
|
|
|
}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_docstr() {
|
|
|
|
{
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<ClassWithDocs>();
|
2018-06-15 19:21:12 +00:00
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
typeobj,
|
|
|
|
"assert typeobj.__doc__ == 'Line1\\nLine2\\n Line3'"
|
|
|
|
);
|
2020-02-03 08:01:30 +00:00
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
typeobj,
|
|
|
|
"assert typeobj.value.__doc__ == 'Property field'"
|
|
|
|
);
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
typeobj,
|
|
|
|
"assert typeobj.readonly.__doc__ == 'Read-only property field'"
|
|
|
|
);
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
typeobj,
|
|
|
|
"assert typeobj.writeonly.__doc__ == 'Write-only property field'"
|
|
|
|
);
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-12 09:50:25 +00:00
|
|
|
#[pyclass(name = "CustomName")]
|
2018-06-15 19:21:12 +00:00
|
|
|
struct EmptyClass2 {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2019-12-17 17:36:56 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl EmptyClass2 {
|
|
|
|
#[name = "custom_fn"]
|
|
|
|
fn bar(&self) {}
|
|
|
|
|
|
|
|
#[staticmethod]
|
|
|
|
#[name = "custom_static"]
|
|
|
|
fn bar_static() {}
|
|
|
|
}
|
|
|
|
|
2018-05-02 18:49:40 +00:00
|
|
|
#[test]
|
2019-12-17 17:36:56 +00:00
|
|
|
fn custom_names() {
|
2018-05-02 18:49:40 +00:00
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<EmptyClass2>();
|
|
|
|
py_assert!(py, typeobj, "typeobj.__name__ == 'CustomName'");
|
2019-12-17 17:36:56 +00:00
|
|
|
py_assert!(py, typeobj, "typeobj.custom_fn.__name__ == 'custom_fn'");
|
|
|
|
py_assert!(
|
|
|
|
py,
|
|
|
|
typeobj,
|
|
|
|
"typeobj.custom_static.__name__ == 'custom_static'"
|
|
|
|
);
|
|
|
|
py_assert!(py, typeobj, "not hasattr(typeobj, 'bar')");
|
|
|
|
py_assert!(py, typeobj, "not hasattr(typeobj, 'bar_static')");
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 22:14:28 +00:00
|
|
|
#[pyclass]
|
2020-01-27 08:13:55 +00:00
|
|
|
struct RawIdents {
|
|
|
|
#[pyo3(get, set)]
|
|
|
|
r#type: i64,
|
|
|
|
}
|
2019-12-17 22:14:28 +00:00
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl RawIdents {
|
2019-12-17 22:58:34 +00:00
|
|
|
fn r#fn(&self) {}
|
2019-12-17 22:14:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_raw_idents() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let typeobj = py.get_type::<RawIdents>();
|
|
|
|
py_assert!(py, typeobj, "not hasattr(typeobj, 'r#fn')");
|
|
|
|
py_assert!(py, typeobj, "hasattr(typeobj, 'fn')");
|
2020-01-27 08:13:55 +00:00
|
|
|
py_assert!(py, typeobj, "hasattr(typeobj, 'type')");
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
|
|
|
|
2018-07-08 21:33:48 +00:00
|
|
|
#[pyclass]
|
2018-06-15 19:21:12 +00:00
|
|
|
struct EmptyClassInModule {}
|
2018-05-02 18:49:40 +00:00
|
|
|
|
2020-09-01 00:36:26 +00:00
|
|
|
// Ignored because heap types do not show up as being in builtins, instead they
|
|
|
|
// raise AttributeError:
|
|
|
|
// https://github.com/python/cpython/blob/master/Objects/typeobject.c#L544-L573
|
2018-05-02 18:49:40 +00:00
|
|
|
#[test]
|
2020-09-01 00:36:26 +00:00
|
|
|
#[ignore]
|
2018-05-02 18:49:40 +00:00
|
|
|
fn empty_class_in_module() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let module = PyModule::new(py, "test_module.nested").unwrap();
|
|
|
|
module.add_class::<EmptyClassInModule>().unwrap();
|
|
|
|
|
|
|
|
let ty = module.getattr("EmptyClassInModule").unwrap();
|
2018-06-15 19:21:12 +00:00
|
|
|
assert_eq!(
|
|
|
|
ty.getattr("__name__").unwrap().extract::<String>().unwrap(),
|
|
|
|
"EmptyClassInModule"
|
|
|
|
);
|
2019-02-01 16:15:33 +00:00
|
|
|
|
|
|
|
let module: String = ty.getattr("__module__").unwrap().extract().unwrap();
|
|
|
|
|
2019-01-30 15:04:16 +00:00
|
|
|
// Rationale: The class can be added to many modules, but will only be initialized once.
|
|
|
|
// We currently have no way of determining a canonical module, so builtins is better
|
|
|
|
// than using whatever calls init first.
|
2019-03-22 13:07:33 +00:00
|
|
|
assert_eq!(module, "builtins");
|
2018-05-02 18:49:40 +00:00
|
|
|
}
|
2020-02-07 19:31:13 +00:00
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct ClassWithObjectField {
|
2020-05-17 10:45:42 +00:00
|
|
|
// It used to be that PyObject was not supported with (get, set)
|
|
|
|
// - this test is just ensuring it compiles.
|
2020-02-07 19:31:13 +00:00
|
|
|
#[pyo3(get, set)]
|
|
|
|
value: PyObject,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl ClassWithObjectField {
|
|
|
|
#[new]
|
|
|
|
fn new(value: PyObject) -> ClassWithObjectField {
|
|
|
|
Self { value }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn class_with_object_field() {
|
|
|
|
let gil = Python::acquire_gil();
|
|
|
|
let py = gil.python();
|
|
|
|
let ty = py.get_type::<ClassWithObjectField>();
|
|
|
|
py_assert!(py, ty, "ty(5).value == 5");
|
|
|
|
py_assert!(py, ty, "ty(None).value == None");
|
|
|
|
}
|
2020-06-29 03:05:50 +00:00
|
|
|
|
2020-09-01 00:36:26 +00:00
|
|
|
#[pyclass(unsendable, subclass)]
|
2020-06-29 03:05:50 +00:00
|
|
|
struct UnsendableBase {
|
2020-08-07 12:31:17 +00:00
|
|
|
value: std::rc::Rc<usize>,
|
2020-06-29 03:05:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl UnsendableBase {
|
2020-08-07 12:31:17 +00:00
|
|
|
#[new]
|
|
|
|
fn new(value: usize) -> UnsendableBase {
|
|
|
|
Self {
|
|
|
|
value: std::rc::Rc::new(value),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[getter]
|
2020-06-29 03:05:50 +00:00
|
|
|
fn value(&self) -> usize {
|
2020-08-07 12:31:17 +00:00
|
|
|
*self.value
|
2020-06-29 03:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass(extends=UnsendableBase)]
|
|
|
|
struct UnsendableChild {}
|
|
|
|
|
2020-08-07 12:31:17 +00:00
|
|
|
#[pymethods]
|
|
|
|
impl UnsendableChild {
|
|
|
|
#[new]
|
|
|
|
fn new(value: usize) -> (UnsendableChild, UnsendableBase) {
|
|
|
|
(UnsendableChild {}, UnsendableBase::new(value))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn test_unsendable<T: PyClass + 'static>() -> PyResult<()> {
|
|
|
|
let obj = std::thread::spawn(|| -> PyResult<_> {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let obj: Py<T> = PyType::new::<T>(py).call1((5,))?.extract()?;
|
|
|
|
|
|
|
|
// Accessing the value inside this thread should not panic
|
|
|
|
let caught_panic =
|
|
|
|
std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| -> PyResult<_> {
|
|
|
|
assert_eq!(obj.as_ref(py).getattr("value")?.extract::<usize>()?, 5);
|
|
|
|
Ok(())
|
|
|
|
}))
|
|
|
|
.is_err();
|
|
|
|
|
|
|
|
assert_eq!(caught_panic, false);
|
|
|
|
Ok(obj)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
.join()
|
|
|
|
.unwrap()?;
|
|
|
|
|
|
|
|
// This access must panic
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
obj.borrow(py);
|
|
|
|
});
|
|
|
|
|
|
|
|
panic!("Borrowing unsendable from receiving thread did not panic.");
|
|
|
|
}
|
|
|
|
|
2020-06-29 03:05:50 +00:00
|
|
|
/// If a class is marked as `unsendable`, it panics when accessed by another thread.
|
|
|
|
#[test]
|
2020-08-07 12:31:17 +00:00
|
|
|
#[should_panic(
|
|
|
|
expected = "test_class_basics::UnsendableBase is unsendable, but sent to another thread!"
|
|
|
|
)]
|
|
|
|
fn panic_unsendable_base() {
|
|
|
|
test_unsendable::<UnsendableBase>().unwrap();
|
|
|
|
}
|
2020-06-30 08:58:31 +00:00
|
|
|
|
2020-08-07 12:31:17 +00:00
|
|
|
#[test]
|
|
|
|
#[should_panic(
|
|
|
|
expected = "test_class_basics::UnsendableBase is unsendable, but sent to another thread!"
|
|
|
|
)]
|
|
|
|
fn panic_unsendable_child() {
|
|
|
|
test_unsendable::<UnsendableChild>().unwrap();
|
2020-06-29 03:05:50 +00:00
|
|
|
}
|
2021-02-20 15:15:20 +00:00
|
|
|
|
|
|
|
fn get_length(obj: &PyAny) -> PyResult<usize> {
|
|
|
|
let length = obj.len()?;
|
|
|
|
|
|
|
|
Ok(length)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct ClassWithFromPyWithMethods {}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl ClassWithFromPyWithMethods {
|
|
|
|
fn instance_method(&self, #[pyo3(from_py_with = "get_length")] argument: usize) -> usize {
|
|
|
|
argument
|
|
|
|
}
|
|
|
|
#[classmethod]
|
|
|
|
fn classmethod(_cls: &PyType, #[pyo3(from_py_with = "PyAny::len")] argument: usize) -> usize {
|
|
|
|
argument
|
|
|
|
}
|
|
|
|
|
|
|
|
#[staticmethod]
|
|
|
|
fn staticmethod(#[pyo3(from_py_with = "get_length")] argument: usize) -> usize {
|
|
|
|
argument
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_pymethods_from_py_with() {
|
|
|
|
Python::with_gil(|py| {
|
|
|
|
let instance = Py::new(py, ClassWithFromPyWithMethods {}).unwrap();
|
|
|
|
|
|
|
|
py_run!(
|
|
|
|
py,
|
|
|
|
instance,
|
|
|
|
r#"
|
|
|
|
arg = {1: 1, 2: 3}
|
|
|
|
|
|
|
|
assert instance.instance_method(arg) == 2
|
|
|
|
assert instance.classmethod(arg) == 2
|
|
|
|
assert instance.staticmethod(arg) == 2
|
|
|
|
"#
|
|
|
|
);
|
|
|
|
})
|
|
|
|
}
|