mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-26 03:23:59 +03:00
Fix compat issues for #1231
This commit is contained in:
parent
a6ca943faa
commit
ddd17c69e7
|
@ -69,6 +69,13 @@ try:
|
|||
except ImportError:
|
||||
import urlparse
|
||||
|
||||
# UserDict moves in Python 3
|
||||
try:
|
||||
from UserDict import UserDict
|
||||
from UserDict import DictMixin
|
||||
except ImportError:
|
||||
from collections import UserDict
|
||||
from collections import MutableMapping as DictMixin
|
||||
|
||||
# Try to import PIL in either of the two ways it can end up installed.
|
||||
try:
|
||||
|
|
|
@ -15,12 +15,11 @@ from rest_framework.renderers import BaseRenderer, JSONRenderer, YAMLRenderer, \
|
|||
from rest_framework.parsers import YAMLParser, XMLParser
|
||||
from rest_framework.settings import api_settings
|
||||
from rest_framework.test import APIRequestFactory
|
||||
from collections import MutableMapping
|
||||
import datetime
|
||||
import json
|
||||
import pickle
|
||||
import re
|
||||
import UserDict
|
||||
import collections
|
||||
import json
|
||||
|
||||
|
||||
DUMMYSTATUS = status.HTTP_200_OK
|
||||
|
@ -277,26 +276,8 @@ class JSONRendererTests(TestCase):
|
|||
ret = JSONRenderer().render(_('test'))
|
||||
self.assertEqual(ret, b'"test"')
|
||||
|
||||
def test_render_userdict_obj(self):
|
||||
class DictLike(UserDict.DictMixin):
|
||||
def __init__(self):
|
||||
self._dict = dict()
|
||||
def __getitem__(self, key):
|
||||
return self._dict.__getitem__(key)
|
||||
def __setitem__(self, key, value):
|
||||
return self._dict.__setitem__(key, value)
|
||||
def __delitem__(self, key):
|
||||
return self._dict.__delitem__(key)
|
||||
def keys(self):
|
||||
return self._dict.keys()
|
||||
x = DictLike()
|
||||
x['a'] = 1
|
||||
x['b'] = "string value"
|
||||
ret = JSONRenderer().render(x)
|
||||
self.assertEquals(json.loads(ret), {'a': 1, 'b': 'string value'})
|
||||
|
||||
def test_render_dict_abc_obj(self):
|
||||
class Dict(collections.MutableMapping):
|
||||
class Dict(MutableMapping):
|
||||
def __init__(self):
|
||||
self._dict = dict()
|
||||
def __getitem__(self, key):
|
||||
|
@ -309,13 +290,15 @@ class JSONRendererTests(TestCase):
|
|||
return self._dict.__iter__()
|
||||
def __len__(self):
|
||||
return self._dict.__len__()
|
||||
def keys(self):
|
||||
return self._dict.keys()
|
||||
|
||||
x = Dict()
|
||||
x['key'] = 'string value'
|
||||
x[2] = 3
|
||||
ret = JSONRenderer().render(x)
|
||||
self.assertEquals(json.loads(ret), {'key': 'string value', '2': 3})
|
||||
|
||||
data = json.loads(ret.decode('utf-8'))
|
||||
self.assertEquals(data, {'key': 'string value', '2': 3})
|
||||
|
||||
def test_render_obj_with_getitem(self):
|
||||
class DictLike(object):
|
||||
|
|
|
@ -47,8 +47,7 @@ class JSONEncoder(json.JSONEncoder):
|
|||
elif hasattr(o, '__getitem__'):
|
||||
try:
|
||||
return dict(o)
|
||||
except KeyError:
|
||||
# Couldn't convert to a dict, fall through
|
||||
except:
|
||||
pass
|
||||
elif hasattr(o, '__iter__'):
|
||||
return [i for i in o]
|
||||
|
|
Loading…
Reference in New Issue
Block a user