Skip to content

Commit d265254

Browse files
authored
ci: tidy up testing for send / sync w. probes (#5308)
* ci: tidy up testing for send / sync w. probes * simplify value_of macro
1 parent 4671c90 commit d265254

File tree

8 files changed

+37
-21
lines changed

8 files changed

+37
-21
lines changed

pyo3-macros-backend/src/pyclass.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ fn impl_complex_enum_struct_variant_cls(
12291229
let field_getter_impl = quote! {
12301230
fn #field_name(slf: #pyo3_path::PyClassGuard<'_, Self>, py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> {
12311231
#[allow(unused_imports)]
1232-
use #pyo3_path::impl_::pyclass::Probe;
1232+
use #pyo3_path::impl_::pyclass::Probe as _;
12331233
match &*slf.into_super() {
12341234
#enum_name::#variant_ident { #field_name, .. } =>
12351235
#pyo3_path::impl_::pyclass::ConvertField::<
@@ -1305,7 +1305,7 @@ fn impl_complex_enum_tuple_variant_field_getters(
13051305
let field_getter_impl: syn::ImplItemFn = parse_quote! {
13061306
fn #field_name(slf: #pyo3_path::PyClassGuard<'_, Self>, py: #pyo3_path::Python<'_>) -> #pyo3_path::PyResult<#pyo3_path::PyObject> {
13071307
#[allow(unused_imports)]
1308-
use #pyo3_path::impl_::pyclass::Probe;
1308+
use #pyo3_path::impl_::pyclass::Probe as _;
13091309
match &*slf.into_super() {
13101310
#enum_name::#variant_ident ( #(#field_access_tokens), *) =>
13111311
#pyo3_path::impl_::pyclass::ConvertField::<

pyo3-macros-backend/src/pymethod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ pub fn impl_py_getter_def(
853853
#cfg_attrs
854854
{
855855
#[allow(unused_imports)] // might not be used if all probes are positve
856-
use #pyo3_path::impl_::pyclass::Probe;
856+
use #pyo3_path::impl_::pyclass::Probe as _;
857857

858858
struct Offset;
859859
unsafe impl #pyo3_path::impl_::pyclass::OffsetCalculator<#cls, #ty> for Offset {

src/err/mod.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,7 @@ impl_signed_integer!(isize);
852852
mod tests {
853853
use super::PyErrState;
854854
use crate::exceptions::{self, PyTypeError, PyValueError};
855+
use crate::impl_::pyclass::{value_of, IsSend, IsSync};
855856
use crate::{ffi, PyErr, PyTypeInfo, Python};
856857

857858
#[test]
@@ -977,14 +978,11 @@ mod tests {
977978

978979
#[test]
979980
fn test_pyerr_send_sync() {
980-
fn is_send<T: Send>() {}
981-
fn is_sync<T: Sync>() {}
981+
assert!(value_of!(IsSend, PyErr));
982+
assert!(value_of!(IsSync, PyErr));
982983

983-
is_send::<PyErr>();
984-
is_sync::<PyErr>();
985-
986-
is_send::<PyErrState>();
987-
is_sync::<PyErrState>();
984+
assert!(value_of!(IsSend, PyErrState));
985+
assert!(value_of!(IsSync, PyErrState));
988986
}
989987

990988
#[test]

src/impl_/pyclass.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use std::{
2525

2626
mod assertions;
2727
mod lazy_type_object;
28+
#[macro_use]
2829
mod probes;
2930

3031
pub use assertions::*;

src/impl_/pyclass/probes.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ where
4646
pub const VALUE: bool = true;
4747
}
4848

49+
probe!(IsSend);
50+
51+
impl<T: Send> IsSend<T> {
52+
pub const VALUE: bool = true;
53+
}
54+
4955
probe!(IsSync);
5056

5157
impl<T: Sync> IsSync<T> {
@@ -57,3 +63,15 @@ probe!(IsOption);
5763
impl<T> IsOption<Option<T>> {
5864
pub const VALUE: bool = true;
5965
}
66+
67+
#[cfg(test)]
68+
macro_rules! value_of {
69+
($probe:ident, $ty:ty) => {{
70+
#[allow(unused_imports)] // probe trait not used if VALUE is true
71+
use crate::impl_::pyclass::Probe as _;
72+
$probe::<$ty>::VALUE
73+
}};
74+
}
75+
76+
#[cfg(test)]
77+
pub(crate) use value_of;

src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,11 @@ pub use inventory; // Re-exported for `#[pyclass]` and `#[pymethods]` with `mult
411411
#[macro_use]
412412
mod tests;
413413

414+
// Macro dependencies, also contains macros exported for use across the codebase and
415+
// in expanded macros.
416+
#[doc(hidden)]
417+
pub mod impl_;
418+
414419
#[macro_use]
415420
mod internal_tricks;
416421
mod internal;
@@ -424,8 +429,6 @@ pub mod coroutine;
424429
mod err;
425430
pub mod exceptions;
426431
pub mod ffi;
427-
#[doc(hidden)]
428-
pub mod impl_;
429432
mod instance;
430433
mod interpreter_lifecycle;
431434
pub mod marker;

src/pybacked.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,7 @@ use impl_traits;
315315
#[cfg(test)]
316316
mod test {
317317
use super::*;
318+
use crate::impl_::pyclass::{value_of, IsSend, IsSync};
318319
use crate::types::PyAnyMethods as _;
319320
use crate::{IntoPyObject, Python};
320321
use std::collections::hash_map::DefaultHasher;
@@ -424,14 +425,11 @@ mod test {
424425

425426
#[test]
426427
fn test_backed_types_send_sync() {
427-
fn is_send<T: Send>() {}
428-
fn is_sync<T: Sync>() {}
428+
assert!(value_of!(IsSend, PyBackedStr));
429+
assert!(value_of!(IsSync, PyBackedStr));
429430

430-
is_send::<PyBackedStr>();
431-
is_sync::<PyBackedStr>();
432-
433-
is_send::<PyBackedBytes>();
434-
is_sync::<PyBackedBytes>();
431+
assert!(value_of!(IsSend, PyBackedBytes));
432+
assert!(value_of!(IsSync, PyBackedBytes));
435433
}
436434

437435
#[cfg(feature = "py-clone")]

src/test_utils.rs

Lines changed: 0 additions & 2 deletions
This file was deleted.

0 commit comments

Comments
 (0)