blob: 94e47d688e60cb10b5f7e6e67c83193c27b0b4d8 [file] [log] [blame]
Thomas Woutersa9773292006-04-21 09:43:23 +00001# Test the runpy module
2import unittest
3import os
4import os.path
5import sys
Nick Coghlan16eb0fb2009-11-18 11:35:25 +00006import re
Thomas Woutersa9773292006-04-21 09:43:23 +00007import tempfile
Barry Warsaw28a691b2010-04-17 00:19:56 +00008import py_compile
Brett Cannon61b14252010-07-03 21:48:25 +00009from test.support import forget, make_legacy_pyc, run_unittest, unload, verbose
Barry Warsaw28a691b2010-04-17 00:19:56 +000010from test.script_helper import (
11 make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000012
Nick Coghlan260bd3e2009-11-16 06:49:25 +000013
14from runpy import _run_code, _run_module_code, run_module, run_path
Christian Heimescbf3b5c2007-12-03 21:02:03 +000015# Note: This module can't safely test _run_module_as_main as it
16# runs its tests in the current process, which would mess with the
17# real __main__ module (usually test.regrtest)
18# See test_cmd_line_script for a test that executes that code path
Thomas Woutersa9773292006-04-21 09:43:23 +000019
20# Set up the test code and expected results
21
22class RunModuleCodeTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000023 """Unit tests for runpy._run_code and runpy._run_module_code"""
Thomas Woutersa9773292006-04-21 09:43:23 +000024
25 expected_result = ["Top level assignment", "Lower level reference"]
26 test_source = (
27 "# Check basic code execution\n"
28 "result = ['Top level assignment']\n"
29 "def f():\n"
30 " result.append('Lower level reference')\n"
31 "f()\n"
32 "# Check the sys module\n"
33 "import sys\n"
34 "run_argv0 = sys.argv[0]\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000035 "run_name_in_sys_modules = __name__ in sys.modules\n"
36 "if run_name_in_sys_modules:\n"
37 " module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000038 "# Check nested operation\n"
39 "import runpy\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000040 "nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000041 )
42
Thomas Woutersed03b412007-08-28 21:37:11 +000043 def test_run_code(self):
44 saved_argv0 = sys.argv[0]
45 d = _run_code(self.test_source, {})
Nick Coghlan260bd3e2009-11-16 06:49:25 +000046 self.assertEqual(d["result"], self.expected_result)
47 self.assertIs(d["__name__"], None)
48 self.assertIs(d["__file__"], None)
Barry Warsaw28a691b2010-04-17 00:19:56 +000049 self.assertIs(d["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000050 self.assertIs(d["__loader__"], None)
51 self.assertIs(d["__package__"], None)
52 self.assertIs(d["run_argv0"], saved_argv0)
53 self.assertNotIn("run_name", d)
54 self.assertIs(sys.argv[0], saved_argv0)
Thomas Woutersa9773292006-04-21 09:43:23 +000055
56 def test_run_module_code(self):
57 initial = object()
58 name = "<Nonsense>"
59 file = "Some other nonsense"
60 loader = "Now you're just being silly"
Christian Heimescbf3b5c2007-12-03 21:02:03 +000061 package = '' # Treat as a top level module
Thomas Woutersa9773292006-04-21 09:43:23 +000062 d1 = dict(initial=initial)
63 saved_argv0 = sys.argv[0]
Thomas Woutersed03b412007-08-28 21:37:11 +000064 d2 = _run_module_code(self.test_source,
65 d1,
66 name,
67 file,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000068 loader,
69 package)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000070 self.assertNotIn("result", d1)
71 self.assertIs(d2["initial"], initial)
Thomas Woutersed03b412007-08-28 21:37:11 +000072 self.assertEqual(d2["result"], self.expected_result)
73 self.assertEqual(d2["nested"]["x"], 1)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000074 self.assertIs(d2["__name__"], name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000075 self.assertTrue(d2["run_name_in_sys_modules"])
76 self.assertTrue(d2["module_in_sys_modules"])
Nick Coghlan260bd3e2009-11-16 06:49:25 +000077 self.assertIs(d2["__file__"], file)
Barry Warsaw28a691b2010-04-17 00:19:56 +000078 self.assertIs(d2["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000079 self.assertIs(d2["run_argv0"], file)
80 self.assertIs(d2["__loader__"], loader)
81 self.assertIs(d2["__package__"], package)
82 self.assertIs(sys.argv[0], saved_argv0)
83 self.assertNotIn(name, sys.modules)
Thomas Woutersed03b412007-08-28 21:37:11 +000084
Thomas Woutersa9773292006-04-21 09:43:23 +000085
86class RunModuleTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000087 """Unit tests for runpy.run_module"""
Thomas Woutersa9773292006-04-21 09:43:23 +000088
89 def expect_import_error(self, mod_name):
90 try:
91 run_module(mod_name)
92 except ImportError:
93 pass
94 else:
95 self.fail("Expected import error for " + mod_name)
96
97 def test_invalid_names(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000098 # Builtin module
Thomas Woutersa9773292006-04-21 09:43:23 +000099 self.expect_import_error("sys")
Guido van Rossum806c2462007-08-06 23:33:07 +0000100 # Non-existent modules
Thomas Woutersa9773292006-04-21 09:43:23 +0000101 self.expect_import_error("sys.imp.eric")
102 self.expect_import_error("os.path.half")
103 self.expect_import_error("a.bee")
104 self.expect_import_error(".howard")
105 self.expect_import_error("..eaten")
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000106 # Package without __main__.py
107 self.expect_import_error("multiprocessing")
Thomas Woutersa9773292006-04-21 09:43:23 +0000108
109 def test_library_module(self):
110 run_module("runpy")
111
Guido van Rossum806c2462007-08-06 23:33:07 +0000112 def _add_pkg_dir(self, pkg_dir):
113 os.mkdir(pkg_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000114 pkg_fname = os.path.join(pkg_dir, "__init__.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000115 pkg_file = open(pkg_fname, "w")
116 pkg_file.close()
117 return pkg_fname
118
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000119 def _make_pkg(self, source, depth, mod_base="runpy_test"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000120 pkg_name = "__runpy_pkg__"
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000121 test_fname = mod_base+os.extsep+"py"
Thomas Woutersa9773292006-04-21 09:43:23 +0000122 pkg_dir = sub_dir = tempfile.mkdtemp()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000123 if verbose: print(" Package tree in:", sub_dir)
Thomas Woutersa9773292006-04-21 09:43:23 +0000124 sys.path.insert(0, pkg_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000125 if verbose: print(" Updated sys.path:", sys.path[0])
Thomas Woutersa9773292006-04-21 09:43:23 +0000126 for i in range(depth):
127 sub_dir = os.path.join(sub_dir, pkg_name)
Guido van Rossum806c2462007-08-06 23:33:07 +0000128 pkg_fname = self._add_pkg_dir(sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000129 if verbose: print(" Next level in:", sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 if verbose: print(" Created:", pkg_fname)
Thomas Woutersa9773292006-04-21 09:43:23 +0000131 mod_fname = os.path.join(sub_dir, test_fname)
132 mod_file = open(mod_fname, "w")
133 mod_file.write(source)
134 mod_file.close()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000135 if verbose: print(" Created:", mod_fname)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000136 mod_name = (pkg_name+".")*depth + mod_base
Thomas Woutersa9773292006-04-21 09:43:23 +0000137 return pkg_dir, mod_fname, mod_name
138
139 def _del_pkg(self, top, depth, mod_name):
Guido van Rossum806c2462007-08-06 23:33:07 +0000140 for entry in list(sys.modules):
141 if entry.startswith("__runpy_pkg__"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000142 del sys.modules[entry]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000143 if verbose: print(" Removed sys.modules entries")
Thomas Woutersa9773292006-04-21 09:43:23 +0000144 del sys.path[0]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000145 if verbose: print(" Removed sys.path entry")
Thomas Woutersa9773292006-04-21 09:43:23 +0000146 for root, dirs, files in os.walk(top, topdown=False):
147 for name in files:
148 try:
149 os.remove(os.path.join(root, name))
Guido van Rossumb940e112007-01-10 16:19:56 +0000150 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000151 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000152 for name in dirs:
153 fullname = os.path.join(root, name)
154 try:
155 os.rmdir(fullname)
Guido van Rossumb940e112007-01-10 16:19:56 +0000156 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000157 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000158 try:
159 os.rmdir(top)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000160 if verbose: print(" Removed package tree")
Guido van Rossumb940e112007-01-10 16:19:56 +0000161 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000162 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000163
164 def _check_module(self, depth):
165 pkg_dir, mod_fname, mod_name = (
166 self._make_pkg("x=1\n", depth))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000167 forget(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000168 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000169 if verbose: print("Running from source:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000170 d1 = run_module(mod_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000171 self.assertIn("x", d1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000172 self.assertEqual(d1["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000173 del d1 # Ensure __loader__ entry doesn't keep file open
174 __import__(mod_name)
175 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000176 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000177 unload(mod_name) # In case loader caches paths
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000178 if verbose: print("Running from compiled:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000179 d2 = run_module(mod_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000180 self.assertIn("x", d2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000181 self.assertEqual(d2["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000182 del d2 # Ensure __loader__ entry doesn't keep file open
183 finally:
184 self._del_pkg(pkg_dir, depth, mod_name)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000185 if verbose: print("Module executed successfully")
Thomas Woutersa9773292006-04-21 09:43:23 +0000186
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000187 def _check_package(self, depth):
188 pkg_dir, mod_fname, mod_name = (
189 self._make_pkg("x=1\n", depth, "__main__"))
190 pkg_name, _, _ = mod_name.rpartition(".")
191 forget(mod_name)
192 try:
193 if verbose: print("Running from source:", pkg_name)
194 d1 = run_module(pkg_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000195 self.assertIn("x", d1)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000196 self.assertTrue(d1["x"] == 1)
197 del d1 # Ensure __loader__ entry doesn't keep file open
198 __import__(mod_name)
199 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000200 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000201 unload(mod_name) # In case loader caches paths
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000202 if verbose: print("Running from compiled:", pkg_name)
203 d2 = run_module(pkg_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000204 self.assertIn("x", d2)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000205 self.assertTrue(d2["x"] == 1)
206 del d2 # Ensure __loader__ entry doesn't keep file open
207 finally:
208 self._del_pkg(pkg_dir, depth, pkg_name)
209 if verbose: print("Package executed successfully")
210
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000211 def _add_relative_modules(self, base_dir, source, depth):
Guido van Rossum806c2462007-08-06 23:33:07 +0000212 if depth <= 1:
213 raise ValueError("Relative module test needs depth > 1")
214 pkg_name = "__runpy_pkg__"
215 module_dir = base_dir
216 for i in range(depth):
217 parent_dir = module_dir
218 module_dir = os.path.join(module_dir, pkg_name)
219 # Add sibling module
Skip Montanaro7a98be22007-08-16 14:35:24 +0000220 sibling_fname = os.path.join(module_dir, "sibling.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000221 sibling_file = open(sibling_fname, "w")
222 sibling_file.close()
223 if verbose: print(" Added sibling module:", sibling_fname)
224 # Add nephew module
225 uncle_dir = os.path.join(parent_dir, "uncle")
226 self._add_pkg_dir(uncle_dir)
227 if verbose: print(" Added uncle package:", uncle_dir)
228 cousin_dir = os.path.join(uncle_dir, "cousin")
229 self._add_pkg_dir(cousin_dir)
230 if verbose: print(" Added cousin package:", cousin_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000231 nephew_fname = os.path.join(cousin_dir, "nephew.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000232 nephew_file = open(nephew_fname, "w")
233 nephew_file.close()
234 if verbose: print(" Added nephew module:", nephew_fname)
235
236 def _check_relative_imports(self, depth, run_name=None):
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000237 contents = r"""\
Guido van Rossum806c2462007-08-06 23:33:07 +0000238from __future__ import absolute_import
239from . import sibling
240from ..uncle.cousin import nephew
241"""
242 pkg_dir, mod_fname, mod_name = (
243 self._make_pkg(contents, depth))
244 try:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000245 self._add_relative_modules(pkg_dir, contents, depth)
246 pkg_name = mod_name.rpartition('.')[0]
Guido van Rossum806c2462007-08-06 23:33:07 +0000247 if verbose: print("Running from source:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000248 d1 = run_module(mod_name, run_name=run_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000249 self.assertIn("__package__", d1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000250 self.assertTrue(d1["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000251 self.assertIn("sibling", d1)
252 self.assertIn("nephew", d1)
Guido van Rossum806c2462007-08-06 23:33:07 +0000253 del d1 # Ensure __loader__ entry doesn't keep file open
254 __import__(mod_name)
255 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000256 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000257 unload(mod_name) # In case the loader caches paths
Guido van Rossum806c2462007-08-06 23:33:07 +0000258 if verbose: print("Running from compiled:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000259 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000260 self.assertIn("__package__", d2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000261 self.assertTrue(d2["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000262 self.assertIn("sibling", d2)
263 self.assertIn("nephew", d2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000264 del d2 # Ensure __loader__ entry doesn't keep file open
265 finally:
266 self._del_pkg(pkg_dir, depth, mod_name)
267 if verbose: print("Module executed successfully")
268
Thomas Woutersa9773292006-04-21 09:43:23 +0000269 def test_run_module(self):
270 for depth in range(4):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000271 if verbose: print("Testing package depth:", depth)
Thomas Woutersa9773292006-04-21 09:43:23 +0000272 self._check_module(depth)
273
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000274 def test_run_package(self):
275 for depth in range(1, 4):
276 if verbose: print("Testing package depth:", depth)
277 self._check_package(depth)
278
Guido van Rossum806c2462007-08-06 23:33:07 +0000279 def test_explicit_relative_import(self):
280 for depth in range(2, 5):
281 if verbose: print("Testing relative imports at depth:", depth)
282 self._check_relative_imports(depth)
283
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000284 def test_main_relative_import(self):
285 for depth in range(2, 5):
286 if verbose: print("Testing main relative imports at depth:", depth)
287 self._check_relative_imports(depth, "__main__")
288
Thomas Woutersa9773292006-04-21 09:43:23 +0000289
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000290class RunPathTest(unittest.TestCase):
291 """Unit tests for runpy.run_path"""
292 # Based on corresponding tests in test_cmd_line_script
293
294 test_source = """\
295# Script may be run with optimisation enabled, so don't rely on assert
296# statements being executed
297def assertEqual(lhs, rhs):
298 if lhs != rhs:
299 raise AssertionError('%r != %r' % (lhs, rhs))
300def assertIs(lhs, rhs):
301 if lhs is not rhs:
302 raise AssertionError('%r is not %r' % (lhs, rhs))
303# Check basic code execution
304result = ['Top level assignment']
305def f():
306 result.append('Lower level reference')
307f()
308assertEqual(result, ['Top level assignment', 'Lower level reference'])
309# Check the sys module
310import sys
311assertIs(globals(), sys.modules[__name__].__dict__)
312argv0 = sys.argv[0]
313"""
314
315 def _make_test_script(self, script_dir, script_basename, source=None):
316 if source is None:
317 source = self.test_source
318 return make_script(script_dir, script_basename, source)
319
320 def _check_script(self, script_name, expected_name, expected_file,
321 expected_argv0, expected_package):
322 result = run_path(script_name)
323 self.assertEqual(result["__name__"], expected_name)
324 self.assertEqual(result["__file__"], expected_file)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000325 self.assertEqual(result["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000326 self.assertIn("argv0", result)
327 self.assertEqual(result["argv0"], expected_argv0)
328 self.assertEqual(result["__package__"], expected_package)
329
330 def _check_import_error(self, script_name, msg):
Nick Coghlan16eb0fb2009-11-18 11:35:25 +0000331 msg = re.escape(msg)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000332 self.assertRaisesRegexp(ImportError, msg, run_path, script_name)
333
334 def test_basic_script(self):
335 with temp_dir() as script_dir:
336 mod_name = 'script'
337 script_name = self._make_test_script(script_dir, mod_name)
338 self._check_script(script_name, "<run_path>", script_name,
339 script_name, None)
340
341 def test_script_compiled(self):
342 with temp_dir() as script_dir:
343 mod_name = 'script'
344 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000345 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000346 os.remove(script_name)
347 self._check_script(compiled_name, "<run_path>", compiled_name,
348 compiled_name, None)
349
350 def test_directory(self):
351 with temp_dir() as script_dir:
352 mod_name = '__main__'
353 script_name = self._make_test_script(script_dir, mod_name)
354 self._check_script(script_dir, "<run_path>", script_name,
355 script_dir, '')
356
357 def test_directory_compiled(self):
358 with temp_dir() as script_dir:
359 mod_name = '__main__'
360 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000361 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000362 os.remove(script_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000363 legacy_pyc = make_legacy_pyc(script_name)
364 self._check_script(script_dir, "<run_path>", legacy_pyc,
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000365 script_dir, '')
366
367 def test_directory_error(self):
368 with temp_dir() as script_dir:
369 mod_name = 'not_main'
370 script_name = self._make_test_script(script_dir, mod_name)
371 msg = "can't find '__main__' module in %r" % script_dir
372 self._check_import_error(script_dir, msg)
373
374 def test_zipfile(self):
375 with temp_dir() as script_dir:
376 mod_name = '__main__'
377 script_name = self._make_test_script(script_dir, mod_name)
378 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
379 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
380
381 def test_zipfile_compiled(self):
382 with temp_dir() as script_dir:
383 mod_name = '__main__'
384 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000385 compiled_name = py_compile.compile(script_name, doraise=True)
386 zip_name, fname = make_zip_script(script_dir, 'test_zip',
387 compiled_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000388 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
389
390 def test_zipfile_error(self):
391 with temp_dir() as script_dir:
392 mod_name = 'not_main'
393 script_name = self._make_test_script(script_dir, mod_name)
394 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
395 msg = "can't find '__main__' module in %r" % zip_name
396 self._check_import_error(zip_name, msg)
397
398 def test_main_recursion_error(self):
399 with temp_dir() as script_dir, temp_dir() as dummy_dir:
400 mod_name = '__main__'
401 source = ("import runpy\n"
402 "runpy.run_path(%r)\n") % dummy_dir
403 script_name = self._make_test_script(script_dir, mod_name, source)
404 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
405 msg = "recursion depth exceeded"
406 self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name)
407
408
409
Thomas Woutersa9773292006-04-21 09:43:23 +0000410def test_main():
Brett Cannon61b14252010-07-03 21:48:25 +0000411 run_unittest(
412 RunModuleCodeTest,
413 RunModuleTest,
414 RunPathTest
415 )
Thomas Woutersa9773292006-04-21 09:43:23 +0000416
417if __name__ == "__main__":
418 test_main()