]> granicus.if.org Git - python/commitdiff
bpo-36763: Cleanup precmdline in _PyCoreConfig_Read() (GH-13371)
authorVictor Stinner <vstinner@redhat.com>
Fri, 17 May 2019 01:15:12 +0000 (03:15 +0200)
committerGitHub <noreply@github.com>
Fri, 17 May 2019 01:15:12 +0000 (03:15 +0200)
precmdline is only needed in _PyCoreConfig_Read(), not in
config_read_cmdline(). Reorganize _PyCoreConfig_Read().

Include/internal/pycore_getopt.h
Python/coreconfig.c
Python/getopt.c
Python/preconfig.c

index 834b8c8a14092fa52ff2836359068ba81cf429e0..7f0dd13ae577f78491f19b7e8fa3ac8c5042bb11 100644 (file)
@@ -17,6 +17,6 @@ typedef struct {
     int val;
 } _PyOS_LongOption;
 
-extern int _PyOS_GetOpt(Py_ssize_t argc, wchar_t **argv, int *longindex);
+extern int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex);
 
 #endif /* !Py_INTERNAL_PYGETOPT_H */
index e51bf9424a308390b86361471672b1955e034334..ce778a640d3c9e05c1d1849265b2ae0c342b3a7d 100644 (file)
@@ -1486,21 +1486,11 @@ config_init_fs_encoding(_PyCoreConfig *config, const _PyPreConfig *preconfig)
 
 
 static _PyInitError
-config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline)
+config_read(_PyCoreConfig *config)
 {
     _PyInitError err;
     const _PyPreConfig *preconfig = &_PyRuntime.preconfig;
 
-    if (_PyPreCmdline_SetCoreConfig(cmdline, config) < 0) {
-        return _Py_INIT_NO_MEMORY();
-    }
-
-    if (config->isolated > 0) {
-        /* _PyPreCmdline_Read() sets use_environment to 0 if isolated is set,
-           _PyPreCmdline_SetCoreConfig() overrides config->use_environment. */
-        config->user_site_directory = 0;
-    }
-
     if (config->use_environment) {
         err = config_read_env_vars(config);
         if (_Py_INIT_FAILED(err)) {
@@ -1584,6 +1574,19 @@ config_read(_PyCoreConfig *config, _PyPreCmdline *cmdline)
             return _Py_INIT_NO_MEMORY();
         }
     }
+
+    if (config->check_hash_pycs_mode == NULL) {
+        err = _PyCoreConfig_SetString(&config->check_hash_pycs_mode,
+                                      L"default");
+        if (_Py_INIT_FAILED(err)) {
+            return err;
+        }
+    }
+
+    if (config->configure_c_stdio < 0) {
+        config->configure_c_stdio = 1;
+    }
+
     return _Py_INIT_OK();
 }
 
@@ -1669,11 +1672,11 @@ config_usage(int error, const wchar_t* program)
 
 /* Parse the command line arguments */
 static _PyInitError
