From a617271dbd150549b021a5161d009869ad62f7b2 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Fri, 31 Dec 2004 19:15:26 +0000 Subject: [PATCH] Use cStringIO where available. --- Lib/SimpleHTTPServer.py | 5 ++++- Lib/SocketServer.py | 9 ++++++--- Lib/cgi.py | 5 ++++- Lib/gettext.py | 5 ++++- Lib/mhlib.py | 5 ++++- Lib/tarfile.py | 7 +++++-- Lib/urllib.py | 31 +++++++++++++++++++++++-------- Lib/urlparse.py | 7 +++++-- 8 files changed, 55 insertions(+), 19 deletions(-) diff --git a/Lib/SimpleHTTPServer.py b/Lib/SimpleHTTPServer.py index 93662ab1948..93af10900aa 100644 --- a/Lib/SimpleHTTPServer.py +++ b/Lib/SimpleHTTPServer.py @@ -17,7 +17,10 @@ import urllib import cgi import shutil import mimetypes -from StringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler): diff --git a/Lib/SocketServer.py b/Lib/SocketServer.py index 06ed134b0f3..345a6d16e16 100644 --- a/Lib/SocketServer.py +++ b/Lib/SocketServer.py @@ -575,10 +575,13 @@ class DatagramRequestHandler(BaseRequestHandler): """Define self.rfile and self.wfile for datagram sockets.""" def setup(self): - import StringIO + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO self.packet, self.socket = self.request - self.rfile = StringIO.StringIO(self.packet) - self.wfile = StringIO.StringIO() + self.rfile = StringIO(self.packet) + self.wfile = StringIO() def finish(self): self.socket.sendto(self.wfile.getvalue(), self.client_address) diff --git a/Lib/cgi.py b/Lib/cgi.py index fb40ed36a3c..cf849e85cac 100755 --- a/Lib/cgi.py +++ b/Lib/cgi.py @@ -40,7 +40,10 @@ import urllib import mimetools import rfc822 import UserDict -from StringIO import StringIO +try: + from cStringIO import StringIO +except ImportError: + from StringIO import StringIO __all__ = ["MiniFieldStorage", "FieldStorage", "FormContentDict", "SvFormContentDict", "InterpFormContentDict", "FormContent", diff --git a/Lib/gettext.py b/Lib/gettext.py index 6e291766a54..a20c6f15224 100644 --- a/Lib/gettext.py +++ b/Lib/gettext.py @@ -77,7 +77,10 @@ def c2py(plural): Python lambda function that implements an equivalent expression. """ # Security check, allow only the "n" identifier - from StringIO import StringIO + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO import token, tokenize tokens = tokenize.generate_tokens(StringIO(plural).readline) try: diff --git a/Lib/mhlib.py b/Lib/mhlib.py index 0a8c444f2c6..1086121584f 100644 --- a/Lib/mhlib.py +++ b/Lib/mhlib.py @@ -697,7 +697,10 @@ class Message(mimetools.Message): encoding = self.getencoding() if not decode or encoding in ('', '7bit', '8bit', 'binary'): return self.fp.read() - from StringIO import StringIO + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO output = StringIO() mimetools.decode(self.fp, output, encoding) return output.getvalue() diff --git a/Lib/tarfile.py b/Lib/tarfile.py index b85f117c976..0dc7a4270be 100644 --- a/Lib/tarfile.py +++ b/Lib/tarfile.py @@ -1936,12 +1936,15 @@ class TarFileCompat: def write(self, filename, arcname=None, compress_type=None): self.tarfile.add(filename, arcname) def writestr(self, zinfo, bytes): - import StringIO + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO import calendar zinfo.name = zinfo.filename zinfo.size = zinfo.file_size zinfo.mtime = calendar.timegm(zinfo.date_time) - self.tarfile.addfile(zinfo, StringIO.StringIO(bytes)) + self.tarfile.addfile(zinfo, StringIO(bytes)) def close(self): self.tarfile.close() #class TarFileCompat diff --git a/Lib/urllib.py b/Lib/urllib.py index e2f01c59c4e..74b2aec79e8 100644 --- a/Lib/urllib.py +++ b/Lib/urllib.py @@ -410,7 +410,11 @@ class URLopener: def open_local_file(self, url): """Use local file.""" - import mimetypes, mimetools, email.Utils, StringIO + import mimetypes, mimetools, email.Utils + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO host, file = splithost(url) localname = url2pathname(file) try: @@ -420,7 +424,7 @@ class URLopener: size = stats.st_size modified = email.Utils.formatdate(stats.st_mtime, usegmt=True) mtype = mimetypes.guess_type(url)[0] - headers = mimetools.Message(StringIO.StringIO( + headers = mimetools.Message(StringIO( 'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' % (mtype or 'text/plain', size, modified))) if not host: @@ -441,7 +445,11 @@ class URLopener: def open_ftp(self, url): """Use FTP protocol.""" - import mimetypes, mimetools, StringIO + import mimetypes, mimetools + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO host, path = splithost(url) if not host: raise IOError, ('ftp error', 'no host given') host, port = splitport(host) @@ -490,7 +498,7 @@ class URLopener: headers += "Content-Type: %s\n" % mtype if retrlen is not None and retrlen >= 0: headers += "Content-Length: %d\n" % retrlen - headers = mimetools.Message(StringIO.StringIO(headers)) + headers = mimetools.Message(StringIO(headers)) return addinfourl(fp, headers, "ftp:" + url) except ftperrors(), msg: raise IOError, ('ftp error', msg), sys.exc_info()[2] @@ -504,7 +512,11 @@ class URLopener: # mediatype := [ type "/" subtype ] *( ";" parameter ) # data := *urlchar # parameter := attribute "=" value - import StringIO, mimetools + import mimetools + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO try: [type, data] = url.split(',', 1) except ValueError: @@ -530,7 +542,7 @@ class URLopener: msg.append('') msg.append(data) msg = '\n'.join(msg) - f = StringIO.StringIO(msg) + f = StringIO(msg) headers = mimetools.Message(f, 0) f.fileno = None # needed for addinfourl return addinfourl(f, headers, url) @@ -697,8 +709,11 @@ def noheaders(): global _noheaders if _noheaders is None: import mimetools - import StringIO - _noheaders = mimetools.Message(StringIO.StringIO(), 0) + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO + _noheaders = mimetools.Message(StringIO(), 0) _noheaders.fp.close() # Recycle file descriptor return _noheaders diff --git a/Lib/urlparse.py b/Lib/urlparse.py index 37dd36265b8..9c762725474 100644 --- a/Lib/urlparse.py +++ b/Lib/urlparse.py @@ -243,8 +243,11 @@ def test(): else: fp = open(fn) else: - import StringIO - fp = StringIO.StringIO(test_input) + try: + from cStringIO import StringIO + except ImportError: + from StringIO import StringIO + fp = StringIO(test_input) while 1: line = fp.readline() if not line: break