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

@ -7,10 +7,7 @@ import shutil
import tempfile
import time
from datetime import datetime, timedelta
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from io import BytesIO
try:
import threading
@ -483,7 +480,7 @@ class DimensionClosingBug(unittest.TestCase):
"""
Open files passed into get_image_dimensions() should stay opened.
"""
empty_io = StringIO()
empty_io = BytesIO()
try:
get_image_dimensions(empty_io)
finally:
@ -557,10 +554,10 @@ class NoNameFileTestCase(unittest.TestCase):
urllib.urlopen()
"""
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):
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):
"""

View file

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

View file

@ -1,8 +1,5 @@
import os
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from io import BytesIO
from django.core.management import CommandError
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()
# et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead
out = StringIO()
out = BytesIO()
self.assertRaises(CommandError, compile_messages, out, locale=self.LOCALE)
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()
# et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead
out = StringIO()
out = BytesIO()
compile_messages(out, locale=self.LOCALE)
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()
# et al) because CommandError's cause exit(1) there. We test the
# underlying compile_messages function instead
out = StringIO()
out = BytesIO()
compile_messages(out, locale=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" %}')

View file

@ -1,9 +1,6 @@
from __future__ import absolute_import
try:
from cStringIO import StringIO
except ImportError:
from StringIO import StringIO
from io import BytesIO
from django.core import management
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}
out = StringIO()
out = BytesIO()
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)
out = StringIO()
out = BytesIO()
management.call_command("dumpdata", "m2m_through_regress", format="xml",
indent=2, stdout=out)
self.assertEqual(out.getvalue().strip(), """
@ -145,6 +142,6 @@ class ThroughLoadDataTestCase(TestCase):
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"
out = StringIO()
out = BytesIO()
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"}}]""")

View file

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