1
1
use crate :: err:: PyResult ;
2
2
use crate :: ffi_ptr_ext:: FfiPtrExt ;
3
3
use crate :: py_result_ext:: PyResultExt ;
4
+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
5
+ use crate :: sync:: PyOnceLock ;
4
6
use crate :: types:: any:: PyAny ;
5
- use crate :: { ffi, Borrowed , Bound , BoundObject , IntoPyObject , IntoPyObjectExt } ;
6
-
7
7
#[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
8
- use crate :: type_object:: PyTypeCheck ;
8
+ use crate :: types:: typeobject:: PyTypeMethods ;
9
+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
10
+ use crate :: types:: PyType ;
11
+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
12
+ use crate :: Py ;
13
+ use crate :: { ffi, Borrowed , Bound , BoundObject , IntoPyObject , IntoPyObjectExt } ;
9
14
10
15
use super :: PyWeakrefMethods ;
11
16
@@ -16,7 +21,7 @@ use super::PyWeakrefMethods;
16
21
pub struct PyWeakrefReference ( PyAny ) ;
17
22
18
23
#[ cfg( not( any( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
19
- pyobject_subclassable_native_type ! ( PyWeakrefReference , crate :: ffi:: PyWeakReference ) ;
24
+ pyobject_subclassable_native_type ! ( PyWeakrefReference , ffi:: PyWeakReference ) ;
20
25
21
26
#[ cfg( not( any( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
22
27
pyobject_native_type ! (
@@ -30,18 +35,17 @@ pyobject_native_type!(
30
35
31
36
// When targeting alternative or multiple interpreters, it is better to not use the internal API.
32
37
#[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
33
- pyobject_native_type_named ! ( PyWeakrefReference ) ;
34
-
35
- #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
36
- impl PyTypeCheck for PyWeakrefReference {
37
- const NAME : & ' static str = "weakref.ReferenceType" ;
38
- #[ cfg( feature = "experimental-inspect" ) ]
39
- const PYTHON_TYPE : & ' static str = "weakref.ReferenceType" ;
40
-
41
- fn type_check ( object : & Bound < ' _ , PyAny > ) -> bool {
42
- unsafe { ffi:: PyWeakref_CheckRef ( object. as_ptr ( ) ) > 0 }
43
- }
44
- }
38
+ pyobject_native_type_core ! (
39
+ PyWeakrefReference ,
40
+ |py| {
41
+ static TYPE : PyOnceLock <Py <PyType >> = PyOnceLock :: new( ) ;
42
+ TYPE . import( py, "weakref" , "ref" )
43
+ . unwrap( )
44
+ . as_type_ptr( )
45
+ } ,
46
+ #module=Some ( "weakref" ) ,
47
+ #checkfunction=ffi:: PyWeakref_CheckRef
48
+ ) ;
45
49
46
50
impl PyWeakrefReference {
47
51
/// Constructs a new Weak Reference (`weakref.ref`/`weakref.ReferenceType`) for the given object.
@@ -238,7 +242,7 @@ mod tests {
238
242
239
243
mod python_class {
240
244
use super :: * ;
241
- use crate :: ffi;
245
+ use crate :: { ffi, PyTypeInfo } ;
242
246
use crate :: { py_result_ext:: PyResultExt , types:: PyType } ;
243
247
use std:: ptr;
244
248
@@ -372,6 +376,18 @@ mod tests {
372
376
Ok ( ( ) )
373
377
} )
374
378
}
379
+
380
+ #[ test]
381
+ fn test_type_object ( ) -> PyResult < ( ) > {
382
+ Python :: attach ( |py| {
383
+ let class = get_type ( py) ?;
384
+ let object = class. call0 ( ) ?;
385
+ let reference = PyWeakrefReference :: new ( & object) ?;
386
+
387
+ assert ! ( reference. is_instance( & PyWeakrefReference :: type_object( py) ) ?) ;
388
+ Ok ( ( ) )
389
+ } )
390
+ }
375
391
}
376
392
377
393
// under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable.
0 commit comments