@@ -230,7 +230,11 @@ unsafe impl<T> AsPyPointer for Py2<'_, T> {
230
230
/// The advantage of this over `&Py2` is that it avoids the need to have a pointer-to-pointer, as Py2
231
231
/// is already a pointer to a PyObject.
232
232
#[ repr( transparent) ]
233
- pub ( crate ) struct Py2Borrowed < ' a , ' py , T > ( NonNull < ffi:: PyObject > , PhantomData < & ' a Py2 < ' py , T > > ) ; // TODO is it useful to have the generic form?
233
+ pub ( crate ) struct Py2Borrowed < ' a , ' py , T > (
234
+ NonNull < ffi:: PyObject > ,
235
+ PhantomData < & ' a Py < T > > ,
236
+ Python < ' py > ,
237
+ ) ; // TODO is it useful to have the generic form?
234
238
235
239
impl < ' a , ' py > Py2Borrowed < ' a , ' py , PyAny > {
236
240
/// # Safety
@@ -241,18 +245,18 @@ impl<'a, 'py> Py2Borrowed<'a, 'py, PyAny> {
241
245
py : Python < ' py > ,
242
246
ptr : * mut ffi:: PyObject ,
243
247
) -> PyResult < Self > {
244
- NonNull :: new ( ptr) . map_or_else ( || Err ( PyErr :: fetch ( py) ) , |ptr| Ok ( Self ( ptr, PhantomData ) ) )
248
+ NonNull :: new ( ptr) . map_or_else (
249
+ || Err ( PyErr :: fetch ( py) ) ,
250
+ |ptr| Ok ( Self ( ptr, PhantomData , py) ) ,
251
+ )
245
252
}
246
253
247
254
/// # Safety
248
255
/// This is similar to `std::slice::from_raw_parts`, the lifetime `'a` is completely defined by
249
256
/// the caller and it's the caller's responsibility to ensure that the reference this is
250
257
/// derived from is valid for the lifetime `'a`.
251
- pub ( crate ) unsafe fn from_ptr_or_opt (
252
- _py : Python < ' py > ,
253
- ptr : * mut ffi:: PyObject ,
254
- ) -> Option < Self > {
255
- NonNull :: new ( ptr) . map ( |ptr| Self ( ptr, PhantomData ) )
258
+ pub ( crate ) unsafe fn from_ptr_or_opt ( py : Python < ' py > , ptr : * mut ffi:: PyObject ) -> Option < Self > {
259
+ NonNull :: new ( ptr) . map ( |ptr| Self ( ptr, PhantomData , py) )
256
260
}
257
261
258
262
/// # Safety
@@ -263,6 +267,7 @@ impl<'a, 'py> Py2Borrowed<'a, 'py, PyAny> {
263
267
Self (
264
268
NonNull :: new ( ptr) . unwrap_or_else ( || crate :: err:: panic_after_error ( py) ) ,
265
269
PhantomData ,
270
+ py,
266
271
)
267
272
}
268
273
}
@@ -273,6 +278,7 @@ impl<'a, 'py, T> From<&'a Py2<'py, T>> for Py2Borrowed<'a, 'py, T> {
273
278
Self (
274
279
unsafe { NonNull :: new_unchecked ( instance. as_ptr ( ) ) } ,
275
280
PhantomData ,
281
+ instance. py ( ) ,
276
282
)
277
283
}
278
284
}
@@ -852,6 +858,10 @@ impl<T> Py<T> {
852
858
Py2 ( py, ManuallyDrop :: new ( self ) )
853
859
}
854
860
861
+ pub ( crate ) fn attach_borrow < ' a , ' py > ( & ' a self , py : Python < ' py > ) -> Py2Borrowed < ' a , ' py , T > {
862
+ Py2Borrowed ( self . 0 , PhantomData , py)
863
+ }
864
+
855
865
/// Returns whether `self` and `other` point to the same object. To compare
856
866
/// the equality of two objects (the `==` operator), use [`eq`](PyAny::eq).
857
867
///
0 commit comments