accept both bytes and str for cnfvar readers
authorPhilipp Gesang <philipp.gesang@intra2net.com>
Thu, 7 Feb 2019 14:25:04 +0000 (15:25 +0100)
committerChristian Herdtweck <christian.herdtweck@intra2net.com>
Thu, 7 Feb 2019 15:50:34 +0000 (16:50 +0100)
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

index 1b6b5bb..a05fe0e 100644 (file)
@@ -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.")