Patchwork [6,of,7] hgclient: handle cStringIO.StringIO/io.StringIO divergence

login
register
mail settings
Submitter timeless@mozdev.org
Date April 8, 2016, 4:22 p.m.
Message ID <d08199e5ab0891449c03.1460132570@waste.org>
Download mbox | patch
Permalink /patch/14443/
State Accepted
Headers show

Comments

timeless@mozdev.org - April 8, 2016, 4:22 p.m.
# HG changeset patch
# User timeless <timeless@mozdev.org>
# Date 1459974874 0
#      Wed Apr 06 20:34:34 2016 +0000
# Node ID d08199e5ab0891449c03373c1abb5fc491241b20
# Parent  f7e68d1a69ec5eabf99bfadbe522d75ac06b15e1
hgclient: handle cStringIO.StringIO/io.StringIO divergence

Patch

diff --git a/contrib/hgclient.py b/contrib/hgclient.py
--- a/contrib/hgclient.py
+++ b/contrib/hgclient.py
@@ -1,7 +1,6 @@ 
 # A minimal client for Mercurial's command server
 
 from __future__ import absolute_import, print_function
-import cStringIO
 import os
 import signal
 import socket
@@ -10,6 +9,13 @@ 
 import sys
 import time
 
+try:
+    import cStringIO as io
+    stringio = io.StringIO
+except ImportError:
+    import io
+    stringio = io.StringIO
+
 def connectpipe(path=None):
     cmdline = ['hg', 'serve', '--cmdserver', 'pipe']
     if path:
@@ -83,7 +89,7 @@ 
     writeblock(server, '\0'.join(args))
 
     if not input:
-        input = cStringIO.StringIO()
+        input = stringio()
 
     while True:
         ch, data = readchannel(server)
diff --git a/tests/test-commandserver.t b/tests/test-commandserver.t
--- a/tests/test-commandserver.t
+++ b/tests/test-commandserver.t
@@ -102,8 +102,7 @@ 
   ...     print 'server exit code =', server.wait()
   server exit code = 1
 
-  >>> import cStringIO
-  >>> from hgclient import readchannel, runcommand, check
+  >>> from hgclient import readchannel, runcommand, check, stringio
   >>> @check
   ... def serverinput(server):
   ...     readchannel(server)
@@ -123,7 +122,7 @@ 
   ... +1
   ... """
   ... 
-  ...     runcommand(server, ['import', '-'], input=cStringIO.StringIO(patch))
+  ...     runcommand(server, ['import', '-'], input=stringio(patch))
   ...     runcommand(server, ['log'])
   *** runcommand import -
   applying patch from stdin
@@ -211,15 +210,14 @@ 
   >     print 'now try to read something: %r' % sys.stdin.read()
   > EOF
 
-  >>> import cStringIO
-  >>> from hgclient import readchannel, runcommand, check
+  >>> from hgclient import readchannel, runcommand, check, stringio
   >>> @check
   ... def hookoutput(server):
   ...     readchannel(server)
   ...     runcommand(server, ['--config',
   ...                         'hooks.pre-identify=python:hook.hook',
   ...                         'id'],
-  ...                input=cStringIO.StringIO('some input'))
+  ...                input=stringio('some input'))
   *** runcommand --config hooks.pre-identify=python:hook.hook id
   hook talking
   now try to read something: 'some input'
@@ -587,17 +585,16 @@ 
   > dbgui = dbgui.py
   > EOF
 
-  >>> import cStringIO
-  >>> from hgclient import readchannel, runcommand, check
+  >>> from hgclient import readchannel, runcommand, check, stringio
   >>> @check
   ... def getpass(server):
   ...     readchannel(server)
   ...     runcommand(server, ['debuggetpass', '--config',
   ...                         'ui.interactive=True'],
-  ...                input=cStringIO.StringIO('1234\n'))
+  ...                input=stringio('1234\n'))
   ...     runcommand(server, ['debugprompt', '--config',
   ...                         'ui.interactive=True'],
-  ...                input=cStringIO.StringIO('5678\n'))
+  ...                input=stringio('5678\n'))
   ...     runcommand(server, ['debugreadstdin'])
   ...     runcommand(server, ['debugwritestdout'])
   *** runcommand debuggetpass --config ui.interactive=True
@@ -611,14 +608,13 @@ 
 
 run commandserver in commandserver, which is silly but should work:
 
-  >>> import cStringIO
-  >>> from hgclient import readchannel, runcommand, check
+  >>> from hgclient import readchannel, runcommand, check, stringio
   >>> @check
   ... def nested(server):
   ...     print '%c, %r' % readchannel(server)
   ...     class nestedserver(object):
-  ...         stdin = cStringIO.StringIO('getencoding\n')
-  ...         stdout = cStringIO.StringIO()
+  ...         stdin = stringio('getencoding\n')
+  ...         stdout = stringio()
   ...     runcommand(server, ['serve', '--cmdserver', 'pipe'],
   ...                output=nestedserver.stdout, input=nestedserver.stdin)
   ...     nestedserver.stdout.seek(0)
@@ -674,8 +670,7 @@ 
 
 #if unix-socket unix-permissions
 
-  >>> import cStringIO
-  >>> from hgclient import unixserver, readchannel, runcommand, check
+  >>> from hgclient import unixserver, readchannel, runcommand, check, stringio
   >>> server = unixserver('.hg/server.sock', '.hg/server.log')
   >>> def hellomessage(conn):
   ...     ch, data = readchannel(conn)
@@ -704,7 +699,7 @@ 
   ...  1
   ... +2
   ... """
-  ...     runcommand(conn, ['import', '-'], input=cStringIO.StringIO(patch))
+  ...     runcommand(conn, ['import', '-'], input=stringio(patch))
   ...     runcommand(conn, ['log', '-rtip', '-q'])
   >>> check(serverinput, server.connect)
   *** runcommand import -