Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
156 changes: 71 additions & 85 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to call PyErr_Clear() after PyErr_Print().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code is 11 years old, I didn't touch it. Please open a new issue if you want to fix it.

See commit e69a08e.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has been fixed in 8b58468.

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
Expand All @@ -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);
Expand All @@ -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);
}


Expand Down Expand Up @@ -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);
Expand Down