From 7b353b26c70282f3de1e190130faa4feda625e71 Mon Sep 17 00:00:00 2001 From: Thomas Pellissier-Tanon Date: Tue, 24 Nov 2026 09:37:44 +0100 Subject: [PATCH] tests: fixes a name conflict between capsules Two tests were using the same `builtins.capsule` name, causing potential errors if they happen to run at the same time on the same interpreter. This PR renames one to avoid the issue. Also tests the error types --- src/types/capsule.rs | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/types/capsule.rs b/src/types/capsule.rs index f6cde84181c..63f9f6c769e 100644 --- a/src/types/capsule.rs +++ b/src/types/capsule.rs @@ -693,6 +693,7 @@ fn name_ptr(name: Option<&CStr>) -> *const c_char { mod tests { extern crate std; + use crate::exceptions::{PyAttributeError, PyImportError, PySystemError, PyValueError}; use crate::platform::prelude::*; use crate::prelude::PyModule; use crate::types::capsule::PyCapsuleMethods; @@ -797,7 +798,10 @@ mod tests { // check error when wrong named passed for capsule. // SAFETY: this function will fail so the cast is never done let result: PyResult<&Foo> = unsafe { PyCapsule::import(py, c"builtins.non_existent") }; - assert!(result.is_err()); + let Err(err) = result else { + panic!("importing a non-existent capsule should fail"); + }; + assert!(err.is_instance_of::(py)); // correct name is okay. // SAFETY: we know the capsule at `name` contains a `Foo` @@ -815,16 +819,20 @@ mod tests { Python::attach(|py| { let ptr = NonNull::new(129 as *mut c_void).unwrap(); - let name = c"builtins.capsule"; + let name = c"builtins.misaligned_capsule"; // SAFETY: the pointer will never be dereferenced let capsule = unsafe { PyCapsule::new_with_pointer(py, ptr, name) }.unwrap(); let module = PyModule::import(py, "builtins").unwrap(); - module.add("capsule", capsule).unwrap(); + module.add("misaligned_capsule", capsule).unwrap(); // SAFETY: this should return an error so no reference will be created - assert!(unsafe { PyCapsule::import::(py, name) }.is_err()); + let Err(err) = (unsafe { PyCapsule::import::(py, name) }) else { + panic!("importing a misaligned capsule should fail"); + }; + assert!(err.is_instance_of::(py)); + assert!(err.to_string().contains("is not aligned to rust type")); }); } @@ -981,11 +989,11 @@ mod tests { // Requesting with wrong name should fail let result = cap.pointer_checked(Some(c"wrong.name")); - assert!(result.is_err()); + assert!(result.unwrap_err().is_instance_of::(py)); // Requesting with None when capsule has a name should also fail let result = cap.pointer_checked(None); - assert!(result.is_err()); + assert!(result.unwrap_err().is_instance_of::(py)); }); } @@ -1001,7 +1009,7 @@ mod tests { // Should fail with Some(name) let result = cap_no_name.pointer_checked(Some(c"some.name")); - assert!(result.is_err()); + assert!(result.unwrap_err().is_instance_of::(py)); }); } @@ -1047,8 +1055,11 @@ mod tests { } // context() on invalid capsule should fail - let result = cap.context(); - assert!(result.is_err()); + assert!( + cap.context() + .unwrap_err() + .is_instance_of::(py) + ); }); } @@ -1059,7 +1070,7 @@ mod tests { // SAFETY: we expect this to fail, no cast will occur let result: PyResult<&u32> = unsafe { PyCapsule::import(py, c"nonexistent_module.capsule") }; - assert!(result.is_err()); + assert!(result.unwrap_err().is_instance_of::(py)); }); } @@ -1075,7 +1086,7 @@ mod tests { // SAFETY: we expect this to fail let result: PyResult<&u32> = unsafe { PyCapsule::import(py, c"builtins.wrong_attribute") }; - assert!(result.is_err()); + assert!(result.unwrap_err().is_instance_of::(py)); }); }