-config_parse_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline,
-                     _PyWstrList *warnoptions, int *opt_index)
+config_parse_cmdline(_PyCoreConfig *config, _PyWstrList *warnoptions,
+                     int *opt_index)
 {
     _PyInitError err;
-    const _PyWstrList *argv = &precmdline->argv;
+    const _PyWstrList *argv = &config->argv;
     int print_version = 0;
 
     _PyOS_ResetGetOpt();
@@ -1890,9 +1893,9 @@ config_init_env_warnoptions(const _PyCoreConfig *config, _PyWstrList *warnoption
 
 
 static _PyInitError
-config_init_program(_PyCoreConfig *config, const _PyPreCmdline *cmdline)
+config_init_program(_PyCoreConfig *config)
 {
-    const _PyWstrList *argv = &cmdline->argv;
+    const _PyWstrList *argv = &config->argv;
     wchar_t *program;
     if (argv->length >= 1) {
         program = argv->items[0];
@@ -1987,10 +1990,9 @@ config_init_warnoptions(_PyCoreConfig *config,
 
 
 static _PyInitError
-config_update_argv(_PyCoreConfig *config, const _PyPreCmdline *cmdline,
-                   int opt_index)
+config_update_argv(_PyCoreConfig *config, int opt_index)
 {
-    const _PyWstrList *cmdline_argv = &cmdline->argv;
+    const _PyWstrList *cmdline_argv = &config->argv;
     _PyWstrList config_argv = _PyWstrList_INIT;
 
     /* Copy argv to be able to modify it (to force -c/-m) */
@@ -2054,6 +2056,16 @@ core_read_precmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
     _PyCoreConfig_GetCoreConfig(&preconfig, config);
 
     err = _PyPreCmdline_Read(precmdline, &preconfig);
+    if (_Py_INIT_FAILED(err)) {
+        goto done;
+    }
+
+    if (_PyPreCmdline_SetCoreConfig(precmdline, config) < 0) {
+        err = _Py_INIT_NO_MEMORY();
+        goto done;
+    }
+
+    err = _Py_INIT_OK();
 
 done:
     _PyPreConfig_Clear(&preconfig);
@@ -2062,7 +2074,7 @@ done:
 
 
 static _PyInitError
-config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
+config_read_cmdline(_PyCoreConfig *config)
 {
     _PyInitError err;
     _PyWstrList cmdline_warnoptions = _PyWstrList_INIT;
@@ -2071,29 +2083,27 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
     if (config->parse_argv < 0) {
         config->parse_argv = 1;
     }
-    if (config->configure_c_stdio < 0) {
-        config->configure_c_stdio = 1;
+
+    if (config->program == NULL) {
+        err = config_init_program(config);
+        if (_Py_INIT_FAILED(err)) {
+            goto done;
+        }
     }
 
     if (config->parse_argv) {
         int opt_index;
-        err = config_parse_cmdline(config, precmdline, &cmdline_warnoptions,
-                                   &opt_index);
+        err = config_parse_cmdline(config, &cmdline_warnoptions, &opt_index);
         if (_Py_INIT_FAILED(err)) {
             goto done;
         }
 
-        err = config_update_argv(config, precmdline, opt_index);
+        err = config_update_argv(config, opt_index);
         if (_Py_INIT_FAILED(err)) {
             goto done;
         }
     }
 
-    err = config_read(config, precmdline);
-    if (_Py_INIT_FAILED(err)) {
-        goto done;
-    }
-
     if (config->use_environment) {
         err = config_init_env_warnoptions(config, &env_warnoptions);
         if (_Py_INIT_FAILED(err)) {
@@ -2107,13 +2117,6 @@ config_read_cmdline(_PyCoreConfig *config, _PyPreCmdline *precmdline)
         goto done;
     }
 
-    if (config->check_hash_pycs_mode == NULL) {
-        err = _PyCoreConfig_SetString(&config->check_hash_pycs_mode, L"default");
-        if (_Py_INIT_FAILED(err)) {
-            goto done;
-        }
-    }
-
     err = _Py_INIT_OK();
 
 done:
@@ -2199,14 +2202,18 @@ _PyCoreConfig_Read(_PyCoreConfig *config)
         goto done;
     }
 
-    if (config->program == NULL) {
-        err = config_init_program(config, &precmdline);
-        if (_Py_INIT_FAILED(err)) {
-            goto done;
-        }
+    assert(config->isolated >= 0);
+    if (config->isolated) {
+        config->use_environment = 0;
+        config->user_site_directory = 0;
+    }
+
+    err = config_read_cmdline(config);
+    if (_Py_INIT_FAILED(err)) {
+        goto done;
     }
 
-    err = config_read_cmdline(config, &precmdline);
+    err = config_read(config);
     if (_Py_INIT_FAILED(err)) {
         goto done;
     }
index 10bd1d49d7d1b78c634659ab5e23118f407ad23f..1a7db3fce888ee5e9aa603411e097034203b1836 100644 (file)
@@ -41,7 +41,7 @@ int _PyOS_opterr = 1;                 /* generate error messages */
 Py_ssize_t _PyOS_optind = 1;          /* index into argv array   */
 const wchar_t *_PyOS_optarg = NULL;   /* optional argument       */
 
-static wchar_t *opt_ptr = L"";
+static const wchar_t *opt_ptr = L"";
 
 /* Python command line short and long options */
 
@@ -61,7 +61,7 @@ void _PyOS_ResetGetOpt(void)
     opt_ptr = L"";
 }
 
-int _PyOS_GetOpt(Py_ssize_t argc, wchar_t **argv, int *longindex)
+int _PyOS_GetOpt(Py_ssize_t argc, wchar_t * const *argv, int *longindex)
 {
     wchar_t *ptr;
     wchar_t option;
index 48b9e8383aae146c697c0da1fae629eacaa1f255..2d0ace5df2957e07d86fe812434b9efb021d6294 100644 (file)
@@ -174,7 +174,7 @@ _PyPreCmdline_SetCoreConfig(const _PyPreCmdline *cmdline, _PyCoreConfig *config)
 static _PyInitError
 precmdline_parse_cmdline(_PyPreCmdline *cmdline)
 {
-    _PyWstrList *argv = &cmdline->argv;
+    const _PyWstrList *argv = &cmdline->argv;
 
     _PyOS_ResetGetOpt();
     /* Don't log parsing errors into stderr here: _PyCoreConfig_Read()