Skip to content

Commit 09e59f2

Browse files
committed
Removed custom python_2_unicode_compatible. Closes #2183
1 parent f8fdfe5 commit 09e59f2

File tree

5 files changed

+21
-38
lines changed

5 files changed

+21
-38
lines changed

rest_framework/authtoken/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import binascii
22
import os
3+
34
from django.conf import settings
45
from django.db import models
6+
from django.utils.encoding import python_2_unicode_compatible
57

68

79
# Prior to Django 1.5, the AUTH_USER_MODEL setting does not exist.
@@ -11,6 +13,7 @@
1113
AUTH_USER_MODEL = getattr(settings, 'AUTH_USER_MODEL', 'auth.User')
1214

1315

16+
@python_2_unicode_compatible
1417
class Token(models.Model):
1518
"""
1619
The default authorization token model.
@@ -35,5 +38,5 @@ def save(self, *args, **kwargs):
3538
def generate_key(self):
3639
return binascii.hexlify(os.urandom(20)).decode()
3740

38-
def __unicode__(self):
41+
def __str__(self):
3942
return self.key

rest_framework/compat.py

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
# flake8: noqa
77
from __future__ import unicode_literals
88

9+
import inspect
10+
911
from django.core.exceptions import ImproperlyConfigured
1012
from django.conf import settings
1113
from django.utils import six
1214
import django
13-
import inspect
1415

1516

1617
# Handle django.utils.encoding rename in 1.5 onwards.
@@ -49,7 +50,6 @@
4950
except ImportError:
5051
django_filters = None
5152

52-
5353
if django.VERSION >= (1, 6):
5454
def clean_manytomany_helptext(text):
5555
return text
@@ -123,7 +123,6 @@ def _allowed_methods(self):
123123
return [m.upper() for m in self.http_method_names if hasattr(self, m)]
124124

125125

126-
127126
# MinValueValidator, MaxValueValidator et al. only accept `message` in 1.8+
128127
if django.VERSION >= (1, 8):
129128
from django.core.validators import MinValueValidator, MaxValueValidator
@@ -187,28 +186,30 @@ def __init__(self, *args, **kwargs):
187186
# RequestFactory only provides `generic` from 1.5 onwards
188187
from django.test.client import RequestFactory as DjangoRequestFactory
189188
from django.test.client import FakePayload
189+
190190
try:
191191
# In 1.5 the test client uses force_bytes
192192
from django.utils.encoding import force_bytes as force_bytes_or_smart_bytes
193193
except ImportError:
194194
# In 1.4 the test client just uses smart_str
195195
from django.utils.encoding import smart_str as force_bytes_or_smart_bytes
196196

197+
197198
class RequestFactory(DjangoRequestFactory):
198199
def generic(self, method, path,
199200
data='', content_type='application/octet-stream', **extra):
200201
parsed = urlparse.urlparse(path)
201202
data = force_bytes_or_smart_bytes(data, settings.DEFAULT_CHARSET)
202203
r = {
203-
'PATH_INFO': self._get_path(parsed),
204-
'QUERY_STRING': force_text(parsed[4]),
204+
'PATH_INFO': self._get_path(parsed),
205+
'QUERY_STRING': force_text(parsed[4]),
205206
'REQUEST_METHOD': six.text_type(method),
206207
}
207208
if data:
208209
r.update({
209210
'CONTENT_LENGTH': len(data),
210-
'CONTENT_TYPE': six.text_type(content_type),
211-
'wsgi.input': FakePayload(data),
211+
'CONTENT_TYPE': six.text_type(content_type),
212+
'wsgi.input': FakePayload(data),
212213
})
213214
elif django.VERSION <= (1, 4):
214215
# For 1.3 we need an empty WSGI payload
@@ -287,10 +288,12 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
287288
import provider as oauth2_provider
288289
from provider import scope as oauth2_provider_scope
289290
from provider import constants as oauth2_constants
291+
290292
if oauth2_provider.__version__ in ('0.2.3', '0.2.4'):
291293
# 0.2.3 and 0.2.4 are supported version that do not support
292294
# timezone aware datetimes
293295
import datetime
296+
294297
provider_now = datetime.datetime.now
295298
else:
296299
# Any other supported version does use timezone aware datetimes
@@ -301,7 +304,7 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
301304
oauth2_constants = None
302305
provider_now = None
303306

304-
# `seperators` argument to `json.dumps()` differs between 2.x and 3.x
307+
# `separators` argument to `json.dumps()` differs between 2.x and 3.x
305308
# See: http://bugs.python.org/issue22767
306309
if six.PY3:
307310
SHORT_SEPARATORS = (',', ':')
@@ -316,30 +319,9 @@ def check_nonce(request, oauth_request, oauth_nonce, oauth_timestamp):
316319

317320
if six.PY3:
318321
def is_non_str_iterable(obj):
319-
if (isinstance(obj, str) or
320-
(isinstance(obj, Promise) and obj._delegate_text)):
322+
if isinstance(obj, str) or (isinstance(obj, Promise) and obj._delegate_text):
321323
return False
322324
return hasattr(obj, '__iter__')
323325
else:
324326
def is_non_str_iterable(obj):
325327
return hasattr(obj, '__iter__')
326-
327-
328-
try:
329-
from django.utils.encoding import python_2_unicode_compatible
330-
except ImportError:
331-
def python_2_unicode_compatible(klass):
332-
"""
333-
A decorator that defines __unicode__ and __str__ methods under Python 2.
334-
Under Python 3 it does nothing.
335-
336-
To support Python 2 and 3 with a single code base, define a __str__ method
337-
returning text and apply this decorator to the class.
338-
"""
339-
if '__str__' not in klass.__dict__:
340-
raise ValueError("@python_2_unicode_compatible cannot be applied "
341-
"to %s because it doesn't define __str__()." %
342-
klass.__name__)
343-
klass.__unicode__ = klass.__str__
344-
klass.__str__ = lambda self: self.__unicode__().encode('utf-8')
345-
return klass

rest_framework/utils/mediatypes.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
"""
66
from __future__ import unicode_literals
77
from django.http.multipartparser import parse_header
8+
from django.utils.encoding import python_2_unicode_compatible
89
from rest_framework import HTTP_HEADER_ENCODING
910

