From df484f92a033b0e7db5f6c539e2a24acd14cf60f Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sat, 13 Mar 2021 14:20:42 -0500 Subject: [PATCH 1/2] Make call always return stdout as a string, optionally force binary data. --- inkex/command.py | 10 +++++++++- scribus_export_pdf.py | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/inkex/command.py b/inkex/command.py index d56de21ba..29c027973 100644 --- a/inkex/command.py +++ b/inkex/command.py @@ -33,6 +33,7 @@ it yourself, to take advantage of the security settings and testing functions. import os import sys + from subprocess import Popen, PIPE from tempfile import TemporaryDirectory from lxml.etree import ElementTree @@ -178,7 +179,14 @@ def call(program, *args, **kwargs): Will raise ProgramRunError() if return code is not 0. """ - return _call(program, *args, **kwargs) + # We use this long input because it's less likely to conflict with --binary= + binary = kwargs.pop('return_binary', False) + stdout = _call(program, *args, **kwargs) + # Convert binary to string when we wish to have strings we do this here + # so the mock tests will also run the conversion (always returns bytes) + if not binary and isinstance(stdout, bytes): + return stdout.decode(sys.stdout.encoding or 'utf-8') + return stdout def inkscape(svg_file, *args, **kwargs): """ diff --git a/scribus_export_pdf.py b/scribus_export_pdf.py index 8da42d370..ec3f4c1bb 100644 --- a/scribus_export_pdf.py +++ b/scribus_export_pdf.py @@ -111,7 +111,7 @@ class exportPDF(): exportPDF()""") def save(self, stream): - scribus_version = call(SCRIBUS_EXE, '-g', '--version').decode('utf-8') + scribus_version = call(SCRIBUS_EXE, '-g', '--version') version_match = VERSION_REGEX.search(scribus_version) if version_match is None: raise AbortExtension(f"Could not detect Scribus version ({scribus_version})") -- GitLab From df253dc3057a2114f60fb7d8cfbfe9553d533094 Mon Sep 17 00:00:00 2001 From: Martin Owens Date: Sat, 13 Mar 2021 14:23:55 -0500 Subject: [PATCH 2/2] Improve documentation for call function --- inkex/command.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/inkex/command.py b/inkex/command.py index 29c027973..9872ecc82 100644 --- a/inkex/command.py +++ b/inkex/command.py @@ -178,6 +178,10 @@ def call(program, *args, **kwargs): stdout = call('executable', arg1, arg2, dash_dash_arg='foo', d=True, ...) Will raise ProgramRunError() if return code is not 0. + + * return_binary - Should stdout return raw bytes (default: False) + * stdin - The string or bytes containing the stdin (default: None) + * All other arguments converted using to_args(...) function. """ # We use this long input because it's less likely to conflict with --binary= binary = kwargs.pop('return_binary', False) @@ -190,7 +194,7 @@ def call(program, *args, **kwargs): def inkscape(svg_file, *args, **kwargs): """ - Call Inkscape with the given svg_file and the given arguments + Call Inkscape with the given svg_file and the given arguments, see call() """ return call(INKSCAPE_EXECUTABLE_NAME, svg_file, *args, **kwargs) -- GitLab