From d2f67f549d60fe17c8f36215527082e8702c385e Mon Sep 17 00:00:00 2001 From: Mike Dirolf Date: Wed, 7 Jan 2009 13:56:27 -0500 Subject: [PATCH] support for python strings (as mongo binary type --- bson.py | 8 +++++++- test/qcheck.py | 1 + 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/bson.py b/bson.py index 187f6626a..bab7bffa0 100644 --- a/bson.py +++ b/bson.py @@ -179,6 +179,10 @@ def _get_array(data): break return (result, data) +def _get_binary(data): + (length, data) = _get_int(data) + return (data[:length], data[length:]) + def _get_boolean(data): return (data[0] == "\x01", data[1:]) @@ -187,7 +191,7 @@ _element_getter = { "\x02": _get_string, "\x03": _get_object, "\x04": _get_array, -# "\x05": _get_binary, + "\x05": _get_binary, # "\x06": _get_undefined, # "\x07": _get_oid, "\x08": _get_boolean, @@ -234,6 +238,8 @@ def _value_to_bson(value): if isinstance(value, types.ListType): as_dict = dict(zip([str(i) for i in range(len(value))], value)) return ("\x04", BSON.from_dict(as_dict)) + if isinstance(value, types.StringType): + return ("\x05", _int_to_bson(len(value)) + value) if isinstance(value, types.BooleanType): if value: return ("\x08", "\x01") diff --git a/test/qcheck.py b/test/qcheck.py index 2a6560d0f..3ac671ba4 100644 --- a/test/qcheck.py +++ b/test/qcheck.py @@ -48,6 +48,7 @@ def gen_dict(gen_key, gen_value, gen_length): def gen_mongo_value(depth): choices = [gen_unicode(gen_range(0, 50)), + gen_string(gen_range(0, 1000)), gen_int(), gen_float(), gen_boolean()]