diff options
Diffstat (limited to 'Python')
-rw-r--r-- | Python/fileutils.c | 16 | ||||
-rw-r--r-- | Python/pathconfig.c | 50 |
2 files changed, 56 insertions, 10 deletions
diff --git a/Python/fileutils.c b/Python/fileutils.c index 1ccd4baa6d..645a179366 100644 --- a/Python/fileutils.c +++ b/Python/fileutils.c @@ -20,8 +20,6 @@ extern int winerror_to_errno(int); #include <fcntl.h> #endif /* HAVE_FCNTL_H */ -extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size, - size_t *p_wlen); extern char* _Py_EncodeUTF8_surrogateescape(const wchar_t *text, size_t *error_pos, int raw_malloc); @@ -194,7 +192,7 @@ encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos, int raw_mal len = wcslen(text); - /* +1 for NUL byte */ + /* +1 for NULL byte */ if (raw_malloc) { result = PyMem_RawMalloc(len + 1); } @@ -467,13 +465,11 @@ encode_current_locale(const wchar_t *text, size_t *error_pos, int raw_malloc) else converted = wcstombs(NULL, buf, 0); if (converted == (size_t)-1) { - if (result != NULL) { - if (raw_malloc) { - PyMem_RawFree(result); - } - else { - PyMem_Free(result); - } + if (raw_malloc) { + PyMem_RawFree(result); + } + else { + PyMem_Free(result); } if (error_pos != NULL) *error_pos = i; diff --git a/Python/pathconfig.c b/Python/pathconfig.c index acb25b6fa8..9591fcc491 100644 --- a/Python/pathconfig.c +++ b/Python/pathconfig.c @@ -354,6 +354,56 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv) return PyUnicode_FromWideChar(argv0, n); } + +/* Search for a prefix value in an environment file (pyvenv.cfg). + If found, copy it into the provided buffer. */ +int +_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key, + wchar_t *value, size_t value_size) +{ + int result = 0; /* meaning not found */ + char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */ + + fseek(env_file, 0, SEEK_SET); + while (!feof(env_file)) { + char * p = fgets(buffer, MAXPATHLEN*2, env_file); + wchar_t *tmpbuffer; + int n; + + if (p == NULL) { + break; + } + n = strlen(p); + if (p[n - 1] != '\n') { + /* line has overflowed - bail */ + break; + } + if (p[0] == '#') { + /* Comment - skip */ + continue; + } + tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL); + if (tmpbuffer != NULL) { + wchar_t * state; + wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state); + if ((tok != NULL) && !wcscmp(tok, key)) { + tok = wcstok(NULL, L" \t", &state); + if ((tok != NULL) && !wcscmp(tok, L"=")) { + tok = wcstok(NULL, L"\r\n", &state); + if (tok != NULL) { + wcsncpy(value, tok, MAXPATHLEN); + result = 1; + PyMem_RawFree(tmpbuffer); + break; + } + } + } + PyMem_RawFree(tmpbuffer); + } + } + return result; +} + #ifdef __cplusplus } #endif |