Skip to content

Commit 54d82f5

Browse files
committed
Py3 compat fix
1 parent d13c807 commit 54d82f5

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

rest_framework/request.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
from django.conf import settings
1313
from django.http import QueryDict
1414
from django.http.multipartparser import parse_header
15+
from django.utils import six
1516
from django.utils.datastructures import MultiValueDict
1617
from django.utils.datastructures import MergeDict as DjangoMergeDict
17-
from django.utils.six import BytesIO
1818
from rest_framework import HTTP_HEADER_ENCODING
1919
from rest_framework import exceptions
2020
from rest_framework.settings import api_settings
@@ -363,7 +363,7 @@ def _load_stream(self):
363363
elif hasattr(self._request, 'read'):
364364
self._stream = self._request
365365
else:
366-
self._stream = BytesIO(self.raw_post_data)
366+
self._stream = six.BytesIO(self.raw_post_data)
367367

368368
def _perform_form_overloading(self):
369369
"""
@@ -405,7 +405,7 @@ def _perform_form_overloading(self):
405405
self._CONTENTTYPE_PARAM in self._data
406406
):
407407
self._content_type = self._data[self._CONTENTTYPE_PARAM]
408-
self._stream = BytesIO(self._data[self._CONTENT_PARAM].encode(self.parser_context['encoding']))
408+
self._stream = six.BytesIO(self._data[self._CONTENT_PARAM].encode(self.parser_context['encoding']))
409409
self._data, self._files, self._full_data = (Empty, Empty, Empty)
410410

411411
def _parse(self):
@@ -498,4 +498,4 @@ def __getattribute__(self, attr):
498498
try:
499499
return getattr(self._request, attr)
500500
except AttributeError:
501-
raise info[0], info[1], info[2].tb_next
501+
six.reraise(info[0], info[1], info[2].tb_next)

tests/test_request.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,26 @@ def test_logged_in_user_is_set_on_wrapped_request(self):
249249
login(self.request, self.user)
250250
self.assertEqual(self.wrapped_request.user, self.user)
251251

252+
def test_calling_user_fails_when_attribute_error_is_raised(self):
253+
"""
254+
This proves that when an AttributeError is raised inside of the request.user
255+
property, that we can handle this and report the true, underlying error.
256+
"""
257+
class AuthRaisesAttributeError(object):
258+
def authenticate(self, request):
259+
import rest_framework
260+
rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
261+
262+
self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),))
263+
SessionMiddleware().process_request(self.request)
264+
265+
login(self.request, self.user)
266+
try:
267+
self.request.user
268+
except AttributeError as error:
269+
self.assertEqual(str(error), "'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'")
270+
else:
271+
assert False, 'AttributeError not raised'
252272

253273
class TestAuthSetter(TestCase):
254274

0 commit comments

Comments
 (0)