mirror of
https://github.com/python/cpython.git
synced 2025-08-30 05:35:08 +00:00
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.
This commit is contained in:
parent
9dd762013f
commit
9bee329130
6 changed files with 80 additions and 135 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue