Replaced cStringIO.StringIO by io.BytesIO.

Also replaced StringIO.StringIO by BytesIO in some other appropriate
places. StringIO is not available in Python 3.
This commit is contained in:
Claude Paroz 2012-05-05 19:47:03 +02:00
parent 1583d40224
commit d7dfab59ea
26 changed files with 94 additions and 167 deletions

View File

@ -203,12 +203,12 @@ class GEOSTest(unittest.TestCase, TestDataMixin):
def test01k_fromfile(self): def test01k_fromfile(self):
"Testing the fromfile() factory." "Testing the fromfile() factory."
from StringIO import StringIO from io import BytesIO
ref_pnt = GEOSGeometry('POINT(5 23)') ref_pnt = GEOSGeometry('POINT(5 23)')
wkt_f = StringIO() wkt_f = BytesIO()
wkt_f.write(ref_pnt.wkt) wkt_f.write(ref_pnt.wkt)
wkb_f = StringIO() wkb_f = BytesIO()
wkb_f.write(str(ref_pnt.wkb)) wkb_f.write(str(ref_pnt.wkb))
# Other tests use `fromfile()` on string filenames so those # Other tests use `fromfile()` on string filenames so those

View File

@ -1,8 +1,5 @@
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import zipfile import zipfile
from io import BytesIO
from django.conf import settings from django.conf import settings
from django.http import HttpResponse from django.http import HttpResponse
@ -10,7 +7,7 @@ from django.template import loader
def compress_kml(kml): def compress_kml(kml):
"Returns compressed KMZ from the given KML string." "Returns compressed KMZ from the given KML string."
kmz = StringIO() kmz = BytesIO()
zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED) zf = zipfile.ZipFile(kmz, 'a', zipfile.ZIP_DEFLATED)
zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET)) zf.writestr('doc.kml', kml.encode(settings.DEFAULT_CHARSET))
zf.close() zf.close()

View File

@ -1,9 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from xml.dom import minidom from xml.dom import minidom
import zipfile import zipfile
@ -66,7 +63,7 @@ class GeoSitemapTest(TestCase):
kml_doc = minidom.parseString(self.client.get(kml_url).content) kml_doc = minidom.parseString(self.client.get(kml_url).content)
elif kml_type == 'kmz': elif kml_type == 'kmz':
# Have to decompress KMZ before parsing. # Have to decompress KMZ before parsing.
buf = StringIO(self.client.get(kml_url).content) buf = BytesIO(self.client.get(kml_url).content)
zf = zipfile.ZipFile(buf) zf = zipfile.ZipFile(buf)
self.assertEqual(1, len(zf.filelist)) self.assertEqual(1, len(zf.filelist))
self.assertEqual('doc.kml', zf.filelist[0].filename) self.assertEqual('doc.kml', zf.filelist[0].filename)

View File

@ -1,8 +1,5 @@
import os import os
try: from io import BytesIO, StringIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.utils.encoding import smart_str, smart_unicode from django.utils.encoding import smart_str, smart_unicode
from django.core.files.utils import FileProxyMixin from django.core.files.utils import FileProxyMixin
@ -89,7 +86,7 @@ class File(FileProxyMixin):
# Iterate over this file-like object by newlines # Iterate over this file-like object by newlines
buffer_ = None buffer_ = None
for chunk in self.chunks(): for chunk in self.chunks():
chunk_buffer = StringIO(chunk) chunk_buffer = BytesIO(chunk)
for line in chunk_buffer: for line in chunk_buffer:
if buffer_: if buffer_:

View File

@ -3,10 +3,7 @@ Classes representing uploaded files.
""" """
import os import os
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.conf import settings from django.conf import settings
from django.core.files.base import File from django.core.files.base import File
@ -110,8 +107,8 @@ class SimpleUploadedFile(InMemoryUploadedFile):
A simple representation of a file, which just has content, size, and a name. A simple representation of a file, which just has content, size, and a name.
""" """
def __init__(self, name, content, content_type='text/plain'): def __init__(self, name, content, content_type='text/plain'):
content = content or '' content = content or b''
super(SimpleUploadedFile, self).__init__(StringIO(content), None, name, super(SimpleUploadedFile, self).__init__(BytesIO(content), None, name,
content_type, len(content), None) content_type, len(content), None)
def from_dict(cls, file_dict): def from_dict(cls, file_dict):

View File

