@@ -1347,8 +1347,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1347
1347
unsigned char tz_aware , unsigned char uuid_subtype ) {
1348
1348
struct module_state * state = GETSTATE (self );
1349
1349
1350
- PyObject * value ;
1351
- PyObject * error ;
1350
+ PyObject * value = NULL ;
1352
1351
switch (type ) {
1353
1352
case 1 :
1354
1353
{
@@ -1358,9 +1357,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1358
1357
}
1359
1358
memcpy (& d , buffer + * position , 8 );
1360
1359
value = PyFloat_FromDouble (d );
1361
- if (!value ) {
1362
- return NULL ;
1363
- }
1364
1360
* position += 8 ;
1365
1361
break ;
1366
1362
}
@@ -1373,9 +1369,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1373
1369
}
1374
1370
* position += 4 ;
1375
1371
value = PyUnicode_DecodeUTF8 (buffer + * position , value_length , "strict" );
1376
- if (!value ) {
1377
- return NULL ;
1378
- }
1379
1372
* position += value_length + 1 ;
1380
1373
break ;
1381
1374
}
@@ -1389,10 +1382,10 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1389
1382
}
1390
1383
value = elements_to_dict (self , buffer + * position + 4 ,
1391
1384
size - 5 , as_class , tz_aware , uuid_subtype );
1385
+
1392
1386
if (!value ) {
1393
- return NULL ;
1387
+ goto invalid ;
1394
1388
}
1395
-
1396
1389
/* Decoding for DBRefs */
1397
1390
collection = PyDict_GetItemString (value , "$ref" );
1398
1391
if (collection ) { /* DBRef */
@@ -1428,9 +1421,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1428
1421
Py_DECREF (id );
1429
1422
Py_DECREF (collection );
1430
1423
Py_DECREF (database );
1431
- if (!value ) {
1432
- return NULL ;
1433
- }
1434
1424
}
1435
1425
1436
1426
* position += size ;
@@ -1450,7 +1440,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1450
1440
1451
1441
value = PyList_New (0 );
1452
1442
if (!value ) {
1453
- return NULL ;
1443
+ goto invalid ;
1454
1444
}
1455
1445
while (* position < end ) {
1456
1446
PyObject * to_append ;
@@ -1467,7 +1457,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1467
1457
max - (int )key_size , as_class , tz_aware , uuid_subtype );
1468
1458
if (!to_append ) {
1469
1459
Py_DECREF (value );
1470
- return NULL ;
1460
+ goto invalid ;
1471
1461
}
1472
1462
PyList_Append (value , to_append );
1473
1463
Py_DECREF (to_append );
@@ -1506,20 +1496,20 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1506
1496
}
1507
1497
#endif
1508
1498
if (!data ) {
1509
- return NULL ;
1499
+ goto invalid ;
1510
1500
}
1511
1501
if ((subtype == 3 || subtype == 4 ) && state -> UUID ) { // Encode as UUID, not Binary
1512
1502
PyObject * kwargs ;
1513
1503
PyObject * args = PyTuple_New (0 );
1514
1504
if (!args ) {
1515
1505
Py_DECREF (data );
1516
- return NULL ;
1506
+ goto invalid ;
1517
1507
}
1518
1508
kwargs = PyDict_New ();
1519
1509
if (!kwargs ) {
1520
1510
Py_DECREF (data );
1521
1511
Py_DECREF (args );
1522
- return NULL ;
1512
+ goto invalid ;
1523
1513
}
1524
1514
1525
1515
assert (length == 16 ); // UUID should always be 16 bytes
@@ -1553,10 +1543,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1553
1543
Py_DECREF (args );
1554
1544
Py_DECREF (kwargs );
1555
1545
Py_DECREF (data );
1556
- if (!value ) {
1557
- return NULL ;
1558
- }
1559
-
1560
1546
* position += length + 5 ;
1561
1547
break ;
1562
1548
@@ -1579,9 +1565,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1579
1565
value = PyObject_CallFunctionObjArgs (state -> Binary , data , st , NULL );
1580
1566
Py_DECREF (st );
1581
1567
Py_DECREF (data );
1582
- if (!value ) {
1583
- return NULL ;
1584
- }
1585
1568
* position += length + 5 ;
1586
1569
break ;
1587
1570
}
@@ -1602,9 +1585,6 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1602
1585
#else
1603
1586
value = PyObject_CallFunction (state -> ObjectId , "s#" , buffer + * position , 12 );
1604
1587
#endif
1605
- if (!value ) {
1606
- return NULL ;
1607
- }
1608
1588
* position += 12 ;
1609
1589
break ;
1610
1590
}
@@ -1631,29 +1611,29 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1631
1611
}
1632
1612
1633
1613
if (!naive ) {
1634
- return NULL ;
1614
+ goto invalid ;
1635
1615
}
1636
1616
replace = PyObject_GetAttrString (naive , "replace" );
1637
1617
Py_DECREF (naive );
1638
1618
if (!replace ) {
1639
- return NULL ;
1619
+ goto invalid ;
1640
1620
}
1641
1621
args = PyTuple_New (0 );
1642
1622
if (!args ) {
1643
1623
Py_DECREF (replace );
1644
- return NULL ;
1624
+ goto invalid ;
1645
1625
}
1646
1626
kwargs = PyDict_New ();
1647
1627
if (!kwargs ) {
1648
1628
Py_DECREF (replace );
1649
1629
Py_DECREF (args );
1650
- return NULL ;
1630
+ goto invalid ;
1651
1631
}
1652
1632
if (PyDict_SetItemString (kwargs , "tzinfo" , state -> UTC ) == -1 ) {
1653
1633
Py_DECREF (replace );
1654
1634
Py_DECREF (args );
1655
1635
Py_DECREF (kwargs );
1656
- return NULL ;
1636
+ goto invalid ;
1657
1637
}
1658
1638
value = PyObject_Call (replace , args , kwargs );
1659
1639
Py_DECREF (replace );
@@ -1672,7 +1652,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1672
1652
}
1673
1653
pattern = PyUnicode_DecodeUTF8 (buffer + * position , pattern_length , "strict" );
1674
1654
if (!pattern ) {
1675
- return NULL ;
1655
+ goto invalid ;
1676
1656
}
1677
1657
* position += (int )pattern_length + 1 ;
1678
1658
if ((flags_length = strlen (buffer + * position )) > BSON_MAX_SIZE ) {
@@ -1718,14 +1698,14 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1718
1698
collection = PyUnicode_DecodeUTF8 (buffer + * position ,
1719
1699
coll_length , "strict" );
1720
1700
if (!collection ) {
1721
- return NULL ;
1701
+ goto invalid ;
1722
1702
}
1723
1703
* position += (int )coll_length + 1 ;
1724
1704
1725
1705
id = PyObject_CallFunction (state -> ObjectId , "s#" , buffer + * position , 12 );
1726
1706
if (!id ) {
1727
1707
Py_DECREF (collection );
1728
- return NULL ;
1708
+ goto invalid ;
1729
1709
}
1730
1710
* position += 12 ;
1731
1711
value = PyObject_CallFunctionObjArgs (state -> DBRef , collection , id , NULL );
@@ -1743,7 +1723,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1743
1723
* position += 4 ;
1744
1724
code = PyUnicode_DecodeUTF8 (buffer + * position , value_length , "strict" );
1745
1725
if (!code ) {
1746
- return NULL ;
1726
+ goto invalid ;
1747
1727
}
1748
1728
* position += value_length + 1 ;
1749
1729
value = PyObject_CallFunctionObjArgs (state -> Code , code , NULL , NULL );
@@ -1764,7 +1744,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1764
1744
}
1765
1745
code = PyUnicode_DecodeUTF8 (buffer + * position , code_length , "strict" );
1766
1746
if (!code ) {
1767
- return NULL ;
1747
+ goto invalid ;
1768
1748
}
1769
1749
* position += (int )code_length + 1 ;
1770
1750
@@ -1773,7 +1753,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1773
1753
(PyObject * )& PyDict_Type , tz_aware , uuid_subtype );
1774
1754
if (!scope ) {
1775
1755
Py_DECREF (code );
1776
- return NULL ;
1756
+ goto invalid ;
1777
1757
}
1778
1758
* position += scope_size ;
1779
1759
@@ -1795,7 +1775,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1795
1775
value = PyInt_FromLong (i );
1796
1776
#endif
1797
1777
if (!value ) {
1798
- return NULL ;
1778
+ goto invalid ;
1799
1779
}
1800
1780
* position += 4 ;
1801
1781
break ;
@@ -1810,7 +1790,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1810
1790
memcpy (& time , buffer + * position + 4 , 4 );
1811
1791
value = PyObject_CallFunction (state -> Timestamp , "II" , time , inc );
1812
1792
if (!value ) {
1813
- return NULL ;
1793
+ goto invalid ;
1814
1794
}
1815
1795
* position += 8 ;
1816
1796
break ;
@@ -1824,7 +1804,7 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1824
1804
memcpy (& ll , buffer + * position , 8 );
1825
1805
value = PyLong_FromLongLong (ll );
1826
1806
if (!value ) {
1827
- return NULL ;
1807
+ goto invalid ;
1828
1808
}
1829
1809
* position += 8 ;
1830
1810
break ;
@@ -1850,14 +1830,45 @@ static PyObject* get_value(PyObject* self, const char* buffer, int* position,
1850
1830
return NULL ;
1851
1831
}
1852
1832
}
1853
- return value ;
1833
+
1834
+ if (value ) {
1835
+ return value ;
1836
+ }
1854
1837
1855
1838
invalid :
1856
1839
1857
- error = _error ("InvalidBSON" );
1858
- if (error ) {
1859
- PyErr_SetNone (error );
1860
- Py_DECREF (error );
1840
+ /* Wrap any non-InvalidBSON errors in InvalidBSON. */
1841
+ if (PyErr_Occurred ()) {
1842
+ /* Calling _error clears the error state, so fetch it first. */
1843
+ PyObject * etype , * evalue , * etrace , * InvalidBSON ;
1844
+ PyErr_Fetch (& etype , & evalue , & etrace );
1845
+ InvalidBSON = _error ("InvalidBSON" );
1846
+ if (InvalidBSON ) {
1847
+ if (!PyErr_GivenExceptionMatches (etype , InvalidBSON )) {
1848
+ /* Raise InvalidBSON(str(e)). */
1849
+ PyObject * msg = NULL ;
1850
+ Py_DECREF (etype );
1851
+ etype = InvalidBSON ;
1852
+
1853
+ if (evalue ) {
1854
+ msg = PyObject_Str (evalue );
1855
+ Py_DECREF (evalue );
1856
+ evalue = msg ;
1857
+ }
1858
+ PyErr_NormalizeException (& etype , & evalue , & etrace );
1859
+ Py_XDECREF (msg );
1860
+ }
1861
+ }
1862
+ /* Steals references to args. */
1863
+ PyErr_Restore (etype , evalue , etrace );
1864
+ Py_XDECREF (InvalidBSON );
1865
+ return NULL ;
1866
+ } else {
1867
+ PyObject * InvalidBSON = _error ("InvalidBSON" );
1868
+ if (InvalidBSON ) {
1869
+ PyErr_SetNone (InvalidBSON );
1870
+ Py_DECREF (InvalidBSON );
1871
+ }
1861
1872
}
1862
1873
return NULL ;
1863
1874
}
0 commit comments