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

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

Issue 20778: ModuleFinder.load_module skips incorrect number of bytes in pyc files
Patch Set: Created 6 years, 1 month 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/modulefinder.py ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import os 1 import os
2 import errno 2 import errno
3 import imp
4 import py_compile
3 import shutil 5 import shutil
4 import unittest 6 import unittest
5 import tempfile 7 import tempfile
6 8
7 from test import support 9 from test import support
8 10
9 import modulefinder 11 import modulefinder
10 12
11 TEST_DIR = tempfile.mkdtemp() 13 TEST_DIR = tempfile.mkdtemp()
12 TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)] 14 TEST_PATH = [TEST_DIR, os.path.dirname(tempfile.__file__)]
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 relative_import_test_4 = [ 201 relative_import_test_4 = [
200 "a.module", 202 "a.module",
201 ["a", "a.module"], 203 ["a", "a.module"],
202 [], 204 [],
203 [], 205 [],
204 """\ 206 """\
205 a/__init__.py 207 a/__init__.py
206 def foo(): pass 208 def foo(): pass
207 a/module.py 209 a/module.py
208 from . import * 210 from . import *
211 """]
212
213 load_pycompiled_module_test = [
214 "",
215 [],
216 [],
217 [],
218 """\
219 a/__init__.py
220 foo = 1
209 """] 221 """]
210 222
211 223
212 def open_file(path): 224 def open_file(path):
213 dirname = os.path.dirname(path) 225 dirname = os.path.dirname(path)
214 try: 226 try:
215 os.makedirs(dirname) 227 os.makedirs(dirname)
216 except OSError as e: 228 except OSError as e:
217 if e.errno != errno.EEXIST: 229 if e.errno != errno.EEXIST:
218 raise 230 raise
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
280 self._do_test(relative_import_test) 292 self._do_test(relative_import_test)
281 293
282 def test_relative_imports_2(self): 294 def test_relative_imports_2(self):
283 self._do_test(relative_import_test_2) 295 self._do_test(relative_import_test_2)
284 296
285 def test_relative_imports_3(self): 297 def test_relative_imports_3(self):
286 self._do_test(relative_import_test_3) 298 self._do_test(relative_import_test_3)
287 299
288 def test_relative_imports_4(self): 300 def test_relative_imports_4(self):
289 self._do_test(relative_import_test_4) 301 self._do_test(relative_import_test_4)
302
303 def test_load_pycompiled_module(self):
304 # ModuleFinder.load_module will raise ValueError, if load_module
305 # shifts incorrect number of bytes before trying to marshal.load
306 # the bytecode itself
307 create_package(load_pycompiled_module_test[-1])
308 mf = modulefinder.ModuleFinder()
309 init_path = os.path.join(TEST_DIR, "a", "__init__.py")
310 init_compiled = py_compile.compile(init_path)
311
312 fp = open(init_compiled, "rb")
313 try:
314 # pass only the "important" values here
315 mf.load_module(None, fp, None, (None, None, imp.PY_COMPILED))
316 except ValueError as e:
317 self.fail('ModuleFinder.load_module failed with {0}: {1}.'.\
318 format(type(e).__name__, e))
319 finally:
320 fp.close()
321 shutil.rmtree(TEST_DIR)
290 322
291 323
292 def test_main(): 324 def test_main():
293 support.run_unittest(ModuleFinderTest) 325 support.run_unittest(ModuleFinderTest)
294 326
295 if __name__ == "__main__": 327 if __name__ == "__main__":
296 unittest.main() 328 unittest.main()
OLDNEW
« no previous file with comments | « Lib/modulefinder.py ('k') | no next file » | no next file with comments »

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