@@ -17,10 +17,10 @@ pub struct ASTCacheKey {
17
17
#[ pymethods]
18
18
impl ASTCacheKey {
19
19
#[ new]
20
- pub fn new ( py : Python , ast : PyObject ) -> PyResult < Self > {
20
+ pub fn new ( ast : Bound < PyAny > ) -> PyResult < Self > {
21
21
Ok ( ASTCacheKey {
22
- hash : ast. as_any ( ) . bind ( py ) . hash ( ) ?,
23
- ast,
22
+ hash : ast. as_any ( ) . hash ( ) ?,
23
+ ast : ast . into ( ) ,
24
24
} )
25
25
}
26
26
@@ -45,7 +45,7 @@ pub struct Base {
45
45
#[ pyo3( get, set) ]
46
46
args : Py < PyTuple > ,
47
47
#[ pyo3( get, set) ]
48
- length : PyObject ,
48
+ length : usize ,
49
49
#[ pyo3( get, set) ]
50
50
variables : PyObject , // TODO: This should be a HashSet, leave opaque for now
51
51
#[ pyo3( get, set) ]
@@ -86,33 +86,48 @@ pub struct Base {
86
86
#[ pymethods]
87
87
impl Base {
88
88
#[ new]
89
- #[ pyo3( signature = ( op, args, length, variables, symbolic, annotations=None , simplified=None , errored=None , eager_backends=None , uninitialized=None , uc_alloc_depth=None , encoded_name=None , depth=None , uneliminatable_annotations=None , relocatable_annotations=None ) ) ]
89
+ #[ pyo3( signature = (
90
+ op,
91
+ args,
92
+ length,
93
+ variables,
94
+ symbolic,
95
+ annotations=None ,
96
+ simplified=None ,
97
+ errored=None ,
98
+ eager_backends=None ,
99
+ uninitialized=None ,
100
+ uc_alloc_depth=None ,
101
+ encoded_name=None ,
102
+ depth=None ,
103
+ uneliminatable_annotations=None ,
104
+ relocatable_annotations=None
105
+ ) ) ]
90
106
fn new (
91
107
py : Python ,
92
108
op : String ,
93
- args : Py < PyTuple > ,
94
- length : PyObject ,
95
- variables : PyObject ,
109
+ args : Bound < PyTuple > ,
110
+ length : usize ,
111
+ variables : Bound < PyAny > ,
96
112
symbolic : bool ,
97
- annotations : Option < Py < PyTuple > > ,
113
+ annotations : Option < Bound < PyTuple > > ,
98
114
// New stuff
99
- simplified : Option < PyObject > ,
100
- errored : Option < Py < PySet > > ,
101
- eager_backends : Option < PyObject > ,
102
- uninitialized : Option < PyObject > ,
103
- uc_alloc_depth : Option < PyObject > ,
104
- encoded_name : Option < PyObject > ,
115
+ simplified : Option < Bound < PyAny > > ,
116
+ errored : Option < Bound < PySet > > ,
117
+ eager_backends : Option < Bound < PyAny > > ,
118
+ uninitialized : Option < Bound < PyAny > > ,
119
+ uc_alloc_depth : Option < Bound < PyAny > > ,
120
+ encoded_name : Option < Bound < PyAny > > ,
105
121
depth : Option < usize > ,
106
- uneliminatable_annotations : Option < PyObject > ,
107
- relocatable_annotations : Option < PyObject > ,
122
+ uneliminatable_annotations : Option < Bound < PyAny > > ,
123
+ relocatable_annotations : Option < Bound < PyAny > > ,
108
124
) -> PyResult < Self > {
109
- if args. bind ( py ) . len ( ) == 0 {
125
+ if args. len ( ) == 0 {
110
126
return Err ( PyValueError :: new_err ( "AST with no arguments!" ) ) ; // TODO: This should be a custom error
111
127
}
112
128
113
129
let depth = depth. unwrap_or (
114
130
* args
115
- . bind ( py)
116
131
. iter ( )
117
132
. map ( |arg| {
118
133
arg. getattr ( "depth" )
@@ -122,43 +137,41 @@ impl Base {
122
137
. collect :: < Result < Vec < usize > , PyErr > > ( ) ?
123
138
. iter ( )
124
139
. max ( )
125
- . unwrap_or ( & 0 ) + 1
140
+ . unwrap_or ( & 0 )
141
+ + 1 ,
126
142
) ;
127
143
128
144
Ok ( Base {
129
145
op,
130
- args,
146
+ args : args . into ( ) ,
131
147
length,
132
- variables,
148
+ variables : variables . into ( ) ,
133
149
symbolic,
134
- annotations : annotations. unwrap_or_else ( || PyTuple :: empty_bound ( py) . unbind ( ) ) ,
150
+ annotations : annotations
151
+ . unwrap_or_else ( || PyTuple :: empty_bound ( py) )
152
+ . into ( ) ,
135
153
136
154
depth,
137
155
138
156
_hash : None ,
139
- _simplified : simplified,
157
+ _simplified : simplified. map ( |s| s . into ( ) ) ,
140
158
_cache_key : None ,
141
- _cached_encoded_name : encoded_name,
142
- _errored : errored. unwrap_or (
143
- // TODO: Is there really not an easier way to make a set?
144
- py. eval_bound ( "set()" , None , None ) ?
145
- . downcast_into ( ) ?
146
- . unbind ( ) ,
147
- ) ,
148
- _eager_backends : eager_backends,
159
+ _cached_encoded_name : encoded_name. map ( |s| s. into ( ) ) ,
160
+ _errored : errored. unwrap_or ( PySet :: empty_bound ( py) ?) . into ( ) ,
161
+ _eager_backends : eager_backends. map ( |s| s. into ( ) ) ,
149
162
_excavated : None ,
150
163
_burrowed : None ,
151
- _uninitialized : uninitialized,
152
- _uc_alloc_depth : uc_alloc_depth,
153
- _uneliminatable_annotations : uneliminatable_annotations,
154
- _relocatable_annotations : relocatable_annotations,
164
+ _uninitialized : uninitialized. map ( |s| s . into ( ) ) ,
165
+ _uc_alloc_depth : uc_alloc_depth. map ( |s| s . into ( ) ) ,
166
+ _uneliminatable_annotations : uneliminatable_annotations. map ( |s| s . into ( ) ) ,
167
+ _relocatable_annotations : relocatable_annotations. map ( |s| s . into ( ) ) ,
155
168
} )
156
169
}
157
170
158
171
#[ staticmethod]
159
172
fn _arg_serialize < ' py > (
160
173
py : Python < ' py > ,
161
- arg : & Bound < ' _ , PyAny > ,
174
+ arg : Bound < ' _ , PyAny > ,
162
175
) -> PyResult < Option < Cow < ' py , [ u8 ] > > > {
163
176
if arg. is_none ( ) {
164
177
return Ok ( Some ( Cow :: from ( vec ! [ b'\x0f' ] ) ) ) ;
@@ -208,7 +221,7 @@ impl Base {
208
221
if arg. is_instance ( & py. get_type_bound :: < PyTuple > ( ) ) ? {
209
222
let mut result = Vec :: new ( ) ;
210
223
for item in arg. downcast :: < PyTuple > ( ) ?. iter ( ) {
211
- if let Some ( sub_result) = Self :: _arg_serialize ( py, & item) ? {
224
+ if let Some ( sub_result) = Self :: _arg_serialize ( py, item) ? {
212
225
result. extend ( sub_result. iter ( ) ) ;
213
226
} else {
214
227
return Ok ( None ) ; // Do we really want to return None here?
@@ -223,16 +236,16 @@ impl Base {
223
236
fn _ast_serialize < ' py > (
224
237
py : Python < ' py > ,
225
238
op : String ,
226
- args_tuple : & Bound < ' _ , PyTuple > ,
227
- keywords : & Bound < ' _ , PyDict > , // TODO: This should be a struct or seperate args
239
+ args_tuple : Bound < ' _ , PyTuple > ,
240
+ keywords : Bound < ' _ , PyDict > , // TODO: This should be a struct or seperate args
228
241
) -> PyResult < Option < Cow < ' py , [ u8 ] > > > {
229
- let serailized_args = match Base :: _arg_serialize ( py, args_tuple) ? {
242
+ let serailized_args = match Base :: _arg_serialize ( py, args_tuple. into_any ( ) ) ? {
230
243
Some ( args) => args,
231
244
None => return Ok ( None ) ,
232
245
} ;
233
246
234
247
let length = match keywords. contains ( "length" ) ? {
235
- true => match Base :: _arg_serialize ( py, & keywords. get_item ( "length" ) ?. unwrap ( ) ) ? {
248
+ true => match Base :: _arg_serialize ( py, keywords. get_item ( "length" ) ?. unwrap ( ) ) ? {
236
249
Some ( length) => length,
237
250
None => return Ok ( None ) ,
238
251
} ,
@@ -268,7 +281,7 @@ impl Base {
268
281
fn _calc_hash < ' py > (
269
282
py : Python < ' py > ,
270
283
op : String ,
271
- args : & Bound < PyTuple > ,
284
+ args : Bound < PyTuple > ,
272
285
keywords : Bound < PyDict > ,
273
286
) -> PyResult < isize > {
274
287
let mut args_tuple = Vec :: new ( ) ;
@@ -299,7 +312,7 @@ impl Base {
299
312
}
300
313
}
301
314
302
- let to_hash = match Base :: _ast_serialize ( py, op. clone ( ) , & args, & keywords) ? {
315
+ let to_hash = match Base :: _ast_serialize ( py, op. clone ( ) , args, keywords. clone ( ) ) ? {
303
316
Some ( to_hash) => to_hash,
304
317
None => {
305
318
let hash_tuple: Bound < PyTuple > = PyTuple :: new_bound (
0 commit comments