blob: 6051e18eddc9607f1d9d746cf29573fccf747908 [file] [log] [blame]
Barry Warsaw28a691b2010-04-17 00:19:56 +00001# tests command line execution of scripts
Christian Heimes9cd17752007-11-18 19:35:23 +00002
Brett Cannone0d88a12012-04-25 20:54:04 -04003import importlib
Nick Coghlan85e729e2012-07-15 18:09:52 +10004import importlib.machinery
5import zipimport
Christian Heimes9cd17752007-11-18 19:35:23 +00006import unittest
Georg Brandlf5247e32010-10-14 08:08:56 +00007import sys
Christian Heimes9cd17752007-11-18 19:35:23 +00008import os
9import os.path
Barry Warsaw28a691b2010-04-17 00:19:56 +000010import py_compile
11
Nick Coghlan1d5ccdb2012-05-21 23:03:30 +100012import textwrap
Nick Coghland26c18a2010-08-17 13:06:11 +000013from test import support
Barry Warsaw28a691b2010-04-17 00:19:56 +000014from test.script_helper import (
Antoine Pitrouf51d8d32010-10-08 18:05:42 +000015 make_pkg, make_script, make_zip_pkg, make_zip_script,
Nick Coghlan85e729e2012-07-15 18:09:52 +100016 assert_python_ok, assert_python_failure, temp_dir,
17 spawn_python, kill_python)
Christian Heimes9cd17752007-11-18 19:35:23 +000018
Nick Coghland26c18a2010-08-17 13:06:11 +000019verbose = support.verbose
Christian Heimes9cd17752007-11-18 19:35:23 +000020
Nick Coghlan37fc4012012-04-22 17:11:33 +100021example_args = ['test1', 'test2', 'test3']
22
Christian Heimescbf3b5c2007-12-03 21:02:03 +000023test_source = """\
Christian Heimes9cd17752007-11-18 19:35:23 +000024# Script may be run with optimisation enabled, so don't rely on assert
25# statements being executed
26def assertEqual(lhs, rhs):
27 if lhs != rhs:
Christian Heimescbf3b5c2007-12-03 21:02:03 +000028 raise AssertionError('%r != %r' % (lhs, rhs))
Christian Heimes9cd17752007-11-18 19:35:23 +000029def assertIdentical(lhs, rhs):
30 if lhs is not rhs:
Christian Heimescbf3b5c2007-12-03 21:02:03 +000031 raise AssertionError('%r is not %r' % (lhs, rhs))
Christian Heimes9cd17752007-11-18 19:35:23 +000032# Check basic code execution
33result = ['Top level assignment']
34def f():
35 result.append('Lower level reference')
36f()
37assertEqual(result, ['Top level assignment', 'Lower level reference'])
38# Check population of magic variables
39assertEqual(__name__, '__main__')
Nick Coghlan3f94cbf2012-07-15 19:10:39 +100040from importlib.machinery import BuiltinImporter
41_loader = __loader__ if __loader__ is BuiltinImporter else type(__loader__)
Nick Coghlan85e729e2012-07-15 18:09:52 +100042print('__loader__==%a' % _loader)
Victor Stinnere3874ed2010-10-17 01:41:09 +000043print('__file__==%a' % __file__)
Barry Warsaw28a691b2010-04-17 00:19:56 +000044assertEqual(__cached__, None)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000045print('__package__==%r' % __package__)
Christian Heimes9cd17752007-11-18 19:35:23 +000046# Check the sys module
47import sys
48assertIdentical(globals(), sys.modules[__name__].__dict__)
Nick Coghlan37fc4012012-04-22 17:11:33 +100049from test import test_cmd_line_script
50example_args_list = test_cmd_line_script.example_args
51assertEqual(sys.argv[1:], example_args_list)
Victor Stinnere3874ed2010-10-17 01:41:09 +000052print('sys.argv[0]==%a' % sys.argv[0])
53print('sys.path[0]==%a' % sys.path[0])
Nick Coghland26c18a2010-08-17 13:06:11 +000054# Check the working directory
55import os
Victor Stinnere3874ed2010-10-17 01:41:09 +000056print('cwd==%a' % os.getcwd())
Christian Heimescbf3b5c2007-12-03 21:02:03 +000057"""
Christian Heimes9cd17752007-11-18 19:35:23 +000058
Christian Heimescbf3b5c2007-12-03 21:02:03 +000059def _make_test_script(script_dir, script_basename, source=test_source):
Brett Cannone0d88a12012-04-25 20:54:04 -040060 to_return = make_script(script_dir, script_basename, source)
61 importlib.invalidate_caches()
62 return to_return
Christian Heimescbf3b5c2007-12-03 21:02:03 +000063
Nick Coghlanf088e5e2008-12-14 11:50:48 +000064def _make_test_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
65 source=test_source, depth=1):
Brett Cannone0d88a12012-04-25 20:54:04 -040066 to_return = make_zip_pkg(zip_dir, zip_basename, pkg_name, script_basename,
67 source, depth)
68 importlib.invalidate_caches()
69 return to_return
Nick Coghlanf088e5e2008-12-14 11:50:48 +000070
Christian Heimescbf3b5c2007-12-03 21:02:03 +000071# There's no easy way to pass the script directory in to get
72# -m to work (avoiding that is the whole point of making
73# directories and zipfiles executable!)
74# So we fake it for testing purposes with a custom launch script
75launch_source = """\
76import sys, os.path, runpy
Nick Coghlanf088e5e2008-12-14 11:50:48 +000077sys.path.insert(0, %s)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000078runpy._run_module_as_main(%r)
79"""
80
Nick Coghlanf088e5e2008-12-14 11:50:48 +000081def _make_launch_script(script_dir, script_basename, module_name, path=None):
82 if path is None:
83 path = "os.path.dirname(__file__)"
84 else:
85 path = repr(path)
86 source = launch_source % (path, module_name)
Brett Cannone0d88a12012-04-25 20:54:04 -040087 to_return = make_script(script_dir, script_basename, source)
88 importlib.invalidate_caches()
89 return to_return
Christian Heimescbf3b5c2007-12-03 21:02:03 +000090
Christian Heimes9cd17752007-11-18 19:35:23 +000091class CmdLineTest(unittest.TestCase):
Nick Coghland26c18a2010-08-17 13:06:11 +000092 def _check_output(self, script_name, exit_code, data,
93 expected_file, expected_argv0,
Nick Coghlan85e729e2012-07-15 18:09:52 +100094 expected_path0, expected_package,
95 expected_loader):
Nick Coghland26c18a2010-08-17 13:06:11 +000096 if verbose > 1:
Guido van Rossum87c0f1d2007-11-19 18:03:44 +000097 print("Output from test script %r:" % script_name)
98 print(data)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000099 self.assertEqual(exit_code, 0)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000100 printed_loader = '__loader__==%a' % expected_loader
Victor Stinnere3874ed2010-10-17 01:41:09 +0000101 printed_file = '__file__==%a' % expected_file
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000102 printed_package = '__package__==%r' % expected_package
Victor Stinnere3874ed2010-10-17 01:41:09 +0000103 printed_argv0 = 'sys.argv[0]==%a' % expected_argv0
104 printed_path0 = 'sys.path[0]==%a' % expected_path0
105 printed_cwd = 'cwd==%a' % os.getcwd()
Nick Coghland26c18a2010-08-17 13:06:11 +0000106 if verbose > 1:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000107 print('Expected output:')
108 print(printed_file)
109 print(printed_package)
110 print(printed_argv0)
Nick Coghland26c18a2010-08-17 13:06:11 +0000111 print(printed_cwd)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000112 self.assertIn(printed_loader.encode('utf-8'), data)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000113 self.assertIn(printed_file.encode('utf-8'), data)
114 self.assertIn(printed_package.encode('utf-8'), data)
115 self.assertIn(printed_argv0.encode('utf-8'), data)
Nick Coghland26c18a2010-08-17 13:06:11 +0000116 self.assertIn(printed_path0.encode('utf-8'), data)
117 self.assertIn(printed_cwd.encode('utf-8'), data)
118
119 def _check_script(self, script_name, expected_file,
120 expected_argv0, expected_path0,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000121 expected_package, expected_loader,
Nick Coghland26c18a2010-08-17 13:06:11 +0000122 *cmd_line_switches):
Georg Brandlf5247e32010-10-14 08:08:56 +0000123 if not __debug__:
124 cmd_line_switches += ('-' + 'O' * sys.flags.optimize,)
Nick Coghlan37fc4012012-04-22 17:11:33 +1000125 run_args = cmd_line_switches + (script_name,) + tuple(example_args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000126 rc, out, err = assert_python_ok(*run_args)
127 self._check_output(script_name, rc, out + err, expected_file,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000128 expected_argv0, expected_path0,
129 expected_package, expected_loader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000130
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000131 def _check_import_error(self, script_name, expected_msg,
132 *cmd_line_switches):
133 run_args = cmd_line_switches + (script_name,)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000134 rc, out, err = assert_python_failure(*run_args)
Nick Coghland26c18a2010-08-17 13:06:11 +0000135 if verbose > 1:
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000136 print('Output from test script %r:' % script_name)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000137 print(err)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000138 print('Expected output: %r' % expected_msg)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000139 self.assertIn(expected_msg.encode('utf-8'), err)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000140
Nick Coghlan85e729e2012-07-15 18:09:52 +1000141 def test_dash_c_loader(self):
142 rc, out, err = assert_python_ok("-c", "print(__loader__)")
143 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
144 self.assertIn(expected, out)
145
146 def test_stdin_loader(self):
Nick Coghlan37b3b902012-07-15 18:24:42 +1000147 # Unfortunately, there's no way to automatically test the fully
148 # interactive REPL, since that code path only gets executed when
Nick Coghlan8e6e7d32012-07-15 23:13:18 +1000149 # stdin is an interactive tty.
Nick Coghlan85e729e2012-07-15 18:09:52 +1000150 p = spawn_python()
151 try:
152 p.stdin.write(b"print(__loader__)\n")
153 p.stdin.flush()
154 finally:
155 out = kill_python(p)
156 expected = repr(importlib.machinery.BuiltinImporter).encode("utf-8")
157 self.assertIn(expected, out)
158
Christian Heimes9cd17752007-11-18 19:35:23 +0000159 def test_basic_script(self):
160 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000161 script_name = _make_test_script(script_dir, 'script')
Nick Coghland26c18a2010-08-17 13:06:11 +0000162 self._check_script(script_name, script_name, script_name,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000163 script_dir, None,
164 importlib.machinery.SourceFileLoader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000165
166 def test_script_compiled(self):
167 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000168 script_name = _make_test_script(script_dir, 'script')
Nick Coghland26c18a2010-08-17 13:06:11 +0000169 py_compile.compile(script_name, doraise=True)
Christian Heimes9cd17752007-11-18 19:35:23 +0000170 os.remove(script_name)
Nick Coghland26c18a2010-08-17 13:06:11 +0000171 pyc_file = support.make_legacy_pyc(script_name)
172 self._check_script(pyc_file, pyc_file,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000173 pyc_file, script_dir, None,
174 importlib.machinery.SourcelessFileLoader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000175
176 def test_directory(self):
177 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000178 script_name = _make_test_script(script_dir, '__main__')
Nick Coghland26c18a2010-08-17 13:06:11 +0000179 self._check_script(script_dir, script_name, script_dir,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000180 script_dir, '',
181 importlib.machinery.SourceFileLoader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000182
183 def test_directory_compiled(self):
184 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000185 script_name = _make_test_script(script_dir, '__main__')
Nick Coghland26c18a2010-08-17 13:06:11 +0000186 py_compile.compile(script_name, doraise=True)
Christian Heimes9cd17752007-11-18 19:35:23 +0000187 os.remove(script_name)
Nick Coghland26c18a2010-08-17 13:06:11 +0000188 pyc_file = support.make_legacy_pyc(script_name)
189 self._check_script(script_dir, pyc_file, script_dir,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000190 script_dir, '',
191 importlib.machinery.SourcelessFileLoader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000192
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000193 def test_directory_error(self):
194 with temp_dir() as script_dir:
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000195 msg = "can't find '__main__' module in %r" % script_dir
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000196 self._check_import_error(script_dir, msg)
197
Christian Heimes9cd17752007-11-18 19:35:23 +0000198 def test_zipfile(self):
199 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000200 script_name = _make_test_script(script_dir, '__main__')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000201 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000202 self._check_script(zip_name, run_name, zip_name, zip_name, '',
203 zipimport.zipimporter)
Christian Heimes9cd17752007-11-18 19:35:23 +0000204
205 def test_zipfile_compiled(self):
206 with temp_dir() as script_dir:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000207 script_name = _make_test_script(script_dir, '__main__')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000208 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000209 zip_name, run_name = make_zip_script(script_dir, 'test_zip', compiled_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000210 self._check_script(zip_name, run_name, zip_name, zip_name, '',
211 zipimport.zipimporter)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000212
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000213 def test_zipfile_error(self):
214 with temp_dir() as script_dir:
215 script_name = _make_test_script(script_dir, 'not_main')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000216 zip_name, run_name = make_zip_script(script_dir, 'test_zip', script_name)
217 msg = "can't find '__main__' module in %r" % zip_name
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000218 self._check_import_error(zip_name, msg)
219
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000220 def test_module_in_package(self):
221 with temp_dir() as script_dir:
222 pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000223 make_pkg(pkg_dir)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000224 script_name = _make_test_script(pkg_dir, 'script')
225 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script')
Nick Coghlan85e729e2012-07-15 18:09:52 +1000226 self._check_script(launch_name, script_name, script_name,
227 script_dir, 'test_pkg',
228 importlib.machinery.SourceFileLoader)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000229
230 def test_module_in_package_in_zipfile(self):
231 with temp_dir() as script_dir:
232 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script')
233 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.script', zip_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000234 self._check_script(launch_name, run_name, run_name,
235 zip_name, 'test_pkg', zipimport.zipimporter)
Nick Coghlanf088e5e2008-12-14 11:50:48 +0000236
237 def test_module_in_subpackage_in_zipfile(self):
238 with temp_dir() as script_dir:
239 zip_name, run_name = _make_test_zip_pkg(script_dir, 'test_zip', 'test_pkg', 'script', depth=2)
240 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg.test_pkg.script', zip_name)
Nick Coghlan85e729e2012-07-15 18:09:52 +1000241 self._check_script(launch_name, run_name, run_name,
242 zip_name, 'test_pkg.test_pkg',
243 zipimport.zipimporter)
Christian Heimes9cd17752007-11-18 19:35:23 +0000244
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000245 def test_package(self):
246 with temp_dir() as script_dir:
247 pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000248 make_pkg(pkg_dir)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000249 script_name = _make_test_script(pkg_dir, '__main__')
250 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
251 self._check_script(launch_name, script_name,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000252 script_name, script_dir, 'test_pkg',
253 importlib.machinery.SourceFileLoader)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000254
255 def test_package_compiled(self):
256 with temp_dir() as script_dir:
257 pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000258 make_pkg(pkg_dir)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000259 script_name = _make_test_script(pkg_dir, '__main__')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000260 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000261 os.remove(script_name)
Nick Coghland26c18a2010-08-17 13:06:11 +0000262 pyc_file = support.make_legacy_pyc(script_name)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000263 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
Barry Warsaw28a691b2010-04-17 00:19:56 +0000264 self._check_script(launch_name, pyc_file,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000265 pyc_file, script_dir, 'test_pkg',
266 importlib.machinery.SourcelessFileLoader)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000267
268 def test_package_error(self):
269 with temp_dir() as script_dir:
270 pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000271 make_pkg(pkg_dir)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000272 msg = ("'test_pkg' is a package and cannot "
273 "be directly executed")
274 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
275 self._check_import_error(launch_name, msg)
276
277 def test_package_recursion(self):
278 with temp_dir() as script_dir:
279 pkg_dir = os.path.join(script_dir, 'test_pkg')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000280 make_pkg(pkg_dir)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000281 main_dir = os.path.join(pkg_dir, '__main__')
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000282 make_pkg(main_dir)
Nick Coghlan3f48ae32009-02-08 01:58:26 +0000283 msg = ("Cannot use package as __main__ module; "
284 "'test_pkg' is a package and cannot "
285 "be directly executed")
286 launch_name = _make_launch_script(script_dir, 'launch', 'test_pkg')
287 self._check_import_error(launch_name, msg)
288
Nick Coghland26c18a2010-08-17 13:06:11 +0000289 def test_issue8202(self):
290 # Make sure package __init__ modules see "-m" in sys.argv0 while
291 # searching for the module to execute
292 with temp_dir() as script_dir:
293 with support.temp_cwd(path=script_dir):
294 pkg_dir = os.path.join(script_dir, 'test_pkg')
295 make_pkg(pkg_dir, "import sys; print('init_argv0==%r' % sys.argv[0])")
296 script_name = _make_test_script(pkg_dir, 'script')
Nick Coghlan37fc4012012-04-22 17:11:33 +1000297 rc, out, err = assert_python_ok('-m', 'test_pkg.script', *example_args)
Nick Coghland26c18a2010-08-17 13:06:11 +0000298 if verbose > 1:
Nick Coghlan10ac77d2012-04-19 22:19:36 +1000299 print(out)
Nick Coghland26c18a2010-08-17 13:06:11 +0000300 expected = "init_argv0==%r" % '-m'
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000301 self.assertIn(expected.encode('utf-8'), out)
302 self._check_output(script_name, rc, out,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000303 script_name, script_name, '', 'test_pkg',
304 importlib.machinery.SourceFileLoader)
Nick Coghland26c18a2010-08-17 13:06:11 +0000305
306 def test_issue8202_dash_c_file_ignored(self):
307 # Make sure a "-c" file in the current directory
308 # does not alter the value of sys.path[0]
309 with temp_dir() as script_dir:
310 with support.temp_cwd(path=script_dir):
311 with open("-c", "w") as f:
312 f.write("data")
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000313 rc, out, err = assert_python_ok('-c',
Nick Coghland26c18a2010-08-17 13:06:11 +0000314 'import sys; print("sys.path[0]==%r" % sys.path[0])')
315 if verbose > 1:
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000316 print(out)
Nick Coghland26c18a2010-08-17 13:06:11 +0000317 expected = "sys.path[0]==%r" % ''
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000318 self.assertIn(expected.encode('utf-8'), out)
Nick Coghland26c18a2010-08-17 13:06:11 +0000319
320 def test_issue8202_dash_m_file_ignored(self):
321 # Make sure a "-m" file in the current directory
322 # does not alter the value of sys.path[0]
323 with temp_dir() as script_dir:
324 script_name = _make_test_script(script_dir, 'other')
325 with support.temp_cwd(path=script_dir):
326 with open("-m", "w") as f:
327 f.write("data")
Nick Coghlan37fc4012012-04-22 17:11:33 +1000328 rc, out, err = assert_python_ok('-m', 'other', *example_args)
Antoine Pitrouf51d8d32010-10-08 18:05:42 +0000329 self._check_output(script_name, rc, out,
Nick Coghlan85e729e2012-07-15 18:09:52 +1000330 script_name, script_name, '', '',
331 importlib.machinery.SourceFileLoader)
Christian Heimes9cd17752007-11-18 19:35:23 +0000332
Senthil Kumaranf01a3372012-07-04 19:28:16 -0700333 def test_dash_m_error_code_is_one(self):
334 # If a module is invoked with the -m command line flag
335 # and results in an error that the return code to the
336 # shell is '1'
337 with temp_dir() as script_dir:
338 with support.temp_cwd(path=script_dir):
339 pkg_dir = os.path.join(script_dir, 'test_pkg')
340 make_pkg(pkg_dir)
341 script_name = _make_test_script(pkg_dir, 'other',
342 "if __name__ == '__main__': raise ValueError")
343 rc, out, err = assert_python_failure('-m', 'test_pkg.other', *example_args)
344 if verbose > 1:
345 print(out)
346 self.assertEqual(rc, 1)
347
Nick Coghlan1d5ccdb2012-05-21 23:03:30 +1000348 def test_pep_409_verbiage(self):
349 # Make sure PEP 409 syntax properly suppresses
350 # the context of an exception
351 script = textwrap.dedent("""\
352 try:
353 raise ValueError
354 except:
355 raise NameError from None
356 """)
357 with temp_dir() as script_dir:
358 script_name = _make_test_script(script_dir, 'script', script)
359 exitcode, stdout, stderr = assert_python_failure(script_name)
360 text = stderr.decode('ascii').split('\n')
361 self.assertEqual(len(text), 4)
362 self.assertTrue(text[0].startswith('Traceback'))
363 self.assertTrue(text[1].startswith(' File '))
364 self.assertTrue(text[3].startswith('NameError'))
365
Victor Stinnere667e982012-11-12 01:23:15 +0100366 def test_non_ascii(self):
367 # Mac OS X denies the creation of a file with an invalid UTF-8 name.
368 # Windows allows to create a name with an arbitrary bytes name, but
369 # Python cannot a undecodable bytes argument to a subprocess.
Victor Stinner0af71aa2013-01-03 01:50:30 +0100370 if (support.TESTFN_UNDECODABLE
371 and sys.platform not in ('win32', 'darwin')):
372 name = os.fsdecode(support.TESTFN_UNDECODABLE)
373 elif support.TESTFN_NONASCII:
Victor Stinnere667e982012-11-12 01:23:15 +0100374 name = support.TESTFN_NONASCII
375 else:
376 self.skipTest("need support.TESTFN_NONASCII")
377
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200378 # Issue #16218
Victor Stinnere667e982012-11-12 01:23:15 +0100379 source = 'print(ascii(__file__))\n'
380 script_name = _make_test_script(os.curdir, name, source)
381 self.addCleanup(support.unlink, script_name)
382 rc, stdout, stderr = assert_python_ok(script_name)
383 self.assertEqual(
384 ascii(script_name),
385 stdout.rstrip().decode('ascii'),
386 'stdout=%r stderr=%r' % (stdout, stderr))
387 self.assertEqual(0, rc)
388
Andrew Svetlov90c0eb22012-11-01 14:51:14 +0200389
Christian Heimes9cd17752007-11-18 19:35:23 +0000390def test_main():
Nick Coghland26c18a2010-08-17 13:06:11 +0000391 support.run_unittest(CmdLineTest)
392 support.reap_children()
Christian Heimes9cd17752007-11-18 19:35:23 +0000393
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000394if __name__ == '__main__':
Christian Heimes9cd17752007-11-18 19:35:23 +0000395 test_main()