Skip to content

Commit

Permalink
Support legacy ruamel.yaml loaders
Browse files Browse the repository at this point in the history
Upstream ruamel.yaml changes loaders in 0.17.0, with earlier loaders being
deprecated. However, the replacement loaders were introduced with the same
version. This update ensures that both loaders are being supported.
  • Loading branch information
ischoegl committed Jun 14, 2021
1 parent 3dc940d commit 4dec05e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
12 changes: 9 additions & 3 deletions interfaces/cython/cantera/ck2yaml.py
Original file line number Diff line number Diff line change
Expand Up @@ -1375,9 +1375,15 @@ def load_extra_file(self, path):
"""
Load YAML-formatted entries from ``path`` on disk.
"""
yaml_ = yaml.YAML()
with open(path, 'rt', encoding="utf-8") as stream:
yml = yaml_.load(stream)
try:
yaml_ = yaml.YAML(typ="rt")
with open(path, 'rt', encoding="utf-8") as stream:
yml = yaml_.load(stream)
except yaml.constructor.ConstructorError:
with open(path, "rt", encoding="utf-8") as stream:
# Ensure that the loader remains backward-compatible with legacy
# ruamel.yaml versions (prior to 0.17.0).
yml = yaml.round_trip_load(stream)

# do not overwrite reserved field names
reserved = {'generator', 'input-files', 'cantera-version', 'date',
Expand Down
17 changes: 11 additions & 6 deletions interfaces/cython/cantera/test/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,17 @@
CANTERA_DATA_PATH = Path(__file__).parents[1] / "data"


def load_yaml(yml_file, typ="safe"):
# Load YAML data from file. The YAML loader defaults to "safe".
yaml_ = yaml.YAML(typ=typ)
with open(yml_file, "rt", encoding="utf-8") as stream:
return yaml_.load(stream)

def load_yaml(yml_file):
# Load YAML data from file using the "safe" loading option.
try:
yaml_ = yaml.YAML(typ="safe")
with open(yml_file, "rt", encoding="utf-8") as stream:
return yaml_.load(stream)
except yaml.constructor.ConstructorError:
with open(yml_file, "rt", encoding="utf-8") as stream:
# Ensure that the loader remains backward-compatible with legacy
# ruamel.yaml versions (prior to 0.17.0).
return yaml.safe_load(stream)

class CanteraTest(unittest.TestCase):
@classmethod
Expand Down

0 comments on commit 4dec05e

Please sign in to comment.