Skip to content

Commit c8b0aec

Browse files
committed
More
1 parent eeb686a commit c8b0aec

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

src/singledispatch/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ impl SingleDispatchState {
8787
&& !cls_mro.contains(typ)
8888
&& !cls_mro.contains(m)
8989
&& Builtins::cached(py)
90-
.issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py))
91-
.is_ok_and(|res| res)
90+
.issubclass(py, m.wrapped().bind(py), typ.wrapped().bind(py))
91+
.is_ok_and(|res| res)
9292
{
9393
return Err(PyRuntimeError::new_err(format!(
9494
"Ambiguous dispatch: {m} or {typ}"
@@ -98,7 +98,7 @@ impl SingleDispatchState {
9898
eprintln!("MRO match: {m}");
9999
break;
100100
}
101-
_ => {},
101+
_ => {}
102102
}
103103
}
104104
let impl_fn = match mro_match {

src/singledispatch/mro.rs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use pyo3::exceptions::PyRuntimeError;
55
use pyo3::prelude::*;
66
use pyo3::types::PyTuple;
77
use pyo3::{intern, Bound, PyObject, PyResult, Python};
8+
use std::borrow::Borrow;
89
use std::cmp::Reverse;
910
use std::collections::hash_map::Keys;
1011
use 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+
121142
fn 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

Comments
 (0)