Skip to content

Commit 9bee329

Browse files
authored
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.
1 parent 9dd7620 commit 9bee329

File tree

6 files changed

+80
-135
lines changed

6 files changed

+80
-135
lines changed

‎Include/fileutils.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ PyAPI_FUNC(char*) _Py_EncodeLocaleRaw(
1919
size_t *error_pos);
2020
#endif
2121

22+
#ifdef Py_BUILD_CORE
23+
PyAPI_FUNC(wchar_t*) _Py_DecodeUTF8_surrogateescape(
24+
const char *s,
25+
Py_ssize_t size,
26+
size_t *p_wlen);
27+
#endif
28+
2229
#ifndef Py_LIMITED_API
2330
PyAPI_FUNC(PyObject *) _Py_device_encoding(int);
2431

‎Include/pylifecycle.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ PyAPI_FUNC(wchar_t *) Py_GetPath(void);
118118
#ifdef Py_BUILD_CORE
119119
PyAPI_FUNC(_PyInitError) _PyPathConfig_Init(const _PyCoreConfig *core_config);
120120
PyAPI_FUNC(PyObject*) _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv);
121+
PyAPI_FUNC(int) _Py_FindEnvConfigValue(
122+
FILE *env_file,
123+
const wchar_t *key,
124+
wchar_t *value,
125+
size_t value_size);
121126
#endif
122127
PyAPI_FUNC(void) Py_SetPath(const wchar_t *);
123128
#ifdef MS_WINDOWS

‎Modules/getpath.c‎

Lines changed: 9 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -296,62 +296,6 @@ absolutize(wchar_t *path)
296296
}
297297

298298

299-
/* search for a prefix value in an environment file. If found, copy it
300-
to the provided buffer, which is expected to be no more than MAXPATHLEN
301-
bytes long.
302-
*/
303-
static int
304-
find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
305-
{
306-
int result = 0; /* meaning not found */
307-
char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
308-
309-
fseek(env_file, 0, SEEK_SET);
310-
while (!feof(env_file)) {
311-
char * p = fgets(buffer, MAXPATHLEN*2, env_file);
312-
wchar_t tmpbuffer[MAXPATHLEN*2+1];
313-
PyObject * decoded;
314-
int n;
315-
316-
if (p == NULL) {
317-
break;
318-
}
319-
n = strlen(p);
320-
if (p[n - 1] != '\n') {
321-
/* line has overflowed - bail */
322-
break;
323-
}
324-
if (p[0] == '#') {
325-
/* Comment - skip */
326-
continue;
327-
}
328-
decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
329-
if (decoded != NULL) {
330-
Py_ssize_t k;
331-
wchar_t * state;
332-
k = PyUnicode_AsWideChar(decoded,
333-
tmpbuffer, MAXPATHLEN * 2);
334-
Py_DECREF(decoded);
335-
if (k >= 0) {
336-
wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
337-
if ((tok != NULL) && !wcscmp(tok, key)) {
338-
tok = wcstok(NULL, L" \t", &state);
339-
if ((tok != NULL) && !wcscmp(tok, L"=")) {
340-
tok = wcstok(NULL, L"\r\n", &state);
341-
if (tok != NULL) {
342-
wcsncpy(value, tok, MAXPATHLEN);
343-
result = 1;
344-
break;
345-
}
346-
}
347-
}
348-
}
349-
}
350-
}
351-
return result;
352-
}
353-
354-
355299
/* search_for_prefix requires that argv0_path be no more than MAXPATHLEN
356300
bytes long.
357301
*/
@@ -501,24 +445,17 @@ search_for_exec_prefix(const _PyCoreConfig *core_config,
501445
}
502446
else {
503447
char buf[MAXPATHLEN+1];
504-
PyObject *decoded;
505-
wchar_t rel_builddir_path[MAXPATHLEN+1];
448+
wchar_t *rel_builddir_path;
506449
n = fread(buf, 1, MAXPATHLEN, f);
507450
buf[n] = '\0';
508451
fclose(f);
509-
decoded = PyUnicode_DecodeUTF8(buf, n, "surrogateescape");
510-
if (decoded != NULL) {
511-
Py_ssize_t k;
512-
k = PyUnicode_AsWideChar(decoded,
513-
rel_builddir_path, MAXPATHLEN);
514-
Py_DECREF(decoded);
515-
if (k >= 0) {
516-
rel_builddir_path[k] = L'\0';
517-
wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
518-
exec_prefix[MAXPATHLEN] = L'\0';
519-
joinpath(exec_prefix, rel_builddir_path);
520-
return -1;
521-
}
452+
rel_builddir_path = _Py_DecodeUTF8_surrogateescape(buf, n, NULL);
453+
if (rel_builddir_path != NULL) {
454+
wcsncpy(exec_prefix, calculate->argv0_path, MAXPATHLEN);
455+
exec_prefix[MAXPATHLEN] = L'\0';
456+
joinpath(exec_prefix, rel_builddir_path);
457+
PyMem_RawFree(rel_builddir_path );
458+
return -1;
522459
}
523460
}
524461
}
@@ -784,7 +721,7 @@ calculate_read_pyenv(PyCalculatePath *calculate)
784721
}
785722

