diff --git a/django/core/serializers/base.py b/django/core/serializers/base.py index bdb43db9f3..276f9a4738 100644 --- a/django/core/serializers/base.py +++ b/django/core/serializers/base.py @@ -2,8 +2,6 @@ Module for abstract serializer/unserializer base classes. """ -from io import BytesIO - from django.db import models from django.utils.encoding import smart_text from django.utils import six @@ -35,7 +33,7 @@ class Serializer(object): """ self.options = options - self.stream = options.pop("stream", BytesIO()) + self.stream = options.pop("stream", six.StringIO()) self.selected_fields = options.pop("fields", None) self.use_natural_keys = options.pop("use_natural_keys", False) @@ -125,7 +123,7 @@ class Deserializer(object): """ self.options = options if isinstance(stream_or_string, six.string_types): - self.stream = BytesIO(stream_or_string) + self.stream = six.StringIO(stream_or_string) else: self.stream = stream_or_string # hack to make sure that the models have all been loaded before diff --git a/tests/modeltests/serializers/tests.py b/tests/modeltests/serializers/tests.py index ec3cc132db..e39f17b68a 100644 --- a/tests/modeltests/serializers/tests.py +++ b/tests/modeltests/serializers/tests.py @@ -114,8 +114,8 @@ class SerializersTestBase(object): Tests the ability to create new objects by modifying serialized content. """ - old_headline = b"Poker has no place on ESPN" - new_headline = b"Poker has no place on television" + old_headline = "Poker has no place on ESPN" + new_headline = "Poker has no place on television" serial_str = serializers.serialize(self.serializer_name, Article.objects.all()) serial_str = serial_str.replace(old_headline, new_headline) @@ -285,7 +285,7 @@ class SerializersTransactionTestBase(object): class XmlSerializerTestCase(SerializersTestBase, TestCase): serializer_name = "xml" - pkless_str = b""" + pkless_str = """ Reference @@ -331,7 +331,7 @@ class XmlSerializerTestCase(SerializersTestBase, TestCase): class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): serializer_name = "xml" - fwd_ref_str = b""" + fwd_ref_str = """ 1 @@ -351,7 +351,7 @@ class XmlSerializerTransactionTestCase(SerializersTransactionTestBase, Transacti class JsonSerializerTestCase(SerializersTestBase, TestCase): serializer_name = "json" - pkless_str = b"""[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]""" + pkless_str = """[{"pk": null, "model": "serializers.category", "fields": {"name": "Reference"}}]""" @staticmethod def _validate_output(serial_str): @@ -381,7 +381,7 @@ class JsonSerializerTestCase(SerializersTestBase, TestCase): class JsonSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): serializer_name = "json" - fwd_ref_str = b"""[ + fwd_ref_str = """[ { "pk": 1, "model": "serializers.article", @@ -414,7 +414,7 @@ except ImportError: else: class YamlSerializerTestCase(SerializersTestBase, TestCase): serializer_name = "yaml" - fwd_ref_str = b"""- fields: + fwd_ref_str = """- fields: headline: Forward references pose no problem pub_date: 2006-06-16 15:00:00 categories: [1] @@ -430,7 +430,7 @@ else: pk: 1 model: serializers.author""" - pkless_str = b"""- fields: + pkless_str = """- fields: name: Reference pk: null model: serializers.category""" @@ -470,7 +470,7 @@ else: class YamlSerializerTransactionTestCase(SerializersTransactionTestBase, TransactionTestCase): serializer_name = "yaml" - fwd_ref_str = b"""- fields: + fwd_ref_str = """- fields: headline: Forward references pose no problem pub_date: 2006-06-16 15:00:00 categories: [1] diff --git a/tests/regressiontests/serializers_regress/tests.py b/tests/regressiontests/serializers_regress/tests.py index 4e73be015c..f4b3adaa5e 100644 --- a/tests/regressiontests/serializers_regress/tests.py +++ b/tests/regressiontests/serializers_regress/tests.py @@ -10,7 +10,6 @@ from __future__ import absolute_import, unicode_literals import datetime import decimal -from io import BytesIO try: import yaml @@ -23,6 +22,7 @@ from django.core.serializers.base import DeserializationError from django.db import connection, models from django.http import HttpResponse from django.test import TestCase +from django.utils import six from django.utils.functional import curry from django.utils.unittest import skipUnless @@ -502,17 +502,17 @@ def streamTest(format, self): obj.save_base(raw=True) # Serialize the test database to a stream - for stream in (BytesIO(), HttpResponse()): + for stream in (six.StringIO(), HttpResponse()): serializers.serialize(format, [obj], indent=2, stream=stream) # Serialize normally for a comparison string_data = serializers.serialize(format, [obj], indent=2) # Check that the two are the same - if isinstance(stream, BytesIO): + if isinstance(stream, six.StringIO): self.assertEqual(string_data, stream.getvalue()) else: - self.assertEqual(string_data, stream.content) + self.assertEqual(string_data, stream.content.decode('utf-8')) stream.close() for format in serializers.get_serializer_formats():