From 9d463d2a4bf02ef5381d7ab1d5c6f2dafdbb2b9c Mon Sep 17 00:00:00 2001 From: Bernie Hackett Date: Tue, 27 Mar 2012 11:29:20 -0700 Subject: [PATCH] Minor: style. --- bson/_cbsonmodule.c | 23 +++++++++++----------- pymongo/_cmessagemodule.c | 41 ++++++++++----------------------------- 2 files changed, 21 insertions(+), 43 deletions(-) diff --git a/bson/_cbsonmodule.c b/bson/_cbsonmodule.c index eb9a62947..74adc933d 100644 --- a/bson/_cbsonmodule.c +++ b/bson/_cbsonmodule.c @@ -415,13 +415,11 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by { #if PY_MAJOR_VERSION >= 3 const long long_subtype = PyLong_AsLong(subtype_object); -#else - const long long_subtype = PyInt_AsLong(subtype_object); -#endif const char subtype = (const char)long_subtype; -#if PY_MAJOR_VERSION >= 3 const int length = PyBytes_Size(value); #else + const long long_subtype = PyInt_AsLong(subtype_object); + const char subtype = (const char)long_subtype; const int length = PyString_Size(value); #endif @@ -461,6 +459,8 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by } else if (state->UUID && PyObject_IsInstance(value, state->UUID)) { // Just a special case of Binary above, but simpler to do as a separate case + // Could be bytes, bytearray, str... + const char* binarr; // UUID is always 16 bytes int length = 16; const char subtype = (const char)uuid_subtype; @@ -482,15 +482,15 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer, int type_by #if PY_MAJOR_VERSION >= 3 /* Work around http://bugs.python.org/issue7380 */ if (PyByteArray_Check(bytes)) { - if (!buffer_write_bytes(buffer, PyByteArray_AsString(bytes), length)) { - Py_DECREF(bytes); - return 0; - } + binarr = PyByteArray_AsString(bytes); + } + else { + binarr = PyBytes_AsString(bytes); } - else if (!buffer_write_bytes(buffer, PyBytes_AsString(bytes), length)) { #else - if (!buffer_write_bytes(buffer, PyString_AsString(bytes), length)) { + binarr = PyString_AsString(bytes); #endif + if (!buffer_write_bytes(buffer, binarr, length)) { Py_DECREF(bytes); return 0; } @@ -1203,14 +1203,13 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position, in *position += length + 5; break; } -#endif if (subtype == 2) { -#if PY_MAJOR_VERSION >= 3 data = PyBytes_FromStringAndSize(buffer + *position + 9, length - 4); } else { data = PyBytes_FromStringAndSize(buffer + *position + 5, length); } #else + if (subtype == 2) { data = PyString_FromStringAndSize(buffer + *position + 9, length - 4); } else { data = PyString_FromStringAndSize(buffer + *position + 5, length); diff --git a/pymongo/_cmessagemodule.c b/pymongo/_cmessagemodule.c index eb54685b0..a040c2c15 100644 --- a/pymongo/_cmessagemodule.c +++ b/pymongo/_cmessagemodule.c @@ -37,6 +37,12 @@ struct module_state { static struct module_state _state; #endif +#if PY_MAJOR_VERSION >= 3 +#define BYTES_FORMAT_STRING "y#" +#else +#define BYTES_FORMAT_STRING "s#" +#endif + /* Get an error class from the pymongo.errors module. * * Returns a new ref */ @@ -221,17 +227,10 @@ static PyObject* _cbson_insert_message(PyObject* self, PyObject* args) { } /* objectify buffer */ -#if PY_MAJOR_VERSION >= 3 - result = Py_BuildValue("iy#i", request_id, + result = Py_BuildValue("i" BYTES_FORMAT_STRING "i", request_id, buffer_get_buffer(buffer), buffer_get_position(buffer), max_size); -#else - result = Py_BuildValue("is#i", request_id, - buffer_get_buffer(buffer), - buffer_get_position(buffer), - max_size); -#endif buffer_free(buffer); return result; } @@ -332,17 +331,10 @@ static PyObject* _cbson_update_message(PyObject* self, PyObject* args) { } /* objectify buffer */ -#if PY_MAJOR_VERSION >= 3 - result = Py_BuildValue("iy#i", request_id, + result = Py_BuildValue("i" BYTES_FORMAT_STRING "i", request_id, buffer_get_buffer(buffer), buffer_get_position(buffer), max_size); -#else - result = Py_BuildValue("is#i", request_id, - buffer_get_buffer(buffer), - buffer_get_position(buffer), - max_size); -#endif buffer_free(buffer); return result; } @@ -425,17 +417,10 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) { memcpy(buffer_get_buffer(buffer) + length_location, &message_length, 4); /* objectify buffer */ -#if PY_MAJOR_VERSION >= 3 - result = Py_BuildValue("iy#i", request_id, + result = Py_BuildValue("i" BYTES_FORMAT_STRING "i", request_id, buffer_get_buffer(buffer), buffer_get_position(buffer), max_size); -#else - result = Py_BuildValue("is#i", request_id, - buffer_get_buffer(buffer), - buffer_get_position(buffer), - max_size); -#endif buffer_free(buffer); return result; } @@ -494,15 +479,9 @@ static PyObject* _cbson_get_more_message(PyObject* self, PyObject* args) { memcpy(buffer_get_buffer(buffer) + length_location, &message_length, 4); /* objectify buffer */ -#if PY_MAJOR_VERSION >= 3 - result = Py_BuildValue("iy#", request_id, + result = Py_BuildValue("i" BYTES_FORMAT_STRING, request_id, buffer_get_buffer(buffer), buffer_get_position(buffer)); -#else - result = Py_BuildValue("is#", request_id, - buffer_get_buffer(buffer), - buffer_get_position(buffer)); -#endif buffer_free(buffer); return result; }