[Python-checkins] cpython: calculate_path() decodes the PYTHONPATH environment variable from the locale

victor.stinner python-checkins at python.org
Mon Jun 20 14:46:09 CEST 2011


http://hg.python.org/cpython/rev/f571ed28a9ba
changeset:   70898:f571ed28a9ba
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Mon Jun 20 14:45:54 2011 +0200
summary:
  calculate_path() decodes the PYTHONPATH environment variable from the locale
encoding using _Py_char2wchar() instead of mbstowcs() to store undecodable
bytes as surrogates characters (PEP 383) instead of ignoring silently
the PYTHONPATH variable.

files:
  Modules/getpath.c |  16 +++++++++-------
  1 files changed, 9 insertions(+), 7 deletions(-)


diff --git a/Modules/getpath.c b/Modules/getpath.c
--- a/Modules/getpath.c
+++ b/Modules/getpath.c
@@ -406,7 +406,7 @@
     static wchar_t delimiter[2] = {DELIM, '\0'};
     static wchar_t separator[2] = {SEP, '\0'};
     char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */
-    wchar_t rtpypath[MAXPATHLEN+1];
+    wchar_t *rtpypath = NULL;
     wchar_t *home = Py_GetPythonHome();
     char *_path = getenv("PATH");
     wchar_t *path_buffer = NULL;
@@ -606,12 +606,12 @@
     bufsz = 0;
 
     if (_rtpypath) {
-        size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t));
-        if (s == (size_t)-1 || s >=sizeof(rtpypath))
-            /* XXX deal with errors more gracefully */
+        size_t rtpypath_len;
+        rtpypath = _Py_char2wchar(_rtpypath, &rtpypath_len);
+        if (rtpypath != NULL)
+            bufsz += rtpypath_len + 1;
+        else
             _rtpypath = NULL;
-        if (_rtpypath)
-            bufsz += wcslen(rtpypath) + 1;
     }
 
     defpath = _pythonpath;
@@ -645,7 +645,7 @@
     }
     else {
         /* Run-time value of $PYTHONPATH goes first */
-        if (_rtpypath) {
+        if (rtpypath) {
             wcscpy(buf, rtpypath);
             wcscat(buf, delimiter);
         }
@@ -719,6 +719,8 @@
     PyMem_Free(_pythonpath);
     PyMem_Free(_prefix);
     PyMem_Free(_exec_prefix);
+    if (rtpypath != NULL)
+        PyMem_Free(rtpypath);
 }
 
 

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


More information about the Python-checkins mailing list