Python 2.5 and 3.1 support confirmed

This commit is contained in:
Ross Lawley
2012-08-07 13:07:51 +01:00
parent 44bd8cb85b
commit dd4af2df81
12 changed files with 68 additions and 55 deletions

View File

@@ -10,7 +10,7 @@ from queryset import DoesNotExist, MultipleObjectsReturned
from queryset import DO_NOTHING
from mongoengine import signals
from mongoengine.python3_support import PY3, txt_type
from mongoengine.python_support import PY3, PY25, txt_type
import pymongo
from bson import ObjectId
@@ -894,10 +894,8 @@ class BaseDocument(object):
if not is_list and '_cls' in value:
cls = get_document(value['_cls'])
# Make keys str instead of unicode
for key,val in value.items():
del value[key]
value[str(key)] = val
if PY25:
value = dict([(str(k), v) for k, v in value.items()])
value = cls(**value)
value._dynamic = True
value._changed_fields = []
@@ -1019,12 +1017,9 @@ class BaseDocument(object):
raise InvalidDocumentError("""
Invalid data to create a `%s` instance.\n%s""".strip() % (cls._class_name, errors))
# Make all keys str instead of unicode
for key,val in data.items():
del data[key]
data[str(key)] = val
if PY25:
data = dict([(str(k), v) for k, v in data.items()])
obj = cls(**data)
obj._changed_fields = changed_fields
obj._created = False
return obj

View File

@@ -1,7 +1,7 @@
#coding: utf-8
from nose.plugins.skip import SkipTest
from mongoengine.python3_support import PY3
from mongoengine.python_support import PY3
from mongoengine import connect
try:
@@ -18,7 +18,7 @@ except Exception as err:
class MongoTestCase(TestCase):
def setUp(self):
if PY3:
raise SkipTest('django does not have Python 3 support')

View File

@@ -10,8 +10,8 @@ from operator import itemgetter
import gridfs
from bson import Binary, DBRef, SON, ObjectId
from mongoengine.python3_support import (PY3, bin_type,
txt_type, str_types, StringIO)
from mongoengine.python_support import (PY3, bin_type, txt_type,
str_types, StringIO)
from base import (BaseField, ComplexBaseField, ObjectIdField,
ValidationError, get_document, BaseDocument)
from queryset import DO_NOTHING, QuerySet
@@ -840,13 +840,20 @@ class BinaryField(BaseField):
self.max_bytes = max_bytes
super(BinaryField, self).__init__(**kwargs)
def __set__(self, instance, value):
"""Handle bytearrays in python 3.1"""
if PY3 and isinstance(value, bytearray):
value = bin_type(value)
return super(BinaryField, self).__set__(instance, value)
def to_mongo(self, value):
return Binary(value)
def validate(self, value):
if not isinstance(value, (bin_type, txt_type, Binary)):
self.error("BinaryField only accepts instances of "
"(%s, %s, Binary)" % (bin_type.__name__,txt_type.__name__))
"(%s, %s, Binary)" % (
bin_type.__name__, txt_type.__name__))
if self.max_bytes is not None and len(value) > self.max_bytes:
self.error('Binary value is too long')
@@ -1270,9 +1277,9 @@ class SequenceField(IntField):
"""
Generate and Increment the counter
"""
sequence_id = "%s.%s"%(self.owner_document._get_collection_name(),
self.name)
collection = get_db(alias = self.db_alias )[self.collection_name]
sequence_id = "%s.%s" % (self.owner_document._get_collection_name(),
self.name)
collection = get_db(alias=self.db_alias)[self.collection_name]
counter = collection.find_and_modify(query={"_id": sequence_id},
update={"$inc": {"next": 1}},
new=True,

View File

@@ -1,13 +1,14 @@
"""Helper functions and types to aid with Python 3 support."""
"""Helper functions and types to aid with Python 2.5 - 3 support."""
import sys
PY3 = sys.version_info[0] == 3
PY25 = sys.version_info[:2] == (2, 5)
if PY3:
import codecs
from io import BytesIO as StringIO
# return s converted to binary. b('test') should be equivalent to b'test'
# return s converted to binary. b('test') should be equivalent to b'test'
def b(s):
return codecs.latin_1_encode(s)[0]
@@ -24,6 +25,19 @@ else:
return s
bin_type = str
txt_type = unicode
txt_type = unicode
str_types = (bin_type, txt_type)
if PY25:
def product(*args, **kwds):
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x + [y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
reduce = reduce
else:
from itertools import product
from functools import reduce

View File

@@ -3,9 +3,9 @@ import re
import copy
import itertools
import operator
import functools
import sys
from functools import partial
from mongoengine.python_support import product, reduce
import pymongo
from bson.code import Code
@@ -15,18 +15,6 @@ from mongoengine import signals
__all__ = ['queryset_manager', 'Q', 'InvalidQueryError',
'DO_NOTHING', 'NULLIFY', 'CASCADE', 'DENY', 'PULL']
if sys.version_info < (2,6,0):
def product(*args, **kwds):
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
else:
from itertools import product
from functools import reduce
# The maximum number of items to display in a QuerySet.__repr__
REPR_OUTPUT_SIZE = 20