Commit 54d82f59 by Tom Christie

Py3 compat fix

parent d13c8076
...@@ -12,9 +12,9 @@ from __future__ import unicode_literals ...@@ -12,9 +12,9 @@ from __future__ import unicode_literals
from django.conf import settings from django.conf import settings
from django.http import QueryDict from django.http import QueryDict
from django.http.multipartparser import parse_header from django.http.multipartparser import parse_header
from django.utils import six
from django.utils.datastructures import MultiValueDict from django.utils.datastructures import MultiValueDict
from django.utils.datastructures import MergeDict as DjangoMergeDict from django.utils.datastructures import MergeDict as DjangoMergeDict
from django.utils.six import BytesIO
from rest_framework import HTTP_HEADER_ENCODING from rest_framework import HTTP_HEADER_ENCODING
from rest_framework import exceptions from rest_framework import exceptions
from rest_framework.settings import api_settings from rest_framework.settings import api_settings
...@@ -363,7 +363,7 @@ class Request(object): ...@@ -363,7 +363,7 @@ class Request(object):
elif hasattr(self._request, 'read'): elif hasattr(self._request, 'read'):
self._stream = self._request self._stream = self._request
else: else:
self._stream = BytesIO(self.raw_post_data) self._stream = six.BytesIO(self.raw_post_data)
def _perform_form_overloading(self): def _perform_form_overloading(self):
""" """
...@@ -405,7 +405,7 @@ class Request(object): ...@@ -405,7 +405,7 @@ class Request(object):
self._CONTENTTYPE_PARAM in self._data self._CONTENTTYPE_PARAM in self._data
): ):
self._content_type = self._data[self._CONTENTTYPE_PARAM] self._content_type = self._data[self._CONTENTTYPE_PARAM]
self._stream = BytesIO(self._data[self._CONTENT_PARAM].encode(self.parser_context['encoding'])) self._stream = six.BytesIO(self._data[self._CONTENT_PARAM].encode(self.parser_context['encoding']))
self._data, self._files, self._full_data = (Empty, Empty, Empty) self._data, self._files, self._full_data = (Empty, Empty, Empty)
def _parse(self): def _parse(self):
...@@ -498,4 +498,4 @@ class Request(object): ...@@ -498,4 +498,4 @@ class Request(object):
try: try:
return getattr(self._request, attr) return getattr(self._request, attr)
except AttributeError: except AttributeError:
raise info[0], info[1], info[2].tb_next six.reraise(info[0], info[1], info[2].tb_next)
...@@ -249,6 +249,26 @@ class TestUserSetter(TestCase): ...@@ -249,6 +249,26 @@ class TestUserSetter(TestCase):
login(self.request, self.user) login(self.request, self.user)
self.assertEqual(self.wrapped_request.user, self.user) self.assertEqual(self.wrapped_request.user, self.user)
def test_calling_user_fails_when_attribute_error_is_raised(self):
"""
This proves that when an AttributeError is raised inside of the request.user
property, that we can handle this and report the true, underlying error.
"""
class AuthRaisesAttributeError(object):
def authenticate(self, request):
import rest_framework
rest_framework.MISSPELLED_NAME_THAT_DOESNT_EXIST
self.request = Request(factory.get('/'), authenticators=(AuthRaisesAttributeError(),))
SessionMiddleware().process_request(self.request)
login(self.request, self.user)
try:
self.request.user
except AttributeError as error:
self.assertEqual(str(error), "'module' object has no attribute 'MISSPELLED_NAME_THAT_DOESNT_EXIST'")
else:
assert False, 'AttributeError not raised'
class TestAuthSetter(TestCase): class TestAuthSetter(TestCase):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment