Skip to content

Commit a617271

Browse files
committed
Use cStringIO where available.
1 parent 54266fc commit a617271

8 files changed

Lines changed: 55 additions & 19 deletions

File tree

Lib/SimpleHTTPServer.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import cgi
1818
import shutil
1919
import mimetypes
20-
from StringIO import StringIO
20+
try:
21+
from cStringIO import StringIO
22+
except ImportError:
23+
from StringIO import StringIO
2124

2225

2326
class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):

Lib/SocketServer.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,13 @@ class DatagramRequestHandler(BaseRequestHandler):
575575
"""Define self.rfile and self.wfile for datagram sockets."""
576576

577577
def setup(self):
578-
import StringIO
578+
try:
579+
from cStringIO import StringIO
580+
except ImportError:
581+
from StringIO import StringIO
579582
self.packet, self.socket = self.request
580-
self.rfile = StringIO.StringIO(self.packet)
581-
self.wfile = StringIO.StringIO()
583+
self.rfile = StringIO(self.packet)
584+
self.wfile = StringIO()
582585

583586
def finish(self):
584587
self.socket.sendto(self.wfile.getvalue(), self.client_address)

Lib/cgi.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@
4040
import mimetools
4141
import rfc822
4242
import UserDict
43-
from StringIO import StringIO
43+
try:
44+
from cStringIO import StringIO
45+
except ImportError:
46+
from StringIO import StringIO
4447

4548
__all__ = ["MiniFieldStorage", "FieldStorage", "FormContentDict",
4649
"SvFormContentDict", "InterpFormContentDict", "FormContent",

Lib/gettext.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ def c2py(plural):
7777
Python lambda function that implements an equivalent expression.
7878
"""
7979
# Security check, allow only the "n" identifier
80-
from StringIO import StringIO
80+
try:
81+
from cStringIO import StringIO
82+
except ImportError:
83+
from StringIO import StringIO
8184
import token, tokenize
8285
tokens = tokenize.generate_tokens(StringIO(plural).readline)
8386
try:

Lib/mhlib.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,10 @@ def getbodytext(self, decode = 1):
697697
encoding = self.getencoding()
698698
if not decode or encoding in ('', '7bit', '8bit', 'binary'):
699699
return self.fp.read()
700-
from StringIO import StringIO
700+
try:
701+
from cStringIO import StringIO
702+
except ImportError:
703+
from StringIO import StringIO
701704
output = StringIO()
702705
mimetools.decode(self.fp, output, encoding)
703706
return output.getvalue()

Lib/tarfile.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1936,12 +1936,15 @@ def read(self, name):
19361936
def write(self, filename, arcname=None, compress_type=None):
19371937
self.tarfile.add(filename, arcname)
19381938
def writestr(self, zinfo, bytes):
1939-
import StringIO
1939+
try:
1940+
from cStringIO import StringIO
1941+
except ImportError:
1942+
from StringIO import StringIO
19401943
import calendar
19411944
zinfo.name = zinfo.filename
19421945
zinfo.size = zinfo.file_size
19431946
zinfo.mtime = calendar.timegm(zinfo.date_time)
1944-
self.tarfile.addfile(zinfo, StringIO.StringIO(bytes))
1947+
self.tarfile.addfile(zinfo, StringIO(bytes))
19451948
def close(self):
19461949
self.tarfile.close()
19471950
#class TarFileCompat

Lib/urllib.py

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,11 @@ def open_file(self, url):
410410

411411
def open_local_file(self, url):
412412
"""Use local file."""
413-
import mimetypes, mimetools, email.Utils, StringIO
413+
import mimetypes, mimetools, email.Utils
414+
try:
415+
from cStringIO import StringIO
416+
except ImportError:
417+
from StringIO import StringIO
414418
host, file = splithost(url)
415419
localname = url2pathname(file)
416420
try:
@@ -420,7 +424,7 @@ def open_local_file(self, url):
420424
size = stats.st_size
421425
modified = email.Utils.formatdate(stats.st_mtime, usegmt=True)
422426
mtype = mimetypes.guess_type(url)[0]
423-
headers = mimetools.Message(StringIO.StringIO(
427+
headers = mimetools.Message(StringIO(
424428
'Content-Type: %s\nContent-Length: %d\nLast-modified: %s\n' %
425429
(mtype or 'text/plain', size, modified)))
426430
if not host:
@@ -441,7 +445,11 @@ def open_local_file(self, url):
441445

442446
def open_ftp(self, url):
443447
"""Use FTP protocol."""
444-
import mimetypes, mimetools, StringIO
448+
import mimetypes, mimetools
449+
try:
450+
from cStringIO import StringIO
451+
except ImportError:
452+
from StringIO import StringIO
445453
host, path = splithost(url)
446454
if not host: raise IOError, ('ftp error', 'no host given')
447455
host, port = splitport(host)
@@ -490,7 +498,7 @@ def open_ftp(self, url):
490498
headers += "Content-Type: %s\n" % mtype
491499
if retrlen is not None and retrlen >= 0:
492500
headers += "Content-Length: %d\n" % retrlen
493-
headers = mimetools.Message(StringIO.StringIO(headers))
501+
headers = mimetools.Message(StringIO(headers))
494502
return addinfourl(fp, headers, "ftp:" + url)
495503
except ftperrors(), msg:
496504
raise IOError, ('ftp error', msg), sys.exc_info()[2]
@@ -504,7 +512,11 @@ def open_data(self, url, data=None):
504512
# mediatype := [ type "/" subtype ] *( ";" parameter )
505513
# data := *urlchar
506514
# parameter := attribute "=" value
507-
import StringIO, mimetools
515+
import mimetools
516+
try:
517+
from cStringIO import StringIO
518+
except ImportError:
519+
from StringIO import StringIO
508520
try:
509521
[type, data] = url.split(',', 1)
510522
except ValueError:
@@ -530,7 +542,7 @@ def open_data(self, url, data=None):
530542
msg.append('')
531543
msg.append(data)
532544
msg = '\n'.join(msg)
533-
f = StringIO.StringIO(msg)
545+
f = StringIO(msg)
534546
headers = mimetools.Message(f, 0)
535547
f.fileno = None # needed for addinfourl
536548
return addinfourl(f, headers, url)
@@ -697,8 +709,11 @@ def noheaders():
697709
global _noheaders
698710
if _noheaders is None:
699711
import mimetools
700-
import StringIO
701-
_noheaders = mimetools.Message(StringIO.StringIO(), 0)
712+
try:
713+
from cStringIO import StringIO
714+
except ImportError:
715+
from StringIO import StringIO
716+
_noheaders = mimetools.Message(StringIO(), 0)
702717
_noheaders.fp.close() # Recycle file descriptor
703718
return _noheaders
704719

Lib/urlparse.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,11 @@ def test():
243243
else:
244244
fp = open(fn)
245245
else:
246-
import StringIO
247-
fp = StringIO.StringIO(test_input)
246+
try:
247+
from cStringIO import StringIO
248+
except ImportError:
249+
from StringIO import StringIO
250+
fp = StringIO(test_input)
248251
while 1:
249252
line = fp.readline()
250253
if not line: break

0 commit comments

Comments
 (0)