From 72e08a3e8b6427cb93f0f98b42724e31e5b3d8f9 Mon Sep 17 00:00:00 2001 From: Tom Christie Date: Mon, 15 Dec 2014 11:55:17 +0000 Subject: Use unicode internally everywhere for 'repr' --- rest_framework/compat.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'rest_framework/compat.py') diff --git a/rest_framework/compat.py b/rest_framework/compat.py index 71520b92..69fdd793 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. -- cgit v1.2.3