mirror of
https://github.com/checkpoint-restore/criu
synced 2025-09-01 23:05:39 +00:00
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>
This commit is contained in:
committed by
Andrei Vagin
parent
9635d6496e
commit
bf80fee4f4
@@ -11,14 +11,28 @@ def inf(opts):
|
|||||||
if opts['in']:
|
if opts['in']:
|
||||||
return open(opts['in'], 'rb')
|
return open(opts['in'], 'rb')
|
||||||
else:
|
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']:
|
if opts['out']:
|
||||||
return open(opts['out'], 'wb+')
|
mode = 'wb+'
|
||||||
|
if decode:
|
||||||
|
mode = 'w+'
|
||||||
|
return open(opts['out'], mode)
|
||||||
else:
|
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):
|
def dinf(opts, name):
|
||||||
@@ -39,7 +53,7 @@ def decode(opts):
|
|||||||
if opts['pretty']:
|
if opts['pretty']:
|
||||||
indent = 4
|
indent = 4
|
||||||
|
|
||||||
f = outf(opts)
|
f = outf(opts, True)
|
||||||
json.dump(img, f, indent=indent)
|
json.dump(img, f, indent=indent)
|
||||||
if f == sys.stdout:
|
if f == sys.stdout:
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
@@ -47,7 +61,7 @@ def decode(opts):
|
|||||||
|
|
||||||
def encode(opts):
|
def encode(opts):
|
||||||
img = json.load(inf(opts))
|
img = json.load(inf(opts))
|
||||||
pycriu.images.dump(img, outf(opts))
|
pycriu.images.dump(img, outf(opts, False))
|
||||||
|
|
||||||
|
|
||||||
def info(opts):
|
def info(opts):
|
||||||
|
Reference in New Issue
Block a user