bpo-44131: Py_FrozenMain() uses PyConfig_SetBytesArgv() · python/cpython@6ba6d06
@@ -20,74 +20,28 @@ Py_FrozenMain(int argc, char **argv)
2020Py_ExitStatusException(status);
2121 }
222223-const char *p;
24-int i, n, sts = 1;
25-int inspect = 0;
26-int unbuffered = 0;
27-char *oldloc = NULL;
28-wchar_t **argv_copy = NULL;
29-/* We need a second copies, as Python might modify the first one. */
30-wchar_t **argv_copy2 = NULL;
31-32-if (argc > 0) {
33-argv_copy = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
34-argv_copy2 = PyMem_RawMalloc(sizeof(wchar_t*) * argc);
35-if (!argv_copy || !argv_copy2) {
36-fprintf(stderr, "out of memory\n");
37- goto error;
38- }
39- }
40-4123PyConfig config;
4224PyConfig_InitPythonConfig(&config);
43-config.pathconfig_warnings = 0; /* Suppress errors from getpath.c */
44-45-if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') {
46-inspect = 1;
47- }
48-if ((p = Py_GETENV("PYTHONUNBUFFERED")) && *p != '\0') {
49-unbuffered = 1;
50- }
51-52-if (unbuffered) {
53-setbuf(stdin, (char *)NULL);
54-setbuf(stdout, (char *)NULL);
55-setbuf(stderr, (char *)NULL);
56- }
25+// Suppress errors from getpath.c
26+config.pathconfig_warnings = 0;
27+// Don't parse command line options like -E
28+config.parse_argv = 0;
572958-oldloc = _PyMem_RawStrdup(setlocale(LC_ALL, NULL));
59-if (!oldloc) {
60-fprintf(stderr, "out of memory\n");
61-goto error;
30+status = PyConfig_SetBytesArgv(&config, argc, argv);
31+if (PyStatus_Exception(status)) {
32+PyConfig_Clear(&config);
33+Py_ExitStatusException(status);
6234 }
633564-setlocale(LC_ALL, "");
65-for (i = 0; i < argc; i++) {
66-argv_copy[i] = Py_DecodeLocale(argv[i], NULL);
67-argv_copy2[i] = argv_copy[i];
68-if (!argv_copy[i]) {
69-fprintf(stderr,
70-"Unable to decode the command line argument #%i\n",
71-i + 1);
72-argc = i;
73- goto error;
74- }
36+const char *p;
37+int inspect = 0;
38+if ((p = Py_GETENV("PYTHONINSPECT")) && *p != '\0') {
39+inspect = 1;
7540 }
76-setlocale(LC_ALL, oldloc);
77-PyMem_RawFree(oldloc);
78-oldloc = NULL;
79418042#ifdef MS_WINDOWS
8143PyInitFrozenExtensions();
8244#endif /* MS_WINDOWS */
83-if (argc >= 1) {
84-status = PyConfig_SetString(&config, &config.program_name,
85-argv_copy[0]);
86-if (PyStatus_Exception(status)) {
87-PyConfig_Clear(&config);
88-Py_ExitStatusException(status);
89- }
90- }
91459246status = Py_InitializeFromConfig(&config);
9347PyConfig_Clear(&config);
@@ -104,9 +58,8 @@ Py_FrozenMain(int argc, char **argv)
10458Py_GetVersion(), Py_GetCopyright());
10559 }
10660107-PySys_SetArgv(argc, argv_copy);
108-109-n = PyImport_ImportFrozenModule("__main__");
61+int sts = 1;
62+int n = PyImport_ImportFrozenModule("__main__");
11063if (n == 0) {
11164Py_FatalError("the __main__ module is not frozen");
11265 }
@@ -128,14 +81,5 @@ Py_FrozenMain(int argc, char **argv)
12881if (Py_FinalizeEx() < 0) {
12982sts = 120;
13083 }
131-132-error:
133-PyMem_RawFree(argv_copy);
134-if (argv_copy2) {
135-for (i = 0; i < argc; i++)
136-PyMem_RawFree(argv_copy2[i]);
137-PyMem_RawFree(argv_copy2);
138- }
139-PyMem_RawFree(oldloc);
14084return sts;
14185}