@ -2,10 +2,7 @@
Base file upload handler classes, and the built-in concrete subclasses Base file upload handler classes, and the built-in concrete subclasses
""" """
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.conf import settings from django.conf import settings
from django.core.exceptions import ImproperlyConfigured from django.core.exceptions import ImproperlyConfigured
@ -161,12 +158,12 @@ class MemoryFileUploadHandler(FileUploadHandler):
def new_file(self, *args, **kwargs): def new_file(self, *args, **kwargs):
super(MemoryFileUploadHandler, self).new_file(*args, **kwargs) super(MemoryFileUploadHandler, self).new_file(*args, **kwargs)
if self.activated: if self.activated:
self.file = StringIO() self.file = BytesIO()
raise StopFutureHandlers() raise StopFutureHandlers()
def receive_data_chunk(self, raw_data, start): def receive_data_chunk(self, raw_data, start):
""" """
Add the data to the StringIO file. Add the data to the BytesIO file.
""" """
if self.activated: if self.activated:
self.file.write(raw_data) self.file.write(raw_data)

View File

@ -1,9 +1,6 @@
import sys import sys
from io import BytesIO
from threading import Lock from threading import Lock
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django import http from django import http
from django.core import signals from django.core import signals
@ -116,7 +113,7 @@ class LimitedStream(object):
if not chunk: if not chunk:
break break
self.buffer += chunk self.buffer += chunk
sio = StringIO(self.buffer) sio = BytesIO(self.buffer)
if size: if size:
line = sio.readline(size) line = sio.readline(size)
else: else:

View File

@ -9,15 +9,12 @@ from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase from email.mime.base import MIMEBase
from email.header import Header from email.header import Header
from email.utils import formatdate, getaddresses, formataddr, parseaddr from email.utils import formatdate, getaddresses, formataddr, parseaddr
from io import BytesIO
from django.conf import settings from django.conf import settings
from django.core.mail.utils import DNS_NAME from django.core.mail.utils import DNS_NAME
from django.utils.encoding import smart_str, force_unicode from django.utils.encoding import smart_str, force_unicode
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
# Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from # Don't BASE64-encode UTF-8 messages so that we avoid unwanted attention from
# some spam filters. # some spam filters.
@ -132,7 +129,7 @@ class SafeMIMEText(MIMEText):
This overrides the default as_string() implementation to not mangle This overrides the default as_string() implementation to not mangle
lines that begin with 'From '. See bug #13433 for details. lines that begin with 'From '. See bug #13433 for details.
""" """
fp = StringIO() fp = BytesIO()
g = Generator(fp, mangle_from_ = False) g = Generator(fp, mangle_from_ = False)
g.flatten(self, unixfrom=unixfrom) g.flatten(self, unixfrom=unixfrom)
return fp.getvalue() return fp.getvalue()
@ -156,7 +153,7 @@ class SafeMIMEMultipart(MIMEMultipart):
This overrides the default as_string() implementation to not mangle This overrides the default as_string() implementation to not mangle
lines that begin with 'From '. See bug #13433 for details. lines that begin with 'From '. See bug #13433 for details.
""" """
fp = StringIO() fp = BytesIO()
g = Generator(fp, mangle_from_ = False) g = Generator(fp, mangle_from_ = False)
g.flatten(self, unixfrom=unixfrom) g.flatten(self, unixfrom=unixfrom)
return fp.getvalue() return fp.getvalue()

View File

@ -6,6 +6,7 @@ be executed through ``django-admin.py`` or ``manage.py``).
import os import os
import sys import sys
from io import BytesIO
from optparse import make_option, OptionParser from optparse import make_option, OptionParser
import traceback import traceback
@ -258,11 +259,7 @@ class BaseCommand(object):
""" """
from django.core.management.validation import get_validation_errors from django.core.management.validation import get_validation_errors
try: s = BytesIO()
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
s = StringIO()
num_errors = get_validation_errors(s, app) num_errors = get_validation_errors(s, app)
if num_errors: if num_errors:
s.seek(0) s.seek(0)

View File

