@@ -244,13 +244,24 @@ impl<T, D> PyArray<T, D> {
244
244
unsafe { Py :: from_borrowed_ptr ( self . py ( ) , self . as_ptr ( ) ) }
245
245
}
246
246
247
- /// Constructs `PyArray` from raw python object without incrementing reference counts.
247
+ /// Constructs `PyArray` from raw Python object without incrementing reference counts.
248
+ ///
249
+ /// # Safety
250
+ ///
251
+ /// Implementations must ensure the object does not get freed during `'py`
252
+ /// and ensure that `ptr` is of the correct type.
248
253
pub unsafe fn from_owned_ptr ( py : Python < ' _ > , ptr : * mut ffi:: PyObject ) -> & Self {
249
254
py. from_owned_ptr ( ptr)
250
255
}
251
256
252
- /// Constructs PyArray from raw python object and increments reference counts.
253
- pub unsafe fn from_borrowed_ptr ( py : Python < ' _ > , ptr : * mut ffi:: PyObject ) -> & Self {
257
+ /// Constructs PyArray from raw Python object and increments reference counts.
258
+ ///
259
+ /// # Safety
260
+ ///
261
+ /// Implementations must ensure the object does not get freed during `'py`
262
+ /// and ensure that `ptr` is of the correct type.
263
+ /// Note that it must be safe to decrement the reference count of ptr.
264
+ pub unsafe fn from_borrowed_ptr < ' py > ( py : Python < ' py > , ptr : * mut ffi:: PyObject ) -> & ' py Self {
254
265
py. from_borrowed_ptr ( ptr)
255
266
}
256
267
@@ -673,7 +684,10 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
673
684
///
674
685
/// See [NpyIndex](../convert/trait.NpyIndex.html) for what types you can use as index.
675
686
///
676
- /// Passing an invalid index can cause undefined behavior(mostly SIGSEGV).
687
+ /// # Safety
688
+ ///
689
+ /// Passing an invalid index is undefined behavior. The element must also have been initialized.
690
+ /// The elemet must also not be modified by Python code.
677
691
///
678
692
/// # Example
679
693
/// ```
@@ -693,6 +707,11 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
693
707
}
694
708
695
709
/// Same as [uget](#method.uget), but returns `&mut T`.
710
+ ///
711
+ /// # Safety
712
+ ///
713
+ /// Passing an invalid index is undefined behavior. The element must also have been initialized.
714
+ /// The element must also not be accessed by Python code.
696
715
#[ inline( always) ]
697
716
#[ allow( clippy:: mut_from_ref) ]
698
717
pub unsafe fn uget_mut < Idx > ( & self , index : Idx ) -> & mut T
@@ -704,6 +723,9 @@ impl<T: Element, D: Dimension> PyArray<T, D> {
704
723
}
705
724
706
725
/// Same as [uget](#method.uget), but returns `*mut T`.
726
+ ///
727
+ /// # Safety
728
+ /// Passing an invalid index is undefined behavior.
707
729
#[ inline( always) ]
708
730
pub unsafe fn uget_raw < Idx > ( & self , index : Idx ) -> * mut T
709
731
where
0 commit comments