aboutsummaryrefslogtreecommitdiffstats
path: root/rest_framework/compat.py
diff options
context:
space:
mode:
authorTom Christie2014-12-15 12:18:55 +0000
committerTom Christie2014-12-15 12:18:55 +0000
commitb6ee784240b3c7f6cd62af5b6fe6d1014d7bf6d4 (patch)
treee3cdb5a6e210e5ba26d6726cc08a44c8f450776b /rest_framework/compat.py
parent8934e61b67e4aed38b04f2fe18f011ecbf9010cb (diff)
parentaf53e34dd5873f3373e9991c3825e70d92432e14 (diff)
downloaddjango-rest-framework-b6ee784240b3c7f6cd62af5b6fe6d1014d7bf6d4.tar.bz2
Merge master
Diffstat (limited to 'rest_framework/compat.py')
-rw-r--r--rest_framework/compat.py17
1 files changed, 17 insertions, 0 deletions
diff --git a/rest_framework/compat.py b/rest_framework/compat.py
index 43ad6eaa..c5242343 100644
--- a/rest_framework/compat.py
+++ b/rest_framework/compat.py
@@ -16,6 +16,23 @@ from django.utils import six
import django
+def unicode_repr(instance):
+ # Get the repr of an instance, but ensure it is a unicode string
+ # on both python 3 (already the case) and 2 (not the case).
+ if six.PY2:
+ repr(instance).decode('utf-8')
+ return repr(instance)
+
+
+def unicode_to_repr(value):
+ # Coerce a unicode string to the correct repr return type, depending on
+ # the Python version. We wrap all our `__repr__` implementations with
+ # this and then use unicode throughout internally.
+ if six.PY2:
+ return value.encode('utf-8')
+ return value
+
+
# OrderedDict only available in Python 2.7.
# This will always be the case in Django 1.7 and above, as these versions
# no longer support Python 2.6.