@@ -1573,15 +1573,26 @@ _PySys_Init(void)
1573
1573
if (m == NULL )
1574
1574
return NULL ;
1575
1575
sysdict = PyModule_GetDict (m );
1576
+ #define SET_SYS_FROM_STRING_BORROW (key , value ) \
1577
+ do { \
1578
+ int res; \
1579
+ PyObject *v = (value); \
1580
+ if (v == NULL) \
1581
+ return NULL; \
1582
+ res = PyDict_SetItemString(sysdict, key, v); \
1583
+ if (res < 0) { \
1584
+ return NULL; \
1585
+ } \
1586
+ } while (0)
1576
1587
#define SET_SYS_FROM_STRING (key , value ) \
1577
1588
do { \
1578
1589
int res; \
1579
1590
PyObject *v = (value); \
1580
1591
if (v == NULL) \
1581
1592
return NULL; \
1582
1593
res = PyDict_SetItemString(sysdict, key, v); \
1594
+ Py_DECREF(v); \
1583
1595
if (res < 0) { \
1584
- Py_DECREF(v); \
1585
1596
return NULL; \
1586
1597
} \
1587
1598
} while (0)
@@ -1606,10 +1617,10 @@ _PySys_Init(void)
1606
1617
1607
1618
/* stdin/stdout/stderr are now set by pythonrun.c */
1608
1619
1609
- SET_SYS_FROM_STRING ("__displayhook__" ,
1610
- PyDict_GetItemString (sysdict , "displayhook" ));
1611
- SET_SYS_FROM_STRING ("__excepthook__" ,
1612
- PyDict_GetItemString (sysdict , "excepthook" ));
1620
+ SET_SYS_FROM_STRING_BORROW ("__displayhook__" ,
1621
+ PyDict_GetItemString (sysdict , "displayhook" ));
1622
+ SET_SYS_FROM_STRING_BORROW ("__excepthook__" ,
1623
+ PyDict_GetItemString (sysdict , "excepthook" ));
1613
1624
SET_SYS_FROM_STRING ("version" ,
1614
1625
PyUnicode_FromString (Py_GetVersion ()));
1615
1626
SET_SYS_FROM_STRING ("hexversion" ,
@@ -1679,9 +1690,9 @@ _PySys_Init(void)
1679
1690
else {
1680
1691
Py_INCREF (warnoptions );
1681
1692
}
1682
- SET_SYS_FROM_STRING ("warnoptions" , warnoptions );
1693
+ SET_SYS_FROM_STRING_BORROW ("warnoptions" , warnoptions );
1683
1694
1684
- SET_SYS_FROM_STRING ("_xoptions" , get_xoptions ());
1695
+ SET_SYS_FROM_STRING_BORROW ("_xoptions" , get_xoptions ());
1685
1696
1686
1697
/* version_info */
1687
1698
if (VersionInfoType .tp_name == NULL ) {
0 commit comments