1011

@@ -43,6 +44,7 @@ def order_by_precedence(media_type_lst):
4344
return [media_types for media_types in ret if media_types]
4445

4546

47+
@python_2_unicode_compatible
4648
class _MediaType(object):
4749
def __init__(self, media_type_str):
4850
if media_type_str is None:
@@ -79,9 +81,6 @@ def precedence(self):
7981
return 3
8082

8183
def __str__(self):
82-
return self.__unicode__().encode('utf-8')
83-
84-
def __unicode__(self):
8584
ret = "%s/%s" % (self.main_type, self.sub_type)
8685
for key, val in self.params.items():
8786
ret += "; %s=%s" % (key, val)

tests/test_description.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import unicode_literals
44
from django.test import TestCase
5+
from django.utils.encoding import python_2_unicode_compatible
56
from rest_framework.compat import apply_markdown, smart_text
67
from rest_framework.views import APIView
78
from .description import ViewWithNonASCIICharactersInDocstring
@@ -107,16 +108,14 @@ class that can be converted to a string.
107108
"""
108109
# use a mock object instead of gettext_lazy to ensure that we can't end
109110
# up with a test case string in our l10n catalog
111+
@python_2_unicode_compatible
110112
class MockLazyStr(object):
111113
def __init__(self, string):
112114
self.s = string
113115

114116
def __str__(self):
115117
return self.s
116118

117-
def __unicode__(self):
118-
return self.s
119-
120119
class MockView(APIView):
121120
__doc__ = MockLazyStr("a gettext string")
122121

tests/test_relations_generic.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from django.contrib.contenttypes.generic import GenericRelation, GenericForeignKey
44
from django.db import models
55
from django.test import TestCase
6+
from django.utils.encoding import python_2_unicode_compatible
67
from rest_framework import serializers
7-
from rest_framework.compat import python_2_unicode_compatible
88

99

1010
@python_2_unicode_compatible

0 commit comments

Comments
 (0)