Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/types/capsule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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::<PyAttributeError>(py));

// correct name is okay.
// SAFETY: we know the capsule at `name` contains a `Foo`
Expand All @@ -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::<Align128>(py, name) }.is_err());
let Err(err) = (unsafe { PyCapsule::import::<Align128>(py, name) }) else {
panic!("importing a misaligned capsule should fail");
};
assert!(err.is_instance_of::<PySystemError>(py));
assert!(err.to_string().contains("is not aligned to rust type"));
});
}

Expand Down Expand Up @@ -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::<PyValueError>(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::<PyValueError>(py));
});
}

Expand All @@ -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::<PyValueError>(py));
});
}

Expand Down Expand Up @@ -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::<PyValueError>(py)
);
});
}

Expand All @@ -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::<PyImportError>(py));
});
}

Expand All @@ -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::<PyAttributeError>(py));
});
}

Expand Down
Loading