From 87751ec48ec5e237a08c3f64b8c0ae063ae4de2a Mon Sep 17 00:00:00 2001 From: Philipp Gesang Date: Fri, 1 Dec 2017 12:33:11 +0100 Subject: [PATCH] adapt cnfvar stdout writer for python3 stdio output still trips over the bytes / string separation. Fix it by defaulting to bytes which mirrors the reader. Add optional string writer for use with direct text output. --- src/cnfvar.py | 46 +++++++++++++++++++++++++++++++--------------- 1 files changed, 31 insertions(+), 15 deletions(-) diff --git a/src/cnfvar.py b/src/cnfvar.py index b31f6f3..04e97b2 100644 --- a/src/cnfvar.py +++ b/src/cnfvar.py @@ -513,14 +513,14 @@ def renumber_vars(root, parent=None): # serialization # -cnf_line_nest_indent = " " -cnf_line_base_fmt = "%d %s,%d: \"%s\"" -cnf_line_child_fmt = "%d %s(%d) %s,%d: \"%s\"" +cnf_line_nest_indent = b" " +cnf_line_base_fmt = b"%d %s,%d: \"%s\"" +cnf_line_child_fmt = b"%d %s(%d) %s,%d: \"%s\"" def format_cnf_vars(da, var): """ - Return a list of formatted cnf_line strings. + Return a list of formatted cnf_line byte strings. """ depth, acc = da @@ -530,19 +530,19 @@ def format_cnf_vars(da, var): % (var["number"], cnf_line_nest_indent * depth, var["parent"], - to_latin1(var["varname"]).decode (), + to_latin1(var["varname"]), var["instance"], - to_latin1(var["data"]).decode ()) + to_latin1(var["data"])) else: line = cnf_line_base_fmt \ % (var["number"], - to_latin1(var["varname"]).decode (), + to_latin1(var["varname"]), var["instance"], - to_latin1(var["data"]).decode ()) + to_latin1(var["data"])) comment = var.get("comment", None) - if comment and comment is not "": - line = line + (" # %s" % to_latin1(comment)) + if comment and len (comment) != 0: + line = line + (b" # %s" % to_latin1(comment)) acc.append(line) @@ -571,7 +571,7 @@ def write_cnf_raw(*argv, **kw_argv): print_cnf_raw(*argv, **kw_argv) -def output_cnf(root, out, renumber=False): +def output_cnf(root, out, renumber=False, bytes=True): cnf = cnf_root(root) # Cthulhu (a.k.a Autotest) hates us so much he replaced the innocuous # stdout with some chimera that isn't derived from ``file``, rendering @@ -582,8 +582,25 @@ def output_cnf(root, out, renumber=False): _count = renumber(root) if is_cnf(cnf) is True: (_, lines) = functools.reduce(format_cnf_vars, cnf, (0, [])) - out.write("\n".join(lines)) - out.write("\n") + if bytes is True: + out.write ("\n".join (map (from_latin1, lines))) + out.write ("\n") + else: + out.write (b"\n".join (lines)) + out.write (b"\n") + + +def dump_cnf_bytes (root, renumber=False): + """ + dump_cnf_bytes -- Serialize CNF var structure, returning the result as a + byte sequence. + """ + cnf = cnf_root(root) + out = io.BytesIO() + output_cnf(root, out, renumber=renumber, bytes=True) + res = out.getvalue() + out.close() + return res def dump_cnf_string(root, renumber=False): @@ -593,12 +610,11 @@ def dump_cnf_string(root, renumber=False): """ cnf = cnf_root(root) out = io.StringIO() - output_cnf(root, out, renumber=renumber) + output_cnf(root, out, renumber=renumber, bytes=False) res = out.getvalue() out.close() return res - def print_cnf(root, out=None, renumber=False): if root is not None: output_cnf(root, out or sys.stdout, renumber=renumber) -- 1.7.1