◐ Shell
reader mode source ↗
Skip to content
Closed
Changes from 1 commit
File filter
Conversations
Jump to
Diff view
Apply and reload
Show whitespace
Diff view
Apply and reload
Prev Previous commit
Next
_PyCmdline: add ctx
  • Loading branch information
vstinner committed Nov 17, 2018
commit abe35405e058d04fdc919c575bf575b90cb5953c
44 changes: 15 additions & 29 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
/* Main program */

typedef struct {
wchar_t **argv;
int nwarnoption; /* Number of -W command line options */
wchar_t **warnoptions; /* Command line -W options */
Expand Down @@ -369,17 +370,17 @@ pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,
if (pymain->use_bytes_argv) {
/* +1 for a the NULL terminator */
size_t size = sizeof(wchar_t*) * (pymain->argc + 1);
wchar_t** argv = (wchar_t **)PyMem_RawMalloc(size);
if (argv == NULL) {
pymain->err = _Py_INIT_NO_MEMORY();
return -1;
}

for (int i = 0; i < pymain->argc; i++) {
size_t len;
wchar_t *arg = _Py_DecodeLocaleCtx(&config->ctx, pymain->bytes_argv[i], &len);
if (arg == NULL) {
_Py_wstrlist_clear(&config->ctx, i, argv);
pymain->err = DECODE_LOCALE_ERR("command line arguments",
(Py_ssize_t)len);
return -1;
Expand Down Expand Up @@ -411,25 +412,23 @@ pymain_init_cmdline_argv(_PyMain *pymain, _PyCoreConfig *config,


static void
pymain_clear_cmdline(_PyMain *pymain, _PyCoreConfig *config, _PyCmdline *cmdline)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);

_Py_wstrlist_clear(&config->ctx, cmdline->nwarnoption, cmdline->warnoptions);
cmdline->nwarnoption = 0;
cmdline->warnoptions = NULL;

_Py_wstrlist_clear(&config->ctx, cmdline->nenv_warnoption, cmdline->env_warnoptions);
cmdline->nenv_warnoption = 0;
cmdline->env_warnoptions = NULL;

if (pymain->use_bytes_argv && cmdline->argv != NULL) {
_Py_wstrlist_clear(&config->ctx, pymain->argc, cmdline->argv);
}
cmdline->argv = NULL;

PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
}


Expand Down Expand Up @@ -1311,7 +1310,9 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,

/* bpo-34207: Py_DecodeLocale() and Py_EncodeLocale() depend
on Py_UTF8Mode and Py_LegacyWindowsFSEncodingFlag. */
Py_UTF8Mode = config->ctx.utf8_mode;
#ifdef MS_WINDOWS
Py_LegacyWindowsFSEncodingFlag = config->legacy_windows_fs_encoding;
#endif
Expand Down Expand Up @@ -1366,8 +1367,7 @@ pymain_read_conf(_PyMain *pymain, _PyCoreConfig *config,
pymain->err = _Py_INIT_NO_MEMORY();
goto done;
}
pymain_clear_cmdline(pymain, config, cmdline);
memset(cmdline, 0, sizeof(*cmdline));
config->ctx.utf8_mode = new_utf8_mode;
config->coerce_c_locale = new_coerce_c_locale;

Expand Down Expand Up @@ -1722,27 +1722,13 @@ pymain_cmdline_impl(_PyMain *pymain, _PyCoreConfig *config,
static int
pymain_cmdline(_PyMain *pymain, _PyCoreConfig *config)
{
PyMemAllocatorEx old_alloc;
_PyMem_SetDefaultAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
#ifdef Py_DEBUG
PyMemAllocatorEx default_alloc;
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &default_alloc);
#endif

_PyCmdline cmdline;
memset(&cmdline, 0, sizeof(cmdline));

int res = pymain_cmdline_impl(pymain, config, &cmdline);

pymain_clear_cmdline(pymain, config, &cmdline);

#ifdef Py_DEBUG
/* Make sure that PYMEM_DOMAIN_RAW has not been modified */
PyMemAllocatorEx cur_alloc;
PyMem_GetAllocator(PYMEM_DOMAIN_RAW, &cur_alloc);
assert(memcmp(&cur_alloc, &default_alloc, sizeof(cur_alloc)) == 0);
#endif
PyMem_SetAllocator(PYMEM_DOMAIN_RAW, &old_alloc);
return res;
}

Expand Down
Toggle all file notes Toggle all file annotations