pyo3/tests/test_field_cfg.rs
Lily Foote 0dd568d397
Use the new bound API instead of .as_ref(py) (#3853)
* Use the new bound API instead of .as_ref(py)

* Move import into a nested scope

* Use to_cow instead of to_str for compatibility

`to_str` is not available before Python 3.10 on the limited api.

* Relax &self lifetimes

* Use Bound<'py, PyAny> in test Mapping signatures

* Use .as_bytes(py)

* Simplify ThrowCallback::throw signature

* Avoid .as_any call with Py api instead of Bound
2024-02-18 00:09:56 +00:00

36 lines
730 B
Rust

#![cfg(feature = "macros")]
use pyo3::prelude::*;
#[pyclass]
struct CfgClass {
#[pyo3(get, set)]
#[cfg(any())]
pub a: u32,
#[pyo3(get, set)]
// This is always true
#[cfg(any(
target_family = "unix",
target_family = "windows",
target_family = "wasm"
))]
pub b: u32,
}
#[test]
fn test_cfg() {
Python::with_gil(|py| {
let cfg = CfgClass { b: 3 };
let py_cfg = Py::new(py, cfg).unwrap();
assert!(py_cfg.bind(py).as_any().getattr("a").is_err());
let b: u32 = py_cfg
.bind(py)
.as_any()
.getattr("b")
.unwrap()
.extract()
.unwrap();
assert_eq!(b, 3);
});
}