@@ -329,14 +329,10 @@ static int _load_python_objects(PyObject* module) {
329329 _load_object (& state -> MaxKey , "bson.max_key" , "MaxKey" ) ||
330330 _load_object (& state -> UTC , "bson.tz_util" , "utc" ) ||
331331 _load_object (& state -> RECompile , "re" , "compile" ) ||
332- _load_object (& state -> Regex , "bson.regex" , "Regex" )) {
332+ _load_object (& state -> Regex , "bson.regex" , "Regex" ) ||
333+ _load_object (& state -> UUID , "uuid" , "UUID" )) {
333334 return 1 ;
334335 }
335- /* If we couldn't import uuid then we must be on 2.4. Just ignore. */
336- if (_load_object (& state -> UUID , "uuid" , "UUID" ) == 1 ) {
337- state -> UUID = NULL ;
338- PyErr_Clear ();
339- }
340336 /* Reload our REType hack too. */
341337#if PY_MAJOR_VERSION >= 3
342338 empty_string = PyBytes_FromString ("" );
@@ -555,6 +551,8 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
555551 unsigned char uuid_subtype ) {
556552 struct module_state * state = GETSTATE (self );
557553
554+ PyObject * uuid_type ;
555+
558556 /*
559557 * Don't use PyObject_IsInstance for our custom types. It causes
560558 * problems with python sub interpreters. Our custom types should
@@ -1002,85 +1000,79 @@ static int _write_element_to_buffer(PyObject* self, buffer_t buffer,
10021000 /*
10031001 * Try UUID last since we have to import
10041002 * it if we're in a sub-interpreter.
1005- *
1006- * If we're running under python 2.4 there likely
1007- * isn't a uuid module.
10081003 */
1009- if (state -> UUID ) {
1010- PyObject * uuid_type = _get_object (state -> UUID , "uuid" , "UUID" );
1011- if (uuid_type && PyObject_IsInstance (value , uuid_type )) {
1012- /* Just a special case of Binary above, but
1013- * simpler to do as a separate case. */
1014- PyObject * bytes ;
1015- /* Could be bytes, bytearray, str... */
1016- const char * data ;
1017- /* UUID is always 16 bytes */
1018- int size = 16 ;
1019- int subtype ;
1020-
1021- Py_DECREF (uuid_type );
1022-
1023- if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY ) {
1024- subtype = 3 ;
1025- }
1026- else {
1027- subtype = uuid_subtype ;
1028- }
1004+ uuid_type = _get_object (state -> UUID , "uuid" , "UUID" );
1005+ if (uuid_type && PyObject_IsInstance (value , uuid_type )) {
1006+ /* Just a special case of Binary above, but
1007+ * simpler to do as a separate case. */
1008+ PyObject * bytes ;
1009+ /* Could be bytes, bytearray, str... */
1010+ const char * data ;
1011+ /* UUID is always 16 bytes */
1012+ int size = 16 ;
1013+ int subtype ;
10291014
1030- * (buffer_get_buffer (buffer ) + type_byte ) = 0x05 ;
1031- if (!buffer_write_bytes (buffer , (const char * )& size , 4 )) {
1032- return 0 ;
1033- }
1034- if (!buffer_write_bytes (buffer , (const char * )& subtype , 1 )) {
1035- return 0 ;
1036- }
1015+ Py_DECREF (uuid_type );
10371016
1038- if (uuid_subtype == CSHARP_LEGACY ) {
1039- /* Legacy C# byte order */
1040- bytes = PyObject_GetAttrString (value , "bytes_le" );
1041- }
1042- else {
1043- bytes = PyObject_GetAttrString (value , "bytes" );
1044- }
1045- if (!bytes ) {
1046- return 0 ;
1047- }
1017+ if (uuid_subtype == JAVA_LEGACY || uuid_subtype == CSHARP_LEGACY ) {
1018+ subtype = 3 ;
1019+ }
1020+ else {
1021+ subtype = uuid_subtype ;
1022+ }
1023+
1024+ * (buffer_get_buffer (buffer ) + type_byte ) = 0x05 ;
1025+ if (!buffer_write_bytes (buffer , (const char * )& size , 4 )) {
1026+ return 0 ;
1027+ }
1028+ if (!buffer_write_bytes (buffer , (const char * )& subtype , 1 )) {
1029+ return 0 ;
1030+ }
1031+
1032+ if (uuid_subtype == CSHARP_LEGACY ) {
1033+ /* Legacy C# byte order */
1034+ bytes = PyObject_GetAttrString (value , "bytes_le" );
1035+ }
1036+ else {
1037+ bytes = PyObject_GetAttrString (value , "bytes" );
1038+ }
1039+ if (!bytes ) {
1040+ return 0 ;
1041+ }
10481042#if PY_MAJOR_VERSION >= 3
1049- /* Work around http://bugs.python.org/issue7380 */
1050- if (PyByteArray_Check (bytes )) {
1051- data = PyByteArray_AsString (bytes );
1052- }
1053- else {
1054- data = PyBytes_AsString (bytes );
1055- }
1043+ /* Work around http://bugs.python.org/issue7380 */
1044+ if (PyByteArray_Check (bytes )) {
1045+ data = PyByteArray_AsString (bytes );
1046+ }
1047+ else {
1048+ data = PyBytes_AsString (bytes );
1049+ }
10561050#else
1057- data = PyString_AsString (bytes );
1051+ data = PyString_AsString (bytes );
10581052#endif
1059- if (data == NULL ) {
1053+ if (data == NULL ) {
1054+ Py_DECREF (bytes );
1055+ return 0 ;
1056+ }
1057+ if (uuid_subtype == JAVA_LEGACY ) {
1058+ /* Store in legacy java byte order. */
1059+ char as_legacy_java [16 ];
1060+ _fix_java (data , as_legacy_java );
1061+ if (!buffer_write_bytes (buffer , as_legacy_java , size )) {
10601062 Py_DECREF (bytes );
10611063 return 0 ;
10621064 }
1063- if (uuid_subtype == JAVA_LEGACY ) {
1064- /* Store in legacy java byte order. */
1065- char as_legacy_java [16 ];
1066- _fix_java (data , as_legacy_java );
1067- if (!buffer_write_bytes (buffer , as_legacy_java , size )) {
1068- Py_DECREF (bytes );
1069- return 0 ;
1070- }
1071- }
1072- else {
1073- if (!buffer_write_bytes (buffer , data , size )) {
1074- Py_DECREF (bytes );
1075- return 0 ;
1076- }
1065+ }
1066+ else {
1067+ if (!buffer_write_bytes (buffer , data , size )) {
1068+ Py_DECREF (bytes );
1069+ return 0 ;
10771070 }
1078- Py_DECREF (bytes );
1079- return 1 ;
1080- } else {
1081- Py_XDECREF (uuid_type );
10821071 }
1072+ Py_DECREF (bytes );
1073+ return 1 ;
10831074 }
1075+ Py_XDECREF (uuid_type );
10841076 /* We can't determine value's type. Fail. */
10851077 _set_cannot_encode (value );
10861078 return 0 ;
@@ -1621,7 +1613,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, unsigned* positio
16211613 goto invalid ;
16221614 }
16231615 /* Encode as UUID, not Binary */
1624- if (( subtype == 3 || subtype == 4 ) && state -> UUID ) {
1616+ if (subtype == 3 || subtype == 4 ) {
16251617 PyObject * kwargs ;
16261618 PyObject * args = PyTuple_New (0 );
16271619 /* UUID should always be 16 bytes */
0 commit comments