@ -2,7 +2,7 @@
Module for abstract serializer/unserializer base classes. Module for abstract serializer/unserializer base classes.
""" """
from StringIO import StringIO from io import BytesIO
from django.db import models from django.db import models
from django.utils.encoding import smart_unicode from django.utils.encoding import smart_unicode
@ -34,7 +34,7 @@ class Serializer(object):
""" """
self.options = options self.options = options
self.stream = options.pop("stream", StringIO()) self.stream = options.pop("stream", BytesIO())
self.selected_fields = options.pop("fields", None) self.selected_fields = options.pop("fields", None)
self.use_natural_keys = options.pop("use_natural_keys", False) self.use_natural_keys = options.pop("use_natural_keys", False)
@ -121,7 +121,7 @@ class Deserializer(object):
""" """
self.options = options self.options = options
if isinstance(stream_or_string, basestring): if isinstance(stream_or_string, basestring):
self.stream = StringIO(stream_or_string) self.stream = BytesIO(stream_or_string)
else: else:
self.stream = stream_or_string self.stream = stream_or_string
# hack to make sure that the models have all been loaded before # hack to make sure that the models have all been loaded before

View File

@ -8,7 +8,7 @@ from __future__ import absolute_import
import datetime import datetime
import decimal import decimal
import json import json
from StringIO import StringIO from io import BytesIO
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Serializer as PythonSerializer from django.core.serializers.python import Serializer as PythonSerializer
@ -37,7 +37,7 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of JSON data. Deserialize a stream or string of JSON data.
""" """
if isinstance(stream_or_string, basestring): if isinstance(stream_or_string, basestring):
stream = StringIO(stream_or_string) stream = BytesIO(stream_or_string)
else: else:
stream = stream_or_string stream = stream_or_string
try: try:

View File

