@@ -555,6 +555,9 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
555
555
PyObject * client ;
556
556
PyObject * last_error_args ;
557
557
PyObject * result ;
558
+ PyObject * max_bson_size_obj ;
559
+ PyObject * max_message_size_obj ;
560
+ PyObject * send_message_result ;
558
561
unsigned char check_keys ;
559
562
unsigned char safe ;
560
563
unsigned char continue_on_error ;
@@ -578,24 +581,25 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
578
581
options += 1 ;
579
582
}
580
583
584
+ max_bson_size_obj = PyObject_GetAttrString (client , "max_bson_size" );
581
585
#if PY_MAJOR_VERSION >= 3
582
- max_bson_size = PyLong_AsLong (
583
- PyObject_GetAttrString (client , "max_bson_size" ));
586
+ max_bson_size = PyLong_AsLong (max_bson_size_obj );
584
587
#else
585
- max_bson_size = PyInt_AsLong (
586
- PyObject_GetAttrString (client , "max_bson_size" ));
588
+ max_bson_size = PyInt_AsLong (max_bson_size_obj );
587
589
#endif
590
+ Py_XDECREF (max_bson_size_obj );
588
591
if (max_bson_size == -1 ) {
589
592
PyMem_Free (collection_name );
590
593
return NULL ;
591
594
}
595
+
596
+ max_message_size_obj = PyObject_GetAttrString (client , "max_message_size" );
592
597
#if PY_MAJOR_VERSION >= 3
593
- max_message_size = PyLong_AsLong (
594
- PyObject_GetAttrString (client , "max_message_size" ));
598
+ max_message_size = PyLong_AsLong (max_message_size_obj );
595
599
#else
596
- max_message_size = PyInt_AsLong (
597
- PyObject_GetAttrString (client , "max_message_size" ));
600
+ max_message_size = PyInt_AsLong (max_message_size_obj );
598
601
#endif
602
+ Py_XDECREF (max_message_size_obj );
599
603
if (max_message_size == -1 ) {
600
604
PyMem_Free (collection_name );
601
605
return NULL ;
@@ -707,8 +711,10 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
707
711
request_id = new_request_id ;
708
712
length_location = message_start ;
709
713
710
- if (!PyObject_CallMethod (client ,
711
- "_send_message" , "NO" , result , send_gle )) {
714
+ send_message_result = PyObject_CallMethod (client , "_send_message" ,
715
+ "NO" , result , send_gle );
716
+
717
+ if (!send_message_result ) {
712
718
PyObject * etype = NULL , * evalue = NULL , * etrace = NULL ;
713
719
PyObject * OperationFailure ;
714
720
PyErr_Fetch (& etype , & evalue , & etrace );
@@ -746,6 +752,8 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
746
752
* acknowledged writes. Re-raise immediately. */
747
753
PyErr_Restore (etype , evalue , etrace );
748
754
goto iterfail ;
755
+ } else {
756
+ Py_DECREF (send_message_result );
749
757
}
750
758
}
751
759
}
@@ -783,12 +791,17 @@ static PyObject* _cbson_do_batched_insert(PyObject* self, PyObject* args) {
783
791
buffer_free (buffer );
784
792
785
793
/* Send the last (or only) batch */
786
- if (!PyObject_CallMethod (client , "_send_message" , "NN" ,
787
- result , PyBool_FromLong ((long )safe ))) {
794
+ send_message_result = PyObject_CallMethod (client , "_send_message" , "NN" ,
795
+ result ,
796
+ PyBool_FromLong ((long )safe ));
797
+
798
+ if (!send_message_result ) {
788
799
Py_XDECREF (exc_type );
789
800
Py_XDECREF (exc_value );
790
801
Py_XDECREF (exc_trace );
791
802
return NULL ;
803
+ } else {
804
+ Py_DECREF (send_message_result );
792
805
}
793
806
794
807
if (exc_type ) {
0 commit comments