Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/checkpoint-restore/criu.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAdrian Reber <areber@redhat.com>2021-03-10 20:14:41 +0300
committerAndrei Vagin <avagin@gmail.com>2021-09-03 20:31:00 +0300
commitbf80fee4f4a7cb93fa804b3ed41f6ac29cd2bddc (patch)
tree8420ba01f34abeb0082483074329d60d051a839c /lib
parent9635d6496e974ce5b1ef5e813f1d296c97451c5a (diff)
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 <areber@redhat.com>
Diffstat (limited to 'lib')
-rwxr-xr-xlib/py/cli.py26
1 files changed, 20 insertions, 6 deletions
diff --git a/lib/py/cli.py b/lib/py/cli.py
index df96729e2..1d8b8515f 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):