@ -4,9 +4,9 @@ YAML serializer.
Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__. Requires PyYaml (http://pyyaml.org/), but that's checked for in __init__.
""" """
from StringIO import StringIO
import decimal import decimal
import yaml import yaml
from io import BytesIO
from django.db import models from django.db import models
from django.core.serializers.base import DeserializationError from django.core.serializers.base import DeserializationError
@ -49,7 +49,7 @@ def Deserializer(stream_or_string, **options):
Deserialize a stream or string of YAML data. Deserialize a stream or string of YAML data.
""" """
if isinstance(stream_or_string, basestring): if isinstance(stream_or_string, basestring):
stream = StringIO(stream_or_string) stream = BytesIO(stream_or_string)
else: else:
stream = stream_or_string stream = stream_or_string
try: try:

View File

@ -10,10 +10,7 @@ import os
import re import re
import urlparse import urlparse
from decimal import Decimal, DecimalException from decimal import Decimal, DecimalException
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.core import validators from django.core import validators
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
@ -554,9 +551,9 @@ class ImageField(FileField):
file = data.temporary_file_path() file = data.temporary_file_path()
else: else:
if hasattr(data, 'read'): if hasattr(data, 'read'):
file = StringIO(data.read()) file = BytesIO(data.read())
else: else:
file = StringIO(data['content']) file = BytesIO(data['content'])
try: try:
# load() is the only method that can spot a truncated JPEG, # load() is the only method that can spot a truncated JPEG,

View File

@ -7,13 +7,10 @@ import sys
import time import time
import warnings import warnings
from io import BytesIO
from pprint import pformat from pprint import pformat
from urllib import urlencode, quote from urllib import urlencode, quote
from urlparse import urljoin, parse_qsl from urlparse import urljoin, parse_qsl
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
import Cookie import Cookie
# Some versions of Python 2.7 and later won't need this encoding bug fix: # Some versions of Python 2.7 and later won't need this encoding bug fix:
@ -292,7 +289,7 @@ class HttpRequest(object):
self._body = self.read() self._body = self.read()
except IOError as e: except IOError as e:
raise UnreadablePostError, e, sys.exc_traceback raise UnreadablePostError, e, sys.exc_traceback
self._stream = StringIO(self._body) self._stream = BytesIO(self._body)
return self._body return self._body
@property @property
@ -317,7 +314,7 @@ class HttpRequest(object):
if self.META.get('CONTENT_TYPE', '').startswith('multipart'): if self.META.get('CONTENT_TYPE', '').startswith('multipart'):
if hasattr(self, '_body'): if hasattr(self, '_body'):
# Use already read data # Use already read data
data = StringIO(self._body) data = BytesIO(self._body)
else: else:
data = self data = self
try: try:
@ -340,7 +337,7 @@ class HttpRequest(object):
## Expects self._stream to be set to an appropriate source of bytes by ## Expects self._stream to be set to an appropriate source of bytes by
## a corresponding request subclass (e.g. WSGIRequest). ## a corresponding request subclass (e.g. WSGIRequest).
## Also when request data has already been read by request.POST or ## Also when request data has already been read by request.POST or
## request.body, self._stream points to a StringIO instance ## request.body, self._stream points to a BytesIO instance
## containing that data. ## containing that data.
def read(self, *args, **kwargs): def read(self, *args, **kwargs):

View File

@ -4,11 +4,8 @@ import os
import re import re
import mimetypes import mimetypes
from copy import copy from copy import copy
from io import BytesIO
from urlparse import urlparse, urlsplit from urlparse import urlparse, urlsplit
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.conf import settings from django.conf import settings
from django.contrib.auth import authenticate, login from django.contrib.auth import authenticate, login
@ -35,13 +32,13 @@ CONTENT_TYPE_RE = re.compile('.*; charset=([\w\d-]+);?')
class FakePayload(object): class FakePayload(object):
""" """
A wrapper around StringIO that restricts what can be read since data from A wrapper around BytesIO that restricts what can be read since data from
the network can't be seeked and cannot be read outside of its content the network can't be seeked and cannot be read outside of its content
length. This makes sure that views can't do anything under the test client length. This makes sure that views can't do anything under the test client
that wouldn't work in Real Life. that wouldn't work in Real Life.
""" """
def __init__(self, content): def __init__(self, content):
self.__content = StringIO(content) self.__content = BytesIO(content)
self.__len = len(content) self.__len = len(content)
def read(self, num_bytes=None): def read(self, num_bytes=None):
@ -175,7 +172,7 @@ class RequestFactory(object):
def __init__(self, **defaults): def __init__(self, **defaults):
self.defaults = defaults self.defaults = defaults
self.cookies = SimpleCookie() self.cookies = SimpleCookie()
self.errors = StringIO() self.errors = BytesIO()
def _base_environ(self, **request): def _base_environ(self, **request):
""" """

View File

@ -174,8 +174,8 @@ class SyndicationFeed(object):
""" """
Returns the feed in the given encoding as a string. Returns the feed in the given encoding as a string.
""" """
from StringIO import StringIO from io import BytesIO
s = StringIO() s = BytesIO()
self.write(s, encoding) self.write(s, encoding)
return s.getvalue() return s.getvalue()

View File

@ -3,11 +3,7 @@ import unicodedata
import warnings import warnings
from gzip import GzipFile from gzip import GzipFile
from htmlentitydefs import name2codepoint from htmlentitydefs import name2codepoint
from io import BytesIO
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.utils.encoding import force_unicode from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy, SimpleLazyObject from django.utils.functional import allow_lazy, SimpleLazyObject
@ -277,7 +273,7 @@ phone2numeric = allow_lazy(phone2numeric)
# From http://www.xhaus.com/alan/python/httpcomp.html#gzip # From http://www.xhaus.com/alan/python/httpcomp.html#gzip
# Used with permission. # Used with permission.
def compress_string(s): def compress_string(s):
zbuf = StringIO() zbuf = BytesIO()
zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf) zfile = GzipFile(mode='wb', compresslevel=6, fileobj=zbuf)
zfile.write(s) zfile.write(s)
zfile.close() zfile.close()

View File

