@@ -34,8 +34,8 @@ use std::{borrow::Borrow, collections::HashSet, fmt, ops::Deref, pin::Pin, ptr::
3434#[ pyclass( module = false , name = "type" , traverse = "manual" ) ]
3535pub struct PyType {
3636 pub base : Option < PyTypeRef > ,
37- pub bases : Vec < PyTypeRef > ,
38- pub mro : Vec < PyTypeRef > ,
37+ pub bases : PyRwLock < Vec < PyTypeRef > > ,
38+ pub mro : PyRwLock < Vec < PyTypeRef > > ,
3939 pub subclasses : PyRwLock < Vec < PyRef < PyWeak > > > ,
4040 pub attributes : PyRwLock < PyAttributes > ,
4141 pub slots : PyTypeSlots ,
@@ -178,29 +178,33 @@ impl PyType {
178178 Self :: new_heap_inner ( base, bases, attrs, slots, heaptype_ext, metaclass, ctx)
179179 }
180180
181- #[ allow( clippy:: too_many_arguments) ]
182- fn new_heap_inner (
183- base : PyRef < Self > ,
184- bases : Vec < PyRef < Self > > ,
185- attrs : PyAttributes ,
186- mut slots : PyTypeSlots ,
187- heaptype_ext : HeapTypeExt ,
188- metaclass : PyRef < Self > ,
189- ctx : & Context ,
190- ) -> Result < PyRef < Self > , String > {
181+ fn resolve_mro ( bases : & [ PyRef < Self > ] ) -> Result < Vec < PyTypeRef > , String > {
191182 // Check for duplicates in bases.
192183 let mut unique_bases = HashSet :: new ( ) ;
193- for base in & bases {
184+ for base in bases {
194185 if !unique_bases. insert ( base. get_id ( ) ) {
195186 return Err ( format ! ( "duplicate base class {}" , base. name( ) ) ) ;
196187 }
197188 }
198189
199190 let mros = bases
200191 . iter ( )
201- . map ( |x| x . iter_mro ( ) . map ( |x| x . to_owned ( ) ) . collect ( ) )
192+ . map ( |base| base . mro_map_collect ( |t| t . to_owned ( ) ) )
202193 . collect ( ) ;
203- let mro = linearise_mro ( mros) ?;
194+ linearise_mro ( mros)
195+ }
196+
197+ #[ allow( clippy:: too_many_arguments) ]
198+ fn new_heap_inner (
199+ base : PyRef < Self > ,
200+ bases : Vec < PyRef < Self > > ,
201+ attrs : PyAttributes ,
202+ mut slots : PyTypeSlots ,
203+ heaptype_ext : HeapTypeExt ,
204+ metaclass : PyRef < Self > ,
205+ ctx : & Context ,
206+ ) -> Result < PyRef < Self > , String > {
207+ let mro = Self :: resolve_mro ( & bases) ?;
204208
205209 if base. slots . flags . has_feature ( PyTypeFlags :: HAS_DICT ) {
206210 slots. flags |= PyTypeFlags :: HAS_DICT
@@ -221,8 +225,8 @@ impl PyType {
221225 let new_type = PyRef :: new_ref (
222226 PyType {
223227 base : Some ( base) ,
224- bases,
225- mro,
228+ bases : PyRwLock :: new ( bases ) ,
229+ mro : PyRwLock :: new ( mro ) ,
226230 subclasses : PyRwLock :: default ( ) ,
227231 attributes : PyRwLock :: new ( attrs) ,
228232 slots,
@@ -235,7 +239,7 @@ impl PyType {
235239 new_type. init_slots ( ctx) ;
236240
237241 let weakref_type = super :: PyWeak :: static_type ( ) ;
238- for base in & new_type. bases {
242+ for base in new_type. bases . read ( ) . iter ( ) {
239243 base. subclasses . write ( ) . push (
240244 new_type
241245 . as_object ( )
@@ -260,14 +264,14 @@ impl PyType {
260264 slots. basicsize = base. slots . basicsize ;
261265 }
262266
263- let bases = vec ! [ base. clone( ) ] ;
264- let mro = base. iter_mro ( ) . map ( |x| x. to_owned ( ) ) . collect ( ) ;
267+ let bases = PyRwLock :: new ( vec ! [ base. clone( ) ] ) ;
268+ let mro = base. mro_map_collect ( |x| x. to_owned ( ) ) ;
265269
266270 let new_type = PyRef :: new_ref (
267271 PyType {
268272 base : Some ( base) ,
269273 bases,
270- mro,
274+ mro : PyRwLock :: new ( mro ) ,
271275 subclasses : PyRwLock :: default ( ) ,
272276 attributes : PyRwLock :: new ( attrs) ,
273277 slots,
@@ -278,7 +282,7 @@ impl PyType {
278282 ) ;
279283
280284 let weakref_type = super :: PyWeak :: static_type ( ) ;
281- for base in & new_type. bases {
285+ for base in new_type. bases . read ( ) . iter ( ) {
282286 base. subclasses . write ( ) . push (
283287 new_type
284288 . as_object ( )
@@ -294,7 +298,7 @@ impl PyType {
294298 #[ allow( clippy:: mutable_key_type) ]
295299 let mut slot_name_set = std:: collections:: HashSet :: new ( ) ;
296300
297- for cls in self . mro . iter ( ) {
301+ for cls in self . mro . read ( ) . iter ( ) {
298302 for & name in cls. attributes . read ( ) . keys ( ) {
299303 if name == identifier ! ( ctx, __new__) {
300304 continue ;
@@ -314,23 +318,6 @@ impl PyType {
314318 }
315319 }
316320
317- pub fn iter_mro ( & self ) -> impl DoubleEndedIterator < Item = & PyType > {
318- std:: iter:: once ( self ) . chain ( self . mro . iter ( ) . map ( |cls| -> & PyType { cls } ) )
319- }
320-
321- pub ( crate ) fn mro_find_map < F , R > ( & self , f : F ) -> Option < R >
322- where
323- F : Fn ( & Self ) -> Option < R > ,
324- {
325- // the hot path will be primitive types which usually hit the result from itself.
326- // try std::intrinsics::likely once it is stablized
327- if let Some ( r) = f ( self ) {
328- Some ( r)
329- } else {
330- self . mro . iter ( ) . find_map ( |cls| f ( cls) )
331- }
332- }
333-
334321 // This is used for class initialisation where the vm is not yet available.
335322 pub fn set_str_attr < V : Into < PyObjectRef > > (
336323 & self ,
@@ -361,6 +348,7 @@ impl PyType {
361348
362349 pub fn get_super_attr ( & self , attr_name : & ' static PyStrInterned ) -> Option < PyObjectRef > {
363350 self . mro
351+ . read ( )
364352 . iter ( )
365353 . find_map ( |class| class. attributes . read ( ) . get ( attr_name) . cloned ( ) )
366354 }
@@ -370,6 +358,7 @@ impl PyType {
370358 self . attributes . read ( ) . contains_key ( attr_name)
371359 || self
372360 . mro
361+ . read ( )
373362 . iter ( )
374363 . any ( |c| c. attributes . read ( ) . contains_key ( attr_name) )
375364 }
@@ -378,7 +367,10 @@ impl PyType {
378367 // Gather all members here:
379368 let mut attributes = PyAttributes :: default ( ) ;
380369
381- for bc in self . iter_mro ( ) . rev ( ) {
370+ for bc in std:: iter:: once ( self )
371+ . chain ( self . mro . read ( ) . iter ( ) . map ( |cls| -> & PyType { cls } ) )
372+ . rev ( )
373+ {
382374 for ( name, value) in bc. attributes . read ( ) . iter ( ) {
383375 attributes. insert ( name. to_owned ( ) , value. clone ( ) ) ;
384376 }
@@ -432,11 +424,37 @@ impl Py<PyType> {
432424 /// so only use this if `cls` is known to have not overridden the base __subclasscheck__ magic
433425 /// method.
434426 pub fn fast_issubclass ( & self , cls : & impl Borrow < crate :: PyObject > ) -> bool {
435- self . as_object ( ) . is ( cls. borrow ( ) ) || self . mro . iter ( ) . any ( |c| c. is ( cls. borrow ( ) ) )
427+ self . as_object ( ) . is ( cls. borrow ( ) ) || self . mro . read ( ) . iter ( ) . any ( |c| c. is ( cls. borrow ( ) ) )
436428 }
437429
438- pub fn iter_mro ( & self ) -> impl DoubleEndedIterator < Item = & Py < PyType > > {
439- std:: iter:: once ( self ) . chain ( self . mro . iter ( ) . map ( |x| x. deref ( ) ) )
430+ pub fn mro_map_collect < F , R > ( & self , f : F ) -> Vec < R >
431+ where
432+ F : Fn ( & Self ) -> R ,
433+ {
434+ std:: iter:: once ( self )
435+ . chain ( self . mro . read ( ) . iter ( ) . map ( |x| x. deref ( ) ) )
436+ . map ( f)
437+ . collect ( )
438+ }
439+
440+ pub fn mro_collect ( & self ) -> Vec < PyRef < PyType > > {
441+ std:: iter:: once ( self )
442+ . chain ( self . mro . read ( ) . iter ( ) . map ( |x| x. deref ( ) ) )
443+ . map ( |x| x. to_owned ( ) )
444+ . collect ( )
445+ }
446+
447+ pub ( crate ) fn mro_find_map < F , R > ( & self , f : F ) -> Option < R >
448+ where
449+ F : Fn ( & Self ) -> Option < R > ,
450+ {
451+ // the hot path will be primitive types which usually hit the result from itself.
452+ // try std::intrinsics::likely once it is stablized
453+ if let Some ( r) = f ( self ) {
454+ Some ( r)
455+ } else {
456+ self . mro . read ( ) . iter ( ) . find_map ( |cls| f ( cls) )
457+ }
440458 }
441459
442460 pub fn iter_base_chain ( & self ) -> impl Iterator < Item = & Py < PyType > > {
@@ -460,11 +478,66 @@ impl PyType {
460478 fn bases ( & self , vm : & VirtualMachine ) -> PyTupleRef {
461479 vm. ctx . new_tuple (
462480 self . bases
481+ . read ( )
463482 . iter ( )
464483 . map ( |x| x. as_object ( ) . to_owned ( ) )
465484 . collect ( ) ,
466485 )
467486 }
487+ #[ pygetset( setter, name = "__bases__" ) ]
488+ fn set_bases ( zelf : & Py < Self > , bases : Vec < PyTypeRef > , vm : & VirtualMachine ) -> PyResult < ( ) > {
489+ // TODO: Assigning to __bases__ is only used in typing.NamedTupleMeta.__new__
490+ // Rather than correctly reinitializing the class, we are skipping a few steps for now
491+ if zelf. slots . flags . has_feature ( PyTypeFlags :: IMMUTABLETYPE ) {
492+ return Err ( vm. new_type_error ( format ! (
493+ "cannot set '__bases__' attribute of immutable type '{}'" ,
494+ zelf. name( )
495+ ) ) ) ;
496+ }
497+ if bases. is_empty ( ) {
498+ return Err ( vm. new_type_error ( format ! (
499+ "can only assign non-empty tuple to %s.__bases__, not {}" ,
500+ zelf. name( )
501+ ) ) ) ;
502+ }
503+
504+ // TODO: check for mro cycles
505+
506+ // TODO: Remove this class from all subclass lists
507+ // for base in self.bases.read().iter() {
508+ // let subclasses = base.subclasses.write();
509+ // // TODO: how to uniquely identify the subclasses to remove?
510+ // }
511+
512+ * zelf. bases . write ( ) = bases;
513+ // Recursively update the mros of this class and all subclasses
514+ fn update_mro_recursively ( cls : & PyType , vm : & VirtualMachine ) -> PyResult < ( ) > {
515+ * cls. mro . write ( ) =
516+ PyType :: resolve_mro ( & cls. bases . read ( ) ) . map_err ( |msg| vm. new_type_error ( msg) ) ?;
517+ for subclass in cls. subclasses . write ( ) . iter ( ) {
518+ let subclass = subclass. upgrade ( ) . unwrap ( ) ;
519+ let subclass: & PyType = subclass. payload ( ) . unwrap ( ) ;
520+ update_mro_recursively ( subclass, vm) ?;
521+ }
522+ Ok ( ( ) )
523+ }
524+ update_mro_recursively ( zelf, vm) ?;
525+
526+ // TODO: do any old slots need to be cleaned up first?
527+ zelf. init_slots ( & vm. ctx ) ;
528+
529+ // Register this type as a subclass of its new bases
530+ let weakref_type = super :: PyWeak :: static_type ( ) ;
531+ for base in zelf. bases . read ( ) . iter ( ) {
532+ base. subclasses . write ( ) . push (
533+ zelf. as_object ( )
534+ . downgrade_with_weakref_typ_opt ( None , weakref_type. to_owned ( ) )
535+ . unwrap ( ) ,
536+ ) ;
537+ }
538+
539+ Ok ( ( ) )
540+ }
468541
469542 #[ pygetset( magic) ]
470543 fn base ( & self ) -> Option < PyTypeRef > {
@@ -964,8 +1037,7 @@ impl PyType {
9641037impl Py < PyType > {
9651038 #[ pygetset( name = "__mro__" ) ]
9661039 fn get_mro ( & self ) -> PyTuple {
967- let elements: Vec < PyObjectRef > =
968- self . iter_mro ( ) . map ( |x| x. as_object ( ) . to_owned ( ) ) . collect ( ) ;
1040+ let elements: Vec < PyObjectRef > = self . mro_map_collect ( |x| x. as_object ( ) . to_owned ( ) ) ;
9691041 PyTuple :: new_unchecked ( elements. into_boxed_slice ( ) )
9701042 }
9711043
@@ -996,7 +1068,7 @@ impl Py<PyType> {
9961068
9971069 #[ pymethod]
9981070 fn mro ( & self ) -> Vec < PyObjectRef > {
999- self . iter_mro ( ) . map ( |cls| cls. to_owned ( ) . into ( ) ) . collect ( )
1071+ self . mro_map_collect ( |cls| cls. to_owned ( ) . into ( ) )
10001072 }
10011073}
10021074
@@ -1226,12 +1298,11 @@ pub(crate) fn call_slot_new(
12261298 args : FuncArgs ,
12271299 vm : & VirtualMachine ,
12281300) -> PyResult {
1229- for cls in typ. deref ( ) . iter_mro ( ) {
1230- if let Some ( slot_new) = cls. slots . new . load ( ) {
1231- return slot_new ( subtype, args, vm) ;
1232- }
1233- }
1234- unreachable ! ( "Should be able to find a new slot somewhere in the mro" )
1301+ let slot_new = typ
1302+ . deref ( )
1303+ . mro_find_map ( |cls| cls. slots . new . load ( ) )
1304+ . expect ( "Should be able to find a new slot somewhere in the mro" ) ;
1305+ slot_new ( subtype, args, vm)
12351306}
12361307
12371308pub ( super ) fn or_ ( zelf : PyObjectRef , other : PyObjectRef , vm : & VirtualMachine ) -> PyObjectRef {
0 commit comments