786723
/* Look for a 'home' variable and set argv0_path to it, if found */
787-
if (find_env_config_value(env_file, L"home", tmpbuffer)) {
724+
if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
788725
wcscpy(calculate->argv0_path, tmpbuffer);
789726
}
790727
fclose(env_file);

‎PC/getpathp.c‎

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -564,58 +564,6 @@ get_program_full_path(const _PyCoreConfig *core_config,
564564
}
565565

566566

567-
static int
568-
find_env_config_value(FILE * env_file, const wchar_t * key, wchar_t * value)
569-
{
570-
int result = 0; /* meaning not found */
571-
char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
572-
573-
fseek(env_file, 0, SEEK_SET);
574-
while (!feof(env_file)) {
575-
char * p = fgets(buffer, MAXPATHLEN*2, env_file);
576-
wchar_t tmpbuffer[MAXPATHLEN*2+1];
577-
PyObject * decoded;
578-
size_t n;
579-
580-
if (p == NULL) {
581-
break;
582-
}
583-
n = strlen(p);
584-
if (p[n - 1] != '\n') {
585-
/* line has overflowed - bail */
586-
break;
587-
}
588-
if (p[0] == '#') {
589-
/* Comment - skip */
590-
continue;
591-
}
592-
decoded = PyUnicode_DecodeUTF8(buffer, n, "surrogateescape");
593-
if (decoded != NULL) {
594-
Py_ssize_t k;
595-
k = PyUnicode_AsWideChar(decoded,
596-
tmpbuffer, MAXPATHLEN * 2);
597-
Py_DECREF(decoded);
598-
if (k >= 0) {
599-
wchar_t * context = NULL;
600-
wchar_t * tok = wcstok_s(tmpbuffer, L" \t\r\n", &context);
601-
if ((tok != NULL) && !wcscmp(tok, key)) {
602-
tok = wcstok_s(NULL, L" \t", &context);
603-
if ((tok != NULL) && !wcscmp(tok, L"=")) {
604-
tok = wcstok_s(NULL, L"\r\n", &context);
605-
if (tok != NULL) {
606-
wcsncpy(value, tok, MAXPATHLEN);
607-
result = 1;
608-
break;
609-
}
610-
}
611-
}
612-
}
613-
}
614-
}
615-
return result;
616-
}
617-
618-
619567
static int
620568
read_pth_file(_PyPathConfig *config, wchar_t *prefix, const wchar_t *path,
621569
int *isolated, int *nosite)
@@ -765,9 +713,11 @@ calculate_pyvenv_file(PyCalculatePath *calculate)
765713
FILE *env_file = _Py_wfopen(envbuffer, L"r");
766714
if (env_file == NULL) {
767715
errno = 0;
716+
768717
reduce(envbuffer);
769718
reduce(envbuffer);
770719
join(envbuffer, env_cfg);
720+
771721
env_file = _Py_wfopen(envbuffer, L"r");
772722
if (env_file == NULL) {
773723
errno = 0;
@@ -780,7 +730,7 @@ calculate_pyvenv_file(PyCalculatePath *calculate)
780730

781731
/* Look for a 'home' variable and set argv0_path to it, if found */
782732
wchar_t tmpbuffer[MAXPATHLEN+1];
783-
if (find_env_config_value(env_file, L"home", tmpbuffer)) {
733+
if (_Py_FindEnvConfigValue(env_file, L"home", tmpbuffer, MAXPATHLEN)) {
784734
wcscpy_s(calculate->argv0_path, MAXPATHLEN+1, tmpbuffer);
785735
}
786736
fclose(env_file);

‎Python/fileutils.c‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ extern int winerror_to_errno(int);
2020
#include <fcntl.h>
2121
#endif /* HAVE_FCNTL_H */
2222

23-
extern wchar_t* _Py_DecodeUTF8_surrogateescape(const char *s, Py_ssize_t size,
24-
size_t *p_wlen);
2523
extern char* _Py_EncodeUTF8_surrogateescape(const wchar_t *text,
2624
size_t *error_pos, int raw_malloc);
2725

@@ -194,7 +192,7 @@ encode_ascii_surrogateescape(const wchar_t *text, size_t *error_pos, int raw_mal
194192

195193
len = wcslen(text);
196194

197-
/* +1 for NUL byte */
195+
/* +1 for NULL byte */
198196
if (raw_malloc) {
199197
result = PyMem_RawMalloc(len + 1);
200198
}
@@ -467,13 +465,11 @@ encode_current_locale(const wchar_t *text, size_t *error_pos, int raw_malloc)
467465
else
468466
converted = wcstombs(NULL, buf, 0);
469467
if (converted == (size_t)-1) {
470-
if (result != NULL) {
471-
if (raw_malloc) {
472-
PyMem_RawFree(result);
473-
}
474-
else {
475-
PyMem_Free(result);
476-
}
468+
if (raw_malloc) {
469+
PyMem_RawFree(result);
470+
}
471+
else {
472+
PyMem_Free(result);
477473
}
478474
if (error_pos != NULL)
479475
*error_pos = i;

‎Python/pathconfig.c‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,56 @@ _PyPathConfig_ComputeArgv0(int argc, wchar_t **argv)
354354
return PyUnicode_FromWideChar(argv0, n);
355355
}
356356

357+
358+
/* Search for a prefix value in an environment file (pyvenv.cfg).
359+
If found, copy it into the provided buffer. */
360+
int
361+
_Py_FindEnvConfigValue(FILE *env_file, const wchar_t *key,
362+
wchar_t *value, size_t value_size)
363+
{
364+
int result = 0; /* meaning not found */
365+
char buffer[MAXPATHLEN*2+1]; /* allow extra for key, '=', etc. */
366+
367+
fseek(env_file, 0, SEEK_SET);
368+
while (!feof(env_file)) {
369+
char * p = fgets(buffer, MAXPATHLEN*2, env_file);
370+
wchar_t *tmpbuffer;
371+
int n;
372+
373+
if (p == NULL) {
374+
break;
375+
}
376+
n = strlen(p);
377+
if (p[n - 1] != '\n') {
378+
/* line has overflowed - bail */
379+
break;
380+
}
381+
if (p[0] == '#') {
382+
/* Comment - skip */
383+
continue;
384+
}
385+
tmpbuffer = _Py_DecodeUTF8_surrogateescape(buffer, n, NULL);
386+
if (tmpbuffer != NULL) {
387+
wchar_t * state;
388+
wchar_t * tok = wcstok(tmpbuffer, L" \t\r\n", &state);
389+
if ((tok != NULL) && !wcscmp(tok, key)) {
390+
tok = wcstok(NULL, L" \t", &state);
391+
if ((tok != NULL) && !wcscmp(tok, L"=")) {
392+
tok = wcstok(NULL, L"\r\n", &state);
393+
if (tok != NULL) {
394+
wcsncpy(value, tok, MAXPATHLEN);
395+
result = 1;
396+
PyMem_RawFree(tmpbuffer);
397+
break;
398+
}
399+
}
400+
}
401+
PyMem_RawFree(tmpbuffer);
402+
}
403+
}
404+
return result;
405+
}
406+
357407
#ifdef __cplusplus
358408
}
359409
#endif

0 commit comments

Comments
 (0)