Skip to content

Commit 99563b1

Browse files
author
Victor Stinner
committed
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.
1 parent d6b3840 commit 99563b1

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

Modules/getpath.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ calculate_path(void)
406406
static wchar_t delimiter[2] = {DELIM, '\0'};
407407
static wchar_t separator[2] = {SEP, '\0'};
408408
char *_rtpypath = Py_GETENV("PYTHONPATH"); /* XXX use wide version on Windows */
409-
wchar_t rtpypath[MAXPATHLEN+1];
409+
wchar_t *rtpypath = NULL;
410410
wchar_t *home = Py_GetPythonHome();
411411
char *_path = getenv("PATH");
412412
wchar_t *path_buffer = NULL;
@@ -606,12 +606,12 @@ calculate_path(void)
606606
bufsz = 0;
607607

608608
if (_rtpypath) {
609-
size_t s = mbstowcs(rtpypath, _rtpypath, sizeof(rtpypath)/sizeof(wchar_t));
610-
if (s == (size_t)-1 || s >=sizeof(rtpypath))
611-
/* XXX deal with errors more gracefully */
609+
size_t rtpypath_len;
610+
rtpypath = _Py_char2wchar(_rtpypath, &rtpypath_len);
611+
if (rtpypath != NULL)
612+
bufsz += rtpypath_len + 1;
613+
else
612614
_rtpypath = NULL;
613-
if (_rtpypath)
614-
bufsz += wcslen(rtpypath) + 1;
615615
}
616616

617617
defpath = _pythonpath;
@@ -645,7 +645,7 @@ calculate_path(void)
645645
}
646646
else {
647647
/* Run-time value of $PYTHONPATH goes first */
648-
if (_rtpypath) {
648+
if (rtpypath) {
649649
wcscpy(buf, rtpypath);
650650
wcscat(buf, delimiter);
651651
}
@@ -719,6 +719,8 @@ calculate_path(void)
719719
PyMem_Free(_pythonpath);
720720
PyMem_Free(_prefix);
721721
PyMem_Free(_exec_prefix);
722+
if (rtpypath != NULL)
723+
PyMem_Free(rtpypath);
722724
}
723725

724726

0 commit comments

Comments
 (0)