Skip to content

Commit 5980e5b

Browse files
committed
fix working
1 parent 0cd11fe commit 5980e5b

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

src/input/input_python.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,20 @@ use super::{
4848
Input,
4949
};
5050

51+
static ORDERED_DICT_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
52+
53+
fn get_ordered_dict_type(py: Python<'_>) -> &Bound<'_, PyType> {
54+
ORDERED_DICT_TYPE
55+
.get_or_init(py, || {
56+
py.import("collections")
57+
.and_then(|collections_module| collections_module.getattr("OrderedDict"))
58+
.unwrap()
59+
.extract()
60+
.unwrap()
61+
})
62+
.bind(py)
63+
}
64+
5165
static FRACTION_TYPE: PyOnceLock<Py<PyType>> = PyOnceLock::new();
5266

5367
pub fn get_fraction_type(py: Python<'_>) -> &Bound<'_, PyType> {
@@ -399,6 +413,13 @@ impl<'py> Input<'py> for Bound<'py, PyAny> {
399413
}
400414

401415
fn lax_dict<'a>(&'a self) -> ValResult<GenericPyMapping<'a, 'py>> {
416+
let ordered_dict_type = get_ordered_dict_type(self.py());
417+
if self.is_instance(ordered_dict_type).unwrap_or(false) {
418+
// OrderedDict is a subclass of dict, but we want to treat it as a mapping to preserve order
419+
if let Ok(mapping) = self.downcast::<PyMapping>() {
420+
return Ok(GenericPyMapping::Mapping(mapping));
421+
}
422+
}
402423
if let Ok(dict) = self.downcast::<PyDict>() {
403424
Ok(GenericPyMapping::Dict(dict))
404425
} else if let Ok(mapping) = self.downcast::<PyMapping>() {

0 commit comments

Comments
 (0)