11use crate :: err:: PyResult ;
22use crate :: ffi_ptr_ext:: FfiPtrExt ;
33use crate :: py_result_ext:: PyResultExt ;
4+ #[ cfg( any( PyPy , GraalPy , Py_LIMITED_API ) ) ]
5+ use crate :: sync:: PyOnceLock ;
46use crate :: types:: any:: PyAny ;
5- use crate :: { ffi, Borrowed , Bound , BoundObject , IntoPyObject , IntoPyObjectExt } ;
6-
77#[ 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 } ;
914
1015use super :: PyWeakrefMethods ;
1116
@@ -16,7 +21,7 @@ use super::PyWeakrefMethods;
1621pub struct PyWeakrefReference ( PyAny ) ;
1722
1823#[ 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 ) ;
2025
2126#[ cfg( not( any( PyPy , GraalPy , Py_LIMITED_API ) ) ) ]
2227pyobject_native_type ! (
@@ -30,18 +35,17 @@ pyobject_native_type!(
3035
3136// When targeting alternative or multiple interpreters, it is better to not use the internal API.
3237#[ 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+ ) ;
4549
4650impl PyWeakrefReference {
4751 /// Constructs a new Weak Reference (`weakref.ref`/`weakref.ReferenceType`) for the given object.
@@ -238,7 +242,7 @@ mod tests {
238242
239243 mod python_class {
240244 use super :: * ;
241- use crate :: ffi;
245+ use crate :: { ffi, PyTypeInfo } ;
242246 use crate :: { py_result_ext:: PyResultExt , types:: PyType } ;
243247 use std:: ptr;
244248
@@ -372,6 +376,18 @@ mod tests {
372376 Ok ( ( ) )
373377 } )
374378 }
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+ }
375391 }
376392
377393 // under 'abi3-py37' and 'abi3-py38' PyClass cannot be weakreferencable.
0 commit comments