aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--rest_framework/compat.py2
-rw-r--r--rest_framework/fields.py3
-rw-r--r--rest_framework/parsers.py16
-rw-r--r--rest_framework/request.py9
-rw-r--r--rest_framework/templatetags/rest_framework.py3
-rw-r--r--rest_framework/tests/files.py6
6 files changed, 23 insertions, 16 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 8c7617c1..6ffada48 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -21,6 +21,8 @@ try:
except ImportError:
from six import StringIO
+from six import BytesIO
+
def get_concrete_model(model_cls):
try:
diff --git a/rest_framework/fields.py b/rest_framework/fields.py
index 42c9a203..5c5a86c1 100644
--- a/rest_framework/fields.py
+++ b/rest_framework/fields.py
@@ -8,8 +8,6 @@ import inspect
import re
import warnings
-from io import BytesIO
-
from django.core import validators
from django.core.exceptions import ObjectDoesNotExist, ValidationError
from django.core.urlresolvers import resolve, get_script_prefix
@@ -25,6 +23,7 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework.reverse import reverse
from rest_framework.compat import parse_date, parse_datetime
from rest_framework.compat import timezone
+from rest_framework.compat import BytesIO
try:
from urllib.parse import urlparse
except ImportError:
diff --git a/rest_framework/parsers.py b/rest_framework/parsers.py
index 361dfb77..d5cfaaf8 100644
--- a/rest_framework/parsers.py
+++ b/rest_framework/parsers.py
@@ -5,6 +5,8 @@ They give us a generic way of being able to handle various media types
on the request, such as form content or json encoded data.
"""
+import six
+
from django.http import QueryDict
from django.http.multipartparser import MultiPartParser as DjangoMultiPartParser
from django.http.multipartparser import MultiPartParserError
@@ -55,9 +57,10 @@ class JSONParser(BaseParser):
`files` will always be `None`.
"""
try:
- return json.load(stream)
+ data = stream.read().decode('iso-8859-1')
+ return json.loads(data)
except ValueError as exc:
- raise ParseError('JSON parse error - %s' % unicode(exc))
+ raise ParseError('JSON parse error - %s' % six.text_type(exc))
class YAMLParser(BaseParser):
@@ -75,9 +78,10 @@ class YAMLParser(BaseParser):
`files` will always be `None`.
"""
try:
- return yaml.safe_load(stream)
+ data = stream.read().decode('iso-8859-1')
+ return yaml.safe_load(data)
except (ValueError, yaml.parser.ParserError) as exc:
- raise ParseError('YAML parse error - %s' % unicode(exc))
+ raise ParseError('YAML parse error - %s' % six.u(exc))
class FormParser(BaseParser):
@@ -122,7 +126,7 @@ class MultiPartParser(BaseParser):
data, files = parser.parse()
return DataAndFiles(data, files)
except MultiPartParserError as exc:
- raise ParseError('Multipart form parse error - %s' % unicode(exc))
+ raise ParseError('Multipart form parse error - %s' % six.u(exc))
class XMLParser(BaseParser):
@@ -136,7 +140,7 @@ class XMLParser(BaseParser):
try:
tree = ET.parse(stream)
except (ExpatError, ETParseError, ValueError) as exc:
- raise ParseError('XML parse error - %s' % unicode(exc))
+ raise ParseError('XML parse error - %s' % six.u(exc))
data = self._xml_convert(tree.getroot())
return data
diff --git a/rest_framework/request.py b/rest_framework/request.py
index d0c4ded6..05424f21 100644
--- a/rest_framework/request.py
+++ b/rest_framework/request.py
@@ -9,7 +9,8 @@ The wrapped request then offers a richer API, in particular :
- full support of PUT method, including support for file uploads
- form overloading of HTTP method, content type and content
"""
-from rest_framework.compat import StringIO
+import six
+from rest_framework.compat import BytesIO
from django.http.multipartparser import parse_header
from rest_framework import exceptions
@@ -20,7 +21,7 @@ def is_form_media_type(media_type):
"""
Return True if the media type is a valid form media type.
"""
- base_media_type, params = parse_header(media_type.encode('utf8'))
+ base_media_type, params = parse_header(media_type.encode('iso-8859-1'))
return (base_media_type == 'application/x-www-form-urlencoded' or
base_media_type == 'multipart/form-data')
@@ -216,7 +217,7 @@ class Request(object):
elif hasattr(self._request, 'read'):
self._stream = self._request
else:
- self._stream = StringIO(self.raw_post_data)
+ self._stream = BytesIO(self.raw_post_data)
def _perform_form_overloading(self):
"""
@@ -251,7 +252,7 @@ class Request(object):
self._CONTENT_PARAM in self._data and
self._CONTENTTYPE_PARAM in self._data):
self._content_type = self._data[self._CONTENTTYPE_PARAM]
- self._stream = StringIO(self._data[self._CONTENT_PARAM])
+ self._stream = BytesIO(self._data[self._CONTENT_PARAM].encode('iso-8859-1'))
self._data, self._files = (Empty, Empty)
def _parse(self):
diff --git a/rest_framework/templatetags/rest_framework.py b/rest_framework/templatetags/rest_framework.py
index 7b9e2c37..1fc174ff 100644
--- a/rest_framework/templatetags/rest_framework.py
+++ b/rest_framework/templatetags/rest_framework.py
@@ -1,4 +1,5 @@
from __future__ import unicode_literals
+import six
from django import template
from django.core.urlresolvers import reverse
@@ -104,7 +105,7 @@ def add_class(value, css_class):
In the case of REST Framework, the filter is used to add Bootstrap-specific
classes to the forms.
"""
- html = unicode(value)
+ html = six.text_type(value)
match = class_re.search(html)
if match:
m = re.search(r'^%s$|^%s\s|\s%s\s|\s%s$' % (css_class, css_class,
diff --git a/rest_framework/tests/files.py b/rest_framework/tests/files.py
index e7609706..a69695ca 100644
--- a/rest_framework/tests/files.py
+++ b/rest_framework/tests/files.py
@@ -1,4 +1,4 @@
-from rest_framework.compat import StringIO
+from rest_framework.compat import BytesIO
import datetime
@@ -29,9 +29,9 @@ class FileSerializerTests(TestCase):
def test_create(self):
now = datetime.datetime.now()
- file = StringIO('stuff')
+ file = BytesIO(b'stuff')
file.name = 'stuff.txt'
- file.size = file.len
+ file.size = len(file.getvalue())
serializer = UploadedFileSerializer(data={'created': now}, files={'file': file})
uploaded_file = UploadedFile(file=file, created=now)
self.assertTrue(serializer.is_valid())