Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(5)

Side by Side Diff: Lib/test/test_runpy.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/test/test_robotparser.py ('k') | Lib/test/test_shutil.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 # Test the runpy module 1 # Test the runpy module
2 import unittest 2 import unittest
3 import os 3 import os
4 import os.path 4 import os.path
5 import sys 5 import sys
6 import re 6 import re
7 import tempfile 7 import tempfile
8 import py_compile 8 import py_compile
9 from test.support import ( 9 from test.support import (
10 forget, make_legacy_pyc, run_unittest, unload, verbose, no_tracing, 10 forget, make_legacy_pyc, run_unittest, unload, verbose, no_tracing)
11 create_empty_file)
12 from test.script_helper import ( 11 from test.script_helper import (
13 make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir) 12 make_pkg, make_script, make_zip_pkg, make_zip_script, temp_dir)
14 13
15 14
16 from runpy import _run_code, _run_module_code, run_module, run_path 15 from runpy import _run_code, _run_module_code, run_module, run_path
17 # Note: This module can't safely test _run_module_as_main as it 16 # Note: This module can't safely test _run_module_as_main as it
18 # runs its tests in the current process, which would mess with the 17 # runs its tests in the current process, which would mess with the
19 # real __main__ module (usually test.regrtest) 18 # real __main__ module (usually test.regrtest)
20 # See test_cmd_line_script for a test that executes that code path 19 # See test_cmd_line_script for a test that executes that code path
21 20
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 self.expect_import_error("..eaten") 106 self.expect_import_error("..eaten")
108 # Package without __main__.py 107 # Package without __main__.py
109 self.expect_import_error("multiprocessing") 108 self.expect_import_error("multiprocessing")
110 109
111 def test_library_module(self): 110 def test_library_module(self):
112 run_module("runpy") 111 run_module("runpy")
113 112
114 def _add_pkg_dir(self, pkg_dir): 113 def _add_pkg_dir(self, pkg_dir):
115 os.mkdir(pkg_dir) 114 os.mkdir(pkg_dir)
116 pkg_fname = os.path.join(pkg_dir, "__init__.py") 115 pkg_fname = os.path.join(pkg_dir, "__init__.py")
117 create_empty_file(pkg_fname) 116 pkg_file = open(pkg_fname, "w")
117 pkg_file.close()
118 return pkg_fname 118 return pkg_fname
119 119
120 def _make_pkg(self, source, depth, mod_base="runpy_test"): 120 def _make_pkg(self, source, depth, mod_base="runpy_test"):
121 pkg_name = "__runpy_pkg__" 121 pkg_name = "__runpy_pkg__"
122 test_fname = mod_base+os.extsep+"py" 122 test_fname = mod_base+os.extsep+"py"
123 pkg_dir = sub_dir = tempfile.mkdtemp() 123 pkg_dir = sub_dir = tempfile.mkdtemp()
124 if verbose: print(" Package tree in:", sub_dir) 124 if verbose: print(" Package tree in:", sub_dir)
125 sys.path.insert(0, pkg_dir) 125 sys.path.insert(0, pkg_dir)
126 if verbose: print(" Updated sys.path:", sys.path[0]) 126 if verbose: print(" Updated sys.path:", sys.path[0])
127 for i in range(depth): 127 for i in range(depth):
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
212 def _add_relative_modules(self, base_dir, source, depth): 212 def _add_relative_modules(self, base_dir, source, depth):
213 if depth <= 1: 213 if depth <= 1:
214 raise ValueError("Relative module test needs depth > 1") 214 raise ValueError("Relative module test needs depth > 1")
215 pkg_name = "__runpy_pkg__" 215 pkg_name = "__runpy_pkg__"
216 module_dir = base_dir 216 module_dir = base_dir
217 for i in range(depth): 217 for i in range(depth):
218 parent_dir = module_dir 218 parent_dir = module_dir
219 module_dir = os.path.join(module_dir, pkg_name) 219 module_dir = os.path.join(module_dir, pkg_name)
220 # Add sibling module 220 # Add sibling module
221 sibling_fname = os.path.join(module_dir, "sibling.py") 221 sibling_fname = os.path.join(module_dir, "sibling.py")
222 create_empty_file(sibling_fname) 222 sibling_file = open(sibling_fname, "w")
223 sibling_file.close()
223 if verbose: print(" Added sibling module:", sibling_fname) 224 if verbose: print(" Added sibling module:", sibling_fname)
224 # Add nephew module 225 # Add nephew module
225 uncle_dir = os.path.join(parent_dir, "uncle") 226 uncle_dir = os.path.join(parent_dir, "uncle")
226 self._add_pkg_dir(uncle_dir) 227 self._add_pkg_dir(uncle_dir)
227 if verbose: print(" Added uncle package:", uncle_dir) 228 if verbose: print(" Added uncle package:", uncle_dir)
228 cousin_dir = os.path.join(uncle_dir, "cousin") 229 cousin_dir = os.path.join(uncle_dir, "cousin")
229 self._add_pkg_dir(cousin_dir) 230 self._add_pkg_dir(cousin_dir)
230 if verbose: print(" Added cousin package:", cousin_dir) 231 if verbose: print(" Added cousin package:", cousin_dir)
231 nephew_fname = os.path.join(cousin_dir, "nephew.py") 232 nephew_fname = os.path.join(cousin_dir, "nephew.py")
232 create_empty_file(nephew_fname) 233 nephew_file = open(nephew_fname, "w")
234 nephew_file.close()
233 if verbose: print(" Added nephew module:", nephew_fname) 235 if verbose: print(" Added nephew module:", nephew_fname)
234 236
235 def _check_relative_imports(self, depth, run_name=None): 237 def _check_relative_imports(self, depth, run_name=None):
236 contents = r"""\ 238 contents = r"""\
237 from __future__ import absolute_import 239 from __future__ import absolute_import
238 from . import sibling 240 from . import sibling
239 from ..uncle.cousin import nephew 241 from ..uncle.cousin import nephew
240 """ 242 """
241 pkg_dir, mod_fname, mod_name = ( 243 pkg_dir, mod_fname, mod_name = (
242 self._make_pkg(contents, depth)) 244 self._make_pkg(contents, depth))
(...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 def test_main_recursion_error(self): 400 def test_main_recursion_error(self):
399 with temp_dir() as script_dir, temp_dir() as dummy_dir: 401 with temp_dir() as script_dir, temp_dir() as dummy_dir:
400 mod_name = '__main__' 402 mod_name = '__main__'
401 source = ("import runpy\n" 403 source = ("import runpy\n"
402 "runpy.run_path(%r)\n") % dummy_dir 404 "runpy.run_path(%r)\n") % dummy_dir
403 script_name = self._make_test_script(script_dir, mod_name, source) 405 script_name = self._make_test_script(script_dir, mod_name, source)
404 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_nam e) 406 zip_name, fname = make_zip_script(script_dir, 'test_zip', script_nam e)
405 msg = "recursion depth exceeded" 407 msg = "recursion depth exceeded"
406 self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name) 408 self.assertRaisesRegex(RuntimeError, msg, run_path, zip_name)
407 409
408 def test_encoding(self):
409 with temp_dir() as script_dir:
410 filename = os.path.join(script_dir, 'script.py')
411 with open(filename, 'w', encoding='latin1') as f:
412 f.write("""
413 #coding:latin1
414 "non-ASCII: h\xe9"
415 """)
416 result = run_path(filename)
417 self.assertEqual(result['__doc__'], "non-ASCII: h\xe9")
418 410
419 411
420 def test_main(): 412 def test_main():
421 run_unittest( 413 run_unittest(
422 RunModuleCodeTest, 414 RunModuleCodeTest,
423 RunModuleTest, 415 RunModuleTest,
424 RunPathTest 416 RunPathTest
425 ) 417 )
426 418
427 if __name__ == "__main__": 419 if __name__ == "__main__":
428 test_main() 420 test_main()
OLDNEW
« no previous file with comments | « Lib/test/test_robotparser.py ('k') | Lib/test/test_shutil.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+