Skip to content
Closed
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
3 changes: 1 addition & 2 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
54 changes: 48 additions & 6 deletions Lib/test/test_cmd_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -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('.')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Deprecate the command line -x option. Using the option now emits a
DeprecationWarning warning.
10 changes: 10 additions & 0 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down