Check if stdin has isatty attribute

When the stdin is closed, it has no isatty atrribute which leads to a KeyError.

Closes-Bug: #1980890
Change-Id: If9a3bf68b8dfd953b346697241166578d18bb563
(cherry picked from commit 8df9328a60)
(cherry picked from commit 2ad5f0a6b0)
This commit is contained in:
Benedikt Loeffler 2021-10-13 13:50:36 +02:00 committed by Cyril Roelandt
parent 0b4d5a6200
commit f0ccd09ad5
2 changed files with 3 additions and 3 deletions

View File

@ -405,7 +405,7 @@ def get_data_file(args):
except OSError:
# (1) stdin is not valid (closed...)
return None
if not sys.stdin.isatty():
if hasattr(sys.stdin, 'isatty') and not sys.stdin.isatty():
# (2) image data is provided through standard input
image = sys.stdin
if hasattr(sys.stdin, 'buffer'):

View File

@ -93,7 +93,7 @@ def do_image_create(gc, args):
backend = args.store
file_name = fields.pop('file', None)
using_stdin = not sys.stdin.isatty()
using_stdin = hasattr(sys.stdin, 'isatty') and not sys.stdin.isatty()
if args.store and not (file_name or using_stdin):
utils.exit("--store option should only be provided with --file "
"option or stdin.")
@ -204,7 +204,7 @@ def do_image_create_via_import(gc, args):
fields[key] = value
file_name = fields.pop('file', None)
using_stdin = not sys.stdin.isatty()
using_stdin = hasattr(sys.stdin, 'isatty') and not sys.stdin.isatty()
# special processing for backward compatibility with image-create
if args.import_method is None and (file_name or using_stdin):