diff --git a/pymongo/_cbsonmodule.c b/pymongo/_cbsonmodule.c index 9d740af24..0648919e0 100644 --- a/pymongo/_cbsonmodule.c +++ b/pymongo/_cbsonmodule.c @@ -104,7 +104,14 @@ static PyObject* _cbson_element_to_bson(PyObject* self, PyObject* args) { * here we check for type equivalence, not isinstance in some * places. */ if (PyString_CheckExact(value)) { - return build_string(0x02, value, name); + // we have to do the encoding so we can fail fast if they give us non utf-8 + PyObject* encoded = PyString_AsEncodedObject(value, "utf-8", "strict"); + if (!encoded) { + return NULL; + } + PyObject* result = build_string(0x02, encoded, name); + Py_DECREF(encoded); + return result; } else if (PyUnicode_CheckExact(value)) { PyObject* encoded = PyUnicode_AsUTF8String(value); if (!encoded) { diff --git a/pymongo/bson.py b/pymongo/bson.py index 7461afabd..5840926e0 100644 --- a/pymongo/bson.py +++ b/pymongo/bson.py @@ -52,10 +52,8 @@ def _get_c_string(data): return (unicode(data[:end], "utf-8"), data[end + 1:]) -def _make_c_string(string, encode=True): - if encode and isinstance(string, unicode): - return string.encode("utf-8") + "\x00" - return string + "\x00" +def _make_c_string(string): + return string.encode("utf-8") + "\x00" def _validate_number(data): assert len(data) >= 8 @@ -298,11 +296,11 @@ def _element_to_bson(key, value): value = struct.pack("