diff options
| author | Tom Christie | 2014-12-15 12:04:46 +0000 |
|---|---|---|
| committer | Tom Christie | 2014-12-15 12:04:46 +0000 |
| commit | af53e34dd5873f3373e9991c3825e70d92432e14 (patch) | |
| tree | f5503776ef204ff125f476116b11ca0801fd3b8e /rest_framework/compat.py | |
| parent | 1f6fd924fea05b9b7eb4bedf44dfdcb2f14c5cad (diff) | |
| parent | dc66cce16da6793efe4a4a4dcdd18db62c859abb (diff) | |
| download | django-rest-framework-af53e34dd5873f3373e9991c3825e70d92432e14.tar.bz2 | |
Merge pull request #2279 from tomchristie/fix-serializer-repr-unicode-bug
Use unicode internally everywhere for 'repr'.
Diffstat (limited to 'rest_framework/compat.py')
| -rw-r--r-- | rest_framework/compat.py | 17 |
1 files changed, 17 insertions, 0 deletions
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. |
