2021-12-03 00:03:32 +00:00
|
|
|
#![cfg(feature = "macros")]
|
|
|
|
|
2023-02-18 15:33:07 +00:00
|
|
|
use pyo3::prelude::*;
|
2020-05-05 19:54:09 +00:00
|
|
|
|
2023-09-24 12:34:53 +00:00
|
|
|
#[path = "../src/tests/common.rs"]
|
2020-05-05 19:54:09 +00:00
|
|
|
mod common;
|
|
|
|
|
|
|
|
#[pyclass]
|
2020-05-07 18:13:10 +00:00
|
|
|
struct Foo {
|
|
|
|
#[pyo3(get)]
|
|
|
|
x: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pyclass]
|
|
|
|
struct Bar {
|
|
|
|
#[pyo3(get)]
|
|
|
|
x: i32,
|
|
|
|
}
|
2020-05-05 19:54:09 +00:00
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl Foo {
|
2020-05-07 19:20:34 +00:00
|
|
|
#[classattr]
|
|
|
|
const MY_CONST: &'static str = "foobar";
|
|
|
|
|
2021-04-17 21:22:06 +00:00
|
|
|
#[classattr]
|
|
|
|
#[pyo3(name = "RENAMED_CONST")]
|
|
|
|
const MY_CONST_2: &'static str = "foobar_2";
|
|
|
|
|
2020-05-05 19:54:09 +00:00
|
|
|
#[classattr]
|
|
|
|
fn a() -> i32 {
|
|
|
|
5
|
|
|
|
}
|
|
|
|
|
|
|
|
#[classattr]
|
2021-04-17 21:22:06 +00:00
|
|
|
#[pyo3(name = "B")]
|
2020-05-05 19:54:09 +00:00
|
|
|
fn b() -> String {
|
|
|
|
"bar".to_string()
|
|
|
|
}
|
2020-05-07 18:13:10 +00:00
|
|
|
|
|
|
|
#[classattr]
|
|
|
|
fn bar() -> Bar {
|
|
|
|
Bar { x: 2 }
|
|
|
|
}
|
2020-06-22 21:13:23 +00:00
|
|
|
|
|
|
|
#[classattr]
|
2021-08-17 06:51:08 +00:00
|
|
|
fn a_foo() -> Foo {
|
2020-06-22 21:13:23 +00:00
|
|
|
Foo { x: 1 }
|
|
|
|
}
|
2022-06-16 10:08:53 +00:00
|
|
|
|
|
|
|
#[classattr]
|
|
|
|
fn a_foo_with_py(py: Python<'_>) -> Py<Foo> {
|
|
|
|
Py::new(py, Foo { x: 1 }).unwrap()
|
|
|
|
}
|
2020-05-07 18:13:10 +00:00
|
|
|
}
|
|
|
|
|
2020-05-05 19:54:09 +00:00
|
|
|
#[test]
|
|
|
|
fn class_attributes() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let foo_obj = py.get_type_bound::<Foo>();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(py, foo_obj, "foo_obj.MY_CONST == 'foobar'");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.RENAMED_CONST == 'foobar_2'");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.a == 5");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.B == 'bar'");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.a_foo.x == 1");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.a_foo_with_py.x == 1");
|
|
|
|
});
|
2020-05-05 19:54:09 +00:00
|
|
|
}
|
2020-05-06 18:11:29 +00:00
|
|
|
|
2020-09-01 00:36:26 +00:00
|
|
|
// Ignored because heap types are not immutable:
|
|
|
|
// https://github.com/python/cpython/blob/master/Objects/typeobject.c#L3399-L3409
|
2020-05-06 18:11:29 +00:00
|
|
|
#[test]
|
2020-09-01 00:36:26 +00:00
|
|
|
#[ignore]
|
2020-05-06 18:11:29 +00:00
|
|
|
fn class_attributes_are_immutable() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let foo_obj = py.get_type_bound::<Foo>();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_expect_exception!(py, foo_obj, "foo_obj.a = 6", PyTypeError);
|
|
|
|
});
|
2020-05-06 18:11:29 +00:00
|
|
|
}
|
2020-05-07 18:13:10 +00:00
|
|
|
|
2020-06-14 15:29:40 +00:00
|
|
|
#[pymethods]
|
2020-06-22 21:13:23 +00:00
|
|
|
impl Bar {
|
2020-06-14 15:29:40 +00:00
|
|
|
#[classattr]
|
2021-08-17 06:51:08 +00:00
|
|
|
fn a_foo() -> Foo {
|
2020-06-22 21:13:23 +00:00
|
|
|
Foo { x: 3 }
|
|
|
|
}
|
2020-06-14 15:29:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-07 18:13:10 +00:00
|
|
|
#[test]
|
|
|
|
fn recursive_class_attributes() {
|
2022-07-19 17:34:23 +00:00
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let foo_obj = py.get_type_bound::<Foo>();
|
|
|
|
let bar_obj = py.get_type_bound::<Bar>();
|
2022-07-19 17:34:23 +00:00
|
|
|
py_assert!(py, foo_obj, "foo_obj.a_foo.x == 1");
|
|
|
|
py_assert!(py, foo_obj, "foo_obj.bar.x == 2");
|
|
|
|
py_assert!(py, bar_obj, "bar_obj.a_foo.x == 3");
|
|
|
|
});
|
2020-05-07 18:13:10 +00:00
|
|
|
}
|
2022-05-17 18:06:09 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_fallible_class_attribute() {
|
2023-02-18 15:33:07 +00:00
|
|
|
use pyo3::{exceptions::PyValueError, types::PyString};
|
|
|
|
|
|
|
|
struct CaptureStdErr<'py> {
|
2024-02-14 00:24:37 +00:00
|
|
|
oldstderr: Bound<'py, PyAny>,
|
|
|
|
string_io: Bound<'py, PyAny>,
|
2023-02-18 15:33:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'py> CaptureStdErr<'py> {
|
|
|
|
fn new(py: Python<'py>) -> PyResult<Self> {
|
2024-02-14 00:24:37 +00:00
|
|
|
let sys = py.import_bound("sys")?;
|
2023-02-18 15:33:07 +00:00
|
|
|
let oldstderr = sys.getattr("stderr")?;
|
2024-02-14 00:24:37 +00:00
|
|
|
let string_io = py.import_bound("io")?.getattr("StringIO")?.call0()?;
|
|
|
|
sys.setattr("stderr", &string_io)?;
|
2023-02-18 15:33:07 +00:00
|
|
|
Ok(Self {
|
|
|
|
oldstderr,
|
|
|
|
string_io,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn reset(self) -> PyResult<String> {
|
|
|
|
let py = self.string_io.py();
|
|
|
|
let payload = self
|
|
|
|
.string_io
|
|
|
|
.getattr("getvalue")?
|
|
|
|
.call0()?
|
|
|
|
.downcast::<PyString>()?
|
2024-02-14 00:24:37 +00:00
|
|
|
.to_cow()?
|
|
|
|
.into_owned();
|
|
|
|
let sys = py.import_bound("sys")?;
|
2023-02-18 15:33:07 +00:00
|
|
|
sys.setattr("stderr", self.oldstderr)?;
|
|
|
|
Ok(payload)
|
|
|
|
}
|
|
|
|
}
|
2023-02-14 08:21:04 +00:00
|
|
|
|
2022-05-17 18:06:09 +00:00
|
|
|
#[pyclass]
|
|
|
|
struct BrokenClass;
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl BrokenClass {
|
|
|
|
#[classattr]
|
|
|
|
fn fails_to_init() -> PyResult<i32> {
|
|
|
|
Err(PyValueError::new_err("failed to create class attribute"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2023-02-11 20:51:52 +00:00
|
|
|
let stderr = CaptureStdErr::new(py).unwrap();
|
2024-02-18 18:27:19 +00:00
|
|
|
assert!(std::panic::catch_unwind(|| py.get_type_bound::<BrokenClass>()).is_err());
|
2023-02-11 20:51:52 +00:00
|
|
|
assert_eq!(
|
|
|
|
stderr.reset().unwrap().trim(),
|
|
|
|
"\
|
|
|
|
ValueError: failed to create class attribute
|
|
|
|
|
|
|
|
The above exception was the direct cause of the following exception:
|
|
|
|
|
|
|
|
RuntimeError: An error occurred while initializing `BrokenClass.fails_to_init`
|
|
|
|
|
|
|
|
The above exception was the direct cause of the following exception:
|
|
|
|
|
|
|
|
RuntimeError: An error occurred while initializing class BrokenClass"
|
|
|
|
)
|
2023-02-18 15:33:07 +00:00
|
|
|
});
|
2023-02-11 20:51:52 +00:00
|
|
|
}
|
2023-08-14 21:29:44 +00:00
|
|
|
|
|
|
|
#[pyclass(get_all, set_all, rename_all = "camelCase")]
|
|
|
|
struct StructWithRenamedFields {
|
|
|
|
first_field: bool,
|
|
|
|
second_field: u8,
|
|
|
|
#[pyo3(name = "third_field")]
|
|
|
|
fourth_field: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[pymethods]
|
|
|
|
impl StructWithRenamedFields {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self {
|
|
|
|
first_field: true,
|
|
|
|
second_field: 5,
|
|
|
|
fourth_field: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_renaming_all_struct_fields() {
|
|
|
|
use pyo3::types::PyBool;
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let struct_class = py.get_type_bound::<StructWithRenamedFields>();
|
2023-08-14 21:29:44 +00:00
|
|
|
let struct_obj = struct_class.call0().unwrap();
|
|
|
|
assert!(struct_obj
|
2024-01-30 20:55:22 +00:00
|
|
|
.setattr("firstField", PyBool::new_bound(py, false))
|
2023-08-14 21:29:44 +00:00
|
|
|
.is_ok());
|
|
|
|
py_assert!(py, struct_obj, "struct_obj.firstField == False");
|
|
|
|
py_assert!(py, struct_obj, "struct_obj.secondField == 5");
|
|
|
|
assert!(struct_obj
|
2024-01-30 20:55:22 +00:00
|
|
|
.setattr("third_field", PyBool::new_bound(py, true))
|
2023-08-14 21:29:44 +00:00
|
|
|
.is_ok());
|
|
|
|
py_assert!(py, struct_obj, "struct_obj.third_field == True");
|
|
|
|
});
|
|
|
|
}
|
2023-08-16 16:24:19 +00:00
|
|
|
|
|
|
|
macro_rules! test_case {
|
|
|
|
($struct_name: ident, $rule: literal, $field_name: ident, $renamed_field_name: literal, $test_name: ident) => {
|
|
|
|
#[pyclass(get_all, set_all, rename_all = $rule)]
|
|
|
|
#[allow(non_snake_case)]
|
|
|
|
struct $struct_name {
|
|
|
|
$field_name: u8,
|
|
|
|
}
|
|
|
|
#[pymethods]
|
|
|
|
impl $struct_name {
|
|
|
|
#[new]
|
|
|
|
fn new() -> Self {
|
|
|
|
Self { $field_name: 0 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#[test]
|
|
|
|
fn $test_name() {
|
|
|
|
//use pyo3::types::PyInt;
|
|
|
|
|
|
|
|
Python::with_gil(|py| {
|
2024-02-18 18:27:19 +00:00
|
|
|
let struct_class = py.get_type_bound::<$struct_name>();
|
2023-08-16 16:24:19 +00:00
|
|
|
let struct_obj = struct_class.call0().unwrap();
|
|
|
|
assert!(struct_obj.setattr($renamed_field_name, 2).is_ok());
|
|
|
|
let attr = struct_obj.getattr($renamed_field_name).unwrap();
|
2024-02-18 18:27:19 +00:00
|
|
|
assert_eq!(2, attr.extract().unwrap());
|
2023-08-16 16:24:19 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
test_case!(
|
|
|
|
LowercaseTest,
|
|
|
|
"lowercase",
|
|
|
|
fieldOne,
|
|
|
|
"fieldone",
|
|
|
|
test_rename_all_lowercase
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
CamelCaseTest,
|
|
|
|
"camelCase",
|
|
|
|
field_one,
|
|
|
|
"fieldOne",
|
|
|
|
test_rename_all_camel_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
KebabCaseTest,
|
|
|
|
"kebab-case",
|
|
|
|
field_one,
|
|
|
|
"field-one",
|
|
|
|
test_rename_all_kebab_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
PascalCaseTest,
|
|
|
|
"PascalCase",
|
|
|
|
field_one,
|
|
|
|
"FieldOne",
|
|
|
|
test_rename_all_pascal_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
ScreamingSnakeCaseTest,
|
|
|
|
"SCREAMING_SNAKE_CASE",
|
|
|
|
field_one,
|
|
|
|
"FIELD_ONE",
|
|
|
|
test_rename_all_screaming_snake_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
ScreamingKebabCaseTest,
|
|
|
|
"SCREAMING-KEBAB-CASE",
|
|
|
|
field_one,
|
|
|
|
"FIELD-ONE",
|
|
|
|
test_rename_all_screaming_kebab_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
SnakeCaseTest,
|
|
|
|
"snake_case",
|
|
|
|
fieldOne,
|
|
|
|
"field_one",
|
|
|
|
test_rename_all_snake_case
|
|
|
|
);
|
|
|
|
test_case!(
|
|
|
|
UppercaseTest,
|
|
|
|
"UPPERCASE",
|
|
|
|
fieldOne,
|
|
|
|
"FIELDONE",
|
|
|
|
test_rename_all_uppercase
|
|
|
|
);
|