@ -5,13 +5,9 @@ import os
import re import re
import sys import sys
import gettext as gettext_module import gettext as gettext_module
from io import BytesIO
from threading import local from threading import local
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.utils.importlib import import_module from django.utils.importlib import import_module
from django.utils.safestring import mark_safe, SafeData from django.utils.safestring import mark_safe, SafeData
@ -442,7 +438,7 @@ def templatize(src, origin=None):
""" """
from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK, from django.template import (Lexer, TOKEN_TEXT, TOKEN_VAR, TOKEN_BLOCK,
TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK) TOKEN_COMMENT, TRANSLATOR_COMMENT_MARK)
out = StringIO() out = BytesIO()
message_context = None message_context = None
intrans = False intrans = False
inplural = False inplural = False
@ -453,16 +449,16 @@ def templatize(src, origin=None):
for t in Lexer(src, origin).tokenize(): for t in Lexer(src, origin).tokenize():
if incomment: if incomment:
if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment': if t.token_type == TOKEN_BLOCK and t.contents == 'endcomment':
content = ''.join(comment) content = b''.join(comment)
translators_comment_start = None translators_comment_start = None
for lineno, line in enumerate(content.splitlines(True)): for lineno, line in enumerate(content.splitlines(True)):
if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK): if line.lstrip().startswith(TRANSLATOR_COMMENT_MARK):
translators_comment_start = lineno translators_comment_start = lineno
for lineno, line in enumerate(content.splitlines(True)): for lineno, line in enumerate(content.splitlines(True)):
if translators_comment_start is not None and lineno >= translators_comment_start: if translators_comment_start is not None and lineno >= translators_comment_start:
out.write(' # %s' % line) out.write(b' # %s' % line)
else: else:
out.write(' #\n') out.write(b' #\n')
incomment = False incomment = False
comment = [] comment = []
else: else:
@ -474,18 +470,18 @@ def templatize(src, origin=None):
if endbmatch: if endbmatch:
if inplural: if inplural:
if message_context: if message_context:
out.write(' npgettext(%r, %r, %r,count) ' % (message_context, ''.join(singular), ''.join(plural))) out.write(b' npgettext(%r, %r, %r,count) ' % (message_context, ''.join(singular), ''.join(plural)))
else: else:
out.write(' ngettext(%r, %r, count) ' % (''.join(singular), ''.join(plural))) out.write(b' ngettext(%r, %r, count) ' % (''.join(singular), ''.join(plural)))
for part in singular: for part in singular:
out.write(blankout(part, 'S')) out.write(blankout(part, 'S'))
for part in plural: for part in plural:
out.write(blankout(part, 'P')) out.write(blankout(part, 'P'))
else: else:
if message_context: if message_context:
out.write(' pgettext(%r, %r) ' % (message_context, ''.join(singular))) out.write(b' pgettext(%r, %r) ' % (message_context, ''.join(singular)))
else: else:
out.write(' gettext(%r) ' % ''.join(singular)) out.write(b' gettext(%r) ' % ''.join(singular))
for part in singular: for part in singular:
out.write(blankout(part, 'S')) out.write(blankout(part, 'S'))
message_context = None message_context = None
@ -531,10 +527,10 @@ def templatize(src, origin=None):
message_context = message_context.strip('"') message_context = message_context.strip('"')
elif message_context[0] == "'": elif message_context[0] == "'":
message_context = message_context.strip("'") message_context = message_context.strip("'")
out.write(' pgettext(%r, %r) ' % (message_context, g)) out.write(b' pgettext(%r, %r) ' % (message_context, g))
message_context = None message_context = None
else: else:
out.write(' gettext(%r) ' % g) out.write(b' gettext(%r) ' % g)
elif bmatch: elif bmatch:
for fmatch in constant_re.findall(t.contents): for fmatch in constant_re.findall(t.contents):
out.write(' _(%s) ' % fmatch) out.write(' _(%s) ' % fmatch)
@ -552,7 +548,7 @@ def templatize(src, origin=None):
plural = [] plural = []
elif cmatches: elif cmatches:
for cmatch in cmatches: for cmatch in cmatches:
out.write(' _(%s) ' % cmatch) out.write(b' _(%s) ' % cmatch)
elif t.contents == 'comment': elif t.contents == 'comment':
incomment = True incomment = True
else: else:
@ -561,14 +557,14 @@ def templatize(src, origin=None):
parts = t.contents.split('|') parts = t.contents.split('|')
cmatch = constant_re.match(parts[0]) cmatch = constant_re.match(parts[0])
if cmatch: if cmatch:
out.write(' _(%s) ' % cmatch.group(1)) out.write(b' _(%s) ' % cmatch.group(1))
for p in parts[1:]: for p in parts[1:]:
if p.find(':_(') >= 0: if p.find(':_(') >= 0:
out.write(' %s ' % p.split(':',1)[1]) out.write(b' %s ' % p.split(':',1)[1])
else: else:
out.write(blankout(p, 'F')) out.write(blankout(p, 'F'))
elif t.token_type == TOKEN_COMMENT: elif t.token_type == TOKEN_COMMENT:
out.write(' # %s' % t.contents) out.write(b' # %s' % t.contents)
else: else:
out.write(blankout(t.contents, 'X')) out.write(blankout(t.contents, 'X'))
return out.getvalue() return out.getvalue()

View File

