support for MaxKey and MinKey PYTHON-75
This commit is contained in:
parent
0a6c5bb179
commit
359fbd8535
@ -40,6 +40,8 @@ static PyObject* DBRef = NULL;
|
||||
static PyObject* RECompile = NULL;
|
||||
static PyObject* UUID = NULL;
|
||||
static PyObject* Timestamp = NULL;
|
||||
static PyObject* MinKey = NULL;
|
||||
static PyObject* MaxKey = NULL;
|
||||
static PyTypeObject* REType = NULL;
|
||||
|
||||
#if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
|
||||
@ -272,6 +274,8 @@ static int _reload_python_objects(void) {
|
||||
_reload_object(&ObjectId, "pymongo.objectid", "ObjectId") ||
|
||||
_reload_object(&DBRef, "pymongo.dbref", "DBRef") ||
|
||||
_reload_object(&Timestamp, "pymongo.timestamp", "Timestamp") ||
|
||||
_reload_object(&MinKey, "pymongo.min_key", "MinKey") ||
|
||||
_reload_object(&MaxKey, "pymongo.max_key", "MaxKey") ||
|
||||
_reload_object(&RECompile, "re", "compile")) {
|
||||
return 1;
|
||||
}
|
||||
@ -666,6 +670,12 @@ static int write_element_to_buffer(bson_buffer* buffer, int type_byte, PyObject*
|
||||
}
|
||||
*(buffer->buffer + type_byte) = 0x0B;
|
||||
return 1;
|
||||
} else if (PyObject_IsInstance(value, MinKey)) {
|
||||
*(buffer->buffer + type_byte) = 0xFF;
|
||||
return 1;
|
||||
} else if (PyObject_IsInstance(value, MaxKey)) {
|
||||
*(buffer->buffer + type_byte) = 0x7F;
|
||||
return 1;
|
||||
} else if (first_attempt) {
|
||||
/* Try reloading the modules and having one more go at it. */
|
||||
if (WARN(PyExc_RuntimeWarning, "couldn't encode - reloading python "
|
||||
@ -1518,6 +1528,16 @@ static PyObject* get_value(const char* buffer, int* position, int type,
|
||||
*position += 8;
|
||||
break;
|
||||
}
|
||||
case -1:
|
||||
{
|
||||
value = PyObject_CallFunctionObjArgs(MinKey, NULL);
|
||||
break;
|
||||
}
|
||||
case 127:
|
||||
{
|
||||
value = PyObject_CallFunctionObjArgs(MaxKey, NULL);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
PyObject* InvalidDocument = _error("InvalidDocument");
|
||||
|
||||
@ -28,6 +28,8 @@ from pymongo.errors import (InvalidBSON,
|
||||
InvalidDocument,
|
||||
InvalidName,
|
||||
InvalidStringData)
|
||||
from pymongo.max_key import MaxKey
|
||||
from pymongo.min_key import MinKey
|
||||
from pymongo.objectid import ObjectId
|
||||
from pymongo.son import SON
|
||||
from timestamp import Timestamp
|
||||
@ -206,6 +208,8 @@ _element_getter = {
|
||||
"\x10": _get_int, # number_int
|
||||
"\x11": _get_timestamp,
|
||||
"\x12": _get_long,
|
||||
"\xFF": lambda x, y: (MinKey(), x),
|
||||
"\x7F": lambda x, y: (MaxKey(), x)
|
||||
}
|
||||
|
||||
|
||||
@ -329,6 +333,10 @@ def _element_to_bson(key, value, check_keys):
|
||||
_make_c_string(flags)
|
||||
if isinstance(value, DBRef):
|
||||
return _element_to_bson(key, value.as_doc(), False)
|
||||
if isinstance(value, MinKey):
|
||||
return "\xFF" + name
|
||||
if isinstance(value, MaxKey):
|
||||
return "\x7F" + name
|
||||
|
||||
raise InvalidDocument("cannot convert value of type %s to bson" %
|
||||
type(value))
|
||||
|
||||
30
pymongo/max_key.py
Normal file
30
pymongo/max_key.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Copyright 2010 10gen, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Representation for the MongoDB internal MaxKey type.
|
||||
|
||||
.. versionadded:: 1.6+
|
||||
"""
|
||||
|
||||
class MaxKey(object):
|
||||
"""MongoDB internal MaxKey type.
|
||||
"""
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, MaxKey):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
def __repr__(self):
|
||||
return "MaxKey()"
|
||||
30
pymongo/min_key.py
Normal file
30
pymongo/min_key.py
Normal file
@ -0,0 +1,30 @@
|
||||
# Copyright 2010 10gen, Inc.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Representation for the MongoDB internal MinKey type.
|
||||
|
||||
.. versionadded:: 1.6+
|
||||
"""
|
||||
|
||||
class MinKey(object):
|
||||
"""MongoDB internal MinKey type.
|
||||
"""
|
||||
|
||||
def __eq__(self, other):
|
||||
if isinstance(other, MinKey):
|
||||
return True
|
||||
return NotImplemented
|
||||
|
||||
def __repr__(self):
|
||||
return "MinKey()"
|
||||
@ -38,6 +38,8 @@ from pymongo.son import SON
|
||||
from pymongo.timestamp import Timestamp
|
||||
from pymongo.bson import BSON, is_valid, _to_dicts
|
||||
from pymongo.errors import InvalidDocument, InvalidStringData
|
||||
from pymongo.max_key import MaxKey
|
||||
from pymongo.min_key import MinKey
|
||||
import qcheck
|
||||
|
||||
class SomeZone(datetime.tzinfo):
|
||||
@ -171,6 +173,8 @@ class TestBSON(unittest.TestCase):
|
||||
helper({"ref": DBRef("coll", 5)})
|
||||
helper({"ref": DBRef("coll", 5, "foo")})
|
||||
helper({"ref": Timestamp(1,2)})
|
||||
helper({"foo": MinKey()})
|
||||
helper({"foo": MaxKey()})
|
||||
|
||||
def from_then_to_dict(dict):
|
||||
return dict == (BSON.from_dict(dict)).to_dict()
|
||||
|
||||
Loading…
Reference in New Issue
Block a user