bpo-36142: Add preconfig.c (GH-12128) · python/cpython@91b9ecf · GitHub
Skip to content

Commit 91b9ecf

Browse files
authored
bpo-36142: Add preconfig.c (GH-12128)
* Add _PyArgv_Decode() function * Move _Py_ClearFileSystemEncoding() and _Py_SetFileSystemEncoding() to preconfig.c.
1 parent c991f24 commit 91b9ecf

8 files changed

Lines changed: 115 additions & 87 deletions

File tree

Include/cpython/coreconfig.h

Lines changed: 5 additions & 4 deletions

Include/internal/pycore_coreconfig.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN defined"
99
#endif
1010

11-
/* _Py_wstrlist */
11+
/* --- _Py_wstrlist ----------------------------------------------- */
1212

1313
PyAPI_FUNC(void) _Py_wstrlist_clear(
1414
int len,
@@ -24,12 +24,17 @@ PyAPI_FUNC(PyObject*) _Py_wstrlist_as_pylist(
2424
int len,
2525
wchar_t **list);
2626

27-
/* Py_GetArgcArgv() helpers */
27+
/* --- _PyArgv ---------------------------------------------------- */
28+
29+
PyAPI_FUNC(_PyInitError) _PyArgv_Decode(const _PyArgv *args,
30+
wchar_t*** argv_p);
31+
32+
/* --- Py_GetArgcArgv() helpers ----------------------------------- */
2833

2934
PyAPI_FUNC(void) _Py_ClearArgcArgv(void);
3035
PyAPI_FUNC(int) _Py_SetArgcArgv(int argc, wchar_t * const *argv);
3136

32-
/* _PyCoreConfig */
37+
/* --- _PyCoreConfig ---------------------------------------------- */
3338

3439
PyAPI_FUNC(_PyInitError) _PyCoreConfig_Read(_PyCoreConfig *config);
3540
PyAPI_FUNC(void) _PyCoreConfig_Clear(_PyCoreConfig *);

Include/internal/pycore_fileutils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ extern "C" {
1010

1111
#include <locale.h> /* struct lconv */
1212

13+
PyAPI_DATA(int) _Py_HasFileSystemDefaultEncodeErrors;
14+
1315
PyAPI_FUNC(int) _Py_DecodeUTF8Ex(
1416
const char *arg,
1517
Py_ssize_t arglen,

Makefile.pre.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,7 @@ PYTHON_OBJS= \
357357
Python/mystrtoul.o \
358358
Python/pathconfig.o \
359359
Python/peephole.o \
360+
Python/preconfig.o \
360361
Python/pyarena.o \
361362
Python/pyctype.o \
362363
Python/pyfpe.o \

PCbuild/pythoncore.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@
425425
<ClCompile Include="..\Python\mystrtoul.c" />
426426
<ClCompile Include="..\Python\pathconfig.c" />
427427
<ClCompile Include="..\Python\peephole.c" />
428+
<ClCompile Include="..\Python\preconfig.c" />
428429
<ClCompile Include="..\Python\pyarena.c" />
429430
<ClCompile Include="..\Python\pyctype.c" />
430431
<ClCompile Include="..\Python\pyfpe.c" />

PCbuild/pythoncore.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,9 @@
989989
<ClCompile Include="..\Python\peephole.c">
990990
<Filter>Python</Filter>
991991
</ClCompile>
992+
<ClCompile Include="..\Python\preconfig.c">
993+
<Filter>Python</Filter>
994+
</ClCompile>
992995
<ClCompile Include="..\Python\pyarena.c">
993996
<Filter>Python</Filter>
994997
</ClCompile>

Python/coreconfig.c

Lines changed: 3 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,6 @@ int Py_LegacyWindowsFSEncodingFlag = 0; /* Uses mbcs instead of utf-8 */
136136
int Py_LegacyWindowsStdioFlag = 0; /* Uses FileIO instead of WindowsConsoleIO */
137137
#endif
138138

139-
/* The filesystem encoding is chosen by config_init_fs_encoding(),
140-
see also initfsencoding(). */
141-
const char *Py_FileSystemDefaultEncoding = NULL;
142-
int Py_HasFileSystemDefaultEncoding = 0;
143-
const char *Py_FileSystemDefaultEncodeErrors = NULL;
144-
static int _Py_HasFileSystemDefaultEncodeErrors = 0;
145-
146139

147140
PyObject *
148141
_Py_GetGlobalVariablesAsDict(void)
@@ -296,49 +289,6 @@ _Py_wstrlist_as_pylist(int len, wchar_t **list)
296289
}
297290

298291

299-
void
300-
_Py_ClearFileSystemEncoding(void)
301-
{
302-
if (!Py_HasFileSystemDefaultEncoding && Py_FileSystemDefaultEncoding) {
303-
PyMem_RawFree((char*)Py_FileSystemDefaultEncoding);
304-
Py_FileSystemDefaultEncoding = NULL;
305-
}
306-
if (!_Py_HasFileSystemDefaultEncodeErrors && Py_FileSystemDefaultEncodeErrors) {
307-
PyMem_RawFree((char*)Py_FileSystemDefaultEncodeErrors);
308-
Py_FileSystemDefaultEncodeErrors = NULL;
309-
}
310-
}
311-
312-
313-
/* --- File system encoding/errors -------------------------------- */
314-
315-
/* Set Py_FileSystemDefaultEncoding and Py_FileSystemDefaultEncodeErrors
316-
global configuration variables. */
317-
int
318-
_Py_SetFileSystemEncoding(const char *encoding, const char *errors)
319-
{
320-
char *encoding2 = _PyMem_RawStrdup(encoding);
321-
if (encoding2 == NULL) {
322-
return -1;
323-
}
324-
325-
char *errors2 = _PyMem_RawStrdup(errors);
326-
if (errors2 == NULL) {
327-
PyMem_RawFree(encoding2);
328-
return -1;
329-
}
330-
331-
_Py_ClearFileSystemEncoding();
332-
333-
Py_FileSystemDefaultEncoding = encoding2;
334-
Py_HasFileSystemDefaultEncoding = 0;
335-
336-
Py_FileSystemDefaultEncodeErrors = errors2;
337-
_Py_HasFileSystemDefaultEncodeErrors = 0;
338-
return 0;
339-
}
340-
341-
342292
/* --- Py_SetStandardStreamEncoding() ----------------------------- */
343293

344294
/* Helper to allow an embedding application to override the normal
@@ -1849,6 +1799,7 @@ _PyCoreConfig_AsDict(const _PyCoreConfig *config)
18491799

18501800
typedef struct {
18511801
const _PyArgv *args;
1802+
int argc;
18521803
wchar_t **argv;
18531804
int nwarnoption; /* Number of -W command line options */
18541805
wchar_t **warnoptions; /* Command line -W options */
@@ -1881,35 +1832,7 @@ static _PyInitError
18811832
cmdline_decode_argv(_PyCmdline *cmdline)
18821833
{
18831834
assert(cmdline->argv == NULL);
1884-
1885-
const _PyArgv *args = cmdline->args;
1886-
1887-
if (args->use_bytes_argv) {
1888-
/* +1 for a the NULL terminator */
1889-
size_t size = sizeof(wchar_t*) * (args->argc + 1);
1890-
wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
1891-
if (argv == NULL) {
1892-
return _Py_INIT_NO_MEMORY();
1893-
}
1894-
1895-
for (int i = 0; i < args->argc; i++) {
1896-
size_t len;
1897-
wchar_t *arg = Py_DecodeLocale(args->bytes_argv[i], &len);
1898-
if (arg == NULL) {
1899-
_Py_wstrlist_clear(i, argv);
1900-
return DECODE_LOCALE_ERR("command line arguments",
1901-
(Py_ssize_t)len);
1902-
}
1903-
argv[i] = arg;
1904-
}
1905-
argv[args->argc] = NULL;
1906-
1907-
cmdline->argv = argv;
1908-
}
1909-
else {
1910-
cmdline->argv = args->wchar_argv;
1911-
}
1912-
return _Py_INIT_OK();
1835+
return _PyArgv_Decode(cmdline->args, &cmdline->argv);
19131836
}
19141837

19151838

@@ -2377,7 +2300,7 @@ config_read_from_argv_impl(_PyCoreConfig *config, const _PyArgv *args)
23772300
memset(&cmdline, 0, sizeof(cmdline));
23782301
cmdline.args = args;
23792302

2380-
err = cmdline_decode_argv(&cmdline);
2303+
err = _PyArgv_Decode(cmdline.args, &cmdline.argv);
23812304
if (_Py_INIT_FAILED(err)) {
23822305
goto done;
23832306
}

Python/preconfig.c

Lines changed: 92 additions & 0 deletions

0 commit comments

Comments
 (0)