From 177bf5ea28953dc7b3e8cbece0500ae7ace39e36 Mon Sep 17 00:00:00 2001 From: Nybblista <170842536+nybblista@users.noreply.github.com> Date: Sun, 14 Dec 2025 11:36:55 +0000 Subject: [PATCH] fix #142570: check file type before skipping the source first line --- Modules/main.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/Modules/main.c b/Modules/main.c index 74e48c94732565..6a8551ac23aa8f 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -380,6 +380,14 @@ pymain_run_file_obj(PyObject *program_name, PyObject *filename, return 2; } + struct _Py_stat_struct sb; + if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { + PySys_FormatStderr("%S: %R is a directory, cannot continue\n", + program_name, filename); + fclose(fp); + return 1; + } + if (skip_source_first_line) { int ch; /* Push back first newline so line numbers remain the same */ @@ -391,14 +399,6 @@ pymain_run_file_obj(PyObject *program_name, PyObject *filename, } } - struct _Py_stat_struct sb; - if (_Py_fstat_noraise(fileno(fp), &sb) == 0 && S_ISDIR(sb.st_mode)) { - PySys_FormatStderr("%S: %R is a directory, cannot continue\n", - program_name, filename); - fclose(fp); - return 1; - } - // Call pending calls like signal handlers (SIGINT) if (Py_MakePendingCalls() == -1) { fclose(fp);