Improve unicode key handling for PY25
This commit is contained in:
parent
90fa0f6c4a
commit
e990a6c70c
@ -11,7 +11,8 @@ from queryset import DoesNotExist, MultipleObjectsReturned
|
|||||||
from queryset import DO_NOTHING
|
from queryset import DO_NOTHING
|
||||||
|
|
||||||
from mongoengine import signals
|
from mongoengine import signals
|
||||||
from mongoengine.python_support import PY3, PY25, txt_type
|
from mongoengine.python_support import (PY3, PY25, txt_type,
|
||||||
|
to_str_keys_recursive)
|
||||||
|
|
||||||
import pymongo
|
import pymongo
|
||||||
from bson import ObjectId
|
from bson import ObjectId
|
||||||
@ -961,8 +962,6 @@ class BaseDocument(object):
|
|||||||
|
|
||||||
if not is_list and '_cls' in value:
|
if not is_list and '_cls' in value:
|
||||||
cls = get_document(value['_cls'])
|
cls = get_document(value['_cls'])
|
||||||
if PY25:
|
|
||||||
value = dict([(str(k), v) for k, v in value.items()])
|
|
||||||
return cls(**value)
|
return cls(**value)
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
@ -1046,6 +1045,10 @@ class BaseDocument(object):
|
|||||||
# class if unavailable
|
# class if unavailable
|
||||||
class_name = son.get('_cls', cls._class_name)
|
class_name = son.get('_cls', cls._class_name)
|
||||||
data = dict(("%s" % key, value) for key, value in son.items())
|
data = dict(("%s" % key, value) for key, value in son.items())
|
||||||
|
if PY25:
|
||||||
|
# PY25 cannot handle unicode keys passed to class constructor
|
||||||
|
# example: cls(**data)
|
||||||
|
to_str_keys_recursive(data)
|
||||||
|
|
||||||
if '_types' in data:
|
if '_types' in data:
|
||||||
del data['_types']
|
del data['_types']
|
||||||
@ -1084,8 +1087,6 @@ class BaseDocument(object):
|
|||||||
% (cls._class_name, errors))
|
% (cls._class_name, errors))
|
||||||
raise InvalidDocumentError(msg)
|
raise InvalidDocumentError(msg)
|
||||||
|
|
||||||
if PY25:
|
|
||||||
data = dict([(str(k), v) for k, v in data.items()])
|
|
||||||
obj = cls(**data)
|
obj = cls(**data)
|
||||||
obj._changed_fields = changed_fields
|
obj._changed_fields = changed_fields
|
||||||
obj._created = False
|
obj._created = False
|
||||||
|
@ -41,3 +41,20 @@ if PY25:
|
|||||||
else:
|
else:
|
||||||
from itertools import product
|
from itertools import product
|
||||||
from functools import reduce
|
from functools import reduce
|
||||||
|
|
||||||
|
|
||||||
|
# For use with Python 2.5
|
||||||
|
# converts all keys from unicode to str for d and all nested dictionaries
|
||||||
|
def to_str_keys_recursive(d):
|
||||||
|
if isinstance(d, list):
|
||||||
|
for val in d:
|
||||||
|
if isinstance(val, (dict, list)):
|
||||||
|
to_str_keys_recursive(val)
|
||||||
|
elif isinstance(d, dict):
|
||||||
|
for key, val in d.items():
|
||||||
|
if isinstance(val, (dict, list)):
|
||||||
|
to_str_keys_recursive(val)
|
||||||
|
if isinstance(key, unicode):
|
||||||
|
d[str(key)] = d.pop(key)
|
||||||
|
else:
|
||||||
|
raise ValueError("non list/dict parameter not allowed")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user