summaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/main.c
diff options
context:
space:
mode:
authorshadchin <[email protected]>2022-04-18 12:39:32 +0300
committershadchin <[email protected]>2022-04-18 12:39:32 +0300
commitd4be68e361f4258cf0848fc70018dfe37a2acc24 (patch)
tree153e294cd97ac8b5d7a989612704a0c1f58e8ad4 /contrib/tools/python3/src/Modules/main.c
parent260c02f5ccf242d9d9b8a873afaf6588c00237d6 (diff)
IGNIETFERRO-1816 Update Python 3 from 3.9.12 to 3.10.4
ref:9f96be6d02ee8044fdd6f124b799b270c20ce641
Diffstat (limited to 'contrib/tools/python3/src/Modules/main.c')
-rw-r--r--contrib/tools/python3/src/Modules/main.c145
1 files changed, 67 insertions, 78 deletions
diff --git a/contrib/tools/python3/src/Modules/main.c b/contrib/tools/python3/src/Modules/main.c
index 1eec0f96c09..1ae2b34cac2 100644
--- a/contrib/tools/python3/src/Modules/main.c
+++ b/contrib/tools/python3/src/Modules/main.c
@@ -88,7 +88,7 @@ static inline int config_run_code(const PyConfig *config)
}
-/* Return non-zero is stdin is a TTY or if -i command line option is used */
+/* Return non-zero if stdin is a TTY or if -i command line option is used */
static int
stdin_is_interactive(const PyConfig *config)
{
@@ -224,7 +224,7 @@ pymain_import_readline(const PyConfig *config)
static int
-pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
+pymain_run_command(wchar_t *command)
{
PyObject *unicode, *bytes;
int ret;
@@ -244,7 +244,9 @@ pymain_run_command(wchar_t *command, PyCompilerFlags *cf)
goto error;
}
- ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), cf);
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ cf.cf_flags |= PyCF_IGNORE_COOKIE;
+ ret = PyRun_SimpleStringFlags(PyBytes_AsString(bytes), &cf);
Py_DECREF(bytes);
return (ret != 0);
@@ -306,29 +308,23 @@ pymain_run_module(const wchar_t *modname, int set_argv0)
static int
-pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
+pymain_run_file_obj(PyObject *program_name, PyObject *filename,
+ int skip_source_first_line)
{
- const wchar_t *filename = config->run_filename;
- if (PySys_Audit("cpython.run_file", "u", filename) < 0) {
+ if (PySys_Audit("cpython.run_file", "O", filename) < 0) {
return pymain_exit_err_print();
}
- FILE *fp = _Py_wfopen(filename, L"rb");
+
+ FILE *fp = _Py_fopen_obj(filename, "rb");
if (fp == NULL) {
- char *cfilename_buffer;
- const char *cfilename;
- int err = errno;
- cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL);
- if (cfilename_buffer != NULL)
- cfilename = cfilename_buffer;
- else
- cfilename = "<unprintable file name>";
- fprintf(stderr, "%ls: can't open file '%s': [Errno %d] %s\n",
- config->program_name, cfilename, err, strerror(err));
- PyMem_RawFree(cfilename_buffer);
+ // Ignore the OSError
+ PyErr_Clear();
+ PySys_FormatStderr("%S: can't open file %R: [Errno %d] %s\n",
+ program_name, filename, errno, strerror(errno));
return 2;
}
- if (config->skip_source_first_line) {
+ if (skip_source_first_line) {
int ch;
/* Push back first newline so line numbers remain the same */
while ((ch = getc(fp)) != EOF) {
@@ -341,104 +337,97 @@ pymain_run_file(const PyConfig *config, PyCompilerFlags *cf)
struct _Py_stat_struct sb;
if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) {
- fprintf(stderr,
- "%ls: '%ls' is a directory, cannot continue\n",
- config->program_name, filename);
+ PySys_FormatStderr("%S: %R is a directory, cannot continue\n",
+ program_name, filename);
fclose(fp);
return 1;
}
- /* call pending calls like signal handlers (SIGINT) */
+ // Call pending calls like signal handlers (SIGINT)
if (Py_MakePendingCalls() == -1) {
fclose(fp);
return pymain_exit_err_print();
}
- PyObject *unicode, *bytes = NULL;
- const char *filename_str;
+ /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ int run = _PyRun_AnyFileObject(fp, filename, 1, &cf);
+ return (run != 0);
+}
- unicode = PyUnicode_FromWideChar(filename, wcslen(filename));
- if (unicode != NULL) {
- bytes = PyUnicode_EncodeFSDefault(unicode);
- Py_DECREF(unicode);
- }
- if (bytes != NULL) {
- filename_str = PyBytes_AsString(bytes);
+static int
+pymain_run_file(const PyConfig *config)
+{
+ PyObject *filename = PyUnicode_FromWideChar(config->run_filename, -1);
+ if (filename == NULL) {
+ PyErr_Print();
+ return -1;
}
- else {
- PyErr_Clear();
- filename_str = "<filename encoding error>";
+ PyObject *program_name = PyUnicode_FromWideChar(config->program_name, -1);
+ if (program_name == NULL) {
+ Py_DECREF(filename);
+ PyErr_Print();
+ return -1;
}
- /* PyRun_AnyFileExFlags(closeit=1) calls fclose(fp) before running code */
- int run = PyRun_AnyFileExFlags(fp, filename_str, 1, cf);
- Py_XDECREF(bytes);
- return (run != 0);
+ int res = pymain_run_file_obj(program_name, filename,
+ config->skip_source_first_line);
+ Py_DECREF(filename);
+ Py_DECREF(program_name);
+ return res;
}
static int
-pymain_run_startup(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
+pymain_run_startup(PyConfig *config, int *exitcode)
{
int ret;
- PyObject *startup_obj = NULL;
if (!config->use_environment) {
return 0;
}
+ PyObject *startup = NULL;
#ifdef MS_WINDOWS
- const wchar_t *wstartup = _wgetenv(L"PYTHONSTARTUP");
- if (wstartup == NULL || wstartup[0] == L'\0') {
+ const wchar_t *env = _wgetenv(L"PYTHONSTARTUP");
+ if (env == NULL || env[0] == L'\0') {
return 0;
}
- PyObject *startup_bytes = NULL;
- startup_obj = PyUnicode_FromWideChar(wstartup, wcslen(wstartup));
- if (startup_obj == NULL) {
- goto error;
- }
- startup_bytes = PyUnicode_EncodeFSDefault(startup_obj);
- if (startup_bytes == NULL) {
+ startup = PyUnicode_FromWideChar(env, wcslen(env));
+ if (startup == NULL) {
goto error;
}
- const char *startup = PyBytes_AS_STRING(startup_bytes);
#else
- const char *startup = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
- if (startup == NULL) {
+ const char *env = _Py_GetEnv(config->use_environment, "PYTHONSTARTUP");
+ if (env == NULL) {
return 0;
}
- startup_obj = PyUnicode_DecodeFSDefault(startup);
- if (startup_obj == NULL) {
+ startup = PyUnicode_DecodeFSDefault(env);
+ if (startup == NULL) {
goto error;
}
#endif
- if (PySys_Audit("cpython.run_startup", "O", startup_obj) < 0) {
+ if (PySys_Audit("cpython.run_startup", "O", startup) < 0) {
goto error;
}
-#ifdef MS_WINDOWS
- FILE *fp = _Py_wfopen(wstartup, L"r");
-#else
- FILE *fp = _Py_fopen(startup, "r");
-#endif
+ FILE *fp = _Py_fopen_obj(startup, "r");
if (fp == NULL) {
int save_errno = errno;
PyErr_Clear();
PySys_WriteStderr("Could not open PYTHONSTARTUP\n");
errno = save_errno;
- PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup_obj, NULL);
+ PyErr_SetFromErrnoWithFilenameObjects(PyExc_OSError, startup, NULL);
goto error;
}
- (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf);
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ (void) _PyRun_SimpleFileObject(fp, startup, 0, &cf);
PyErr_Clear();
fclose(fp);
ret = 0;
done:
-#ifdef MS_WINDOWS
- Py_XDECREF(startup_bytes);
-#endif
- Py_XDECREF(startup_obj);
+ Py_XDECREF(startup);
return ret;
error:
@@ -485,14 +474,14 @@ error:
static int
-pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
+pymain_run_stdin(PyConfig *config)
{
if (stdin_is_interactive(config)) {
config->inspect = 0;
Py_InspectFlag = 0; /* do exit on SystemExit */
int exitcode;
- if (pymain_run_startup(config, cf, &exitcode)) {
+ if (pymain_run_startup(config, &exitcode)) {
return exitcode;
}
@@ -510,13 +499,14 @@ pymain_run_stdin(PyConfig *config, PyCompilerFlags *cf)
return pymain_exit_err_print();
}
- int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf);
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, &cf);
return (run != 0);
}
static void
-pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
+pymain_repl(PyConfig *config, int *exitcode)
{
/* Check this environment variable at the end, to give programs the
opportunity to set it from Python. */
@@ -535,7 +525,8 @@ pymain_repl(PyConfig *config, PyCompilerFlags *cf, int *exitcode)
return;
}
- int res = PyRun_AnyFileFlags(stdin, "<stdin>", cf);
+ PyCompilerFlags cf = _PyCompilerFlags_INIT;
+ int res = PyRun_AnyFileFlags(stdin, "<stdin>", &cf);
*exitcode = (res != 0);
}
@@ -581,13 +572,11 @@ pymain_run_python(int *exitcode)
}
}
- PyCompilerFlags cf = _PyCompilerFlags_INIT;
-
pymain_header(config);
pymain_import_readline(config);
if (config->run_command) {
- *exitcode = pymain_run_command(config->run_command, &cf);
+ *exitcode = pymain_run_command(config->run_command);
}
else if (config->run_module) {
*exitcode = pymain_run_module(config->run_module, 1);
@@ -596,13 +585,13 @@ pymain_run_python(int *exitcode)
*exitcode = pymain_run_module(L"__main__", 0);
}
else if (config->run_filename != NULL) {
- *exitcode = pymain_run_file(config, &cf);
+ *exitcode = pymain_run_file(config);
}
else {
- *exitcode = pymain_run_stdin(config, &cf);
+ *exitcode = pymain_run_stdin(config);
}
- pymain_repl(config, &cf, exitcode);
+ pymain_repl(config, exitcode);
goto done;
error: