@@ -434,7 +434,7 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
434
434
buffer_t buffer ;
435
435
int length_location , message_length ;
436
436
unsigned char check_keys = 0 ;
437
- PyObject * result ;
437
+ PyObject * result = NULL ;
438
438
439
439
if (!PyArg_ParseTuple (args , "Iet#iiOOO&|b" ,
440
440
& flags ,
@@ -477,18 +477,14 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
477
477
/* PyDict_GetItemString returns a borrowed reference. */
478
478
Py_INCREF (cluster_time );
479
479
if (-1 == PyMapping_DelItemString (query , "$clusterTime" )) {
480
- destroy_codec_options (& options );
481
- PyMem_Free (collection_name );
482
- return NULL ;
480
+ goto fail ;
483
481
}
484
482
}
485
483
} else if (PyMapping_HasKeyString (query , "$clusterTime" )) {
486
484
cluster_time = PyMapping_GetItemString (query , "$clusterTime" );
487
485
if (!cluster_time
488
486
|| -1 == PyMapping_DelItemString (query , "$clusterTime" )) {
489
- destroy_codec_options (& options );
490
- PyMem_Free (collection_name );
491
- return NULL ;
487
+ goto fail ;
492
488
}
493
489
}
494
490
if (!buffer_write_int32 (buffer , (int32_t )request_id ) ||
@@ -498,20 +494,12 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
498
494
collection_name_length + 1 ) ||
499
495
!buffer_write_int32 (buffer , (int32_t )num_to_skip ) ||
500
496
!buffer_write_int32 (buffer , (int32_t )num_to_return )) {
501
- destroy_codec_options (& options );
502
- buffer_free (buffer );
503
- PyMem_Free (collection_name );
504
- Py_XDECREF (cluster_time );
505
- return NULL ;
497
+ goto fail ;
506
498
}
507
499
508
500
begin = buffer_get_position (buffer );
509
501
if (!write_dict (state -> _cbson , buffer , query , check_keys , & options , 1 )) {
510
- destroy_codec_options (& options );
511
- buffer_free (buffer );
512
- PyMem_Free (collection_name );
513
- Py_XDECREF (cluster_time );
514
- return NULL ;
502
+ goto fail ;
515
503
}
516
504
517
505
/* back up a byte and write $clusterTime */
@@ -522,19 +510,11 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
522
510
buffer_update_position (buffer , buffer_get_position (buffer ) - 1 );
523
511
if (!write_pair (state -> _cbson , buffer , "$clusterTime" , 12 , cluster_time ,
524
512
0 , & options , 1 )) {
525
- destroy_codec_options (& options );
526
- buffer_free (buffer );
527
- PyMem_Free (collection_name );
528
- Py_DECREF (cluster_time );
529
- return NULL ;
513
+ goto fail ;
530
514
}
531
515
532
516
if (!buffer_write_bytes (buffer , & zero , 1 )) {
533
- destroy_codec_options (& options );
534
- buffer_free (buffer );
535
- PyMem_Free (collection_name );
536
- Py_DECREF (cluster_time );
537
- return NULL ;
517
+ goto fail ;
538
518
}
539
519
540
520
length = buffer_get_position (buffer ) - begin ;
@@ -543,14 +523,10 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
543
523
/* undo popping $clusterTime */
544
524
if (-1 == PyMapping_SetItemString (
545
525
query , "$clusterTime" , cluster_time )) {
546
- destroy_codec_options (& options );
547
- buffer_free (buffer );
548
- PyMem_Free (collection_name );
549
- Py_DECREF (cluster_time );
550
- return NULL ;
526
+ goto fail ;
551
527
}
552
528
553
- Py_DECREF (cluster_time );
529
+ Py_CLEAR (cluster_time );
554
530
}
555
531
556
532
max_size = buffer_get_position (buffer ) - begin ;
@@ -559,17 +535,12 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
559
535
begin = buffer_get_position (buffer );
560
536
if (!write_dict (state -> _cbson , buffer , field_selector , 0 ,
561
537
& options , 1 )) {
562
- destroy_codec_options (& options );
563
- buffer_free (buffer );
564
- PyMem_Free (collection_name );
565
- return NULL ;
538
+ goto fail ;
566
539
}
567
540
cur_size = buffer_get_position (buffer ) - begin ;
568
541
max_size = (cur_size > max_size ) ? cur_size : max_size ;
569
542
}
570
543
571
- PyMem_Free (collection_name );
572
-
573
544
message_length = buffer_get_position (buffer ) - length_location ;
574
545
buffer_write_int32_at_position (
575
546
buffer , length_location , (int32_t )message_length );
@@ -579,8 +550,12 @@ static PyObject* _cbson_query_message(PyObject* self, PyObject* args) {
579
550
buffer_get_buffer (buffer ),
580
551
buffer_get_position (buffer ),
581
552
max_size );
553
+
554
+ fail :
555
+ PyMem_Free (collection_name );
582
556
destroy_codec_options (& options );
583
557
buffer_free (buffer );
558
+ Py_XDECREF (cluster_time );
584
559
return result ;
585
560
}
586
561
@@ -1142,11 +1117,11 @@ _batched_op_msg(
1142
1117
int size_location ;
1143
1118
int position ;
1144
1119
int length ;
1145
- PyObject * max_bson_size_obj ;
1146
- PyObject * max_write_batch_size_obj ;
1147
- PyObject * max_message_size_obj ;
1148
- PyObject * doc ;
1149
- PyObject * iterator ;
1120
+ PyObject * max_bson_size_obj = NULL ;
1121
+ PyObject * max_write_batch_size_obj = NULL ;
1122
+ PyObject * max_message_size_obj = NULL ;
1123
+ PyObject * doc = NULL ;
1124
+ PyObject * iterator = NULL ;
1150
1125
char * flags = ack ? "\x00\x00\x00\x00" : "\x02\x00\x00\x00" ;
1151
1126
1152
1127
max_bson_size_obj = PyObject_GetAttrString (ctx , "max_bson_size" );
@@ -1209,23 +1184,23 @@ _batched_op_msg(
1209
1184
case _INSERT :
1210
1185
{
1211
1186
if (!buffer_write_bytes (buffer , "documents\x00" , 10 ))
1212
- goto cmdfail ;
1187
+ goto fail ;
1213
1188
break ;
1214
1189
}
1215
1190
case _UPDATE :
1216
1191
{
1217
1192
/* MongoDB does key validation for update. */
1218
1193
check_keys = 0 ;
1219
1194
if (!buffer_write_bytes (buffer , "updates\x00" , 8 ))
1220
- goto cmdfail ;
1195
+ goto fail ;
1221
1196
break ;
1222
1197
}
1223
1198
case _DELETE :
1224
1199
{
1225
1200
/* Never check keys in a delete command. */
1226
1201
check_keys = 0 ;
1227
1202
if (!buffer_write_bytes (buffer , "deletes\x00" , 8 ))
1228
- goto cmdfail ;
1203
+ goto fail ;
1229
1204
break ;
1230
1205
}
1231
1206
default :
@@ -1255,7 +1230,7 @@ _batched_op_msg(
1255
1230
int unacked_doc_too_large = 0 ;
1256
1231
if (!write_dict (state -> _cbson , buffer , doc , check_keys ,
1257
1232
& options , 1 )) {
1258
- goto cmditerfail ;
1233
+ goto fail ;
1259
1234
}
1260
1235
cur_size = buffer_get_position (buffer ) - cur_doc_begin ;
1261
1236
@@ -1285,7 +1260,7 @@ _batched_op_msg(
1285
1260
Py_DECREF (DocumentTooLarge );
1286
1261
}
1287
1262
}
1288
- goto cmditerfail ;
1263
+ goto fail ;
1289
1264
}
1290
1265
/* We have enough data, return this batch. */
1291
1266
if (buffer_get_position (buffer ) > max_message_size ) {
@@ -1294,10 +1269,11 @@ _batched_op_msg(
1294
1269
* of the last document encoded.
1295
1270
*/
1296
1271
buffer_update_position (buffer , cur_doc_begin );
1272
+ Py_CLEAR (doc );
1297
1273
break ;
1298
1274
}
1299
1275
if (PyList_Append (to_publish , doc ) < 0 ) {
1300
- goto cmditerfail ;
1276
+ goto fail ;
1301
1277
}
1302
1278
Py_CLEAR (doc );
1303
1279
idx += 1 ;
@@ -1306,21 +1282,20 @@ _batched_op_msg(
1306
1282
break ;
1307
1283
}
1308
1284
}
1309
- Py_DECREF (iterator );
1285
+ Py_CLEAR (iterator );
1310
1286
1311
1287
if (PyErr_Occurred ()) {
1312
- goto cmdfail ;
1288
+ goto fail ;
1313
1289
}
1314
1290
1315
1291
position = buffer_get_position (buffer );
1316
1292
length = position - size_location ;
1317
1293
buffer_write_int32_at_position (buffer , size_location , (int32_t )length );
1318
1294
return 1 ;
1319
1295
1320
- cmditerfail :
1296
+ fail :
1321
1297
Py_XDECREF (doc );
1322
- Py_DECREF (iterator );
1323
- cmdfail :
1298
+ Py_XDECREF (iterator );
1324
1299
return 0 ;
1325
1300
}
1326
1301
@@ -1466,10 +1441,10 @@ _batched_write_command(
1466
1441
int lst_len_loc ;
1467
1442
int position ;
1468
1443
int length ;
1469
- PyObject * max_bson_size_obj ;
1470
- PyObject * max_write_batch_size_obj ;
1471
- PyObject * doc ;
1472
- PyObject * iterator ;
1444
+ PyObject * max_bson_size_obj = NULL ;
1445
+ PyObject * max_write_batch_size_obj = NULL ;
1446
+ PyObject * doc = NULL ;
1447
+ PyObject * iterator = NULL ;
1473
1448
1474
1449
max_bson_size_obj = PyObject_GetAttrString (ctx , "max_bson_size" );
1475
1450
#if PY_MAJOR_VERSION >= 3
@@ -1524,23 +1499,23 @@ _batched_write_command(
1524
1499
case _INSERT :
1525
1500
{
1526
1501
if (!buffer_write_bytes (buffer , "documents\x00" , 10 ))
1527
- goto cmdfail ;
1502
+ goto fail ;
1528
1503
break ;
1529
1504
}
1530
1505
case _UPDATE :
1531
1506
{
1532
1507
/* MongoDB does key validation for update. */
1533
1508
check_keys = 0 ;
1534
1509
if (!buffer_write_bytes (buffer , "updates\x00" , 8 ))
1535
- goto cmdfail ;
1510
+ goto fail ;
1536
1511
break ;
1537
1512
}
1538
1513
case _DELETE :
1539
1514
{
1540
1515
/* Never check keys in a delete command. */
1541
1516
check_keys = 0 ;
1542
1517
if (!buffer_write_bytes (buffer , "deletes\x00" , 8 ))
1543
- goto cmdfail ;
1518
+ goto fail ;
1544
1519
break ;
1545
1520
}
1546
1521
default :
@@ -1575,25 +1550,23 @@ _batched_write_command(
1575
1550
int cur_doc_begin ;
1576
1551
int cur_size ;
1577
1552
int enough_data = 0 ;
1578
- int enough_documents = 0 ;
1579
1553
char key [16 ];
1580
1554
INT2STRING (key , idx );
1581
1555
if (!buffer_write_bytes (buffer , "\x03" , 1 ) ||
1582
1556
!buffer_write_bytes (buffer , key , (int )strlen (key ) + 1 )) {
1583
- goto cmditerfail ;
1557
+ goto fail ;
1584
1558
}
1585
1559
cur_doc_begin = buffer_get_position (buffer );
1586
1560
if (!write_dict (state -> _cbson , buffer , doc ,
1587
1561
check_keys , & options , 1 )) {
1588
- goto cmditerfail ;
1562
+ goto fail ;
1589
1563
}
1590
1564
1591
1565
/* We have enough data, return this batch.
1592
1566
* max_cmd_size accounts for the two trailing null bytes.
1593
1567
*/
1594
1568
enough_data = (buffer_get_position (buffer ) > max_cmd_size );
1595
- enough_documents = (idx >= max_write_batch_size );
1596
- if (enough_data || enough_documents ) {
1569
+ if (enough_data ) {
1597
1570
cur_size = buffer_get_position (buffer ) - cur_doc_begin ;
1598
1571
1599
1572
/* This single document is too large for the command. */
@@ -1614,30 +1587,35 @@ _batched_write_command(
1614
1587
Py_DECREF (DocumentTooLarge );
1615
1588
}
1616
1589
}
1617
- goto cmditerfail ;
1590
+ goto fail ;
1618
1591
}
1619
1592
/*
1620
1593
* Roll the existing buffer back to the beginning
1621
1594
* of the last document encoded.
1622
1595
*/
1623
1596
buffer_update_position (buffer , sub_doc_begin );
1597
+ Py_CLEAR (doc );
1624
1598
break ;
1625
1599
}
1626
1600
if (PyList_Append (to_publish , doc ) < 0 ) {
1627
- goto cmditerfail ;
1601
+ goto fail ;
1628
1602
}
1629
1603
Py_CLEAR (doc );
1630
1604
idx += 1 ;
1605
+ /* We have enough documents, return this batch. */
1606
+ if (idx == max_write_batch_size ) {
1607
+ break ;
1608
+ }
1631
1609
}
1632
- Py_DECREF (iterator );
1610
+ Py_CLEAR (iterator );
1633
1611
1634
1612
if (PyErr_Occurred ()) {
1635
- goto cmdfail ;
1613
+ goto fail ;
1636
1614
}
1637
1615
1638
- if (!buffer_write_bytes (buffer , "\x00\x00" , 2 ))
1639
- goto cmdfail ;
1640
-
1616
+ if (!buffer_write_bytes (buffer , "\x00\x00" , 2 )) {
1617
+ goto fail ;
1618
+ }
1641
1619
1642
1620
position = buffer_get_position (buffer );
1643
1621
length = position - lst_len_loc - 1 ;
@@ -1646,10 +1624,9 @@ _batched_write_command(
1646
1624
buffer_write_int32_at_position (buffer , cmd_len_loc , (int32_t )length );
1647
1625
return 1 ;
1648
1626
1649
- cmditerfail :
1627
+ fail :
1650
1628
Py_XDECREF (doc );
1651
- Py_DECREF (iterator );
1652
- cmdfail :
1629
+ Py_XDECREF (iterator );
1653
1630
return 0 ;
1654
1631
}
1655
1632
0 commit comments