@@ -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\n Content-Length: %d\n Last-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
0 commit comments