diff --git a/Doc/using/cmdline.rst b/Doc/using/cmdline.rst index 01869d117f7cd81..70ea78a2440434a 100644 --- a/Doc/using/cmdline.rst +++ b/Doc/using/cmdline.rst @@ -388,8 +388,7 @@ Miscellaneous options Skip the first line of the source, allowing use of non-Unix forms of ``#!cmd``. This is intended for a DOS specific hack only. - .. note:: The line numbers in error messages will be off by one. - + .. deprecated:: 3.7 .. cmdoption:: -X diff --git a/Lib/test/test_cmd_line.py b/Lib/test/test_cmd_line.py index 3dbe75f0a098888..50687ab67fbb73d 100644 --- a/Lib/test/test_cmd_line.py +++ b/Lib/test/test_cmd_line.py @@ -19,6 +19,16 @@ def _kill_python_and_exit_code(p): returncode = p.wait() return data, returncode + +def run_python(args, **kw): + args = (sys.executable,) + tuple(args) + return subprocess.run(args, + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + universal_newlines=True, + **kw) + + class CmdLineTest(unittest.TestCase): def test_directories(self): assert_python_failure('.') @@ -514,12 +524,8 @@ def run_xdev(self, code, check_exitcode=True): # by default for Python compiled in debug mode env['PYTHONMALLOC'] = 'malloc' - args = (sys.executable, '-X', 'dev', '-c', code) - proc = subprocess.run(args, - stdout=subprocess.PIPE, - stderr=subprocess.STDOUT, - universal_newlines=True, - env=env) + args = ('-X', 'dev', '-c', code) + proc = run_python(args, env=env) if check_exitcode: self.assertEqual(proc.returncode, 0, proc) return proc.stdout.rstrip() @@ -547,6 +553,42 @@ def test_xdev(self): out = self.run_xdev(code) self.assertEqual(out, "True") + def test_skipfirst(self): + filename = support.TESTFN + + # Test the -x command line option + self.addCleanup(support.unlink, filename) + + lines = ['First line', 'Second line', 'Last line'] + + with open(filename, "w") as fp: + for line in lines: + print(f"print({line!r})", file=fp) + + def check(skipfirst, expected): + args = ['-E'] + if skipfirst: + args.append('-x') + args.append(filename) + + proc = run_python(args) + self.assertEqual(proc.stdout.splitlines(), expected) + self.assertEqual(proc.returncode, 0) + + check(False, lines) + check(True, lines[1:]) + + def test_skipfirst_deprecated(self): + for error in (False, True): + with self.subTest(error=error): + action = 'error' if error else 'default' + proc = run_python(('-E', '-W', action, '-x', '-c', 'pass')) + self.assertIn('The command line -x option was deprecated', + proc.stdout) + exitcode = 1 if error else 0 + self.assertEqual(proc.returncode, exitcode) + + class IgnoreEnvironmentTest(unittest.TestCase): def run_ignoring_vars(self, predicate, **env_vars): diff --git a/Misc/NEWS.d/next/Core and Builtins/2017-11-16-17-28-01.bpo-32050.hGcHYm.rst b/Misc/NEWS.d/next/Core and Builtins/2017-11-16-17-28-01.bpo-32050.hGcHYm.rst new file mode 100644 index 000000000000000..32b3260064f08e0 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2017-11-16-17-28-01.bpo-32050.hGcHYm.rst @@ -0,0 +1,2 @@ +Deprecate the command line -x option. Using the option now emits a +DeprecationWarning warning. diff --git a/Modules/main.c b/Modules/main.c index 203abf414ccb35d..a4e433d9d222e49 100644 --- a/Modules/main.c +++ b/Modules/main.c @@ -1491,6 +1491,16 @@ pymain_run_python(_PyMain *pymain) pymain_header(pymain); pymain_import_readline(pymain); + if (cmdline->skip_first_line) { + if (PyErr_WarnEx(PyExc_DeprecationWarning, + "The command line -x option was deprecated " + "in Python 3.7", 1)) { + PyErr_Print(); + pymain->status = 1; + return; + } + } + if (cmdline->filename != NULL) { pymain->main_importer_path = pymain_get_importer(cmdline->filename); }