@@ -12,7 +12,7 @@ use crate::{
12
12
pyclass:: { create_type_object, PyClassTypeObject } ,
13
13
sync:: { GILOnceCell , GILProtected } ,
14
14
types:: PyType ,
15
- PyClass , PyErr , PyMethodDefType , PyObject , PyResult , Python ,
15
+ Bound , PyClass , PyErr , PyMethodDefType , PyObject , PyResult , Python ,
16
16
} ;
17
17
18
18
use super :: PyClassItemsIter ;
@@ -46,15 +46,30 @@ impl<T> LazyTypeObject<T> {
46
46
47
47
impl < T : PyClass > LazyTypeObject < T > {
48
48
/// Gets the type object contained by this `LazyTypeObject`, initializing it if needed.
49
+ #[ cfg_attr(
50
+ not( feature = "gil-refs" ) ,
51
+ deprecated(
52
+ since = "0.21.0" ,
53
+ note = "`LazyTypeObject::get_or_init` will be replaced by `LazyTypeObject::get_or_init_bound` in a future PyO3 version"
54
+ )
55
+ ) ]
49
56
pub fn get_or_init < ' py > ( & ' py self , py : Python < ' py > ) -> & ' py PyType {
57
+ self . get_or_init_bound ( py) . as_gil_ref ( )
58
+ }
59
+
60
+ /// Gets the type object contained by this `LazyTypeObject`, initializing it if needed.
61
+ pub fn get_or_init_bound < ' py > ( & ' py self , py : Python < ' py > ) -> & Bound < ' py , PyType > {
50
62
self . get_or_try_init ( py) . unwrap_or_else ( |err| {
51
63
err. print ( py) ;
52
64
panic ! ( "failed to create type object for {}" , T :: NAME )
53
65
} )
54
66
}
55
67
56
68
/// Fallible version of the above.
57
- pub ( crate ) fn get_or_try_init < ' py > ( & ' py self , py : Python < ' py > ) -> PyResult < & ' py PyType > {
69
+ pub ( crate ) fn get_or_try_init < ' py > (
70
+ & ' py self ,
71
+ py : Python < ' py > ,
72
+ ) -> PyResult < & Bound < ' py , PyType > > {
58
73
self . 0
59
74
. get_or_try_init ( py, create_type_object :: < T > , T :: NAME , T :: items_iter ( ) )
60
75
}
@@ -70,13 +85,13 @@ impl LazyTypeObjectInner {
70
85
init : fn ( Python < ' py > ) -> PyResult < PyClassTypeObject > ,
71
86
name : & str ,
72
87
items_iter : PyClassItemsIter ,
73
- ) -> PyResult < & ' py PyType > {
88
+ ) -> PyResult < & Bound < ' py , PyType > > {
74
89
( || -> PyResult < _ > {
75
90
let type_object = self
76
91
. value
77
92
. get_or_try_init ( py, || init ( py) ) ?
78
93
. type_object
79
- . as_ref ( py) ;
94
+ . bind ( py) ;
80
95
self . ensure_init ( type_object, name, items_iter) ?;
81
96
Ok ( type_object)
82
97
} ) ( )
@@ -91,7 +106,7 @@ impl LazyTypeObjectInner {
91
106
92
107
fn ensure_init (
93
108
& self ,
94
- type_object : & PyType ,
109
+ type_object : & Bound < ' _ , PyType > ,
95
110
name : & str ,
96
111
items_iter : PyClassItemsIter ,
97
112
) -> PyResult < ( ) > {
0 commit comments