Skip to content

Commit 4d57d46

Browse files
jdufresnetomchristie
authored andcommitted
Prefer io.BytesIO over six; available on all supported Pythons (#6168)
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.
1 parent 1d5771d commit 4d57d46

File tree

4 files changed

+10
-8
lines changed

4 files changed

+10
-8
lines changed

docs/api-guide/serializers.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ At this point we've translated the model instance into Python native datatypes.
5757

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

60-
from django.utils.six import BytesIO
60+
import io
6161
from rest_framework.parsers import JSONParser
6262

63-
stream = BytesIO(json)
63+
stream = io.BytesIO(json)
6464
data = JSONParser().parse(stream)
6565

6666
...then we restore those native datatypes into a dictionary of validated data.

docs/tutorial/1-serialization.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ At this point we've translated the model instance into Python native datatypes.
154154

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

157-
from django.utils.six import BytesIO
157+
import io
158158

159-
stream = BytesIO(content)
159+
stream = io.BytesIO(content)
160160
data = JSONParser().parse(stream)
161161

162162
...then we restore those native datatypes into a fully populated object instance.

rest_framework/request.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"""
1111
from __future__ import unicode_literals
1212

13+
import io
1314
import sys
1415
from contextlib import contextmanager
1516

@@ -301,7 +302,7 @@ def _load_stream(self):
301302
elif not self._request._read_started:
302303
self._stream = self._request
303304
else:
304-
self._stream = six.BytesIO(self.body)
305+
self._stream = io.BytesIO(self.body)
305306

306307
def _supports_form_parsing(self):
307308
"""

tests/test_parsers.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
import io
45
import math
56

67
import pytest
@@ -10,7 +11,7 @@
1011
)
1112
from django.http.request import RawPostDataException
1213
from django.test import TestCase
13-
from django.utils.six import BytesIO, StringIO
14+
from django.utils.six import StringIO
1415

1516
from rest_framework.exceptions import ParseError
1617
from rest_framework.parsers import (
@@ -43,7 +44,7 @@ class TestFileUploadParser(TestCase):
4344
def setUp(self):
4445
class MockRequest(object):
4546
pass
46-
self.stream = BytesIO(
47+
self.stream = io.BytesIO(
4748
"Test text file".encode('utf-8')
4849
)
4950
request = MockRequest()
@@ -131,7 +132,7 @@ def __replace_content_disposition(self, disposition):
131132

132133
class TestJSONParser(TestCase):
133134
def bytes(self, value):
134-
return BytesIO(value.encode('utf-8'))
135+
return io.BytesIO(value.encode('utf-8'))
135136

136137
def test_float_strictness(self):
137138
parser = JSONParser()

0 commit comments

Comments
 (0)