@@ -5,6 +5,7 @@ use pyo3::exceptions::PyRuntimeError;
55use pyo3:: prelude:: * ;
66use pyo3:: types:: PyTuple ;
77use pyo3:: { intern, Bound , PyObject , PyResult , Python } ;
8+ use std:: borrow:: Borrow ;
89use std:: cmp:: Reverse ;
910use std:: collections:: hash_map:: Keys ;
1011use std:: collections:: HashSet ;
@@ -118,6 +119,26 @@ fn c3_boundary(py: Python, bases: &[PyTypeReference]) -> PyResult<usize> {
118119 Ok ( boundary)
119120}
120121
122+ fn sub_c3_mro < I , G > (
123+ py : Python ,
124+ bases : I ,
125+ abcs : & Vec < & PyTypeReference > ,
126+ ) -> PyResult < Vec < Vec < PyTypeReference > > >
127+ where
128+ G : Borrow < PyTypeReference > ,
129+ I : Iterator < Item = G > ,
130+ {
131+ let mut v: Vec < Vec < PyTypeReference > > = Vec :: new ( ) ;
132+ for b in bases {
133+ v. push ( c3_mro (
134+ py,
135+ b. borrow ( ) . wrapped ( ) . bind ( py) ,
136+ abcs. iter ( ) . map ( |abc| abc. clone_ref ( py) ) . collect ( ) ,
137+ ) ?) ;
138+ }
139+ Ok ( v)
140+ }
141+
121142fn c3_mro (
122143 py : Python ,
123144 cls : & Bound < ' _ , PyAny > ,
@@ -164,34 +185,13 @@ fn c3_mro(
164185 let mut cls_ref = vec ! [ PyTypeReference :: new( cls. clone( ) . unbind( ) ) ] ;
165186 mros. push ( & mut cls_ref) ;
166187
167- let mut explicit_bases_mro = Vec :: from_iter ( explicit_bases. iter ( ) . map ( |b| {
168- c3_mro (
169- py,
170- b. wrapped ( ) . bind ( py) ,
171- new_abcs. iter ( ) . map ( |abc| abc. clone_ref ( py) ) . collect ( ) ,
172- )
173- . unwrap ( )
174- } ) ) ;
188+ let mut explicit_bases_mro = sub_c3_mro ( py, explicit_bases. iter ( ) , & new_abcs) ?;
175189 mros. extend ( & mut explicit_bases_mro) ;
176190
177- let mut abstract_bases_mro = Vec :: from_iter ( abstract_bases. iter ( ) . map ( |b| {
178- c3_mro (
179- py,
180- b. wrapped ( ) . bind ( py) ,
181- new_abcs. iter ( ) . map ( |abc| abc. clone_ref ( py) ) . collect ( ) ,
182- )
183- . unwrap ( )
184- } ) ) ;
191+ let mut abstract_bases_mro = sub_c3_mro ( py, abstract_bases. iter ( ) . map ( |v| * v) , & new_abcs) ?;
185192 mros. extend ( & mut abstract_bases_mro) ;
186193
187- let mut other_bases_mro = Vec :: from_iter ( other_bases. iter ( ) . map ( |b| {
188- c3_mro (
189- py,
190- b. wrapped ( ) . bind ( py) ,
191- new_abcs. iter ( ) . map ( |abc| abc. clone_ref ( py) ) . collect ( ) ,
192- )
193- . unwrap ( )
194- } ) ) ;
194+ let mut other_bases_mro = sub_c3_mro ( py, other_bases. iter ( ) , & new_abcs) ?;
195195 mros. extend ( & mut other_bases_mro) ;
196196
197197 let mut explicit_bases_cloned = Vec :: from_iter ( explicit_bases. iter ( ) . map ( |b| b. clone_ref ( py) ) ) ;
0 commit comments