From 96eb61d7b5e2d7da8804a4f49eebd5ca46aeefd3 Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Thu, 7 Feb 2019 15:25:04 +0100 Subject: [PATCH] accept both bytes and str for cnfvar readers Make both the CNF format and the JSON readers handle bytes and str transparently. (Note there is still a deplorable asymmetry in the routines such that the JSON path will convert to *str* whereas the CNF part, assuming single-byte encoded data, will convert to *bytes*. This is necessary for compatibility reasons until the Intranator gets a more recent Python interpreter.) --- src/cnfvar.py | 5 ++++- 1 files changed, 4 insertions(+), 1 deletions(-) diff --git a/src/cnfvar.py b/src/cnfvar.py index 1b6b5bb..a05fe0e 100644 --- a/src/cnfvar.py +++ b/src/cnfvar.py @@ -300,7 +300,8 @@ def read_cnf_json(cnfdata): .. note:: The JSON module does not decode data for all versions of Python 3 so we handle the decoding ourselves. """ - cnfdata = cnfdata.decode('iso-8859-1') + if isinstance (cnfdata, bytes) is True: + cnfdata = from_latin1 (cnfdata) cnf_json = json.loads(cnfdata, object_hook=make_varname_lowercase) if is_cnf(cnf_json) is False: raise TypeError("Invalid CNF_VAR.") @@ -553,6 +554,8 @@ def read_cnf(data): :return: the parsed cnf data :rtype: {str, {str, str or int}} """ + if isinstance (data, str): + data = to_latin1 (data) state = prepare(data) if state is None: raise InvalidCNF("Empty input string.") -- 1.7.1