Patchwork [3,of,6] py3: reinvent print() function for contrib/hgclient.py

login
register
mail settings
Submitter Yuya Nishihara
Date Oct. 16, 2018, 7:26 a.m.
Message ID <94b452b2cfab5442a50b.1539674802@mimosa>
Download mbox | patch
Permalink /patch/36026/
State Accepted
Headers show

Comments

Yuya Nishihara - Oct. 16, 2018, 7:26 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1539666247 -7200
#      Tue Oct 16 07:04:07 2018 +0200
# Node ID 94b452b2cfab5442a50b43daf3c6796c8ce8126b
# Parent  ec5c140cee73da79dc3f48c4e75991cf6e34b7cd
py3: reinvent print() function for contrib/hgclient.py

Patch

diff --git a/contrib/hgclient.py b/contrib/hgclient.py
--- a/contrib/hgclient.py
+++ b/contrib/hgclient.py
@@ -2,6 +2,7 @@ 
 
 from __future__ import absolute_import, print_function
 import os
+import re
 import signal
 import socket
 import struct
@@ -19,9 +20,14 @@  except ImportError:
 if sys.version_info[0] >= 3:
     stdout = sys.stdout.buffer
     stderr = sys.stderr.buffer
+    def bprint(*args):
+        # remove b'' as well for ease of test migration
+        pargs = [re.sub(br'''\bb(['"])''', br'\1', b'%s' % a) for a in args]
+        stdout.write(b' '.join(pargs) + b'\n')
 else:
     stdout = sys.stdout
     stderr = sys.stderr
+    bprint = print
 
 def connectpipe(path=None):
     cmdline = [b'hg', b'serve', b'--cmdserver', b'pipe']
@@ -90,7 +96,7 @@  def sep(text):
 
 def runcommand(server, args, output=stdout, error=stderr, input=None,
                outfilter=lambda x: x):
-    print(b'*** runcommand', b' '.join(args))
+    bprint(b'*** runcommand', b' '.join(args))
     stdout.flush()
     server.stdin.write(b'runcommand\n')
     writeblock(server, b'\0'.join(args))
@@ -113,10 +119,10 @@  def runcommand(server, args, output=stdo
         elif ch == b'r':
             ret, = struct.unpack('>i', data)
             if ret != 0:
-                print(b' [%d]' % ret)
+                bprint(b' [%d]' % ret)
             return ret
         else:
-            print(b"unexpected channel %c: %r" % (ch, data))
+            bprint(b"unexpected channel %c: %r" % (ch, data))
             if ch.isupper():
                 return