From 27072102cd28f52285ec5dfb4704dc5fca0bb663 Mon Sep 17 00:00:00 2001 From: "Matthew W. Thompson" Date: Thu, 22 Aug 2024 13:39:37 -0500 Subject: [PATCH] Ensure XML files are opened with UTF-8 encoding --- environment-dev.yml | 2 +- foyer/forcefield.py | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/environment-dev.yml b/environment-dev.yml index 5d2c94c9..83fc2aaf 100644 --- a/environment-dev.yml +++ b/environment-dev.yml @@ -13,7 +13,7 @@ dependencies: - openmm - parmed>=3.4.3 - ele - - openff-toolkit >=0.11 + - openff-toolkit-base >=0.11 - pip - pre-commit - pytest diff --git a/foyer/forcefield.py b/foyer/forcefield.py index 3c6ecdc4..d161a23a 100644 --- a/foyer/forcefield.py +++ b/foyer/forcefield.py @@ -66,15 +66,17 @@ def preprocess_forcefield_files(forcefield_files=None): for xml_file in forcefield_files: if not hasattr(xml_file, "read"): - f = open(xml_file) + try: + f = open(xml_file, encoding="utf-8") + xml_contents = f.read() + finally: + f.close() + _, suffix = os.path.split(xml_file) else: - f = xml_file + xml_contents = xml_file.read() suffix = "" - # read and preprocess - xml_contents = f.read() - f.close() xml_contents = re.sub( r"(def\w*=\w*[\"\'])(.*)([\"\'])", lambda m: m.group(1)