Skip to content

Commit 5e321dc

Browse files
authored
Merge pull request #307 from PyO3/missing-debug-impls
Add missing implementations of the Debug trait for the borrow checking wrapper types.
2 parents 82aa55e + 97aea61 commit 5e321dc

File tree

2 files changed

+65
-5
lines changed

2 files changed

+65
-5
lines changed

src/borrow.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,10 @@
161161
//! [base]: https://numpy.org/doc/stable/reference/c-api/types-and-structures.html#c.NPY_AO.base
162162
#![deny(missing_docs)]
163163

164+
use std::any::type_name;
164165
use std::cell::UnsafeCell;
165166
use std::collections::hash_map::{Entry, HashMap};
167+
use std::fmt;
166168
use std::mem::size_of;
167169
use std::ops::Deref;
168170

@@ -500,6 +502,22 @@ where
500502
}
501503
}
502504

505+
impl<'py, T, D> fmt::Debug for PyReadonlyArray<'py, T, D>
506+
where
507+
T: Element,
508+
D: Dimension,
509+
{
510+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
511+
let name = format!(
512+
"PyReadonlyArray<{}, {}>",
513+
type_name::<T>(),
514+
type_name::<D>()
515+
);
516+
517+
f.debug_struct(&name).finish()
518+
}
519+
}
520+
503521
/// Read-write borrow of an array.
504522
///
505523
/// An instance of this type ensures that there are no instances of [`PyReadonlyArray`] and no other instances of [`PyReadwriteArray`],
@@ -636,6 +654,22 @@ where
636654
}
637655
}
638656

657+
impl<'py, T, D> fmt::Debug for PyReadwriteArray<'py, T, D>
658+
where
659+
T: Element,
660+
D: Dimension,
661+
{
662+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
663+
let name = format!(
664+
"PyReadwriteArray<{}, {}>",
665+
type_name::<T>(),
666+
type_name::<D>()
667+
);
668+
669+
f.debug_struct(&name).finish()
670+
}
671+
}
672+
639673
fn base_address<T, D>(array: &PyArray<T, D>) -> usize {
640674
let py = array.py();
641675
let mut array = array.as_array_ptr();
@@ -1210,4 +1244,29 @@ mod tests {
12101244
}
12111245
});
12121246
}
1247+
1248+
#[test]
1249+
fn test_debug_formatting() {
1250+
Python::with_gil(|py| {
1251+
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
1252+
1253+
{
1254+
let shared = array.readonly();
1255+
1256+
assert_eq!(
1257+
format!("{:?}", shared),
1258+
"PyReadonlyArray<f64, ndarray::dimension::dim::Dim<[usize; 3]>>"
1259+
);
1260+
}
1261+
1262+
{
1263+
let exclusive = array.readwrite();
1264+
1265+
assert_eq!(
1266+
format!("{:?}", exclusive),
1267+
"PyReadwriteArray<f64, ndarray::dimension::dim::Dim<[usize; 3]>>"
1268+
);
1269+
}
1270+
});
1271+
}
12131272
}

tests/borrow.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,18 +55,18 @@ fn shared_and_exclusive_borrows() {
5555
}
5656

5757
#[test]
58-
#[should_panic(expected = "AlreadyBorrowed")]
5958
fn multiple_exclusive_borrows() {
6059
Python::with_gil(|py| {
6160
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
6261

63-
let _exclusive1 = array.readwrite();
64-
let _exclusive2 = array.readwrite();
62+
let _exclusive = array.try_readwrite().unwrap();
63+
64+
let err = array.try_readwrite().unwrap_err();
65+
assert_eq!(err.to_string(), "The given array is already borrowed");
6566
});
6667
}
6768

6869
#[test]
69-
#[should_panic(expected = "NotWriteable")]
7070
fn exclusive_borrow_requires_writeable() {
7171
Python::with_gil(|py| {
7272
let array = PyArray::<f64, _>::zeros(py, (1, 2, 3), false);
@@ -75,7 +75,8 @@ fn exclusive_borrow_requires_writeable() {
7575
(*array.as_array_ptr()).flags &= !NPY_ARRAY_WRITEABLE;
7676
}
7777

78-
let _exclusive = array.readwrite();
78+
let err = array.try_readwrite().unwrap_err();
79+
assert_eq!(err.to_string(), "The given array is not writeable");
7980
});
8081
}
8182

0 commit comments

Comments
 (0)