bpo-34170: Cleanup pymain_run_filename() (GH-8631)
https://github.com/python/cpython/commit/72ec3193b5118a2ccc8be8bf03d7b74691c... commit: 72ec3193b5118a2ccc8be8bf03d7b74691c6a264 branch: master author: Victor Stinner <vstinner@redhat.com> committer: GitHub <noreply@github.com> date: 2018-08-02T19:34:20+02:00 summary: bpo-34170: Cleanup pymain_run_filename() (GH-8631) * Inline pymain_run_file() and pymain_open_filename() into pymain_run_filename() * Created pymain_run_stdin() which is pymain_run_filename() with filename=NULL * Rename pymain_run_filename() to pymain_run_file() files: M Modules/main.c diff --git a/Modules/main.c b/Modules/main.c index f99ff86b749d..06aa1124c796 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -300,43 +300,6 @@ pymain_run_command(wchar_t *command, PyCompilerFlags *cf) } -static int -pymain_run_file(FILE *fp, const wchar_t *filename, PyCompilerFlags *p_cf) -{ - PyObject *unicode, *bytes = NULL; - const char *filename_str; - int run; - - /* call pending calls like signal handlers (SIGINT) */ - if (Py_MakePendingCalls() == -1) { - PyErr_Print(); - return 1; - } - - if (filename) { - unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); - if (unicode != NULL) { - bytes = PyUnicode_EncodeFSDefault(unicode); - Py_DECREF(unicode); - } - if (bytes != NULL) { - filename_str = PyBytes_AsString(bytes); - } - else { - PyErr_Clear(); - filename_str = "<encoding error>"; - } - } - else { - filename_str = "<stdin>"; - } - - run = PyRun_AnyFileExFlags(fp, filename_str, filename != NULL, p_cf); - Py_XDECREF(bytes); - return run != 0; -} - - /* Main program */ typedef struct { @@ -1101,17 +1064,43 @@ pymain_import_readline(_PyMain *pymain) } -static FILE* -pymain_open_filename(_PyMain *pymain, _PyCoreConfig *config) +static void +pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) { - FILE* fp; + const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP"); + if (startup == NULL) { + return; + } - fp = _Py_wfopen(pymain->filename, L"r"); + FILE *fp = _Py_fopen(startup, "r"); + if (fp == NULL) { + int save_errno = errno; + PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); + errno = save_errno; + + PyErr_SetFromErrnoWithFilename(PyExc_OSError, + startup); + PyErr_Print(); + PyErr_Clear(); + return; + } + + (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); + PyErr_Clear(); + fclose(fp); +} + + +static void +pymain_run_file(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) +{ + const wchar_t *filename = pymain->filename; + FILE *fp = _Py_wfopen(filename, L"r"); if (fp == NULL) { char *cfilename_buffer; const char *cfilename; int err = errno; - cfilename_buffer = _Py_EncodeLocaleRaw(pymain->filename, NULL); + cfilename_buffer = _Py_EncodeLocaleRaw(filename, NULL); if (cfilename_buffer != NULL) cfilename = cfilename_buffer; else @@ -1120,13 +1109,12 @@ pymain_open_filename(_PyMain *pymain, _PyCoreConfig *config) config->program, cfilename, err, strerror(err)); PyMem_RawFree(cfilename_buffer); pymain->status = 2; - return NULL; + return; } if (pymain->skip_first_line) { int ch; - /* Push back first newline so line numbers - remain the same */ + /* Push back first newline so line numbers remain the same */ while ((ch = getc(fp)) != EOF) { if (ch == '\n') { (void)ungetc(ch, fp); @@ -1136,70 +1124,65 @@ pymain_open_filename(_PyMain *pymain, _PyCoreConfig *config) } struct _Py_stat_struct sb; - if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && - S_ISDIR(sb.st_mode)) { + 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, pymain->filename); - fclose(fp); + config->program, filename); pymain->status = 1; - return NULL; + goto done; } - return fp; -} - - -static void -pymain_run_startup(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) -{ - const char *startup = _PyCoreConfig_GetEnv(config, "PYTHONSTARTUP"); - if (startup == NULL) { - return; + /* call pending calls like signal handlers (SIGINT) */ + if (Py_MakePendingCalls() == -1) { + PyErr_Print(); + pymain->status = 1; + goto done; } - FILE *fp = _Py_fopen(startup, "r"); - if (fp == NULL) { - int save_errno = errno; - PySys_WriteStderr("Could not open PYTHONSTARTUP\n"); - errno = save_errno; + PyObject *unicode, *bytes = NULL; + const char *filename_str; - PyErr_SetFromErrnoWithFilename(PyExc_OSError, - startup); - PyErr_Print(); + unicode = PyUnicode_FromWideChar(filename, wcslen(filename)); + if (unicode != NULL) { + bytes = PyUnicode_EncodeFSDefault(unicode); + Py_DECREF(unicode); + } + if (bytes != NULL) { + filename_str = PyBytes_AsString(bytes); + } + else { PyErr_Clear(); - return; + filename_str = "<filename encoding error>"; } - (void) PyRun_SimpleFileExFlags(fp, startup, 0, cf); - PyErr_Clear(); + int run = PyRun_AnyFileExFlags(fp, filename_str, 0, cf); + Py_XDECREF(bytes); + pymain->status = (run != 0); + +done: fclose(fp); } static void -pymain_run_filename(_PyMain *pymain, _PyCoreConfig *config, - PyCompilerFlags *cf) +pymain_run_stdin(_PyMain *pymain, _PyCoreConfig *config, PyCompilerFlags *cf) { - if (pymain->filename == NULL && pymain->stdin_is_interactive) { + if (pymain->stdin_is_interactive) { Py_InspectFlag = 0; /* do exit on SystemExit */ config->inspect = 0; pymain_run_startup(pymain, config, cf); pymain_run_interactive_hook(); } - FILE *fp; - if (pymain->filename != NULL) { - fp = pymain_open_filename(pymain, config); - if (fp == NULL) { - return; - } - } - else { - fp = stdin; + /* call pending calls like signal handlers (SIGINT) */ + if (Py_MakePendingCalls() == -1) { + PyErr_Print(); + pymain->status = 1; + return; } - pymain->status = pymain_run_file(fp, pymain->filename, cf); + int run = PyRun_AnyFileExFlags(stdin, "<stdin>", 0, cf); + pymain->status = (run != 0); } @@ -1618,8 +1601,11 @@ pymain_run_python(_PyMain *pymain, PyInterpreterState *interp) int sts = pymain_run_module(L"__main__", 0); pymain->status = (sts != 0); } + else if (pymain->filename != NULL) { + pymain_run_file(pymain, config, &cf); + } else { - pymain_run_filename(pymain, config, &cf); + pymain_run_stdin(pymain, config, &cf); } pymain_repl(pymain, config, &cf);
participants (1)
-
Victor Stinner