blob: 9ae882fdde3cf7d361a5d2850d38349083eb8faa [file] [log] [blame]
Christian Heimes9cd17752007-11-18 19:35:23 +00001# Tests invocation of the interpreter with various command line arguments
Nick Coghland26c18a2010-08-17 13:06:11 +00002# Most tests are executed with environment variables ignored
Christian Heimes9cd17752007-11-18 19:35:23 +00003# See test_cmd_line_script.py for testing of script execution
Neal Norwitz11bd1192005-10-03 00:54:56 +00004
Benjamin Petersonee8712c2008-05-20 21:35:26 +00005import test.support, unittest
Antoine Pitrou87695762008-08-14 22:44:29 +00006import os
Neal Norwitz11bd1192005-10-03 00:54:56 +00007import sys
Nick Coghlan260bd3e2009-11-16 06:49:25 +00008import subprocess
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +00009import tempfile
Ezio Melotti7c663192012-11-18 13:55:52 +020010from test.script_helper import (spawn_python, kill_python, assert_python_ok,
11 assert_python_failure)
Thomas Woutersed03b412007-08-28 21:37:11 +000012
Trent Nelson39e307e2008-03-19 06:45:48 +000013
Nick Coghlan260bd3e2009-11-16 06:49:25 +000014# XXX (ncoghlan): Move to script_helper and make consistent with run_python
Trent Nelson39e307e2008-03-19 06:45:48 +000015def _kill_python_and_exit_code(p):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000016 data = kill_python(p)
Trent Nelson39e307e2008-03-19 06:45:48 +000017 returncode = p.wait()
18 return data, returncode
Thomas Woutersed03b412007-08-28 21:37:11 +000019
Neal Norwitz11bd1192005-10-03 00:54:56 +000020class CmdLineTest(unittest.TestCase):
Neal Norwitz11bd1192005-10-03 00:54:56 +000021 def test_directories(self):
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000022 assert_python_failure('.')
23 assert_python_failure('< .')
Neal Norwitz11bd1192005-10-03 00:54:56 +000024
25 def verify_valid_flag(self, cmd_line):
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000026 rc, out, err = assert_python_ok(*cmd_line)
27 self.assertTrue(out == b'' or out.endswith(b'\n'))
28 self.assertNotIn(b'Traceback', out)
29 self.assertNotIn(b'Traceback', err)
Neal Norwitz11bd1192005-10-03 00:54:56 +000030
Neal Norwitz11bd1192005-10-03 00:54:56 +000031 def test_optimize(self):
32 self.verify_valid_flag('-O')
33 self.verify_valid_flag('-OO')
34
Neal Norwitz11bd1192005-10-03 00:54:56 +000035 def test_site_flag(self):
36 self.verify_valid_flag('-S')
37
38 def test_usage(self):
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000039 rc, out, err = assert_python_ok('-h')
40 self.assertIn(b'usage', out)
Neal Norwitz11bd1192005-10-03 00:54:56 +000041
42 def test_version(self):
Guido van Rossuma1c42a92007-08-29 03:47:36 +000043 version = ('Python %d.%d' % sys.version_info[:2]).encode("ascii")
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000044 rc, out, err = assert_python_ok('-V')
45 self.assertTrue(err.startswith(version))
Neal Norwitz11bd1192005-10-03 00:54:56 +000046
Trent Nelson39e307e2008-03-19 06:45:48 +000047 def test_verbose(self):
48 # -v causes imports to write to stderr. If the write to
49 # stderr itself causes an import to happen (for the output
50 # codec), a recursion loop can occur.
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000051 rc, out, err = assert_python_ok('-v')
52 self.assertNotIn(b'stack overflow', err)
53 rc, out, err = assert_python_ok('-vv')
54 self.assertNotIn(b'stack overflow', err)
Trent Nelson39e307e2008-03-19 06:45:48 +000055
Antoine Pitrou9583cac2010-10-21 13:42:28 +000056 def test_xoptions(self):
57 rc, out, err = assert_python_ok('-c', 'import sys; print(sys._xoptions)')
58 opts = eval(out.splitlines()[0])
59 self.assertEqual(opts, {})
60 rc, out, err = assert_python_ok(
61 '-Xa', '-Xb=c,d=e', '-c', 'import sys; print(sys._xoptions)')
62 opts = eval(out.splitlines()[0])
63 self.assertEqual(opts, {'a': True, 'b': 'c,d=e'})
64
Ezio Melotti1f8898a2013-03-26 01:59:56 +020065 def test_showrefcount(self):
66 def run_python(*args):
67 # this is similar to assert_python_ok but doesn't strip
68 # the refcount from stderr. It can be replaced once
69 # assert_python_ok stops doing that.
70 cmd = [sys.executable]
71 cmd.extend(args)
72 PIPE = subprocess.PIPE
73 p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
74 out, err = p.communicate()
75 p.stdout.close()
76 p.stderr.close()
77 rc = p.returncode
78 self.assertEqual(rc, 0)
79 return rc, out, err
80 code = 'import sys; print(sys._xoptions)'
81 # normally the refcount is hidden
82 rc, out, err = run_python('-c', code)
83 self.assertEqual(out.rstrip(), b'{}')
84 self.assertEqual(err, b'')
85 # "-X showrefcount" shows the refcount, but only in debug builds
86 rc, out, err = run_python('-X', 'showrefcount', '-c', code)
87 self.assertEqual(out.rstrip(), b"{'showrefcount': True}")
88 if hasattr(sys, 'gettotalrefcount'): # debug build
89 self.assertRegex(err, br'^\[\d+ refs, \d+ blocks\]')
90 else:
91 self.assertEqual(err, b'')
92
Thomas Wouters477c8d52006-05-27 19:21:47 +000093 def test_run_module(self):
94 # Test expected operation of the '-m' switch
95 # Switch needs an argument
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000096 assert_python_failure('-m')
Thomas Wouters477c8d52006-05-27 19:21:47 +000097 # Check we get an error for a nonexistent module
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000098 assert_python_failure('-m', 'fnord43520xyz')
Thomas Wouters477c8d52006-05-27 19:21:47 +000099 # Check the runpy module also gives an error for
100 # a nonexistent module
Victor Stinner3fa1aae2013-03-26 01:14:08 +0100101 assert_python_failure('-m', 'runpy', 'fnord43520xyz')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 # All good if module is located and run successfully
Victor Stinner3fa1aae2013-03-26 01:14:08 +0100103 assert_python_ok('-m', 'timeit', '-n', '1')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104
Thomas Woutersed03b412007-08-28 21:37:11 +0000105 def test_run_module_bug1764407(self):
106 # -m and -i need to play well together
107 # Runs the timeit module and checks the __main__
108 # namespace has been populated appropriately
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000109 p = spawn_python('-i', '-m', 'timeit', '-n', '1')
Guido van Rossuma1c42a92007-08-29 03:47:36 +0000110 p.stdin.write(b'Timer\n')
111 p.stdin.write(b'exit()\n')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000112 data = kill_python(p)
Thomas Woutersed03b412007-08-28 21:37:11 +0000113 self.assertTrue(data.find(b'1 loop') != -1)
114 self.assertTrue(data.find(b'__main__.Timer') != -1)
115
Thomas Wouters477c8d52006-05-27 19:21:47 +0000116 def test_run_code(self):
117 # Test expected operation of the '-c' switch
118 # Switch needs an argument
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000119 assert_python_failure('-c')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000120 # Check we get an error for an uncaught exception
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000121 assert_python_failure('-c', 'raise Exception')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000122 # All good if execution is successful
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000123 assert_python_ok('-c', 'pass')
Thomas Wouters477c8d52006-05-27 19:21:47 +0000124
Victor Stinner8b219b22012-11-06 23:23:43 +0100125 @unittest.skipUnless(test.support.FS_NONASCII, 'need support.FS_NONASCII')
Victor Stinner073f7592010-10-20 21:56:55 +0000126 def test_non_ascii(self):
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000127 # Test handling of non-ascii data
Victor Stinner8b219b22012-11-06 23:23:43 +0100128 command = ("assert(ord(%r) == %s)"
129 % (test.support.FS_NONASCII, ord(test.support.FS_NONASCII)))
Victor Stinner073f7592010-10-20 21:56:55 +0000130 assert_python_ok('-c', command)
Amaury Forgeot d'Arc9a5499b2008-11-11 23:04:59 +0000131
Victor Stinnerf6211ed2010-10-20 21:52:33 +0000132 # On Windows, pass bytes to subprocess doesn't test how Python decodes the
133 # command line, but how subprocess does decode bytes to unicode. Python
134 # doesn't decode the command line because Windows provides directly the
135 # arguments as unicode (using wmain() instead of main()).
136 @unittest.skipIf(sys.platform == 'win32',
137 'Windows has a native unicode API')
138 def test_undecodable_code(self):
139 undecodable = b"\xff"
140 env = os.environ.copy()
141 # Use C locale to get ascii for the locale encoding
142 env['LC_ALL'] = 'C'
143 code = (
144 b'import locale; '
145 b'print(ascii("' + undecodable + b'"), '
146 b'locale.getpreferredencoding())')
147 p = subprocess.Popen(
148 [sys.executable, "-c", code],
149 stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
150 env=env)
151 stdout, stderr = p.communicate()
152 if p.returncode == 1:
153 # _Py_char2wchar() decoded b'\xff' as '\udcff' (b'\xff' is not
154 # decodable from ASCII) and run_command() failed on
155 # PyUnicode_AsUTF8String(). This is the expected behaviour on
156 # Linux.
157 pattern = b"Unable to decode the command from the command line:"
158 elif p.returncode == 0:
159 # _Py_char2wchar() decoded b'\xff' as '\xff' even if the locale is
160 # C and the locale encoding is ASCII. It occurs on FreeBSD, Solaris
161 # and Mac OS X.
162 pattern = b"'\\xff' "
163 # The output is followed by the encoding name, an alias to ASCII.
164 # Examples: "US-ASCII" or "646" (ISO 646, on Solaris).
165 else:
166 raise AssertionError("Unknown exit code: %s, output=%a" % (p.returncode, stdout))
167 if not stdout.startswith(pattern):
168 raise AssertionError("%a doesn't start with %a" % (stdout, pattern))
169
Victor Stinnerf933e1a2010-10-20 22:58:25 +0000170 @unittest.skipUnless(sys.platform == 'darwin', 'test specific to Mac OS X')
171 def test_osx_utf8(self):
172 def check_output(text):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000173 decoded = text.decode('utf-8', 'surrogateescape')
Victor Stinnerf933e1a2010-10-20 22:58:25 +0000174 expected = ascii(decoded).encode('ascii') + b'\n'
175
176 env = os.environ.copy()
177 # C locale gives ASCII locale encoding, but Python uses UTF-8
178 # to parse the command line arguments on Mac OS X
179 env['LC_ALL'] = 'C'
180
181 p = subprocess.Popen(
182 (sys.executable, "-c", "import sys; print(ascii(sys.argv[1]))", text),
183 stdout=subprocess.PIPE,
184 env=env)
185 stdout, stderr = p.communicate()
186 self.assertEqual(stdout, expected)
187 self.assertEqual(p.returncode, 0)
188
189 # test valid utf-8
190 text = 'e:\xe9, euro:\u20ac, non-bmp:\U0010ffff'.encode('utf-8')
191 check_output(text)
192
193 # test invalid utf-8
194 text = (
195 b'\xff' # invalid byte
196 b'\xc3\xa9' # valid utf-8 character
197 b'\xc3\xff' # invalid byte sequence
198 b'\xed\xa0\x80' # lone surrogate character (invalid)
199 )
200 check_output(text)
201
Antoine Pitrou05608432009-01-09 18:53:14 +0000202 def test_unbuffered_output(self):
203 # Test expected operation of the '-u' switch
204 for stream in ('stdout', 'stderr'):
205 # Binary is unbuffered
206 code = ("import os, sys; sys.%s.buffer.write(b'x'); os._exit(0)"
207 % stream)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000208 rc, out, err = assert_python_ok('-u', '-c', code)
209 data = err if stream == 'stderr' else out
Antoine Pitrou05608432009-01-09 18:53:14 +0000210 self.assertEqual(data, b'x', "binary %s not unbuffered" % stream)
211 # Text is line-buffered
212 code = ("import os, sys; sys.%s.write('x\\n'); os._exit(0)"
213 % stream)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000214 rc, out, err = assert_python_ok('-u', '-c', code)
215 data = err if stream == 'stderr' else out
Antoine Pitrou05608432009-01-09 18:53:14 +0000216 self.assertEqual(data.strip(), b'x',
217 "text %s not line-buffered" % stream)
218
Antoine Pitrou27fe9fc2009-01-26 21:48:00 +0000219 def test_unbuffered_input(self):
220 # sys.stdin still works with '-u'
221 code = ("import sys; sys.stdout.write(sys.stdin.read(1))")
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000222 p = spawn_python('-u', '-c', code)
Antoine Pitrou27fe9fc2009-01-26 21:48:00 +0000223 p.stdin.write(b'x')
224 p.stdin.flush()
225 data, rc = _kill_python_and_exit_code(p)
226 self.assertEqual(rc, 0)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000227 self.assertTrue(data.startswith(b'x'), data)
Antoine Pitrou27fe9fc2009-01-26 21:48:00 +0000228
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000229 def test_large_PYTHONPATH(self):
Antoine Pitrou9bc35682010-11-09 21:33:55 +0000230 path1 = "ABCDE" * 100
231 path2 = "FGHIJ" * 100
232 path = path1 + os.pathsep + path2
Victor Stinner76cf6872010-04-16 15:10:27 +0000233
Antoine Pitrou9bc35682010-11-09 21:33:55 +0000234 code = """if 1:
235 import sys
236 path = ":".join(sys.path)
237 path = path.encode("ascii", "backslashreplace")
238 sys.stdout.buffer.write(path)"""
239 rc, out, err = assert_python_ok('-S', '-c', code,
240 PYTHONPATH=path)
241 self.assertIn(path1.encode('ascii'), out)
242 self.assertIn(path2.encode('ascii'), out)
Amaury Forgeot d'Arc66f8c432009-06-09 21:30:01 +0000243
Andrew Svetlov69032c82012-11-03 13:52:58 +0200244 def test_empty_PYTHONPATH_issue16309(self):
Ezio Melotti32f4e6e2012-11-23 01:51:20 +0200245 # On Posix, it is documented that setting PATH to the
246 # empty string is equivalent to not setting PATH at all,
247 # which is an exception to the rule that in a string like
248 # "/bin::/usr/bin" the empty string in the middle gets
249 # interpreted as '.'
Andrew Svetlov69032c82012-11-03 13:52:58 +0200250 code = """if 1:
251 import sys
252 path = ":".join(sys.path)
253 path = path.encode("ascii", "backslashreplace")
254 sys.stdout.buffer.write(path)"""
255 rc1, out1, err1 = assert_python_ok('-c', code, PYTHONPATH="")
256 rc2, out2, err2 = assert_python_ok('-c', code)
257 # regarding to Posix specification, outputs should be equal
258 # for empty and unset PYTHONPATH
Ezio Melotti32f4e6e2012-11-23 01:51:20 +0200259 self.assertEqual(out1, out2)
Andrew Svetlov69032c82012-11-03 13:52:58 +0200260
Victor Stinner13d49ee2010-12-04 17:24:33 +0000261 def test_displayhook_unencodable(self):
Marc-André Lemburg8f36af72011-02-25 15:42:01 +0000262 for encoding in ('ascii', 'latin-1', 'utf-8'):
Victor Stinner13d49ee2010-12-04 17:24:33 +0000263 env = os.environ.copy()
264 env['PYTHONIOENCODING'] = encoding
265 p = subprocess.Popen(
266 [sys.executable, '-i'],
267 stdin=subprocess.PIPE,
268 stdout=subprocess.PIPE,
269 stderr=subprocess.STDOUT,
270 env=env)
271 # non-ascii, surrogate, non-BMP printable, non-BMP unprintable
272 text = "a=\xe9 b=\uDC80 c=\U00010000 d=\U0010FFFF"
273 p.stdin.write(ascii(text).encode('ascii') + b"\n")
274 p.stdin.write(b'exit()\n')
275 data = kill_python(p)
276 escaped = repr(text).encode(encoding, 'backslashreplace')
277 self.assertIn(escaped, data)
278
Victor Stinnerc0f1a1a2011-02-23 12:07:37 +0000279 def check_input(self, code, expected):
280 with tempfile.NamedTemporaryFile("wb+") as stdin:
281 sep = os.linesep.encode('ASCII')
282 stdin.write(sep.join((b'abc', b'def')))
283 stdin.flush()
284 stdin.seek(0)
285 with subprocess.Popen(
286 (sys.executable, "-c", code),
287 stdin=stdin, stdout=subprocess.PIPE) as proc:
288 stdout, stderr = proc.communicate()
289 self.assertEqual(stdout.rstrip(), expected)
290
291 def test_stdin_readline(self):
292 # Issue #11272: check that sys.stdin.readline() replaces '\r\n' by '\n'
293 # on Windows (sys.stdin is opened in binary mode)
294 self.check_input(
295 "import sys; print(repr(sys.stdin.readline()))",
296 b"'abc\\n'")
297
298 def test_builtin_input(self):
299 # Issue #11272: check that input() strips newlines ('\n' or '\r\n')
300 self.check_input(
301 "print(repr(input()))",
302 b"'abc'")
303
Victor Stinner7b3f0fa2012-08-04 01:28:00 +0200304 def test_output_newline(self):
305 # Issue 13119 Newline for print() should be \r\n on Windows.
306 code = """if 1:
307 import sys
308 print(1)
309 print(2)
310 print(3, file=sys.stderr)
311 print(4, file=sys.stderr)"""
312 rc, out, err = assert_python_ok('-c', code)
313
314 if sys.platform == 'win32':
315 self.assertEqual(b'1\r\n2\r\n', out)
316 self.assertEqual(b'3\r\n4', err)
317 else:
318 self.assertEqual(b'1\n2\n', out)
319 self.assertEqual(b'3\n4', err)
320
R David Murraye697e372011-06-24 13:26:31 -0400321 def test_unmached_quote(self):
322 # Issue #10206: python program starting with unmatched quote
323 # spewed spaces to stdout
324 rc, out, err = assert_python_failure('-c', "'")
325 self.assertRegex(err.decode('ascii', 'ignore'), 'SyntaxError')
326 self.assertEqual(b'', out)
327
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100328 def test_stdout_flush_at_shutdown(self):
329 # Issue #5319: if stdout.flush() fails at shutdown, an error should
330 # be printed out.
331 code = """if 1:
332 import os, sys
333 sys.stdout.write('x')
334 os.close(sys.stdout.fileno())"""
335 rc, out, err = assert_python_ok('-c', code)
336 self.assertEqual(b'', out)
337 self.assertRegex(err.decode('ascii', 'ignore'),
Andrew Svetlov76bcff22012-11-03 15:56:05 +0200338 'Exception ignored in.*\nOSError: .*')
Antoine Pitroud7c8fbf2011-11-26 21:59:36 +0100339
340 def test_closed_stdout(self):
341 # Issue #13444: if stdout has been explicitly closed, we should
342 # not attempt to flush it at shutdown.
343 code = "import sys; sys.stdout.close()"
344 rc, out, err = assert_python_ok('-c', code)
345 self.assertEqual(b'', err)
346
Antoine Pitrou11942a52011-11-28 19:08:36 +0100347 # Issue #7111: Python should work without standard streams
348
349 @unittest.skipIf(os.name != 'posix', "test needs POSIX semantics")
350 def _test_no_stdio(self, streams):
351 code = """if 1:
352 import os, sys
353 for i, s in enumerate({streams}):
354 if getattr(sys, s) is not None:
355 os._exit(i + 1)
356 os._exit(42)""".format(streams=streams)
357 def preexec():
358 if 'stdin' in streams:
359 os.close(0)
360 if 'stdout' in streams:
361 os.close(1)
362 if 'stderr' in streams:
363 os.close(2)
364 p = subprocess.Popen(
365 [sys.executable, "-E", "-c", code],
366 stdin=subprocess.PIPE,
367 stdout=subprocess.PIPE,
368 stderr=subprocess.PIPE,
369 preexec_fn=preexec)
370 out, err = p.communicate()
371 self.assertEqual(test.support.strip_python_stderr(err), b'')
372 self.assertEqual(p.returncode, 42)
373
374 def test_no_stdin(self):
375 self._test_no_stdio(['stdin'])
376
377 def test_no_stdout(self):
378 self._test_no_stdio(['stdout'])
379
380 def test_no_stderr(self):
381 self._test_no_stdio(['stderr'])
382
383 def test_no_std_streams(self):
384 self._test_no_stdio(['stdin', 'stdout', 'stderr'])
385
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100386 def test_hash_randomization(self):
387 # Verify that -R enables hash randomization:
388 self.verify_valid_flag('-R')
389 hashes = []
390 for i in range(2):
391 code = 'print(hash("spam"))'
Benjamin Petersonc9f54cf2012-02-21 16:08:05 -0500392 rc, out, err = assert_python_ok('-c', code)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100393 self.assertEqual(rc, 0)
Georg Brandl09a7c722012-02-20 21:31:46 +0100394 hashes.append(out)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100395 self.assertNotEqual(hashes[0], hashes[1])
396
397 # Verify that sys.flags contains hash_randomization
398 code = 'import sys; print("random is", sys.flags.hash_randomization)'
Benjamin Petersonc9f54cf2012-02-21 16:08:05 -0500399 rc, out, err = assert_python_ok('-c', code)
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100400 self.assertEqual(rc, 0)
Georg Brandl09a7c722012-02-20 21:31:46 +0100401 self.assertIn(b'random is 1', out)
Thomas Wouters477c8d52006-05-27 19:21:47 +0000402
Hynek Schlawack5c6b3e22012-11-07 09:02:24 +0100403 def test_del___main__(self):
404 # Issue #15001: PyRun_SimpleFileExFlags() did crash because it kept a
405 # borrowed reference to the dict of __main__ module and later modify
406 # the dict whereas the module was destroyed
407 filename = test.support.TESTFN
408 self.addCleanup(test.support.unlink, filename)
409 with open(filename, "w") as script:
410 print("import sys", file=script)
411 print("del sys.modules['__main__']", file=script)
412 assert_python_ok(filename)
413
Ezio Melotti7c663192012-11-18 13:55:52 +0200414 def test_unknown_options(self):
Ezio Melottia0dd22e2012-11-23 18:48:32 +0200415 rc, out, err = assert_python_failure('-E', '-z')
416 self.assertIn(b'Unknown option: -z', err)
Ezio Melotti7c663192012-11-18 13:55:52 +0200417 self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
418 self.assertEqual(b'', out)
Ezio Melottia0dd22e2012-11-23 18:48:32 +0200419 # Add "without='-E'" to prevent _assert_python to append -E
420 # to env_vars and change the output of stderr
421 rc, out, err = assert_python_failure('-z', without='-E')
422 self.assertIn(b'Unknown option: -z', err)
423 self.assertEqual(err.splitlines().count(b'Unknown option: -z'), 1)
424 self.assertEqual(b'', out)
425 rc, out, err = assert_python_failure('-a', '-z', without='-E')
426 self.assertIn(b'Unknown option: -a', err)
427 # only the first unknown option is reported
428 self.assertNotIn(b'Unknown option: -z', err)
429 self.assertEqual(err.splitlines().count(b'Unknown option: -a'), 1)
430 self.assertEqual(b'', out)
431
Ezio Melotti7c663192012-11-18 13:55:52 +0200432
Neal Norwitz11bd1192005-10-03 00:54:56 +0000433def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000434 test.support.run_unittest(CmdLineTest)
435 test.support.reap_children()
Neal Norwitz11bd1192005-10-03 00:54:56 +0000436
437if __name__ == "__main__":
438 test_main()