Browse Source

use file io for terminal output

MMGen 6 years ago
parent
commit
46b6abb3c7
5 changed files with 15 additions and 11 deletions
  1. 2 2
      mmgen/globalvars.py
  2. 1 0
      mmgen/main_autosign.py
  3. 4 2
      mmgen/term.py
  4. 5 4
      mmgen/util.py
  5. 3 3
      test/test_py_d/common.py

+ 2 - 2
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
 

+ 1 - 0
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

+ 4 - 2
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

+ 5 - 4
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)

+ 3 - 3
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'