Browse Source

py3port: use os.write() instead of sys.std{out,err}.write() for terminal output

MMGen 6 years ago
parent
commit
a97995c6cb
4 changed files with 22 additions and 26 deletions
  1. 4 0
      mmgen/globalvars.py
  2. 5 4
      mmgen/util.py
  3. 2 6
      test/mmgen_pexpect.py
  4. 11 16
      test/test.py

+ 4 - 0
mmgen/globalvars.py

@@ -49,7 +49,11 @@ class g(object):
 	Cdates    = '2013-2018'
 	keywords  = 'Bitcoin, BTC, cryptocurrency, wallet, cold storage, offline, online, spending, open-source, command-line, Python, Linux, Bitcoin Core, bitcoind, hd, deterministic, hierarchical, secure, anonymous, Electrum, seed, mnemonic, brainwallet, Scrypt, utility, script, scriptable, blockchain, raw, transaction, permissionless, console, terminal, curses, ansi, color, tmux, remote, client, daemon, RPC, json, entropy, xterm, rxvt, PowerShell, MSYS, MinGW, mswin, Armbian, Raspbian, Raspberry Pi, Orange Pi, BCash, BCH, Litecoin, LTC, altcoin, ZEC, Zcash, DASH, Dashpay, ETH, Ethereum, Classic, SHA256Compress, XMR, Monero, monerod, EMC, Emercoin, ERC20, token, deploy, contract, gas, fee, smart contract, solidity, Parity, testnet, devmode, Kovan'
 	max_int   = 0xffffffff
+
 	stdin_tty = bool(sys.stdin.isatty() or os.getenv('MMGEN_TEST_SUITE'))
+	stdout_fileno = sys.stdout.fileno()
+	stderr_fileno = sys.stderr.fileno()
+
 	http_timeout = 60
 
 	# Variables - these might be altered at runtime:

+ 5 - 4
mmgen/util.py

@@ -27,10 +27,11 @@ from string import hexdigits
 from mmgen.color import *
 from mmgen.exception import *
 
-def msg(s):    sys.stderr.write(s.encode() + b'\n')
-def msg_r(s):  sys.stderr.write(s.encode())
-def Msg(s):    sys.stdout.write(s.encode() + b'\n')
-def Msg_r(s):  sys.stdout.write(s.encode())
+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 msgred(s): msg(red(s))
 def rmsg(s):   msg(red(s))
 def rmsg_r(s): msg_r(red(s))

+ 2 - 6
test/mmgen_pexpect.py

@@ -35,10 +35,6 @@ else:
 	send_delay = 0
 	os.environ['MMGEN_DISABLE_HOLD_PROTECT'] = '1'
 
-stderr_save = sys.stderr
-def errmsg(s): stderr_save.write(s+'\n')
-def errmsg_r(s): stderr_save.write(s)
-
 def my_send(p,t,delay=send_delay,s=False):
 	if delay: time.sleep(delay)
 	ret = p.send(t) # returns num bytes written
@@ -126,10 +122,10 @@ class MMGenPexpect(object):
 		if not no_msg:
 			if opt.verbose or opt.print_cmdline or opt.exact_output:
 				clr1,clr2,eol = ((green,cyan,'\n'),(nocolor,nocolor,' '))[bool(opt.print_cmdline)]
-				sys.stderr.write(green('Testing: {}\n'.format(desc)))
+				msg_r(green('Testing: {}\n'.format(desc)))
 				if not msg_only:
 					s = repr(cmd_str) if g.platform == 'win' else cmd_str
-					sys.stderr.write(clr1('Executing {}{}'.format(clr2(s),eol)))
+					msg_r(clr1('Executing {}{}'.format(clr2(s),eol)))
 			else:
 				m = 'Testing {}: '.format(desc)
 				msg_r(m)

+ 11 - 16
test/test.py

@@ -1233,32 +1233,28 @@ def get_segwit_arg(cfg):
 # Tell spawned programs they're running in the test suite
 os.environ['MMGEN_TEST_SUITE'] = '1'
 
-def imsg(s): sys.stderr.write(s.encode('utf8') + '\n') # never gets redefined
+def get_segwit_arg(cfg):
+	return ['--type='+('segwit','bech32')[bool(opt.bech32)]] if cfg['segwit'] else []
 
 if opt.exact_output:
+	def imsg(s):   os.write(2,s.encode() + b'\n')
+	def imsg_r(s): os.write(2,s.encode())
 	def msg(s): pass
-	vmsg = vmsg_r = msg_r = msg
+	qmsg = qmsg_r = vmsg = vmsg_r = msg_r = msg
 else:
-	def msg(s): sys.stderr.write(s+'\n')
-	def vmsg(s):
-		if opt.verbose: sys.stderr.write(s+'\n')
-	def msg_r(s): sys.stderr.write(s)
-	def vmsg_r(s):
-		if opt.verbose: sys.stderr.write(s)
+	def imsg(s): pass
+	def imsg_r(s): pass
 
-stderr_save = sys.stderr
+devnull_fh = open('/dev/null','w')
 
 def silence():
 	if not (opt.verbose or opt.exact_output):
-		f = ('/dev/null','stderr.out')[g.platform=='win']
-		sys.stderr = open(f,'a')
+		g.stderr_fileno = g.stdout_fileno = devnull_fh.fileno()
 
 def end_silence():
 	if not (opt.verbose or opt.exact_output):
-		sys.stderr = stderr_save
-
-def errmsg(s): stderr_save.write(s+'\n')
-def errmsg_r(s): stderr_save.write(s)
+		g.stderr_fileno = 2
+		g.stdout_fileno = 1
 
 if opt.list_cmds:
 	from mmgen.term import get_terminal_size
@@ -3887,7 +3883,6 @@ except opt.traceback and Exception:
 	except: pass
 	die(1,blue('Test script exited with error'))
 except:
-	sys.stderr = stderr_save
 	raise
 
 end_msg()