# 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
% (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)
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
_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):
"""
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)