comparison Python/sysmodule.c @ 84795:31796b188bec

Issue #18520: Fix _PySys_Init(), handle PyDict_SetItemString() errors
author Victor Stinner <victor.stinner@gmail.com>
date Mon, 22 Jul 2013 22:40:00 +0200
parents fc718c177ee6
children dd0d751cc7f1
comparison
equal deleted inserted replaced
84794:9b77b3ee6fb8 84795:31796b188bec
1563 }; 1563 };
1564 1564
1565 PyObject * 1565 PyObject *
1566 _PySys_Init(void) 1566 _PySys_Init(void)
1567 { 1567 {
1568 PyObject *m, *v, *sysdict, *version_info; 1568 PyObject *m, *sysdict, *version_info;
1569 1569
1570 m = PyModule_Create(&sysmodule); 1570 m = PyModule_Create(&sysmodule);
1571 if (m == NULL) 1571 if (m == NULL)
1572 return NULL; 1572 return NULL;
1573 sysdict = PyModule_GetDict(m); 1573 sysdict = PyModule_GetDict(m);
1574 #define SET_SYS_FROM_STRING(key, value) \ 1574 #define SET_SYS_FROM_STRING(key, value) \
1575 v = value; \ 1575 do { \
1576 if (v != NULL) \ 1576 int res; \
1577 PyDict_SetItemString(sysdict, key, v); \ 1577 PyObject *v = (value); \
1578 Py_XDECREF(v) 1578 if (v == NULL) \
1579 return NULL; \
1580 res = PyDict_SetItemString(sysdict, key, v); \
1581 if (res < 0) { \
1582 Py_DECREF(v); \
1583 return NULL; \
1584 } \
1585 } while (0)
1579 1586
1580 /* Check that stdin is not a directory 1587 /* Check that stdin is not a directory
1581 Using shell redirection, you can redirect stdin to a directory, 1588 Using shell redirection, you can redirect stdin to a directory,
1582 crashing the Python interpreter. Catch this common mistake here 1589 crashing the Python interpreter. Catch this common mistake here
1583 and output a useful error message. Note that under MS Windows, 1590 and output a useful error message. Note that under MS Windows,
1595 } 1602 }
1596 #endif 1603 #endif
1597 1604
1598 /* stdin/stdout/stderr are now set by pythonrun.c */ 1605 /* stdin/stdout/stderr are now set by pythonrun.c */
1599 1606
1600 PyDict_SetItemString(sysdict, "__displayhook__", 1607 SET_SYS_FROM_STRING("__displayhook__",
1601 PyDict_GetItemString(sysdict, "displayhook")); 1608 PyDict_GetItemString(sysdict, "displayhook"));
1602 PyDict_SetItemString(sysdict, "__excepthook__", 1609 SET_SYS_FROM_STRING("__excepthook__",
1603 PyDict_GetItemString(sysdict, "excepthook")); 1610 PyDict_GetItemString(sysdict, "excepthook"));
1604 SET_SYS_FROM_STRING("version", 1611 SET_SYS_FROM_STRING("version",
1605 PyUnicode_FromString(Py_GetVersion())); 1612 PyUnicode_FromString(Py_GetVersion()));
1606 SET_SYS_FROM_STRING("hexversion", 1613 SET_SYS_FROM_STRING("hexversion",
1607 PyLong_FromLong(PY_VERSION_HEX)); 1614 PyLong_FromLong(PY_VERSION_HEX));
1608 SET_SYS_FROM_STRING("_mercurial", 1615 SET_SYS_FROM_STRING("_mercurial",
1662 SET_SYS_FROM_STRING("abiflags", 1669 SET_SYS_FROM_STRING("abiflags",
1663 PyUnicode_FromString(ABIFLAGS)); 1670 PyUnicode_FromString(ABIFLAGS));
1664 #endif 1671 #endif
1665 if (warnoptions == NULL) { 1672 if (warnoptions == NULL) {
1666 warnoptions = PyList_New(0); 1673 warnoptions = PyList_New(0);
1674 if (warnoptions == NULL)
1675 return NULL;
1667 } 1676 }
1668 else { 1677 else {
1669 Py_INCREF(warnoptions); 1678 Py_INCREF(warnoptions);
1670 } 1679 }
1671 if (warnoptions != NULL) { 1680 SET_SYS_FROM_STRING("warnoptions", warnoptions);
1672 PyDict_SetItemString(sysdict, "warnoptions", warnoptions); 1681
1673 } 1682 SET_SYS_FROM_STRING("_xoptions", get_xoptions());
1674
1675 v = get_xoptions();
1676 if (v != NULL) {
1677 PyDict_SetItemString(sysdict, "_xoptions", v);
1678 }
1679 1683
1680 /* version_info */ 1684 /* version_info */
1681 if (VersionInfoType.tp_name == NULL) { 1685 if (VersionInfoType.tp_name == NULL) {
1682 if (PyStructSequence_InitType2(&VersionInfoType, 1686 if (PyStructSequence_InitType2(&VersionInfoType,
1683 &version_info_desc) < 0) 1687 &version_info_desc) < 0)