22The `compat` module provides support for backwards compatibility with older
33versions of Django/Python, and compatibility wrappers around optional packages.
44"""
5-
6- from __future__ import unicode_literals
7-
85import sys
6+ from collections .abc import Mapping , MutableMapping # noqa
97
108from django .conf import settings
119from django .core import validators
12- from django .utils import six
1310from django .views .generic import View
1411
15- try :
16- # Python 3
17- from collections .abc import Mapping , MutableMapping # noqa
18- except ImportError :
19- # Python 2.7
20- from collections import Mapping , MutableMapping # noqa
21-
2212try :
2313 from django .urls import ( # noqa
2414 URLPattern ,
3626except ImportError :
3727 ProhibitNullCharactersValidator = None
3828
39- try :
40- from unittest import mock
41- except ImportError :
42- mock = None
43-
4429
4530def get_original_route (urlpattern ):
4631 """
@@ -89,23 +74,6 @@ def make_url_resolver(regex, urlpatterns):
8974 return URLResolver (regex , urlpatterns )
9075
9176
92- def unicode_repr (instance ):
93- # Get the repr of an instance, but ensure it is a unicode string
94- # on both python 3 (already the case) and 2 (not the case).
95- if six .PY2 :
96- return repr (instance ).decode ('utf-8' )
97- return repr (instance )
98-
99-
100- def unicode_to_repr (value ):
101- # Coerce a unicode string to the correct repr return type, depending on
102- # the Python version. We wrap all our `__repr__` implementations with
103- # this and then use unicode throughout internally.
104- if six .PY2 :
105- return value .encode ('utf-8' )
106- return value
107-
108-
10977def unicode_http_header (value ):
11078 # Coerce HTTP header value to unicode.
11179 if isinstance (value , bytes ):
@@ -168,15 +136,6 @@ def is_guardian_installed():
168136 """
169137 django-guardian is optional and only imported if in INSTALLED_APPS.
170138 """
171- try :
172- import guardian
173- except ImportError :
174- guardian = None
175-
176- if six .PY2 and (not guardian or guardian .VERSION >= (1 , 5 )):
177- # Guardian 1.5.0, for Django 2.2 is NOT compatible with Python 2.7.
178- # Remove when dropping PY2.
179- return False
180139 return 'guardian' in settings .INSTALLED_APPS
181140
182141
@@ -289,17 +248,12 @@ def md_filter_add_syntax_highlight(md):
289248
290249# `separators` argument to `json.dumps()` differs between 2.x and 3.x
291250# See: https://bugs.python.org/issue22767
292- if six .PY3 :
293- SHORT_SEPARATORS = (',' , ':' )
294- LONG_SEPARATORS = (', ' , ': ' )
295- INDENT_SEPARATORS = (',' , ': ' )
296- else :
297- SHORT_SEPARATORS = (b',' , b':' )
298- LONG_SEPARATORS = (b', ' , b': ' )
299- INDENT_SEPARATORS = (b',' , b': ' )
251+ SHORT_SEPARATORS = (',' , ':' )
252+ LONG_SEPARATORS = (', ' , ': ' )
253+ INDENT_SEPARATORS = (',' , ': ' )
300254
301255
302- class CustomValidatorMessage ( object ) :
256+ class CustomValidatorMessage :
303257 """
304258 We need to avoid evaluation of `lazy` translated `message` in `django.core.validators.BaseValidator.__init__`.
305259 https://github.com/django/django/blob/75ed5900321d170debef4ac452b8b3cf8a1c2384/django/core/validators.py#L297
@@ -309,7 +263,7 @@ class CustomValidatorMessage(object):
309263
310264 def __init__ (self , * args , ** kwargs ):
311265 self .message = kwargs .pop ('message' , self .message )
312- super (CustomValidatorMessage , self ).__init__ (* args , ** kwargs )
266+ super ().__init__ (* args , ** kwargs )
313267
314268
315269class MinValueValidator (CustomValidatorMessage , validators .MinValueValidator ):
0 commit comments