@ -104,15 +104,11 @@ Complex PDFs
============ ============
If you're creating a complex PDF document with ReportLab, consider using the If you're creating a complex PDF document with ReportLab, consider using the
:mod:`cStringIO` library as a temporary holding place for your PDF file. This :mod:`io` library as a temporary holding place for your PDF file. This
library provides a file-like object interface that is particularly efficient. library provides a file-like object interface that is particularly efficient.
Here's the above "Hello World" example rewritten to use :mod:`cStringIO`:: Here's the above "Hello World" example rewritten to use :mod:`io`::
# Fall back to StringIO in environments where cStringIO is not available from io import BytesIO
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from reportlab.pdfgen import canvas from reportlab.pdfgen import canvas
from django.http import HttpResponse from django.http import HttpResponse
@ -121,9 +117,9 @@ Here's the above "Hello World" example rewritten to use :mod:`cStringIO`::
response = HttpResponse(mimetype='application/pdf') response = HttpResponse(mimetype='application/pdf')
response['Content-Disposition'] = 'attachment; filename=somefilename.pdf' response['Content-Disposition'] = 'attachment; filename=somefilename.pdf'
buffer = StringIO() buffer = BytesIO()
# Create the PDF object, using the StringIO object as its "file." # Create the PDF object, using the BytesIO object as its "file."
p = canvas.Canvas(buffer) p = canvas.Canvas(buffer)
# Draw things on the PDF. Here's where the PDF generation happens. # Draw things on the PDF. Here's where the PDF generation happens.
@ -134,7 +130,7 @@ Here's the above "Hello World" example rewritten to use :mod:`cStringIO`::
p.showPage() p.showPage()
p.save() p.save()
# Get the value of the StringIO buffer and write it to the response. # Get the value of the BytesIO buffer and write it to the response.
pdf = buffer.getvalue() pdf = buffer.getvalue()
buffer.close() buffer.close()
response.write(pdf) response.write(pdf)

View File

@ -1,6 +1,6 @@
import copy import copy
import sys import sys
from cStringIO import StringIO from io import BytesIO
from django.core.management.validation import get_validation_errors from django.core.management.validation import get_validation_errors
from django.db.models.loading import cache, load_app from django.db.models.loading import cache, load_app
@ -16,7 +16,7 @@ class InvalidModelTestCase(unittest.TestCase):
# coloring attached (makes matching the results easier). We restore # coloring attached (makes matching the results easier). We restore
# sys.stderr afterwards. # sys.stderr afterwards.
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
self.stdout = StringIO() self.stdout = BytesIO()
sys.stdout = self.stdout sys.stdout = self.stdout
# This test adds dummy applications to the app cache. These # This test adds dummy applications to the app cache. These

View File

@ -1372,19 +1372,6 @@ class OldFormForXTests(TestCase):
self.assertEqual(instance.image.name, 'foo/test4.png') self.assertEqual(instance.image.name, 'foo/test4.png')
instance.delete() instance.delete()
# Test image field when cStringIO is not available
from django.forms import fields
from StringIO import StringIO
old_StringIO = fields.StringIO
fields.StringIO = StringIO
try:
f = ImageFileForm(
data={'description': u'An image'},
files={'image': SimpleUploadedFile('test.png', image_data)})
self.assertEqual(f.is_valid(), True)
finally:
fields.StringIO = old_StringIO
def test_media_on_modelform(self): def test_media_on_modelform(self):
# Similar to a regular Form class you can define custom media to be used on # Similar to a regular Form class you can define custom media to be used on
# the ModelForm. # the ModelForm.

View File

@ -7,10 +7,7 @@ import shutil
import tempfile import tempfile
import time import time
from datetime import datetime, timedelta from datetime import datetime, timedelta
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try: try:
import threading import threading
@ -483,7 +480,7 @@ class DimensionClosingBug(unittest.TestCase):
""" """
Open files passed into get_image_dimensions() should stay opened. Open files passed into get_image_dimensions() should stay opened.
""" """
empty_io = StringIO() empty_io = BytesIO()
try: try:
get_image_dimensions(empty_io) get_image_dimensions(empty_io)
finally: finally:
@ -557,10 +554,10 @@ class NoNameFileTestCase(unittest.TestCase):
urllib.urlopen() urllib.urlopen()
""" """
def test_noname_file_default_name(self): def test_noname_file_default_name(self):
self.assertEqual(File(StringIO('A file with no name')).name, None) self.assertEqual(File(BytesIO(b'A file with no name')).name, None)
def test_noname_file_get_size(self): def test_noname_file_get_size(self):
self.assertEqual(File(StringIO('A file with no name')).size, 19) self.assertEqual(File(BytesIO(b'A file with no name')).size, 19)
class FileLikeObjectTestCase(LiveServerBase): class FileLikeObjectTestCase(LiveServerBase):
""" """

