From 46b6abb3c7b00aff4ed895b44d042810bfac7a7d Mon Sep 17 00:00:00 2001 From: MMGen Date: Tue, 26 Mar 2019 13:04:47 +0000 Subject: [PATCH] use file io for terminal output --- mmgen/globalvars.py | 4 ++-- mmgen/main_autosign.py | 1 + mmgen/term.py | 6 ++++-- mmgen/util.py | 9 +++++---- test/test_py_d/common.py | 6 +++--- 5 files changed, 15 insertions(+), 11 deletions(-) diff --git a/mmgen/globalvars.py b/mmgen/globalvars.py index 74758b3a..e7b84a52 100755 --- a/mmgen/globalvars.py +++ b/mmgen/globalvars.py @@ -49,8 +49,8 @@ class g(object): max_int = 0xffffffff stdin_tty = bool(sys.stdin.isatty() or os.getenv('MMGEN_TEST_SUITE_POPEN_SPAWN')) - stdout_fileno = sys.stdout.fileno() - stderr_fileno = sys.stderr.fileno() + stdout = sys.stdout + stderr = sys.stderr http_timeout = 60 diff --git a/mmgen/main_autosign.py b/mmgen/main_autosign.py index 6abd84ac..9d5bfd85 100755 --- a/mmgen/main_autosign.py +++ b/mmgen/main_autosign.py @@ -363,6 +363,7 @@ def do_loop(): prev_status = status if not n % 10: msg_r('\r{}\rWaiting'.format(' '*17)) + sys.stderr.flush() time.sleep(1) msg_r('.') n += 1 diff --git a/mmgen/term.py b/mmgen/term.py index 33bfd704..a7475cb3 100755 --- a/mmgen/term.py +++ b/mmgen/term.py @@ -57,8 +57,8 @@ def _kb_hold_protect_unix(): # Request 5 bytes to cover escape sequences generated by F1, F2, .. Fn keys (5 bytes) # as well as UTF8 chars (4 bytes max). def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True,num_chars=5): - fd_err = sys.stderr.fileno() - os.write(fd_err,prompt.encode()) + msg_r(prompt) + sys.stderr.flush() timeout = float(0.3) fd = sys.stdin.fileno() old = termios.tcgetattr(fd) @@ -81,6 +81,7 @@ def _get_keypress_unix(prompt='',immed_chars='',prehold_protect=True,num_chars=5 def _get_keypress_unix_raw(prompt='',immed_chars='',prehold_protect=None,num_chars=5): msg_r(prompt) + sys.stderr.flush() fd = sys.stdin.fileno() old = termios.tcgetattr(fd) tty.setcbreak(fd) @@ -90,6 +91,7 @@ def _get_keypress_unix_raw(prompt='',immed_chars='',prehold_protect=None,num_cha def _get_keypress_unix_stub(prompt='',immed_chars='',prehold_protect=None,num_chars=None): msg_r(prompt) + sys.stderr.flush() return sys.stdin.read(1).encode() #_get_keypress_unix_stub = _get_keypress_unix diff --git a/mmgen/util.py b/mmgen/util.py index 4bee1489..8df2c5f6 100755 --- a/mmgen/util.py +++ b/mmgen/util.py @@ -26,10 +26,10 @@ from string import hexdigits,digits from mmgen.color import * from mmgen.exception import * -def msg(s): os.write(g.stderr_fileno,s.encode() + b'\n') -def msg_r(s): os.write(g.stderr_fileno,s.encode()) -def Msg(s): os.write(g.stdout_fileno,s.encode() + b'\n') -def Msg_r(s): os.write(g.stdout_fileno,s.encode()) +def msg(s): g.stderr.write(s + '\n') +def msg_r(s): g.stderr.write(s) +def Msg(s): g.stdout.write(s + '\n') +def Msg_r(s): g.stdout.write(s) def msgred(s): msg(red(s)) def rmsg(s): msg(red(s)) @@ -750,6 +750,7 @@ def my_raw_input(prompt,echo=True,insert_txt='',use_readline=True): if g.test_suite_popen_spawn: msg(prompt) + sys.stderr.flush() reply = os.read(0,4096).decode() elif echo or not sys.stdin.isatty(): reply = input(prompt) diff --git a/test/test_py_d/common.py b/test/test_py_d/common.py index b43f4837..31c89f6b 100755 --- a/test/test_py_d/common.py +++ b/test/test_py_d/common.py @@ -99,12 +99,12 @@ else: devnull_fh = open('/dev/null','w') def silence(): if not (opt.verbose or opt.exact_output): - g.stderr_fileno = g.stdout_fileno = devnull_fh.fileno() + g.stdout = g.stderr = devnull_fh def end_silence(): if not (opt.verbose or opt.exact_output): - g.stderr_fileno = 2 - g.stdout_fileno = 1 + g.stdout = sys.stdout + g.stderr = sys.stderr def randbool(): return os.urandom(1).hex()[0] in '02468ace'