[Python-checkins] cpython: calculate_path() now fails with a fatal error when it fails to allocate memory

victor.stinner python-checkins at python.org
Sat Nov 16 01:24:15 CET 2013


http://hg.python.org/cpython/rev/255253286aa9
changeset:   87138:255253286aa9
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Sat Nov 16 01:22:04 2013 +0100
summary:
  calculate_path() now fails with a fatal error when it fails to allocate memory
for module_search_path. It was already the case on _Py_char2wchar() failure.

files:
  Modules/getpath.c |  97 +++++++++++++++-------------------
  1 files changed, 44 insertions(+), 53 deletions(-)


diff --git a/Modules/getpath.c b/Modules/getpath.c
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -134,7 +134,6 @@
 static wchar_t exec_prefix[MAXPATHLEN+1];
 static wchar_t progpath[MAXPATHLEN+1];
 static wchar_t *module_search_path = NULL;
-static int module_search_path_malloced = 0;
 
 static void
 reduce(wchar_t *dir)
@@ -740,60 +739,55 @@
     bufsz += wcslen(zip_path) + 1;
     bufsz += wcslen(exec_prefix) + 1;
 
-    buf = (wchar_t *)PyMem_Malloc(bufsz*sizeof(wchar_t));
+    buf = (wchar_t *)PyMem_Malloc(bufsz * sizeof(wchar_t));
+    if (buf == NULL) {
+        Py_FatalError(
+            "Not enough memory for dynamic PYTHONPATH");
+    }
 
-    if (buf == NULL) {
-        /* We can't exit, so print a warning and limp along */
-        fprintf(stderr, "Not enough memory for dynamic PYTHONPATH.\n");
-        fprintf(stderr, "Using default static PYTHONPATH.\n");
-        module_search_path = L"" PYTHONPATH;
+    /* Run-time value of $PYTHONPATH goes first */
+    if (rtpypath) {
+        wcscpy(buf, rtpypath);
+        wcscat(buf, delimiter);
     }
-    else {
-        /* Run-time value of $PYTHONPATH goes first */
-        if (rtpypath) {
-            wcscpy(buf, rtpypath);
-            wcscat(buf, delimiter);
+    else
+        buf[0] = '\0';
+
+    /* Next is the default zip path */
+    wcscat(buf, zip_path);
+    wcscat(buf, delimiter);
+
+    /* Next goes merge of compile-time $PYTHONPATH with
+     * dynamically located prefix.
+     */
+    defpath = _pythonpath;
+    while (1) {
+        wchar_t *delim = wcschr(defpath, DELIM);
+
+        if (defpath[0] != SEP) {
+            wcscat(buf, prefix);
+            wcscat(buf, separator);
         }
-        else
-            buf[0] = '\0';
 
-        /* Next is the default zip path */
-        wcscat(buf, zip_path);
-        wcscat(buf, delimiter);
+        if (delim) {
+            size_t len = delim - defpath + 1;
+            size_t end = wcslen(buf) + len;
+            wcsncat(buf, defpath, len);
+            *(buf + end) = '\0';
+        }
+        else {
+            wcscat(buf, defpath);
+            break;
+        }
+        defpath = delim + 1;
+    }
+    wcscat(buf, delimiter);
 
-        /* Next goes merge of compile-time $PYTHONPATH with
-         * dynamically located prefix.
-         */
-        defpath = _pythonpath;
-        while (1) {
-            wchar_t *delim = wcschr(defpath, DELIM);
+    /* Finally, on goes the directory for dynamic-load modules */
+    wcscat(buf, exec_prefix);
 
-            if (defpath[0] != SEP) {
-                wcscat(buf, prefix);
-                wcscat(buf, separator);
-            }
-
-            if (delim) {
-                size_t len = delim - defpath + 1;
-                size_t end = wcslen(buf) + len;
-                wcsncat(buf, defpath, len);
-                *(buf + end) = '\0';
-            }
-            else {
-                wcscat(buf, defpath);
-                break;
-            }
-            defpath = delim + 1;
-        }
-        wcscat(buf, delimiter);
-
-        /* Finally, on goes the directory for dynamic-load modules */
-        wcscat(buf, exec_prefix);
-
-        /* And publish the results */
-        module_search_path = buf;
-        module_search_path_malloced = 1;
-    }
+    /* And publish the results */
+    module_search_path = buf;
 
     /* Reduce prefix and exec_prefix to their essence,
      * e.g. /usr/local/lib/python1.5 is reduced to /usr/local.
@@ -834,10 +828,8 @@
 Py_SetPath(const wchar_t *path)
 {
     if (module_search_path != NULL) {
-        if (module_search_path_malloced)
-            PyMem_RawFree(module_search_path);
+        PyMem_RawFree(module_search_path);
         module_search_path = NULL;
-        module_search_path_malloced = 0;
     }
     if (path != NULL) {
         extern wchar_t *Py_GetProgramName(void);
@@ -845,7 +837,6 @@
         wcsncpy(progpath, prog, MAXPATHLEN);
         exec_prefix[0] = prefix[0] = L'\0';
         module_search_path = PyMem_RawMalloc((wcslen(path) + 1) * sizeof(wchar_t));
-        module_search_path_malloced = 1;
         if (module_search_path != NULL)
             wcscpy(module_search_path, path);
     }

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list