View File

@ -4,10 +4,7 @@ from __future__ import absolute_import
import os import os
import re import re
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.core import management from django.core import management
from django.core.management.base import CommandError from django.core.management.base import CommandError
@ -119,7 +116,7 @@ class TestFixtures(TestCase):
Test for ticket #4371 -- Loading data of an unknown format should fail Test for ticket #4371 -- Loading data of an unknown format should fail
Validate that error conditions are caught correctly Validate that error conditions are caught correctly
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture1.unkn', 'bad_fixture1.unkn',
@ -138,7 +135,7 @@ class TestFixtures(TestCase):
using explicit filename. using explicit filename.
Validate that error conditions are caught correctly Validate that error conditions are caught correctly
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2.xml', 'bad_fixture2.xml',
@ -157,7 +154,7 @@ class TestFixtures(TestCase):
without file extension. without file extension.
Validate that error conditions are caught correctly Validate that error conditions are caught correctly
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2', 'bad_fixture2',
@ -175,7 +172,7 @@ class TestFixtures(TestCase):
Test for ticket #4371 -- Loading a fixture file with no data returns an error. Test for ticket #4371 -- Loading a fixture file with no data returns an error.
Validate that error conditions are caught correctly Validate that error conditions are caught correctly
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'empty', 'empty',
@ -194,7 +191,7 @@ class TestFixtures(TestCase):
loading is aborted. loading is aborted.
Validate that error conditions are caught correctly Validate that error conditions are caught correctly
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'empty', 'empty',
@ -211,7 +208,7 @@ class TestFixtures(TestCase):
""" """
(Regression for #9011 - error message is correct) (Regression for #9011 - error message is correct)
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'bad_fixture2', 'bad_fixture2',
@ -317,7 +314,7 @@ class TestFixtures(TestCase):
) )
animal.save() animal.save()
stdout = StringIO() stdout = BytesIO()
management.call_command( management.call_command(
'dumpdata', 'dumpdata',
'fixtures_regress.animal', 'fixtures_regress.animal',
@ -346,7 +343,7 @@ class TestFixtures(TestCase):
""" """
Regression for #11428 - Proxy models aren't included when you dumpdata Regression for #11428 - Proxy models aren't included when you dumpdata
""" """
stdout = StringIO() stdout = BytesIO()
# Create an instance of the concrete class # Create an instance of the concrete class
widget = Widget.objects.create(name='grommet') widget = Widget.objects.create(name='grommet')
management.call_command( management.call_command(
@ -379,7 +376,7 @@ class TestFixtures(TestCase):
""" """
Regression for #3615 - Ensure data with nonexistent child key references raises error Regression for #3615 - Ensure data with nonexistent child key references raises error
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'forward_ref_bad_data.json', 'forward_ref_bad_data.json',
@ -414,7 +411,7 @@ class TestFixtures(TestCase):
""" """
Regression for #7043 - Error is quickly reported when no fixtures is provided in the command line. Regression for #7043 - Error is quickly reported when no fixtures is provided in the command line.
""" """
stderr = StringIO() stderr = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
verbosity=0, verbosity=0,
@ -426,7 +423,7 @@ class TestFixtures(TestCase):
) )
def test_loaddata_not_existant_fixture_file(self): def test_loaddata_not_existant_fixture_file(self):
stdout_output = StringIO() stdout_output = BytesIO()
management.call_command( management.call_command(
'loaddata', 'loaddata',
'this_fixture_doesnt_exist', 'this_fixture_doesnt_exist',
@ -511,7 +508,7 @@ class NaturalKeyFixtureTests(TestCase):
commit=False commit=False
) )
stdout = StringIO() stdout = BytesIO()
management.call_command( management.call_command(
'dumpdata', 'dumpdata',
'fixtures_regress.book', 'fixtures_regress.book',

View File

@ -1,8 +1,5 @@
import os import os
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.core.management import CommandError from django.core.management import CommandError
from django.core.management.commands.compilemessages import compile_messages from django.core.management.commands.compilemessages import compile_messages
@ -31,7 +28,7 @@ class PoFileTests(MessageCompilationTests):
# We don't use the django.core.management infrastructure (call_command() # We don't use the django.core.management infrastructure (call_command()
# et al) because CommandError's cause exit(1) there. We test the # et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead # underlying compile_messages function instead
out = StringIO() out = BytesIO()
self.assertRaises(CommandError, compile_messages, out, locale=self.LOCALE) self.assertRaises(CommandError, compile_messages, out, locale=self.LOCALE)
self.assertFalse(os.path.exists(self.MO_FILE)) self.assertFalse(os.path.exists(self.MO_FILE))
@ -51,7 +48,7 @@ class PoFileContentsTests(MessageCompilationTests):
# We don't use the django.core.management infrastructure (call_command() # We don't use the django.core.management infrastructure (call_command()
# et al) because CommandError's cause exit(1) there. We test the # et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead # underlying compile_messages function instead
out = StringIO() out = BytesIO()
compile_messages(out, locale=self.LOCALE) compile_messages(out, locale=self.LOCALE)
self.assertTrue(os.path.exists(self.MO_FILE)) self.assertTrue(os.path.exists(self.MO_FILE))
@ -70,7 +67,7 @@ class PercentRenderingTests(MessageCompilationTests):
# We don't use the django.core.management infrastructure (call_command() # We don't use the django.core.management infrastructure (call_command()
# et al) because CommandError's cause exit(1) there. We test the # et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead # underlying compile_messages function instead
out = StringIO() out = BytesIO()
compile_messages(out, locale=self.LOCALE) compile_messages(out, locale=self.LOCALE)
with translation.override(self.LOCALE): with translation.override(self.LOCALE):
t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}') t = Template('{% load i18n %}{% trans "Looks like a str fmt spec %% o but shouldn\'t be interpreted as such" %}')

View File

@ -1,9 +1,6 @@
from __future__ import absolute_import from __future__ import absolute_import
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from django.core import management from django.core import management
from django.contrib.auth.models import User from django.contrib.auth.models import User
@ -73,11 +70,11 @@ class M2MThroughTestCase(TestCase):
pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk} pks = {"p_pk": p.pk, "g_pk": g.pk, "m_pk": m.pk}
out = StringIO() out = BytesIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks) self.assertEqual(out.getvalue().strip(), """[{"pk": %(m_pk)s, "model": "m2m_through_regress.membership", "fields": {"person": %(p_pk)s, "price": 100, "group": %(g_pk)s}}, {"pk": %(p_pk)s, "model": "m2m_through_regress.person", "fields": {"name": "Bob"}}, {"pk": %(g_pk)s, "model": "m2m_through_regress.group", "fields": {"name": "Roll"}}]""" % pks)
out = StringIO() out = BytesIO()
management.call_command("dumpdata", "m2m_through_regress", format="xml", management.call_command("dumpdata", "m2m_through_regress", format="xml",
indent=2, stdout=out) indent=2, stdout=out)
self.assertEqual(out.getvalue().strip(), """ self.assertEqual(out.getvalue().strip(), """
@ -145,6 +142,6 @@ class ThroughLoadDataTestCase(TestCase):
def test_sequence_creation(self): def test_sequence_creation(self):
"Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107" "Check that sequences on an m2m_through are created for the through model, not a phantom auto-generated m2m table. Refs #11107"
out = StringIO() out = BytesIO()
management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out) management.call_command("dumpdata", "m2m_through_regress", format="json", stdout=out)
self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""") self.assertEqual(out.getvalue().strip(), """[{"pk": 1, "model": "m2m_through_regress.usermembership", "fields": {"price": 100, "group": 1, "user": 1}}, {"pk": 1, "model": "m2m_through_regress.person", "fields": {"name": "Guido"}}, {"pk": 1, "model": "m2m_through_regress.group", "fields": {"name": "Python Core Group"}}]""")

View File

@ -10,10 +10,7 @@ from __future__ import absolute_import
import datetime import datetime
import decimal import decimal
try: from io import BytesIO
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
try: try:
import yaml import yaml
@ -504,7 +501,7 @@ def streamTest(format, self):
obj.save_base(raw=True) obj.save_base(raw=True)
# Serialize the test database to a stream # Serialize the test database to a stream
stream = StringIO() stream = BytesIO()
serializers.serialize(format, [obj], indent=2, stream=stream) serializers.serialize(format, [obj], indent=2, stream=stream)
# Serialize normally for a comparison # Serialize normally for a comparison