From 9f6c9e3a468ead78e6b88743c74649f713b0c3f0 Mon Sep 17 00:00:00 2001 From: Ray Speth Date: Mon, 12 Jan 2015 19:55:37 +0000 Subject: [PATCH] [ck2cti] Fix reading some input files containing non-unicode characters In Python 3, attempting to convert some mechanisms would give errors like the following: UnicodeDecodeError: 'utf-8' codec can't decode byte 0x96 in position 348: invalid start byte --- interfaces/cython/cantera/ck2cti.py | 9 +++++++++ test/data/pdep-test.inp | 1 + 2 files changed, 10 insertions(+) diff --git a/interfaces/cython/cantera/ck2cti.py b/interfaces/cython/cantera/ck2cti.py index a5d58a808..b0b958e85 100644 --- a/interfaces/cython/cantera/ck2cti.py +++ b/interfaces/cython/cantera/ck2cti.py @@ -64,15 +64,24 @@ ENERGY_UNITS = {'CAL/': 'cal/mol', 'KJOULES/MOL': 'kJ/mol', 'KJOULES/MOLE': 'kJ/mol'} +_open = open if sys.version_info[0] == 2: string_types = (str, unicode) def strip_nonascii(s): return s.decode('ascii', 'ignore') + + def open(filename, *args): + return _open(filename, *args) + else: string_types = (str,) def strip_nonascii(s): return s.encode('ascii', 'ignore').decode() + def open(filename, *args): + return _open(filename, *args, errors='ignore') + + def compatible_quantities(quantity_basis, units): if quantity_basis == 'mol': return 'molec' not in units diff --git a/test/data/pdep-test.inp b/test/data/pdep-test.inp index 84efc89d8..651d98362 100644 --- a/test/data/pdep-test.inp +++ b/test/data/pdep-test.inp @@ -1,4 +1,5 @@ ! Generic mechanism header +! This line contains a non-unicode character (–) that should just be ignored. ELEMENTS H C END