[PATCH 2 of 5] keepalive: use print function

Gregory Szorc gregory.szorc at gmail.com
Sat Jan 2 13:54:44 CST 2016


# HG changeset patch
# User Gregory Szorc <gregory.szorc at gmail.com>
# Date 1434953694 25200
#      Sun Jun 21 23:14:54 2015 -0700
# Node ID 1242b5987618511e0bda9a2d506876c2fd10130c
# Parent  73efe1469d54a26d4b438efb3fdfac5d7947b6f6
keepalive: use print function

diff --git a/mercurial/keepalive.py b/mercurial/keepalive.py
--- a/mercurial/keepalive.py
+++ b/mercurial/keepalive.py
@@ -102,17 +102,17 @@ EXTRA ATTRIBUTES AND METHODS
   in python 2.4, so for the most consistent behavior across versions,
   you probably just want to use the defaults, which will give you
   exceptions on errors.
 
 """
 
 # $Id: keepalive.py,v 1.14 2006/04/04 21:00:32 mstenner Exp $
 
-from __future__ import absolute_import
+from __future__ import absolute_import, print_function
 
 import errno
 import httplib
 import socket
 import sys
 import thread
 import urllib2
 
@@ -534,23 +534,23 @@ def safesend(self, str):
             raise httplib.NotConnected
 
     # send the data to the server. if we get a broken pipe, then close
     # the socket. we want to reconnect when somebody tries to send again.
     #
     # NOTE: we DO propagate the error, though, because we cannot simply
     #       ignore the error... the caller will know if they can retry.
     if self.debuglevel > 0:
-        print "send:", repr(str)
+        print("send:", repr(str))
     try:
         blocksize = 8192
         read = getattr(str, 'read', None)
         if read is not None:
             if self.debuglevel > 0:
-                print "sending a read()able"
+                print("sending a read()able")
             data = read(blocksize)
             while data:
                 self.sock.sendall(data)
                 data = read(blocksize)
         else:
             self.sock.sendall(str)
     except socket.error as v:
         reraise = True
@@ -592,88 +592,88 @@ class HTTPConnection(httplib.HTTPConnect
 def error_handler(url):
     global HANDLE_ERRORS
     orig = HANDLE_ERRORS
     keepalive_handler = HTTPHandler()
     opener = urllib2.build_opener(keepalive_handler)
     urllib2.install_opener(opener)
     pos = {0: 'off', 1: 'on'}
     for i in (0, 1):
-        print "  fancy error handling %s (HANDLE_ERRORS = %i)" % (pos[i], i)
+        print("  fancy error handling %s (HANDLE_ERRORS = %i)" % (pos[i], i))
         HANDLE_ERRORS = i
         try:
             fo = urllib2.urlopen(url)
             fo.read()
             fo.close()
             try:
                 status, reason = fo.status, fo.reason
             except AttributeError:
                 status, reason = None, None
         except IOError as e:
-            print "  EXCEPTION: %s" % e
+            print("  EXCEPTION: %s" % e)
             raise
         else:
-            print "  status = %s, reason = %s" % (status, reason)
+            print("  status = %s, reason = %s" % (status, reason))
     HANDLE_ERRORS = orig
     hosts = keepalive_handler.open_connections()
-    print "open connections:", hosts
+    print("open connections:", hosts)
     keepalive_handler.close_all()
 
 def continuity(url):
     from . import util
     md5 = util.md5
     format = '%25s: %s'
 
     # first fetch the file with the normal http handler
     opener = urllib2.build_opener()
     urllib2.install_opener(opener)
     fo = urllib2.urlopen(url)
     foo = fo.read()
     fo.close()
     m = md5(foo)
-    print format % ('normal urllib', m.hexdigest())
+    print(format % ('normal urllib', m.hexdigest()))
 
     # now install the keepalive handler and try again
     opener = urllib2.build_opener(HTTPHandler())
     urllib2.install_opener(opener)
 
     fo = urllib2.urlopen(url)
     foo = fo.read()
     fo.close()
     m = md5(foo)
-    print format % ('keepalive read', m.hexdigest())
+    print(format % ('keepalive read', m.hexdigest()))
 
     fo = urllib2.urlopen(url)
     foo = ''
     while True:
         f = fo.readline()
         if f:
             foo = foo + f
         else: break
     fo.close()
     m = md5(foo)
-    print format % ('keepalive readline', m.hexdigest())
+    print(format % ('keepalive readline', m.hexdigest()))
 
 def comp(N, url):
-    print '  making %i connections to:\n  %s' % (N, url)
+    print('  making %i connections to:\n  %s' % (N, url))
 
     sys.stdout.write('  first using the normal urllib handlers')
     # first use normal opener
     opener = urllib2.build_opener()
     urllib2.install_opener(opener)
     t1 = fetch(N, url)
-    print '  TIME: %.3f s' % t1
+    print('  TIME: %.3f s' % t1)
 
     sys.stdout.write('  now using the keepalive handler       ')
     # now install the keepalive handler and try again
     opener = urllib2.build_opener(HTTPHandler())
     urllib2.install_opener(opener)
     t2 = fetch(N, url)
-    print '  TIME: %.3f s' % t2
-    print '  improvement factor: %.2f' % (t1 / t2)
+    print('  TIME: %.3f s' % t2)
+    print('  improvement factor: %.2f' % (t1 / t2))
 
 def fetch(N, url, delay=0):
     import time
     lens = []
     starttime = time.time()
     for i in range(N):
         if delay and i > 0:
             time.sleep(delay)
@@ -682,72 +682,72 @@ def fetch(N, url, delay=0):
         fo.close()
         lens.append(len(foo))
     diff = time.time() - starttime
 
     j = 0
     for i in lens[1:]:
         j = j + 1
         if not i == lens[0]:
-            print "WARNING: inconsistent length on read %i: %i" % (j, i)
+            print("WARNING: inconsistent length on read %i: %i" % (j, i))
 
     return diff
 
 def test_timeout(url):
     global DEBUG
     dbbackup = DEBUG
     class FakeLogger(object):
         def debug(self, msg, *args):
-            print msg % args
+            print(msg % args)
         info = warning = error = debug
     DEBUG = FakeLogger()
-    print "  fetching the file to establish a connection"
+    print("  fetching the file to establish a connection")
     fo = urllib2.urlopen(url)
     data1 = fo.read()
     fo.close()
 
     i = 20
-    print "  waiting %i seconds for the server to close the connection" % i
+    print("  waiting %i seconds for the server to close the connection" % i)
     while i > 0:
         sys.stdout.write('\r  %2i' % i)
         sys.stdout.flush()
         time.sleep(1)
         i -= 1
     sys.stderr.write('\r')
 
-    print "  fetching the file a second time"
+    print("  fetching the file a second time")
     fo = urllib2.urlopen(url)
     data2 = fo.read()
     fo.close()
 
     if data1 == data2:
-        print '  data are identical'
+        print('  data are identical')
     else:
-        print '  ERROR: DATA DIFFER'
+        print('  ERROR: DATA DIFFER')
 
     DEBUG = dbbackup
 
 
 def test(url, N=10):
-    print "checking error handler (do this on a non-200)"
+    print("checking error handler (do this on a non-200)")
     try: error_handler(url)
     except IOError:
-        print "exiting - exception will prevent further tests"
+        print("exiting - exception will prevent further tests")
         sys.exit()
-    print
-    print "performing continuity test (making sure stuff isn't corrupted)"
+    print('')
+    print("performing continuity test (making sure stuff isn't corrupted)")
     continuity(url)
-    print
-    print "performing speed comparison"
+    print('')
+    print("performing speed comparison")
     comp(N, url)
-    print
-    print "performing dropped-connection check"
+    print('')
+    print("performing dropped-connection check")
     test_timeout(url)
 
 if __name__ == '__main__':
     import time
     try:
         N = int(sys.argv[1])
         url = sys.argv[2]
     except (IndexError, ValueError):
-        print "%s <integer> <url>" % sys.argv[0]
+        print("%s <integer> <url>" % sys.argv[0])
     else:
         test(url, N)
diff --git a/tests/test-check-py3-compat.t b/tests/test-check-py3-compat.t
--- a/tests/test-check-py3-compat.t
+++ b/tests/test-check-py3-compat.t
@@ -93,17 +93,16 @@
   hgext/transplant.py not using absolute_import
   hgext/win32mbcs.py not using absolute_import
   hgext/win32text.py not using absolute_import
   hgext/zeroconf/Zeroconf.py not using absolute_import
   hgext/zeroconf/Zeroconf.py requires print_function
   hgext/zeroconf/__init__.py not using absolute_import
   i18n/check-translation.py not using absolute_import
   i18n/polib.py not using absolute_import
-  mercurial/keepalive.py requires print_function
   mercurial/lsprof.py requires print_function
   mercurial/lsprofcalltree.py requires print_function
   mercurial/mail.py requires print_function
   setup.py not using absolute_import
   tests/filterpyflakes.py requires print_function
   tests/generate-working-copy-states.py requires print_function
   tests/get-with-headers.py requires print_function
   tests/heredoctest.py requires print_function


More information about the Mercurial-devel mailing list