summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorNeal Norwitz <nnorwitz@gmail.com>2006-07-21 05:32:28 (GMT)
committerNeal Norwitz <nnorwitz@gmail.com>2006-07-21 05:32:28 (GMT)
commite1fdb32ff2bb1f4b46b56fc1de03f8016bc6c780 (patch)
treecd676947643217e1aaaa4723beecc6313d9d15b6 /Python
parent1adbb507013bea05cda92ea6aa9a8fef72657923 (diff)
downloadcpython-e1fdb32ff2bb1f4b46b56fc1de03f8016bc6c780.zip
cpython-e1fdb32ff2bb1f4b46b56fc1de03f8016bc6c780.tar.gz
cpython-e1fdb32ff2bb1f4b46b56fc1de03f8016bc6c780.tar.bz2
Handle allocation failures gracefully. Found with failmalloc.
Many (all?) of these could be backported.
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c9
-rw-r--r--Python/pystate.c4
-rw-r--r--Python/sysmodule.c73
3 files changed, 45 insertions, 41 deletions
diff --git a/Python/import.c b/Python/import.c
index 2c0468b..2073c43 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -116,6 +116,8 @@ _PyImport_Init(void)
for (scan = _PyImport_StandardFiletab; scan->suffix != NULL; ++scan)
++countS;
filetab = PyMem_NEW(struct filedescr, countD + countS + 1);
+ if (filetab == NULL)
+ Py_FatalError("Can't intiialize import file table.");
memcpy(filetab, _PyImport_DynLoadFiletab,
countD * sizeof(struct filedescr));
memcpy(filetab + countD, _PyImport_StandardFiletab,
@@ -239,8 +241,11 @@ lock_import(void)
long me = PyThread_get_thread_ident();
if (me == -1)
return; /* Too bad */
- if (import_lock == NULL)
+ if (import_lock == NULL) {
import_lock = PyThread_allocate_lock();
+ if (import_lock == NULL)
+ return; /* Nothing much we can do. */
+ }
if (import_lock_thread == me) {
import_lock_level++;
return;
@@ -259,7 +264,7 @@ static int
unlock_import(void)
{
long me = PyThread_get_thread_ident();
- if (me == -1)
+ if (me == -1 || import_lock == NULL)
return 0; /* Too bad */
if (import_lock_thread != me)
return -1;
diff --git a/Python/pystate.c b/Python/pystate.c
index eca26c7..3fae85b 100644
--- a/Python/pystate.c
+++ b/Python/pystate.c
@@ -63,6 +63,10 @@ PyInterpreterState_New(void)
if (interp != NULL) {
HEAD_INIT();
+#ifdef WITH_THREAD
+ if (head_mutex == NULL)
+ Py_FatalError("Can't initialize threads for interpreter");
+#endif
interp->modules = NULL;
interp->sysdict = NULL;
interp->builtins = NULL;
diff --git a/Python/sysmodule.c b/Python/sysmodule.c
index ea1388b..2dbe283 100644
--- a/Python/sysmodule.c
+++ b/Python/sysmodule.c
@@ -1137,41 +1137,38 @@ _PySys_Init(void)
#elif PY_RELEASE_LEVEL == PY_RELEASE_LEVEL_FINAL
s = "final";
#endif
- PyDict_SetItemString(sysdict, "version_info",
- v = Py_BuildValue("iiisi", PY_MAJOR_VERSION,
+
+#define SET_SYS_FROM_STRING(key, value) \
+ v = value; \
+ if (v != NULL) \
+ PyDict_SetItemString(sysdict, key, v); \
+ Py_XDECREF(v)
+
+ SET_SYS_FROM_STRING("version_info",
+ Py_BuildValue("iiisi", PY_MAJOR_VERSION,
PY_MINOR_VERSION,
PY_MICRO_VERSION, s,
PY_RELEASE_SERIAL));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "api_version",
- v = PyInt_FromLong(PYTHON_API_VERSION));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "copyright",
- v = PyString_FromString(Py_GetCopyright()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "platform",
- v = PyString_FromString(Py_GetPlatform()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "executable",
- v = PyString_FromString(Py_GetProgramFullPath()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "prefix",
- v = PyString_FromString(Py_GetPrefix()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "exec_prefix",
- v = PyString_FromString(Py_GetExecPrefix()));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "maxint",
- v = PyInt_FromLong(PyInt_GetMax()));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("api_version",
+ PyInt_FromLong(PYTHON_API_VERSION));
+ SET_SYS_FROM_STRING("copyright",
+ PyString_FromString(Py_GetCopyright()));
+ SET_SYS_FROM_STRING("platform",
+ PyString_FromString(Py_GetPlatform()));
+ SET_SYS_FROM_STRING("executable",
+ PyString_FromString(Py_GetProgramFullPath()));
+ SET_SYS_FROM_STRING("prefix",
+ PyString_FromString(Py_GetPrefix()));
+ SET_SYS_FROM_STRING("exec_prefix",
+ PyString_FromString(Py_GetExecPrefix()));
+ SET_SYS_FROM_STRING("maxint",
+ PyInt_FromLong(PyInt_GetMax()));
#ifdef Py_USING_UNICODE
- PyDict_SetItemString(sysdict, "maxunicode",
- v = PyInt_FromLong(PyUnicode_GetMax()));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("maxunicode",
+ PyInt_FromLong(PyUnicode_GetMax()));
#endif
- PyDict_SetItemString(sysdict, "builtin_module_names",
- v = list_builtin_module_names());
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("builtin_module_names",
+ list_builtin_module_names());
{
/* Assumes that longs are at least 2 bytes long.
Should be safe! */
@@ -1183,18 +1180,16 @@ _PySys_Init(void)
value = "big";
else
value = "little";
- PyDict_SetItemString(sysdict, "byteorder",
- v = PyString_FromString(value));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("byteorder",
+ PyString_FromString(value));
}
#ifdef MS_COREDLL
- PyDict_SetItemString(sysdict, "dllhandle",
- v = PyLong_FromVoidPtr(PyWin_DLLhModule));
- Py_XDECREF(v);
- PyDict_SetItemString(sysdict, "winver",
- v = PyString_FromString(PyWin_DLLVersionString));
- Py_XDECREF(v);
+ SET_SYS_FROM_STRING("dllhandle",
+ PyLong_FromVoidPtr(PyWin_DLLhModule));
+ SET_SYS_FROM_STRING("winver",
+ PyString_FromString(PyWin_DLLVersionString));
#endif
+#undef SET_SYS_FROM_STRING
if (warnoptions == NULL) {
warnoptions = PyList_New(0);
}