blob: 00f34b182489997fb82689c3f35b1ea1aefecfce [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 Cannon31f59292011-02-21 19:29:56 +00009from test.support import (
10 forget, make_legacy_pyc, run_unittest, unload, verbose, no_tracing)
Barry Warsaw28a691b2010-04-17 00:19:56 +000011from test.script_helper import (
12 make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000013
Nick Coghlan260bd3e2009-11-16 06:49:25 +000014
15from runpy import _run_code, _run_module_code, run_module, run_path
Christian Heimescbf3b5c2007-12-03 21:02:03 +000016# Note: This module can't safely test _run_module_as_main as it
17# runs its tests in the current process, which would mess with the
18# real __main__ module (usually test.regrtest)
19# See test_cmd_line_script for a test that executes that code path
Thomas Woutersa9773292006-04-21 09:43:23 +000020
21# Set up the test code and expected results
22
23class RunModuleCodeTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000024 """Unit tests for runpy._run_code and runpy._run_module_code"""
Thomas Woutersa9773292006-04-21 09:43:23 +000025
26 expected_result = ["Top level assignment", "Lower level reference"]
27 test_source = (
28 "# Check basic code execution\n"
29 "result = ['Top level assignment']\n"
30 "def f():\n"
31 " result.append('Lower level reference')\n"
32 "f()\n"
33 "# Check the sys module\n"
34 "import sys\n"
35 "run_argv0 = sys.argv[0]\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000036 "run_name_in_sys_modules = __name__ in sys.modules\n"
37 "if run_name_in_sys_modules:\n"
38 " module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000039 "# Check nested operation\n"
40 "import runpy\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000041 "nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000042 )
43
Thomas Woutersed03b412007-08-28 21:37:11 +000044 def test_run_code(self):
45 saved_argv0 = sys.argv[0]
46 d = _run_code(self.test_source, {})
Nick Coghlan260bd3e2009-11-16 06:49:25 +000047 self.assertEqual(d["result"], self.expected_result)
48 self.assertIs(d["__name__"], None)
49 self.assertIs(d["__file__"], None)
Barry Warsaw28a691b2010-04-17 00:19:56 +000050 self.assertIs(d["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000051 self.assertIs(d["__loader__"], None)
52 self.assertIs(d["__package__"], None)
53 self.assertIs(d["run_argv0"], saved_argv0)
54 self.assertNotIn("run_name", d)
55 self.assertIs(sys.argv[0], saved_argv0)
Thomas Woutersa9773292006-04-21 09:43:23 +000056
57 def test_run_module_code(self):
58 initial = object()
59 name = "<Nonsense>"
60 file = "Some other nonsense"
61 loader = "Now you're just being silly"
Christian Heimescbf3b5c2007-12-03 21:02:03 +000062 package = '' # Treat as a top level module
Thomas Woutersa9773292006-04-21 09:43:23 +000063 d1 = dict(initial=initial)
64 saved_argv0 = sys.argv[0]
Thomas Woutersed03b412007-08-28 21:37:11 +000065 d2 = _run_module_code(self.test_source,
66 d1,
67 name,
68 file,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000069 loader,
70 package)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000071 self.assertNotIn("result", d1)
72 self.assertIs(d2["initial"], initial)
Thomas Woutersed03b412007-08-28 21:37:11 +000073 self.assertEqual(d2["result"], self.expected_result)
74 self.assertEqual(d2["nested"]["x"], 1)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000075 self.assertIs(d2["__name__"], name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000076 self.assertTrue(d2["run_name_in_sys_modules"])
77 self.assertTrue(d2["module_in_sys_modules"])
Nick Coghlan260bd3e2009-11-16 06:49:25 +000078 self.assertIs(d2["__file__"], file)
Barry Warsaw28a691b2010-04-17 00:19:56 +000079 self.assertIs(d2["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000080 self.assertIs(d2["run_argv0"], file)
81 self.assertIs(d2["__loader__"], loader)
82 self.assertIs(d2["__package__"], package)
83 self.assertIs(sys.argv[0], saved_argv0)
84 self.assertNotIn(name, sys.modules)
Thomas Woutersed03b412007-08-28 21:37:11 +000085
Thomas Woutersa9773292006-04-21 09:43:23 +000086
87class RunModuleTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000088 """Unit tests for runpy.run_module"""
Thomas Woutersa9773292006-04-21 09:43:23 +000089
90 def expect_import_error(self, mod_name):
91 try:
92 run_module(mod_name)
93 except ImportError:
94 pass
95 else:
96 self.fail("Expected import error for " + mod_name)
97
98 def test_invalid_names(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000099 # Builtin module
Thomas Woutersa9773292006-04-21 09:43:23 +0000100 self.expect_import_error("sys")
Guido van Rossum806c2462007-08-06 23:33:07 +0000101 # Non-existent modules
Thomas Woutersa9773292006-04-21 09:43:23 +0000102 self.expect_import_error("sys.imp.eric")
103 self.expect_import_error("os.path.half")
104 self.expect_import_error("a.bee")
105 self.expect_import_error(".howard")
106 self.expect_import_error("..eaten")
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000107 # Package without __main__.py
108 self.expect_import_error("multiprocessing")
Thomas Woutersa9773292006-04-21 09:43:23 +0000109
110 def test_library_module(self):
111 run_module("runpy")
112
Guido van Rossum806c2462007-08-06 23:33:07 +0000113 def _add_pkg_dir(self, pkg_dir):
114 os.mkdir(pkg_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000115 pkg_fname = os.path.join(pkg_dir, "__init__.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000116 pkg_file = open(pkg_fname, "w")
117 pkg_file.close()
118 return pkg_fname
119
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000120 def _make_pkg(self, source, depth, mod_base="runpy_test"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000121 pkg_name = "__runpy_pkg__"
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000122 test_fname = mod_base+os.extsep+"py"
Thomas Woutersa9773292006-04-21 09:43:23 +0000123 pkg_dir = sub_dir = tempfile.mkdtemp()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000124 if verbose: print(" Package tree in:", sub_dir)
Thomas Woutersa9773292006-04-21 09:43:23 +0000125 sys.path.insert(0, pkg_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000126 if verbose: print(" Updated sys.path:", sys.path[0])
Thomas Woutersa9773292006-04-21 09:43:23 +0000127 for i in range(depth):
128 sub_dir = os.path.join(sub_dir, pkg_name)
Guido van Rossum806c2462007-08-06 23:33:07 +0000129 pkg_fname = self._add_pkg_dir(sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000130 if verbose: print(" Next level in:", sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000131 if verbose: print(" Created:", pkg_fname)
Thomas Woutersa9773292006-04-21 09:43:23 +0000132 mod_fname = os.path.join(sub_dir, test_fname)
133 mod_file = open(mod_fname, "w")
134 mod_file.write(source)
135 mod_file.close()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000136 if verbose: print(" Created:", mod_fname)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000137 mod_name = (pkg_name+".")*depth + mod_base
Thomas Woutersa9773292006-04-21 09:43:23 +0000138 return pkg_dir, mod_fname, mod_name
139
140 def _del_pkg(self, top, depth, mod_name):
Guido van Rossum806c2462007-08-06 23:33:07 +0000141 for entry in list(sys.modules):
142 if entry.startswith("__runpy_pkg__"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000143 del sys.modules[entry]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000144 if verbose: print(" Removed sys.modules entries")
Thomas Woutersa9773292006-04-21 09:43:23 +0000145 del sys.path[0]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000146 if verbose: print(" Removed sys.path entry")
Thomas Woutersa9773292006-04-21 09:43:23 +0000147 for root, dirs, files in os.walk(top, topdown=False):
148 for name in files:
149 try:
150 os.remove(os.path.join(root, name))
Guido van Rossumb940e112007-01-10 16:19:56 +0000151 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000152 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000153 for name in dirs:
154 fullname = os.path.join(root, name)
155 try:
156 os.rmdir(fullname)
Guido van Rossumb940e112007-01-10 16:19:56 +0000157 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000158 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000159 try:
160 os.rmdir(top)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000161 if verbose: print(" Removed package tree")
Guido van Rossumb940e112007-01-10 16:19:56 +0000162 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000163 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000164
165 def _check_module(self, depth):
166 pkg_dir, mod_fname, mod_name = (
167 self._make_pkg("x=1\n", depth))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000168 forget(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000169 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000170 if verbose: print("Running from source:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000171 d1 = run_module(mod_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000172 self.assertIn("x", d1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000173 self.assertEqual(d1["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000174 del d1 # Ensure __loader__ entry doesn't keep file open
175 __import__(mod_name)
176 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000177 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000178 unload(mod_name) # In case loader caches paths
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000179 if verbose: print("Running from compiled:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000180 d2 = run_module(mod_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000181 self.assertIn("x", d2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000182 self.assertEqual(d2["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000183 del d2 # Ensure __loader__ entry doesn't keep file open
184 finally:
185 self._del_pkg(pkg_dir, depth, mod_name)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000186 if verbose: print("Module executed successfully")
Thomas Woutersa9773292006-04-21 09:43:23 +0000187
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000188 def _check_package(self, depth):
189 pkg_dir, mod_fname, mod_name = (
190 self._make_pkg("x=1\n", depth, "__main__"))
191 pkg_name, _, _ = mod_name.rpartition(".")
192 forget(mod_name)
193 try:
194 if verbose: print("Running from source:", pkg_name)
195 d1 = run_module(pkg_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000196 self.assertIn("x", d1)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000197 self.assertTrue(d1["x"] == 1)
198 del d1 # Ensure __loader__ entry doesn't keep file open
199 __import__(mod_name)
200 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000201 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000202 unload(mod_name) # In case loader caches paths
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000203 if verbose: print("Running from compiled:", pkg_name)
204 d2 = run_module(pkg_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000205 self.assertIn("x", d2)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000206 self.assertTrue(d2["x"] == 1)
207 del d2 # Ensure __loader__ entry doesn't keep file open
208 finally:
209 self._del_pkg(pkg_dir, depth, pkg_name)
210 if verbose: print("Package executed successfully")
211
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000212 def _add_relative_modules(self, base_dir, source, depth):
Guido van Rossum806c2462007-08-06 23:33:07 +0000213 if depth <= 1:
214 raise ValueError("Relative module test needs depth > 1")
215 pkg_name = "__runpy_pkg__"
216 module_dir = base_dir
217 for i in range(depth):
218 parent_dir = module_dir
219 module_dir = os.path.join(module_dir, pkg_name)
220 # Add sibling module
Skip Montanaro7a98be22007-08-16 14:35:24 +0000221 sibling_fname = os.path.join(module_dir, "sibling.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000222 sibling_file = open(sibling_fname, "w")
223 sibling_file.close()
224 if verbose: print(" Added sibling module:", sibling_fname)
225 # Add nephew module
226 uncle_dir = os.path.join(parent_dir, "uncle")
227 self._add_pkg_dir(uncle_dir)
228 if verbose: print(" Added uncle package:", uncle_dir)
229 cousin_dir = os.path.join(uncle_dir, "cousin")
230 self._add_pkg_dir(cousin_dir)
231 if verbose: print(" Added cousin package:", cousin_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000232 nephew_fname = os.path.join(cousin_dir, "nephew.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000233 nephew_file = open(nephew_fname, "w")
234 nephew_file.close()
235 if verbose: print(" Added nephew module:", nephew_fname)
236
237 def _check_relative_imports(self, depth, run_name=None):
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000238 contents = r"""\
Guido van Rossum806c2462007-08-06 23:33:07 +0000239from __future__ import absolute_import
240from . import sibling
241from ..uncle.cousin import nephew
242"""
243 pkg_dir, mod_fname, mod_name = (
244 self._make_pkg(contents, depth))
245 try:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000246 self._add_relative_modules(pkg_dir, contents, depth)
247 pkg_name = mod_name.rpartition('.')[0]
Guido van Rossum806c2462007-08-06 23:33:07 +0000248 if verbose: print("Running from source:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000249 d1 = run_module(mod_name, run_name=run_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000250 self.assertIn("__package__", d1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000251 self.assertTrue(d1["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000252 self.assertIn("sibling", d1)
253 self.assertIn("nephew", d1)
Guido van Rossum806c2462007-08-06 23:33:07 +0000254 del d1 # Ensure __loader__ entry doesn't keep file open
255 __import__(mod_name)
256 os.remove(mod_fname)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000257 make_legacy_pyc(mod_fname)
Brett Cannon61b14252010-07-03 21:48:25 +0000258 unload(mod_name) # In case the loader caches paths
Guido van Rossum806c2462007-08-06 23:33:07 +0000259 if verbose: print("Running from compiled:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000260 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000261 self.assertIn("__package__", d2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000262 self.assertTrue(d2["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000263 self.assertIn("sibling", d2)
264 self.assertIn("nephew", d2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000265 del d2 # Ensure __loader__ entry doesn't keep file open
266 finally:
267 self._del_pkg(pkg_dir, depth, mod_name)
268 if verbose: print("Module executed successfully")
269
Thomas Woutersa9773292006-04-21 09:43:23 +0000270 def test_run_module(self):
271 for depth in range(4):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000272 if verbose: print("Testing package depth:", depth)
Thomas Woutersa9773292006-04-21 09:43:23 +0000273 self._check_module(depth)
274
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000275 def test_run_package(self):
276 for depth in range(1, 4):
277 if verbose: print("Testing package depth:", depth)
278 self._check_package(depth)
279
Guido van Rossum806c2462007-08-06 23:33:07 +0000280 def test_explicit_relative_import(self):
281 for depth in range(2, 5):
282 if verbose: print("Testing relative imports at depth:", depth)
283 self._check_relative_imports(depth)
284
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000285 def test_main_relative_import(self):
286 for depth in range(2, 5):
287 if verbose: print("Testing main relative imports at depth:", depth)
288 self._check_relative_imports(depth, "__main__")
289
Thomas Woutersa9773292006-04-21 09:43:23 +0000290
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000291class RunPathTest(unittest.TestCase):
292 """Unit tests for runpy.run_path"""
293 # Based on corresponding tests in test_cmd_line_script
294
295 test_source = """\
296# Script may be run with optimisation enabled, so don't rely on assert
297# statements being executed
298def assertEqual(lhs, rhs):
299 if lhs != rhs:
300 raise AssertionError('%r != %r' % (lhs, rhs))
301def assertIs(lhs, rhs):
302 if lhs is not rhs:
303 raise AssertionError('%r is not %r' % (lhs, rhs))
304# Check basic code execution
305result = ['Top level assignment']
306def f():
307 result.append('Lower level reference')
308f()
309assertEqual(result, ['Top level assignment', 'Lower level reference'])
310# Check the sys module
311import sys
312assertIs(globals(), sys.modules[__name__].__dict__)
313argv0 = sys.argv[0]
314"""
315
316 def _make_test_script(self, script_dir, script_basename, source=None):
317 if source is None:
318 source = self.test_source
319 return make_script(script_dir, script_basename, source)
320
321 def _check_script(self, script_name, expected_name, expected_file,
322 expected_argv0, expected_package):
323 result = run_path(script_name)
324 self.assertEqual(result["__name__"], expected_name)
325 self.assertEqual(result["__file__"], expected_file)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000326 self.assertEqual(result["__cached__"], None)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000327 self.assertIn("argv0", result)
328 self.assertEqual(result["argv0"], expected_argv0)
329 self.assertEqual(result["__package__"], expected_package)
330
331 def _check_import_error(self, script_name, msg):
Nick Coghlan16eb0fb2009-11-18 11:35:25 +0000332 msg = re.escape(msg)
Ezio Melottied3a7d22010-12-01 02:32:32 +0000333 self.assertRaisesRegex(ImportError, msg, run_path, script_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000334
335 def test_basic_script(self):
336 with temp_dir() as script_dir:
337 mod_name = 'script'
338 script_name = self._make_test_script(script_dir, mod_name)
339 self._check_script(script_name, "<run_path>", script_name,
340 script_name, None)
341
342 def test_script_compiled(self):
343 with temp_dir() as script_dir:
344 mod_name = 'script'
345 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000346 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000347 os.remove(script_name)
348 self._check_script(compiled_name, "<run_path>", compiled_name,
349 compiled_name, None)
350
351 def test_directory(self):
352 with temp_dir() as script_dir:
353 mod_name = '__main__'
354 script_name = self._make_test_script(script_dir, mod_name)
355 self._check_script(script_dir, "<run_path>", script_name,
356 script_dir, '')
357
358 def test_directory_compiled(self):
359 with temp_dir() as script_dir:
360 mod_name = '__main__'
361 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000362 compiled_name = py_compile.compile(script_name, doraise=True)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000363 os.remove(script_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000364 legacy_pyc = make_legacy_pyc(script_name)
365 self._check_script(script_dir, "<run_path>", legacy_pyc,
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000366 script_dir, '')
367
368 def test_directory_error(self):
369 with temp_dir() as script_dir:
370 mod_name = 'not_main'
371 script_name = self._make_test_script(script_dir, mod_name)
372 msg = "can't find '__main__' module in %r" % script_dir
373 self._check_import_error(script_dir, msg)
374
375 def test_zipfile(self):
376 with temp_dir() as script_dir:
377 mod_name = '__main__'
378 script_name = self._make_test_script(script_dir, mod_name)
379 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
380 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
381
382 def test_zipfile_compiled(self):
383 with temp_dir() as script_dir:
384 mod_name = '__main__'
385 script_name = self._make_test_script(script_dir, mod_name)
Barry Warsaw28a691b2010-04-17 00:19:56 +0000386 compiled_name = py_compile.compile(script_name, doraise=True)
387 zip_name, fname = make_zip_script(script_dir, 'test_zip',
388 compiled_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000389 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
390
391 def test_zipfile_error(self):
392 with temp_dir() as script_dir:
393 mod_name = 'not_main'
394 script_name = self._make_test_script(script_dir, mod_name)
395 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
396 msg = "can't find '__main__' module in %r" % zip_name
397 self._check_import_error(zip_name, msg)
398
Brett Cannon31f59292011-02-21 19:29:56 +0000399 @no_tracing
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000400 def test_main_recursion_error(self):
401 with temp_dir() as script_dir, temp_dir() as dummy_dir:
402 mod_name = '__main__'
403 source = ("import runpy\n"
404 "runpy.run_path(%r)\n") % dummy_dir
405 script_name = self._make_test_script(script_dir, mod_name, source)
406 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
407 msg = "recursion depth exceeded"
Ezio Melottied3a7d22010-12-01 02:32:32 +0000408 self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000409
410
411
Thomas Woutersa9773292006-04-21 09:43:23 +0000412def test_main():
Brett Cannon61b14252010-07-03 21:48:25 +0000413 run_unittest(
414 RunModuleCodeTest,
415 RunModuleTest,
416 RunPathTest
417 )
Thomas Woutersa9773292006-04-21 09:43:23 +0000418
419if __name__ == "__main__":
420 test_main()