comparison Python/sysmodule.c @ 86693:5eb00460e6e8

Issue #18520: fix reference leak in _PySys_Init()
author Victor Stinner <victor.stinner@gmail.com>
date Sun, 27 Oct 2013 17:15:42 +0100
parents 6cb3ae431bef
children 4ef4578db38a
comparison
equal deleted inserted replaced
86692:9b084337f13e 86693:5eb00460e6e8
1571 1571
1572 m = PyModule_Create(&sysmodule); 1572 m = PyModule_Create(&sysmodule);
1573 if (m == NULL) 1573 if (m == NULL)
1574 return NULL; 1574 return NULL;
1575 sysdict = PyModule_GetDict(m); 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 #define SET_SYS_FROM_STRING(key, value) \ 1587 #define SET_SYS_FROM_STRING(key, value) \
1577 do { \ 1588 do { \
1578 int res; \ 1589 int res; \
1579 PyObject *v = (value); \ 1590 PyObject *v = (value); \
1580 if (v == NULL) \ 1591 if (v == NULL) \
1581 return NULL; \ 1592 return NULL; \
1582 res = PyDict_SetItemString(sysdict, key, v); \ 1593 res = PyDict_SetItemString(sysdict, key, v); \
1594 Py_DECREF(v); \
1583 if (res < 0) { \ 1595 if (res < 0) { \
1584 Py_DECREF(v); \
1585 return NULL; \ 1596 return NULL; \
1586 } \ 1597 } \
1587 } while (0) 1598 } while (0)
1588 1599
1589 /* Check that stdin is not a directory 1600 /* Check that stdin is not a directory
1604 } 1615 }
1605 #endif 1616 #endif
1606 1617
1607 /* stdin/stdout/stderr are now set by pythonrun.c */ 1618 /* stdin/stdout/stderr are now set by pythonrun.c */
1608 1619
1609 SET_SYS_FROM_STRING("__displayhook__", 1620 SET_SYS_FROM_STRING_BORROW("__displayhook__",
1610 PyDict_GetItemString(sysdict, "displayhook")); 1621 PyDict_GetItemString(sysdict, "displayhook"));
1611 SET_SYS_FROM_STRING("__excepthook__", 1622 SET_SYS_FROM_STRING_BORROW("__excepthook__",
1612 PyDict_GetItemString(sysdict, "excepthook")); 1623 PyDict_GetItemString(sysdict, "excepthook"));
1613 SET_SYS_FROM_STRING("version", 1624 SET_SYS_FROM_STRING("version",
1614 PyUnicode_FromString(Py_GetVersion())); 1625 PyUnicode_FromString(Py_GetVersion()));
1615 SET_SYS_FROM_STRING("hexversion", 1626 SET_SYS_FROM_STRING("hexversion",
1616 PyLong_FromLong(PY_VERSION_HEX)); 1627 PyLong_FromLong(PY_VERSION_HEX));
1617 SET_SYS_FROM_STRING("_mercurial", 1628 SET_SYS_FROM_STRING("_mercurial",
1677 return NULL; 1688 return NULL;
1678 } 1689 }
1679 else { 1690 else {
1680 Py_INCREF(warnoptions); 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 /* version_info */ 1697 /* version_info */
1687 if (VersionInfoType.tp_name == NULL) { 1698 if (VersionInfoType.tp_name == NULL) {
1688 if (PyStructSequence_InitType2(&VersionInfoType, 1699 if (PyStructSequence_InitType2(&VersionInfoType,
1689 &version_info_desc) < 0) 1700 &version_info_desc) < 0)