symbolize.py: fix exception when stdin is not a terminal
Commit 6b4fc6752b3c ("symbolize.py: disable terminal local echo") uses
termios functions on stdin unconditionally. Unfortunately, this will
cause an exception when stdin is not a terminal, for instance:
$ echo Hello | ./script/symbolize.py
Traceback (most recent call last):
File "./scripts/symbolize.py", line 497, in <module>
main()
File "./scripts/symbolize.py", line 484, in main
old = termios.tcgetattr(fd)
termios.error: (25, 'Inappropriate ioctl for device')
Fix the issue by making sure stdin is a TTY before using the termios
functions.
Fixes: 6b4fc6752b3c ("symbolize.py: disable terminal local echo")
Signed-off-by: Jerome Forissier <jerome@forissier.org>
Acked-by: Jens Wiklander <jens.wiklander@linaro.org>
diff --git a/scripts/symbolize.py b/scripts/symbolize.py
index b61202d..5928932 100755
--- a/scripts/symbolize.py
+++ b/scripts/symbolize.py
@@ -481,16 +481,20 @@
symbolizer = Symbolizer(sys.stdout, args.dirs, args.strip_path)
fd = sys.stdin.fileno()
- old = termios.tcgetattr(fd)
- new = termios.tcgetattr(fd)
- new[3] = new[3] & ~termios.ECHO # lflags
+ isatty = os.isatty(fd)
+ if isatty:
+ old = termios.tcgetattr(fd)
+ new = termios.tcgetattr(fd)
+ new[3] = new[3] & ~termios.ECHO # lflags
try:
- termios.tcsetattr(fd, termios.TCSADRAIN, new)
+ if isatty:
+ termios.tcsetattr(fd, termios.TCSADRAIN, new)
for line in sys.stdin:
symbolizer.write(line)
finally:
symbolizer.flush()
- termios.tcsetattr(fd, termios.TCSADRAIN, old)
+ if isatty:
+ termios.tcsetattr(fd, termios.TCSADRAIN, old)
if __name__ == "__main__":