From bf80fee4f4a7cb93fa804b3ed41f6ac29cd2bddc Mon Sep 17 00:00:00 2001 From: Adrian Reber Date: Wed, 10 Mar 2021 17:14:41 +0000 Subject: [PATCH] lib: correctly handle stdin/stdout (Python 3) This changes stdin to be opened as binary if the input is not a tty. This changes stdout to be opened as binary if encoding or if the output is not a tty. Signed-off-by: Adrian Reber --- lib/py/cli.py | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/py/cli.py b/lib/py/cli.py index df96729e2e..1d8b8515fc 100755 --- a/lib/py/cli.py +++ b/lib/py/cli.py @@ -11,14 +11,28 @@ def inf(opts): if opts['in']: return open(opts['in'], 'rb') else: - return sys.stdin + if (sys.version_info < (3, 0)): + return sys.stdin + if sys.stdin.isatty(): + # If we are reading from a terminal (not a pipe) we want text input and not binary + return sys.stdin + return sys.stdin.buffer -def outf(opts): +def outf(opts, decode): + # Decode means from protobuf to JSON. + # Use text when writing to JSON else use binaray mode if opts['out']: - return open(opts['out'], 'wb+') + mode = 'wb+' + if decode: + mode = 'w+' + return open(opts['out'], mode) else: - return sys.stdout + if (sys.version_info < (3, 0)): + return sys.stdout + if decode: + return sys.stdout + return sys.stdout.buffer def dinf(opts, name): @@ -39,7 +53,7 @@ def decode(opts): if opts['pretty']: indent = 4 - f = outf(opts) + f = outf(opts, True) json.dump(img, f, indent=indent) if f == sys.stdout: f.write("\n") @@ -47,7 +61,7 @@ def decode(opts): def encode(opts): img = json.load(inf(opts)) - pycriu.images.dump(img, outf(opts)) + pycriu.images.dump(img, outf(opts, False)) def info(opts):