blob: 995c8917d5f030e9e23f0d6731f67647dbfbcbd4 [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
Benjamin Petersonee8712c2008-05-20 21:35:26 +00008from test.support import verbose, run_unittest, forget
Nick Coghlan260bd3e2009-11-16 06:49:25 +00009from test.script_helper import (temp_dir, make_script, compile_script,
10 make_pkg, make_zip_script, make_zip_pkg)
Christian Heimescbf3b5c2007-12-03 21:02:03 +000011
Nick Coghlan260bd3e2009-11-16 06:49:25 +000012
13from runpy import _run_code, _run_module_code, run_module, run_path
Christian Heimescbf3b5c2007-12-03 21:02:03 +000014# Note: This module can't safely test _run_module_as_main as it
15# runs its tests in the current process, which would mess with the
16# real __main__ module (usually test.regrtest)
17# See test_cmd_line_script for a test that executes that code path
Thomas Woutersa9773292006-04-21 09:43:23 +000018
19# Set up the test code and expected results
20
21class RunModuleCodeTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000022 """Unit tests for runpy._run_code and runpy._run_module_code"""
Thomas Woutersa9773292006-04-21 09:43:23 +000023
24 expected_result = ["Top level assignment", "Lower level reference"]
25 test_source = (
26 "# Check basic code execution\n"
27 "result = ['Top level assignment']\n"
28 "def f():\n"
29 " result.append('Lower level reference')\n"
30 "f()\n"
31 "# Check the sys module\n"
32 "import sys\n"
33 "run_argv0 = sys.argv[0]\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000034 "run_name_in_sys_modules = __name__ in sys.modules\n"
35 "if run_name_in_sys_modules:\n"
36 " module_in_sys_modules = globals() is sys.modules[__name__].__dict__\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000037 "# Check nested operation\n"
38 "import runpy\n"
Guido van Rossum806c2462007-08-06 23:33:07 +000039 "nested = runpy._run_module_code('x=1\\n', mod_name='<run>')\n"
Thomas Woutersa9773292006-04-21 09:43:23 +000040 )
41
Thomas Woutersed03b412007-08-28 21:37:11 +000042 def test_run_code(self):
43 saved_argv0 = sys.argv[0]
44 d = _run_code(self.test_source, {})
Nick Coghlan260bd3e2009-11-16 06:49:25 +000045 self.assertEqual(d["result"], self.expected_result)
46 self.assertIs(d["__name__"], None)
47 self.assertIs(d["__file__"], None)
48 self.assertIs(d["__loader__"], None)
49 self.assertIs(d["__package__"], None)
50 self.assertIs(d["run_argv0"], saved_argv0)
51 self.assertNotIn("run_name", d)
52 self.assertIs(sys.argv[0], saved_argv0)
Thomas Woutersa9773292006-04-21 09:43:23 +000053
54 def test_run_module_code(self):
55 initial = object()
56 name = "<Nonsense>"
57 file = "Some other nonsense"
58 loader = "Now you're just being silly"
Christian Heimescbf3b5c2007-12-03 21:02:03 +000059 package = '' # Treat as a top level module
Thomas Woutersa9773292006-04-21 09:43:23 +000060 d1 = dict(initial=initial)
61 saved_argv0 = sys.argv[0]
Thomas Woutersed03b412007-08-28 21:37:11 +000062 d2 = _run_module_code(self.test_source,
63 d1,
64 name,
65 file,
Christian Heimescbf3b5c2007-12-03 21:02:03 +000066 loader,
67 package)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000068 self.assertNotIn("result", d1)
69 self.assertIs(d2["initial"], initial)
Thomas Woutersed03b412007-08-28 21:37:11 +000070 self.assertEqual(d2["result"], self.expected_result)
71 self.assertEqual(d2["nested"]["x"], 1)
Nick Coghlan260bd3e2009-11-16 06:49:25 +000072 self.assertIs(d2["__name__"], name)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +000073 self.assertTrue(d2["run_name_in_sys_modules"])
74 self.assertTrue(d2["module_in_sys_modules"])
Nick Coghlan260bd3e2009-11-16 06:49:25 +000075 self.assertIs(d2["__file__"], file)
76 self.assertIs(d2["run_argv0"], file)
77 self.assertIs(d2["__loader__"], loader)
78 self.assertIs(d2["__package__"], package)
79 self.assertIs(sys.argv[0], saved_argv0)
80 self.assertNotIn(name, sys.modules)
Thomas Woutersed03b412007-08-28 21:37:11 +000081
Thomas Woutersa9773292006-04-21 09:43:23 +000082
83class RunModuleTest(unittest.TestCase):
Nick Coghlan260bd3e2009-11-16 06:49:25 +000084 """Unit tests for runpy.run_module"""
Thomas Woutersa9773292006-04-21 09:43:23 +000085
86 def expect_import_error(self, mod_name):
87 try:
88 run_module(mod_name)
89 except ImportError:
90 pass
91 else:
92 self.fail("Expected import error for " + mod_name)
93
94 def test_invalid_names(self):
Guido van Rossum806c2462007-08-06 23:33:07 +000095 # Builtin module
Thomas Woutersa9773292006-04-21 09:43:23 +000096 self.expect_import_error("sys")
Guido van Rossum806c2462007-08-06 23:33:07 +000097 # Non-existent modules
Thomas Woutersa9773292006-04-21 09:43:23 +000098 self.expect_import_error("sys.imp.eric")
99 self.expect_import_error("os.path.half")
100 self.expect_import_error("a.bee")
101 self.expect_import_error(".howard")
102 self.expect_import_error("..eaten")
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000103 # Package without __main__.py
104 self.expect_import_error("multiprocessing")
Thomas Woutersa9773292006-04-21 09:43:23 +0000105
106 def test_library_module(self):
107 run_module("runpy")
108
Guido van Rossum806c2462007-08-06 23:33:07 +0000109 def _add_pkg_dir(self, pkg_dir):
110 os.mkdir(pkg_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000111 pkg_fname = os.path.join(pkg_dir, "__init__.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000112 pkg_file = open(pkg_fname, "w")
113 pkg_file.close()
114 return pkg_fname
115
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000116 def _make_pkg(self, source, depth, mod_base="runpy_test"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000117 pkg_name = "__runpy_pkg__"
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000118 test_fname = mod_base+os.extsep+"py"
Thomas Woutersa9773292006-04-21 09:43:23 +0000119 pkg_dir = sub_dir = tempfile.mkdtemp()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000120 if verbose: print(" Package tree in:", sub_dir)
Thomas Woutersa9773292006-04-21 09:43:23 +0000121 sys.path.insert(0, pkg_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000122 if verbose: print(" Updated sys.path:", sys.path[0])
Thomas Woutersa9773292006-04-21 09:43:23 +0000123 for i in range(depth):
124 sub_dir = os.path.join(sub_dir, pkg_name)
Guido van Rossum806c2462007-08-06 23:33:07 +0000125 pkg_fname = self._add_pkg_dir(sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000126 if verbose: print(" Next level in:", sub_dir)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000127 if verbose: print(" Created:", pkg_fname)
Thomas Woutersa9773292006-04-21 09:43:23 +0000128 mod_fname = os.path.join(sub_dir, test_fname)
129 mod_file = open(mod_fname, "w")
130 mod_file.write(source)
131 mod_file.close()
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000132 if verbose: print(" Created:", mod_fname)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000133 mod_name = (pkg_name+".")*depth + mod_base
Thomas Woutersa9773292006-04-21 09:43:23 +0000134 return pkg_dir, mod_fname, mod_name
135
136 def _del_pkg(self, top, depth, mod_name):
Guido van Rossum806c2462007-08-06 23:33:07 +0000137 for entry in list(sys.modules):
138 if entry.startswith("__runpy_pkg__"):
Thomas Woutersa9773292006-04-21 09:43:23 +0000139 del sys.modules[entry]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000140 if verbose: print(" Removed sys.modules entries")
Thomas Woutersa9773292006-04-21 09:43:23 +0000141 del sys.path[0]
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000142 if verbose: print(" Removed sys.path entry")
Thomas Woutersa9773292006-04-21 09:43:23 +0000143 for root, dirs, files in os.walk(top, topdown=False):
144 for name in files:
145 try:
146 os.remove(os.path.join(root, name))
Guido van Rossumb940e112007-01-10 16:19:56 +0000147 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000148 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000149 for name in dirs:
150 fullname = os.path.join(root, name)
151 try:
152 os.rmdir(fullname)
Guido van Rossumb940e112007-01-10 16:19:56 +0000153 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000154 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000155 try:
156 os.rmdir(top)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000157 if verbose: print(" Removed package tree")
Guido van Rossumb940e112007-01-10 16:19:56 +0000158 except OSError as ex:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000159 if verbose: print(ex) # Persist with cleaning up
Thomas Woutersa9773292006-04-21 09:43:23 +0000160
161 def _check_module(self, depth):
162 pkg_dir, mod_fname, mod_name = (
163 self._make_pkg("x=1\n", depth))
Guido van Rossum04110fb2007-08-24 16:32:05 +0000164 forget(mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000165 try:
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000166 if verbose: print("Running from source:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000167 d1 = run_module(mod_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000168 self.assertIn("x", d1)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000169 self.assertEqual(d1["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000170 del d1 # Ensure __loader__ entry doesn't keep file open
171 __import__(mod_name)
172 os.remove(mod_fname)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000173 if verbose: print("Running from compiled:", mod_name)
Thomas Woutersa9773292006-04-21 09:43:23 +0000174 d2 = run_module(mod_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000175 self.assertIn("x", d2)
Guido van Rossume61fd5b2007-07-11 12:20:59 +0000176 self.assertEqual(d2["x"], 1)
Thomas Woutersa9773292006-04-21 09:43:23 +0000177 del d2 # Ensure __loader__ entry doesn't keep file open
178 finally:
179 self._del_pkg(pkg_dir, depth, mod_name)
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000180 if verbose: print("Module executed successfully")
Thomas Woutersa9773292006-04-21 09:43:23 +0000181
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000182 def _check_package(self, depth):
183 pkg_dir, mod_fname, mod_name = (
184 self._make_pkg("x=1\n", depth, "__main__"))
185 pkg_name, _, _ = mod_name.rpartition(".")
186 forget(mod_name)
187 try:
188 if verbose: print("Running from source:", pkg_name)
189 d1 = run_module(pkg_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000190 self.assertIn("x", d1)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000191 self.assertTrue(d1["x"] == 1)
192 del d1 # Ensure __loader__ entry doesn't keep file open
193 __import__(mod_name)
194 os.remove(mod_fname)
195 if verbose: print("Running from compiled:", pkg_name)
196 d2 = run_module(pkg_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000197 self.assertIn("x", d2)
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000198 self.assertTrue(d2["x"] == 1)
199 del d2 # Ensure __loader__ entry doesn't keep file open
200 finally:
201 self._del_pkg(pkg_dir, depth, pkg_name)
202 if verbose: print("Package executed successfully")
203
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000204 def _add_relative_modules(self, base_dir, source, depth):
Guido van Rossum806c2462007-08-06 23:33:07 +0000205 if depth <= 1:
206 raise ValueError("Relative module test needs depth > 1")
207 pkg_name = "__runpy_pkg__"
208 module_dir = base_dir
209 for i in range(depth):
210 parent_dir = module_dir
211 module_dir = os.path.join(module_dir, pkg_name)
212 # Add sibling module
Skip Montanaro7a98be22007-08-16 14:35:24 +0000213 sibling_fname = os.path.join(module_dir, "sibling.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000214 sibling_file = open(sibling_fname, "w")
215 sibling_file.close()
216 if verbose: print(" Added sibling module:", sibling_fname)
217 # Add nephew module
218 uncle_dir = os.path.join(parent_dir, "uncle")
219 self._add_pkg_dir(uncle_dir)
220 if verbose: print(" Added uncle package:", uncle_dir)
221 cousin_dir = os.path.join(uncle_dir, "cousin")
222 self._add_pkg_dir(cousin_dir)
223 if verbose: print(" Added cousin package:", cousin_dir)
Skip Montanaro7a98be22007-08-16 14:35:24 +0000224 nephew_fname = os.path.join(cousin_dir, "nephew.py")
Guido van Rossum806c2462007-08-06 23:33:07 +0000225 nephew_file = open(nephew_fname, "w")
226 nephew_file.close()
227 if verbose: print(" Added nephew module:", nephew_fname)
228
229 def _check_relative_imports(self, depth, run_name=None):
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000230 contents = r"""\
Guido van Rossum806c2462007-08-06 23:33:07 +0000231from __future__ import absolute_import
232from . import sibling
233from ..uncle.cousin import nephew
234"""
235 pkg_dir, mod_fname, mod_name = (
236 self._make_pkg(contents, depth))
237 try:
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000238 self._add_relative_modules(pkg_dir, contents, depth)
239 pkg_name = mod_name.rpartition('.')[0]
Guido van Rossum806c2462007-08-06 23:33:07 +0000240 if verbose: print("Running from source:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000241 d1 = run_module(mod_name, run_name=run_name) # Read from source
Benjamin Peterson577473f2010-01-19 00:09:57 +0000242 self.assertIn("__package__", d1)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000243 self.assertTrue(d1["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000244 self.assertIn("sibling", d1)
245 self.assertIn("nephew", d1)
Guido van Rossum806c2462007-08-06 23:33:07 +0000246 del d1 # Ensure __loader__ entry doesn't keep file open
247 __import__(mod_name)
248 os.remove(mod_fname)
249 if verbose: print("Running from compiled:", mod_name)
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000250 d2 = run_module(mod_name, run_name=run_name) # Read from bytecode
Benjamin Peterson577473f2010-01-19 00:09:57 +0000251 self.assertIn("__package__", d2)
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000252 self.assertTrue(d2["__package__"] == pkg_name)
Benjamin Peterson577473f2010-01-19 00:09:57 +0000253 self.assertIn("sibling", d2)
254 self.assertIn("nephew", d2)
Guido van Rossum806c2462007-08-06 23:33:07 +0000255 del d2 # Ensure __loader__ entry doesn't keep file open
256 finally:
257 self._del_pkg(pkg_dir, depth, mod_name)
258 if verbose: print("Module executed successfully")
259
Thomas Woutersa9773292006-04-21 09:43:23 +0000260 def test_run_module(self):
261 for depth in range(4):
Guido van Rossumbe19ed72007-02-09 05:37:30 +0000262 if verbose: print("Testing package depth:", depth)
Thomas Woutersa9773292006-04-21 09:43:23 +0000263 self._check_module(depth)
264
Benjamin Petersonf6489f92009-11-25 17:46:26 +0000265 def test_run_package(self):
266 for depth in range(1, 4):
267 if verbose: print("Testing package depth:", depth)
268 self._check_package(depth)
269
Guido van Rossum806c2462007-08-06 23:33:07 +0000270 def test_explicit_relative_import(self):
271 for depth in range(2, 5):
272 if verbose: print("Testing relative imports at depth:", depth)
273 self._check_relative_imports(depth)
274
Christian Heimescbf3b5c2007-12-03 21:02:03 +0000275 def test_main_relative_import(self):
276 for depth in range(2, 5):
277 if verbose: print("Testing main relative imports at depth:", depth)
278 self._check_relative_imports(depth, "__main__")
279
Thomas Woutersa9773292006-04-21 09:43:23 +0000280
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000281class RunPathTest(unittest.TestCase):
282 """Unit tests for runpy.run_path"""
283 # Based on corresponding tests in test_cmd_line_script
284
285 test_source = """\
286# Script may be run with optimisation enabled, so don't rely on assert
287# statements being executed
288def assertEqual(lhs, rhs):
289 if lhs != rhs:
290 raise AssertionError('%r != %r' % (lhs, rhs))
291def assertIs(lhs, rhs):
292 if lhs is not rhs:
293 raise AssertionError('%r is not %r' % (lhs, rhs))
294# Check basic code execution
295result = ['Top level assignment']
296def f():
297 result.append('Lower level reference')
298f()
299assertEqual(result, ['Top level assignment', 'Lower level reference'])
300# Check the sys module
301import sys
302assertIs(globals(), sys.modules[__name__].__dict__)
303argv0 = sys.argv[0]
304"""
305
306 def _make_test_script(self, script_dir, script_basename, source=None):
307 if source is None:
308 source = self.test_source
309 return make_script(script_dir, script_basename, source)
310
311 def _check_script(self, script_name, expected_name, expected_file,
312 expected_argv0, expected_package):
313 result = run_path(script_name)
314 self.assertEqual(result["__name__"], expected_name)
315 self.assertEqual(result["__file__"], expected_file)
316 self.assertIn("argv0", result)
317 self.assertEqual(result["argv0"], expected_argv0)
318 self.assertEqual(result["__package__"], expected_package)
319
320 def _check_import_error(self, script_name, msg):
Nick Coghlan16eb0fb2009-11-18 11:35:25 +0000321 msg = re.escape(msg)
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000322 self.assertRaisesRegexp(ImportError, msg, run_path, script_name)
323
324 def test_basic_script(self):
325 with temp_dir() as script_dir:
326 mod_name = 'script'
327 script_name = self._make_test_script(script_dir, mod_name)
328 self._check_script(script_name, "<run_path>", script_name,
329 script_name, None)
330
331 def test_script_compiled(self):
332 with temp_dir() as script_dir:
333 mod_name = 'script'
334 script_name = self._make_test_script(script_dir, mod_name)
335 compiled_name = compile_script(script_name)
336 os.remove(script_name)
337 self._check_script(compiled_name, "<run_path>", compiled_name,
338 compiled_name, None)
339
340 def test_directory(self):
341 with temp_dir() as script_dir:
342 mod_name = '__main__'
343 script_name = self._make_test_script(script_dir, mod_name)
344 self._check_script(script_dir, "<run_path>", script_name,
345 script_dir, '')
346
347 def test_directory_compiled(self):
348 with temp_dir() as script_dir:
349 mod_name = '__main__'
350 script_name = self._make_test_script(script_dir, mod_name)
351 compiled_name = compile_script(script_name)
352 os.remove(script_name)
353 self._check_script(script_dir, "<run_path>", compiled_name,
354 script_dir, '')
355
356 def test_directory_error(self):
357 with temp_dir() as script_dir:
358 mod_name = 'not_main'
359 script_name = self._make_test_script(script_dir, mod_name)
360 msg = "can't find '__main__' module in %r" % script_dir
361 self._check_import_error(script_dir, msg)
362
363 def test_zipfile(self):
364 with temp_dir() as script_dir:
365 mod_name = '__main__'
366 script_name = self._make_test_script(script_dir, mod_name)
367 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
368 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
369
370 def test_zipfile_compiled(self):
371 with temp_dir() as script_dir:
372 mod_name = '__main__'
373 script_name = self._make_test_script(script_dir, mod_name)
374 compiled_name = compile_script(script_name)
375 zip_name, fname = make_zip_script(script_dir, 'test_zip', compiled_name)
376 self._check_script(zip_name, "<run_path>", fname, zip_name, '')
377
378 def test_zipfile_error(self):
379 with temp_dir() as script_dir:
380 mod_name = 'not_main'
381 script_name = self._make_test_script(script_dir, mod_name)
382 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
383 msg = "can't find '__main__' module in %r" % zip_name
384 self._check_import_error(zip_name, msg)
385
386 def test_main_recursion_error(self):
387 with temp_dir() as script_dir, temp_dir() as dummy_dir:
388 mod_name = '__main__'
389 source = ("import runpy\n"
390 "runpy.run_path(%r)\n") % dummy_dir
391 script_name = self._make_test_script(script_dir, mod_name, source)
392 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_name)
393 msg = "recursion depth exceeded"
394 self.assertRaisesRegexp(RuntimeError, msg, run_path, zip_name)
395
396
397
Thomas Woutersa9773292006-04-21 09:43:23 +0000398def test_main():
Nick Coghlan260bd3e2009-11-16 06:49:25 +0000399 run_unittest(RunModuleCodeTest, RunModuleTest, RunPathTest)
Thomas Woutersa9773292006-04-21 09:43:23 +0000400
401if __name__ == "__main__":
402 test_main()