Skip to content

Commit

Permalink
Prefer io.BytesIO over six; available on all supported Pythons (#6168)
Browse files Browse the repository at this point in the history
On all supported Pythons, the io.BytesIO is always a stream
implementation using an in-memory bytes buffer.

Makes code slightly more forward compatible by reducing use of the six
module and promotes more forward compatible practices in the docs.
  • Loading branch information
jdufresne authored and tomchristie committed Sep 9, 2018
1 parent 1d5771d commit 4d57d46
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 8 deletions.
4 changes: 2 additions & 2 deletions docs/api-guide/serializers.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ At this point we've translated the model instance into Python native datatypes.

Deserialization is similar. First we parse a stream into Python native datatypes...

from django.utils.six import BytesIO
import io
from rest_framework.parsers import JSONParser

stream = BytesIO(json)
stream = io.BytesIO(json)
data = JSONParser().parse(stream)

...then we restore those native datatypes into a dictionary of validated data.
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorial/1-serialization.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,9 @@ At this point we've translated the model instance into Python native datatypes.

Deserialization is similar. First we parse a stream into Python native datatypes...

from django.utils.six import BytesIO
import io

stream = BytesIO(content)
stream = io.BytesIO(content)
data = JSONParser().parse(stream)

...then we restore those native datatypes into a fully populated object instance.
Expand Down
3 changes: 2 additions & 1 deletion rest_framework/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"""
from __future__ import unicode_literals

import io
import sys
from contextlib import contextmanager

Expand Down Expand Up @@ -301,7 +302,7 @@ def _load_stream(self):
elif not self._request._read_started:
self._stream = self._request
else:
self._stream = six.BytesIO(self.body)
self._stream = io.BytesIO(self.body)

def _supports_form_parsing(self):
"""
Expand Down
7 changes: 4 additions & 3 deletions tests/test_parsers.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

import io
import math

import pytest
Expand All @@ -10,7 +11,7 @@
)
from django.http.request import RawPostDataException
from django.test import TestCase
from django.utils.six import BytesIO, StringIO
from django.utils.six import StringIO

from rest_framework.exceptions import ParseError
from rest_framework.parsers import (
Expand Down Expand Up @@ -43,7 +44,7 @@ class TestFileUploadParser(TestCase):
def setUp(self):
class MockRequest(object):
pass
self.stream = BytesIO(
self.stream = io.BytesIO(
"Test text file".encode('utf-8')
)
request = MockRequest()
Expand Down Expand Up @@ -131,7 +132,7 @@ def __replace_content_disposition(self, disposition):

class TestJSONParser(TestCase):
def bytes(self, value):
return BytesIO(value.encode('utf-8'))
return io.BytesIO(value.encode('utf-8'))

def test_float_strictness(self):
parser = JSONParser()
Expand Down

0 comments on commit 4d57d46

Please sign in to comment.