Skip to content

Commit

Permalink
finish and enable C decoder
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Dirolf committed Feb 5, 2009
1 parent 70e7e1a commit 0b5b331
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
36 changes: 35 additions & 1 deletion pymongo/_cbsonmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ static PyObject* Binary;
static PyObject* Code;
static PyObject* ObjectId;
static PyObject* DBRef;
static PyObject* RECompile;

static char* shuffle_oid(const char* oid) {
char* shuffled = (char*) malloc(12);
Expand Down Expand Up @@ -509,6 +510,36 @@ static PyObject* _elements_to_dict(PyObject* elements) {
position += 8;
break;
}
case 11:
{
int pattern_length = strlen(string + position);
PyObject* pattern = PyUnicode_DecodeUTF8(string + position, pattern_length, "strict");
if (!pattern) {
return NULL;
}
position += pattern_length + 1;
int flags_length = strlen(string + position);
int flags = 0;
int i;
for (i = 0; i < flags_length; i++) {
if (string[position + i] == 'i') {
flags |= 2;
} else if (string[position + i] == 'l') {
flags |= 4;
} else if (string[position + i] == 'm') {
flags |= 8;
} else if (string[position + i] == 's') {
flags |= 16;
} else if (string[position + i] == 'u') {
flags |= 32;
} else if (string[position + i] == 'x') {
flags |= 64;
}
}
position += flags_length + 1;
value = PyObject_CallFunction(RECompile, "Oi", pattern, flags);
break;
}
case 12:
{
position += 4;
Expand All @@ -525,7 +556,7 @@ static PyObject* _elements_to_dict(PyObject* elements) {
return NULL;
}
position += 12;
value = PyObject_CallFunction(DBRef, "OO", collection, id);
value = PyObject_CallFunctionObjArgs(DBRef, collection, id, NULL);
break;
}
case 16:
Expand Down Expand Up @@ -610,4 +641,7 @@ PyMODINIT_FUNC init_cbson(void) {

PyObject* dbref_module = PyImport_ImportModule("pymongo.dbref");
DBRef = PyObject_GetAttrString(dbref_module, "DBRef");

PyObject* re_module = PyImport_ImportModule("re");
RECompile = PyObject_GetAttrString(re_module, "compile");
}
4 changes: 2 additions & 2 deletions pymongo/bson.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,8 @@ def _bson_to_dict(data):
obj_size = struct.unpack("<i", data[:4])[0]
elements = data[4:obj_size - 1]
return (_elements_to_dict(elements), data[obj_size:])
# if _use_c:
# _bson_to_dict = _cbson._bson_to_dict
if _use_c:
_bson_to_dict = _cbson._bson_to_dict

def _shuffle_oid(data):
return data[7::-1] + data[:7:-1]
Expand Down

0 comments on commit 0b5b331

Please sign in to comment.