Skip to content

Commit 789296c

Browse files
author
Mike Dirolf
committed
Split C extension in two (pymongo, bson) PYTHON-60
1 parent b0f9875 commit 789296c

File tree

14 files changed

+500
-388
lines changed

14 files changed

+500
-388
lines changed

bson/__init__.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,3 +500,15 @@ def decode(self, as_class=dict, tz_aware=False):
500500
"""
501501
(document, _) = _bson_to_dict(self, as_class, tz_aware)
502502
return document
503+
504+
505+
def has_c():
506+
"""Is the C extension installed?
507+
508+
.. versionadded:: 1.8.1+
509+
"""
510+
try:
511+
from bson import _cbson
512+
return True
513+
except ImportError:
514+
return False

bson/_cbson.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2009-2010 10gen, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include <Python.h>
18+
19+
/* A buffer representing some data being encoded to BSON. */
20+
typedef struct {
21+
char* buffer;
22+
int size;
23+
int position;
24+
} bson_buffer;
25+
26+
bson_buffer* buffer_new(void);
27+
28+
int buffer_save_bytes(bson_buffer* buffer, int size);
29+
30+
int buffer_write_bytes(bson_buffer* buffer, const char* bytes, int size);
31+
32+
void buffer_free(bson_buffer* buffer);
33+
34+
int write_dict(bson_buffer* buffer, PyObject* dict,
35+
unsigned char check_keys, unsigned char top_level);
36+
37+
PyObject* elements_to_dict(const char* string, int max,
38+
PyObject* as_class, unsigned char tz_aware);
39+
40+
int write_pair(bson_buffer* buffer, const char* name,
41+
Py_ssize_t name_length, PyObject* value,
42+
unsigned char check_keys, unsigned char allow_id);
43+
44+
int decode_and_write_pair(bson_buffer* buffer,
45+
PyObject* key, PyObject* value,
46+
unsigned char check_keys, unsigned char top_level);

0 commit comments

Comments
 (0)