@@ -187,25 +187,6 @@ impl PyType {
187
187
188
188
* slots. name . get_mut ( ) = Some ( String :: from ( name) ) ;
189
189
190
- #[ allow( clippy:: mutable_key_type) ]
191
- let mut slot_name_set = HashSet :: new ( ) ;
192
-
193
- for cls in mro. iter ( ) {
194
- for & name in cls. attributes . read ( ) . keys ( ) {
195
- if name != identifier ! ( ctx, __new__)
196
- && name. as_str ( ) . starts_with ( "__" )
197
- && name. as_str ( ) . ends_with ( "__" )
198
- {
199
- slot_name_set. insert ( name) ;
200
- }
201
- }
202
- }
203
- for & name in attrs. keys ( ) {
204
- if name. as_str ( ) . starts_with ( "__" ) && name. as_str ( ) . ends_with ( "__" ) {
205
- slot_name_set. insert ( name) ;
206
- }
207
- }
208
-
209
190
let new_type = PyRef :: new_ref (
210
191
PyType {
211
192
base : Some ( base) ,
@@ -220,9 +201,7 @@ impl PyType {
220
201
None ,
221
202
) ;
222
203
223
- for attr_name in slot_name_set {
224
- new_type. update_slot :: < true > ( attr_name, ctx) ;
225
- }
204
+ new_type. init_slots ( ctx) ;
226
205
227
206
let weakref_type = super :: PyWeak :: static_type ( ) ;
228
207
for base in & new_type. bases {
@@ -280,6 +259,30 @@ impl PyType {
280
259
Ok ( new_type)
281
260
}
282
261
262
+ pub ( crate ) fn init_slots ( & self , ctx : & Context ) {
263
+ #[ allow( clippy:: mutable_key_type) ]
264
+ let mut slot_name_set = std:: collections:: HashSet :: new ( ) ;
265
+
266
+ for cls in self . mro . iter ( ) {
267
+ for & name in cls. attributes . read ( ) . keys ( ) {
268
+ if name == identifier ! ( ctx, __new__) {
269
+ continue ;
270
+ }
271
+ if name. as_str ( ) . starts_with ( "__" ) && name. as_str ( ) . ends_with ( "__" ) {
272
+ slot_name_set. insert ( name) ;
273
+ }
274
+ }
275
+ }
276
+ for & name in self . attributes . read ( ) . keys ( ) {
277
+ if name. as_str ( ) . starts_with ( "__" ) && name. as_str ( ) . ends_with ( "__" ) {
278
+ slot_name_set. insert ( name) ;
279
+ }
280
+ }
281
+ for attr_name in slot_name_set {
282
+ self . update_slot :: < true > ( attr_name, ctx) ;
283
+ }
284
+ }
285
+
283
286
pub fn slot_name ( & self ) -> String {
284
287
self . slots . name . read ( ) . as_ref ( ) . unwrap ( ) . to_string ( )
285
288
}
@@ -1329,3 +1332,64 @@ mod tests {
1329
1332
) ;
1330
1333
}
1331
1334
}
1335
+
1336
+ impl crate :: PyObject {
1337
+ // temporary tool to fill missing number protocols for builtin types
1338
+ pub fn init_builtin_number_slots ( & self , ctx : & Context ) {
1339
+ let typ = self
1340
+ . downcast_ref :: < PyType > ( )
1341
+ . expect ( "not called from a type" ) ;
1342
+ macro_rules! call_update_slot {
1343
+ ( $name: ident) => {
1344
+ let id = identifier!( ctx, $name) ;
1345
+ if typ. has_attr( id) {
1346
+ typ. update_slot:: <true >( identifier!( ctx, $name) , ctx) ;
1347
+ }
1348
+ } ;
1349
+ }
1350
+ call_update_slot ! ( __add__) ;
1351
+ call_update_slot ! ( __radd__) ;
1352
+ call_update_slot ! ( __iadd__) ;
1353
+ call_update_slot ! ( __sub__) ;
1354
+ call_update_slot ! ( __rsub__) ;
1355
+ call_update_slot ! ( __isub__) ;
1356
+ call_update_slot ! ( __mul__) ;
1357
+ call_update_slot ! ( __rmul__) ;
1358
+ call_update_slot ! ( __imul__) ;
1359
+ call_update_slot ! ( __mod__) ;
1360
+ call_update_slot ! ( __rmod__) ;
1361
+ call_update_slot ! ( __imod__) ;
1362
+ call_update_slot ! ( __div__) ;
1363
+ call_update_slot ! ( __rdiv__) ;
1364
+ call_update_slot ! ( __idiv__) ;
1365
+ call_update_slot ! ( __divmod__) ;
1366
+ call_update_slot ! ( __rdivmod__) ;
1367
+ call_update_slot ! ( __pow__) ;
1368
+ call_update_slot ! ( __rpow__) ;
1369
+ call_update_slot ! ( __ipow__) ;
1370
+ call_update_slot ! ( __lshift__) ;
1371
+ call_update_slot ! ( __rlshift__) ;
1372
+ call_update_slot ! ( __ilshift__) ;
1373
+ call_update_slot ! ( __rshift__) ;
1374
+ call_update_slot ! ( __rrshift__) ;
1375
+ call_update_slot ! ( __irshift__) ;
1376
+ call_update_slot ! ( __and__) ;
1377
+ call_update_slot ! ( __rand__) ;
1378
+ call_update_slot ! ( __iand__) ;
1379
+ call_update_slot ! ( __xor__) ;
1380
+ call_update_slot ! ( __rxor__) ;
1381
+ call_update_slot ! ( __ixor__) ;
1382
+ call_update_slot ! ( __or__) ;
1383
+ call_update_slot ! ( __ror__) ;
1384
+ call_update_slot ! ( __ior__) ;
1385
+ call_update_slot ! ( __floordiv__) ;
1386
+ call_update_slot ! ( __rfloordiv__) ;
1387
+ call_update_slot ! ( __ifloordiv__) ;
1388
+ call_update_slot ! ( __truediv__) ;
1389
+ call_update_slot ! ( __rtruediv__) ;
1390
+ call_update_slot ! ( __itruediv__) ;
1391
+ call_update_slot ! ( __matmul__) ;
1392
+ call_update_slot ! ( __rmatmul__) ;
1393
+ call_update_slot ! ( __imatmul__) ;
1394
+ }
1395
+ }
0 commit comments