diff --git a/interfaces/cython/cantera/test/test_utils.py b/interfaces/cython/cantera/test/test_utils.py index 483c1665d71..7b61733579f 100644 --- a/interfaces/cython/cantera/test/test_utils.py +++ b/interfaces/cython/cantera/test/test_utils.py @@ -168,3 +168,17 @@ def test_unconvertible(self): def test_unconvertible2(self): self.check_raises([3+4j, 1-2j], ct.CanteraError, "Unable to convert") + + def test_yaml(self): + out, held_type = _py_to_any_to_py("{a: 1, b: 2., c: eggs, d: True}") + self.assertEqual(out, {"a": 1, "b": 2., "c": "eggs", "d": True}) + self.assertEqual(held_type, "AnyMap") + + def test_mixed_yaml(self): + out, held_type = _py_to_any_to_py({"a": 1, "b": 2., "c": "{d: eggs}"}) + self.assertEqual(out, {"a": 1, "b": 2., "c": {"d": "eggs"}}) + self.assertEqual(held_type, "AnyMap") + + def test_invalid_yaml(self): + # a regular string is returned as the input is not a valid YAML sequence + self.check_conversion("a: 1, b: 2., c: eggs, d: True", "string") diff --git a/interfaces/cython/cantera/utils.pyx b/interfaces/cython/cantera/utils.pyx index 2da49172bf1..640c7c2ca28 100644 --- a/interfaces/cython/cantera/utils.pyx +++ b/interfaces/cython/cantera/utils.pyx @@ -284,7 +284,10 @@ cdef CxxAnyValue python_to_anyvalue(item, name=None) except *: else: v = list_to_anyvalue(item) elif isinstance(item, str): - v = stringify(item) + try: + v = AnyMapFromYamlString(stringify(item)) + except BaseException: + v = stringify(item) elif isinstance(item, bool): v = (item) elif isinstance(item, int):