Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions Lib/test/test_cmd_line_script.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,23 @@ def test_non_ascii(self):
'stdout=%r stderr=%r' % (stdout, stderr))
self.assertEqual(0, rc)

def test_skip_source_first_line(self):
script = textwrap.dedent("""\
print("First line")
print("Second line")
""")
with os_helper.temp_dir() as script_dir:
script_name = _make_test_script(script_dir, 'script', script)
exitcode, stdout, stderr = assert_python_ok(script_name)
text = stdout.decode('ascii')
self.assertIn("First line", text)
self.assertIn("Second line", text)

exitcode, stdout, stderr = assert_python_ok('-x', script_name)
text = stdout.decode('ascii')
self.assertNotIn("First line", text)
self.assertIn("Second line", text)

def test_issue20500_exit_with_exception_value(self):
script = textwrap.dedent("""\
import sys
Expand Down
16 changes: 8 additions & 8 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand All @@ -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);
Expand Down
Loading