summaryrefslogtreecommitdiffstats
path: root/PC/getpathp.c
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2017-12-21 15:49:13 (GMT)
committerGitHub <noreply@github.com>2017-12-21 15:49:13 (GMT)
commit9bee329130aae5a13050c08dab9d349b76e66835 (patch)
tree828e5eba0807b15d60bb7f5162685d0dc993fc42 /PC/getpathp.c
parent9dd762013fd9fcf975ad51700b55d050ca9ed60e (diff)
downloadcpython-9bee329130aae5a13050c08dab9d349b76e66835.zip
cpython-9bee329130aae5a13050c08dab9d349b76e66835.tar.gz
cpython-9bee329130aae5a13050c08dab9d349b76e66835.tar.bz2
bpo-32030: Add _Py_FindEnvConfigValue() (#4963)
Add a new _Py_FindEnvConfigValue() function: code shared between Windows and Unix implementations of _PyPathConfig_Calculate() to read the pyenv.cfg file. _Py_FindEnvConfigValue() now uses _Py_DecodeUTF8_surrogateescape() instead of using a Python Unicode string, the Python API must not be used early during Python initialization. Same change in Unix search_for_exec_prefix(): use _Py_DecodeUTF8_surrogateescape(). Cleanup also encode_current_locale(): PyMem_RawFree/PyMem_Free can be called with NULL. Fix also "NUL byte" => "NULL byte" typo.
Diffstat (limited to 'PC/getpathp.c')
-rw-r--r--PC/getpathp.c56
1 files changed, 3 insertions, 53 deletions
diff --git a/PC/getpathp.c b/PC/getpathp.c
index fe4ef1a..c4977e7 100644
--- a/PC/getpathp.c
+++ b/PC/getpathp.c
@@ -565,58 +565,6 @@ done:
static int
-find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
-{
- 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[MAXPATHLEN*2+1];
- PyObject * decoded;
- size_t n;
-
- if (p == NULL) {
- break;
- }
- n = strlen(p);
- if (p[n - 1] != '\n') {
- /* line has overflowed - bail */
- break;
- }
- if (p[0] == '#') {
- /* Comment - skip */
- continue;
- }
- decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
- if (decoded != NULL) {
- Py_ssize_t k;
- k = PyUnicode_AsWideChar(decoded,
- tmpbuffer, MAXPATHLEN * 2);
- Py_DECREF(decoded);
- if (k >= 0) {
- wchar_t * context = NULL;
- wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
- if ((tok != NULL) && !wcscmp(tok, key)) {
- tok = wcstok_s(NULL, L" \t", &context);
- if ((tok != NULL) && !wcscmp(tok, L"=")) {
- tok = wcstok_s(NULL, L"\r\n", &context);
- if (tok != NULL) {
- wcsncpy(value, tok, MAXPATHLEN);
- result = 1;
- break;
- }
- }
- }
- }
- }
- }
- return result;
-}
-
-
-static int
read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
int *isolated, int *nosite)
{
@@ -765,9 +713,11 @@ calculate_pyvenv_file(PyCalculatePath *calculate)
FILE *env_file = _Py_wfopen(envbuffer, L"r");
if (env_file == NULL) {
errno = 0;
+
reduce(envbuffer);
reduce(envbuffer);
join(envbuffer, env_cfg);
+
env_file = _Py_wfopen(envbuffer, L"r");
if (env_file == NULL) {
errno = 0;
@@ -780,7 +730,7 @@ calculate_pyvenv_file(PyCalculatePath *calculate)
/* Look for a 'home' variable and set argv0_path to it, if found */
wchar_t tmpbuffer[MAXPATHLEN+1];
- if (find_env_config_value(env_file, L"home", tmpbuffer)) {
+ if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
}
fclose(env_file);