From: Ben Darnell Date: Thu, 24 Feb 2011 22:43:58 +0000 (-0800) Subject: On python3, use BytesIO instead of StringIO X-Git-Tag: v2.0.0~85^2~15 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=a4b5323d5e24121a21d781812bc9c5cd2c627d87;p=thirdparty%2Ftornado.git On python3, use BytesIO instead of StringIO --- diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 03e26e4e3..c94328468 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -1,7 +1,6 @@ #!/usr/bin/env python from __future__ import with_statement -from cStringIO import StringIO from tornado.escape import utf8 from tornado.httpclient import HTTPRequest, HTTPResponse, HTTPError, AsyncHTTPClient from tornado.httputil import HTTPHeaders @@ -24,6 +23,11 @@ import time import urlparse import zlib +try: + from io import BytesIO # python 3 +except ImportError: + from cStringIO import StringIO as BytesIO # python 2 + try: import ssl # python 2.6+ except ImportError: @@ -275,9 +279,9 @@ class _HTTPConnection(object): # if chunks is not None, we already called streaming_callback # in _on_chunk_data self.request.streaming_callback(data) - buffer = StringIO() + buffer = BytesIO() else: - buffer = StringIO(data) # TODO: don't require one big string? + buffer = BytesIO(data) # TODO: don't require one big string? original_request = getattr(self.request, "original_request", self.request) if (self.request.follow_redirects and diff --git a/tornado/web.py b/tornado/web.py index 2a3b3ac97..50de8fccc 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -54,7 +54,6 @@ from __future__ import with_statement import Cookie import base64 import binascii -import cStringIO import calendar import contextlib import datetime @@ -84,6 +83,11 @@ from tornado import template from tornado.escape import utf8 from tornado.util import b, bytes_type +try: + from io import BytesIO # python 3 +except ImportError: + from cStringIO import StringIO as BytesIO # python 2 + class RequestHandler(object): """Subclass this class and define get() or post() to make a handler. @@ -1430,7 +1434,7 @@ class GZipContentEncoding(OutputTransform): ("Content-Encoding" not in headers) if self._gzipping: headers["Content-Encoding"] = "gzip" - self._gzip_value = cStringIO.StringIO() + self._gzip_value = BytesIO() self._gzip_file = gzip.GzipFile(mode="w", fileobj=self._gzip_value) self._gzip_pos = 0 chunk = self.transform_chunk(chunk, finishing)