From: Philipp Gesang Date: Thu, 7 Feb 2019 14:25:04 +0000 (+0100) Subject: accept both bytes and str for cnfvar readers X-Git-Tag: v1.4~12^2~1 X-Git-Url: http://developer.intra2net.com/git/?a=commitdiff_plain;h=96eb61d7b5e2d7da8804a4f49eebd5ca46aeefd3;p=pyi2ncommon 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.) --- 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.")