Mercurial > cpython-lru_cache-weakref
annotate Lib/importlib/test/source/test_file_loader.py @ 78069:a8aa77458e12 3.2
Add c_size_t and c_ssize_t to the ctypes summary table.
| author | Antoine Pitrou <solipsis@pitrou.net> |
|---|---|
| date | Thu, 12 Jul 2012 20:31:50 +0200 |
| parents | c79d0a7ac79d |
| children | 2b62348a1c03 2370ae9ee052 |
| rev | line source |
|---|---|
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
1 import importlib |
|
52142
04a5b6438526
Finish properly hiding importlib implementation code.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
2 from importlib import _bootstrap |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
3 from .. import abc |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
4 from .. import util |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
5 from . import util as source_util |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 |
|
74603
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
7 import errno |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 import imp |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
9 import marshal |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
10 import os |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 import py_compile |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
12 import shutil |
|
57137
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
13 import stat |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 import sys |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 import unittest |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
16 |
|
61692
bec818a76902
Repair test failure. Bug 8727.
Barry Warsaw <barry@python.org>
parents:
60648
diff
changeset
|
17 from test.support import make_legacy_pyc |
|
bec818a76902
Repair test failure. Bug 8727.
Barry Warsaw <barry@python.org>
parents:
60648
diff
changeset
|
18 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
19 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
20 class SimpleTest(unittest.TestCase): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
21 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
22 """Should have no issue importing a source module [basic]. And if there is |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
23 a syntax error, it should raise a SyntaxError [syntax error]. |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
24 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
25 """ |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
26 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
27 # [basic] |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
28 def test_module(self): |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
29 with source_util.create_modules('_temp') as mapping: |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
30 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
31 module = loader.load_module('_temp') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
32 self.assertTrue('_temp' in sys.modules) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
33 check = {'__name__': '_temp', '__file__': mapping['_temp'], |
|
51430
5cdc5f0c134c
Move importlib completely over to using rpartition and accepting the empty
Brett Cannon <bcannon@gmail.com>
parents:
51236
diff
changeset
|
34 '__package__': ''} |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
35 for attr, value in check.items(): |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
36 self.assertEqual(getattr(module, attr), value) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
37 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
38 def test_package(self): |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
39 with source_util.create_modules('_pkg.__init__') as mapping: |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
40 loader = _bootstrap._SourceFileLoader('_pkg', |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
41 mapping['_pkg.__init__']) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
42 module = loader.load_module('_pkg') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
43 self.assertTrue('_pkg' in sys.modules) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
44 check = {'__name__': '_pkg', '__file__': mapping['_pkg.__init__'], |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
45 '__path__': [os.path.dirname(mapping['_pkg.__init__'])], |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
46 '__package__': '_pkg'} |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
47 for attr, value in check.items(): |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
48 self.assertEqual(getattr(module, attr), value) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
49 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
50 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
51 def test_lacking_parent(self): |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
52 with source_util.create_modules('_pkg.__init__', '_pkg.mod')as mapping: |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
53 loader = _bootstrap._SourceFileLoader('_pkg.mod', |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
54 mapping['_pkg.mod']) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
55 module = loader.load_module('_pkg.mod') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
56 self.assertTrue('_pkg.mod' in sys.modules) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
57 check = {'__name__': '_pkg.mod', '__file__': mapping['_pkg.mod'], |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
58 '__package__': '_pkg'} |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
59 for attr, value in check.items(): |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
60 self.assertEqual(getattr(module, attr), value) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
61 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
62 def fake_mtime(self, fxn): |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
63 """Fake mtime to always be higher than expected.""" |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
64 return lambda name: fxn(name) + 1 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
65 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
66 def test_module_reuse(self): |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
67 with source_util.create_modules('_temp') as mapping: |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
68 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
69 module = loader.load_module('_temp') |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
70 module_id = id(module) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
71 module_dict_id = id(module.__dict__) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
72 with open(mapping['_temp'], 'w') as file: |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
73 file.write("testing_var = 42\n") |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
74 # For filesystems where the mtime is only to a second granularity, |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
75 # everything that has happened above can be too fast; |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
76 # force an mtime on the source that is guaranteed to be different |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
77 # than the original mtime. |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
78 loader.path_mtime = self.fake_mtime(loader.path_mtime) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
79 module = loader.load_module('_temp') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
80 self.assertTrue('testing_var' in module.__dict__, |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
81 "'testing_var' not in " |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
82 "{0}".format(list(module.__dict__.keys()))) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
83 self.assertEqual(module, sys.modules['_temp']) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
84 self.assertEqual(id(module), module_id) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
85 self.assertEqual(id(module.__dict__), module_dict_id) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
86 |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
87 def test_state_after_failure(self): |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
88 # A failed reload should leave the original module intact. |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
89 attributes = ('__file__', '__path__', '__package__') |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
90 value = '<test>' |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
91 name = '_temp' |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
92 with source_util.create_modules(name) as mapping: |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
93 orig_module = imp.new_module(name) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
94 for attr in attributes: |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
95 setattr(orig_module, attr, value) |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
96 with open(mapping[name], 'w') as file: |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
97 file.write('+++ bad syntax +++') |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
98 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
|
55737
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
99 with self.assertRaises(SyntaxError): |
|
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
100 loader.load_module(name) |
|
51232
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
101 for attr in attributes: |
|
815589d62a9a
Move source loader tests (including reload tests) over to
Brett Cannon <bcannon@gmail.com>
parents:
50845
diff
changeset
|
102 self.assertEqual(getattr(orig_module, attr), value) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
103 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
104 # [syntax error] |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
105 def test_bad_syntax(self): |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
106 with source_util.create_modules('_temp') as mapping: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
107 with open(mapping['_temp'], 'w') as file: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
108 file.write('=') |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
109 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
|
55737
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
110 with self.assertRaises(SyntaxError): |
|
b65ebed06eb0
Move over to using assertRaises as a context manager for importlib tests.
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
111 loader.load_module('_temp') |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
112 self.assertTrue('_temp' not in sys.modules) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
113 |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
114 def test_file_from_empty_string_dir(self): |
|
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
115 # Loading a module found from an empty string entry on sys.path should |
|
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
116 # not only work, but keep all attributes relative. |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
117 file_path = '_temp.py' |
|
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
118 with open(file_path, 'w') as file: |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
119 file.write("# test file for importlib") |
|
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
120 try: |
|
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
121 with util.uncache('_temp'): |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
122 loader = _bootstrap._SourceFileLoader('_temp', file_path) |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
123 mod = loader.load_module('_temp') |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
124 self.assertEqual(file_path, mod.__file__) |
|
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
125 self.assertEqual(imp.cache_from_source(file_path), |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
126 mod.__cached__) |
|
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
127 finally: |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
128 os.unlink(file_path) |
|
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
129 pycache = os.path.dirname(imp.cache_from_source(file_path)) |
|
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
130 shutil.rmtree(pycache) |
|
62760
34f015bca54e
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
131 |
|
74596
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
132 def test_timestamp_overflow(self): |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
133 # When a modification timestamp is larger than 2**32, it should be |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
134 # truncated rather than raise an OverflowError. |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
135 with source_util.create_modules('_temp') as mapping: |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
136 source = mapping['_temp'] |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
137 compiled = imp.cache_from_source(source) |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
138 with open(source, 'w') as f: |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
139 f.write("x = 5") |
|
74603
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
140 try: |
|
74611
c79d0a7ac79d
Port import fixes from 2.7.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74603
diff
changeset
|
141 os.utime(source, (2 ** 33 - 5, 2 ** 33 - 5)) |
|
74603
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
142 except OverflowError: |
|
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
143 self.skipTest("cannot set modification time to large integer") |
|
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
144 except OSError as e: |
|
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
145 if e.errno != getattr(errno, 'EOVERFLOW', None): |
|
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
146 raise |
|
37ac757a9145
Port remaining test fixes, and fix test_importlib too.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74596
diff
changeset
|
147 self.skipTest("cannot set modification time to large integer ({})".format(e)) |
|
74596
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
148 loader = _bootstrap._SourceFileLoader('_temp', mapping['_temp']) |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
149 mod = loader.load_module('_temp') |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
150 # Sanity checks. |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
151 self.assertEqual(mod.__cached__, compiled) |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
152 self.assertEqual(mod.x, 5) |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
153 # The pyc file was created. |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
154 os.stat(compiled) |
|
a2f3d6986bfa
Issue #11235: Fix OverflowError when trying to import a source file whose modification time doesn't fit in a 32-bit timestamp.
Antoine Pitrou <solipsis@pitrou.net>
parents:
74201
diff
changeset
|
155 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
156 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
157 class BadBytecodeTest(unittest.TestCase): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
158 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
159 def import_(self, file, module_name): |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
160 loader = self.loader(module_name, file) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
161 module = loader.load_module(module_name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
53996
diff
changeset
|
162 self.assertTrue(module_name in sys.modules) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
163 |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
164 def manipulate_bytecode(self, name, mapping, manipulator, *, |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
165 del_source=False): |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
166 """Manipulate the bytecode of a module by passing it into a callable |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
167 that returns what to use as the new bytecode.""" |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
168 try: |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
169 del sys.modules['_temp'] |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
170 except KeyError: |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
171 pass |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
172 py_compile.compile(mapping[name]) |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
173 if not del_source: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
174 bytecode_path = imp.cache_from_source(mapping[name]) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
175 else: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
176 os.unlink(mapping[name]) |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
177 bytecode_path = make_legacy_pyc(mapping[name]) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
178 if manipulator: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
179 with open(bytecode_path, 'rb') as file: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
180 bc = file.read() |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
181 new_bc = manipulator(bc) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
182 with open(bytecode_path, 'wb') as file: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
183 if new_bc is not None: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
184 file.write(new_bc) |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
185 return bytecode_path |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
186 |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
187 def _test_empty_file(self, test, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
188 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
189 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
190 lambda bc: b'', |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
191 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
192 test('_temp', mapping, bc_path) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
193 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
194 @source_util.writes_bytecode_files |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
195 def _test_partial_magic(self, test, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
196 # When their are less than 4 bytes to a .pyc, regenerate it if |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
197 # possible, else raise ImportError. |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
198 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
199 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
200 lambda bc: bc[:3], |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
201 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
202 test('_temp', mapping, bc_path) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
203 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
204 def _test_magic_only(self, test, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
205 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
206 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
207 lambda bc: bc[:4], |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
208 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
209 test('_temp', mapping, bc_path) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
210 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
211 def _test_partial_timestamp(self, test, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
212 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
213 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
214 lambda bc: bc[:7], |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
215 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
216 test('_temp', mapping, bc_path) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
217 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
218 def _test_no_marshal(self, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
219 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
220 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
221 lambda bc: bc[:8], |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
222 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
223 file_path = mapping['_temp'] if not del_source else bc_path |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
224 with self.assertRaises(EOFError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
225 self.import_(file_path, '_temp') |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
226 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
227 def _test_non_code_marshal(self, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
228 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
229 bytecode_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
230 lambda bc: bc[:8] + marshal.dumps(b'abcd'), |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
231 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
232 file_path = mapping['_temp'] if not del_source else bytecode_path |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
233 with self.assertRaises(ImportError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
234 self.import_(file_path, '_temp') |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
235 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
236 def _test_bad_marshal(self, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
237 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
238 bytecode_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
239 lambda bc: bc[:8] + b'<test>', |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
240 del_source=del_source) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
241 file_path = mapping['_temp'] if not del_source else bytecode_path |
|
71131
edba722f3b02
Closes #12291: Fixed bug which was found when doing multiple loads from one stream.
Vinay Sajip <vinay_sajip@yahoo.co.uk>
parents:
68257
diff
changeset
|
242 with self.assertRaises(EOFError): |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
243 self.import_(file_path, '_temp') |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
244 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
245 def _test_bad_magic(self, test, *, del_source=False): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
246 with source_util.create_modules('_temp') as mapping: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
247 bc_path = self.manipulate_bytecode('_temp', mapping, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
248 lambda bc: b'\x00\x00\x00\x00' + bc[4:]) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
249 test('_temp', mapping, bc_path) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
250 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
251 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
252 class SourceLoaderBadBytecodeTest(BadBytecodeTest): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
253 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
254 loader = _bootstrap._SourceFileLoader |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
255 |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
256 @source_util.writes_bytecode_files |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
257 def test_empty_file(self): |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
258 # When a .pyc is empty, regenerate it if possible, else raise |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
259 # ImportError. |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
260 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
261 self.import_(mapping[name], name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
262 with open(bytecode_path, 'rb') as file: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
263 self.assertGreater(len(file.read()), 8) |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
264 |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
265 self._test_empty_file(test) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
266 |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
267 def test_partial_magic(self): |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
268 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
269 self.import_(mapping[name], name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
270 with open(bytecode_path, 'rb') as file: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
271 self.assertGreater(len(file.read()), 8) |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
272 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
273 self._test_partial_magic(test) |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
274 |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
275 @source_util.writes_bytecode_files |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
276 def test_magic_only(self): |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
277 # When there is only the magic number, regenerate the .pyc if possible, |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
278 # else raise EOFError. |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
279 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
280 self.import_(mapping[name], name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
281 with open(bytecode_path, 'rb') as file: |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
282 self.assertGreater(len(file.read()), 8) |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
283 |
|
74201
cd30304713ae
Fix no-op tests in importlib.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71131
diff
changeset
|
284 self._test_magic_only(test) |
|
cd30304713ae
Fix no-op tests in importlib.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71131
diff
changeset
|
285 |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
286 @source_util.writes_bytecode_files |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
287 def test_bad_magic(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
288 # When the magic number is different, the bytecode should be |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
289 # regenerated. |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
290 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
291 self.import_(mapping[name], name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
292 with open(bytecode_path, 'rb') as bytecode_file: |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
293 self.assertEqual(bytecode_file.read(4), imp.get_magic()) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
294 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
295 self._test_bad_magic(test) |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
296 |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
297 @source_util.writes_bytecode_files |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
298 def test_partial_timestamp(self): |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
299 # When the timestamp is partial, regenerate the .pyc, else |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
300 # raise EOFError. |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
301 def test(name, mapping, bc_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
302 self.import_(mapping[name], name) |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
303 with open(bc_path, 'rb') as file: |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
304 self.assertGreater(len(file.read()), 8) |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
305 |
|
74201
cd30304713ae
Fix no-op tests in importlib.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71131
diff
changeset
|
306 self._test_partial_timestamp(test) |
|
cd30304713ae
Fix no-op tests in importlib.
Antoine Pitrou <solipsis@pitrou.net>
parents:
71131
diff
changeset
|
307 |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
308 @source_util.writes_bytecode_files |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
309 def test_no_marshal(self): |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
310 # When there is only the magic number and timestamp, raise EOFError. |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
311 self._test_no_marshal() |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
312 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52142
diff
changeset
|
313 @source_util.writes_bytecode_files |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
314 def test_non_code_marshal(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
315 self._test_non_code_marshal() |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
316 # XXX ImportError when sourceless |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
317 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
318 # [bad marshal] |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
319 @source_util.writes_bytecode_files |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
320 def test_bad_marshal(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
321 # Bad marshal data should raise a ValueError. |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
322 self._test_bad_marshal() |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
323 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
324 # [bad timestamp] |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52142
diff
changeset
|
325 @source_util.writes_bytecode_files |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
326 def test_old_timestamp(self): |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
327 # When the timestamp is older than the source, bytecode should be |
|
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
328 # regenerated. |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
329 zeros = b'\x00\x00\x00\x00' |
|
51234
4b3246c83e3f
Split out support code that is specific to source tests out of
Brett Cannon <bcannon@gmail.com>
parents:
51232
diff
changeset
|
330 with source_util.create_modules('_temp') as mapping: |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
331 py_compile.compile(mapping['_temp']) |
| 60648 | 332 bytecode_path = imp.cache_from_source(mapping['_temp']) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
333 with open(bytecode_path, 'r+b') as bytecode_file: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
334 bytecode_file.seek(4) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
335 bytecode_file.write(zeros) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
336 self.import_(mapping['_temp'], '_temp') |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
337 source_mtime = os.path.getmtime(mapping['_temp']) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
338 source_timestamp = importlib._w_long(source_mtime) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
339 with open(bytecode_path, 'rb') as bytecode_file: |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
340 bytecode_file.seek(4) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
341 self.assertEqual(bytecode_file.read(4), source_timestamp) |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
342 |
|
57137
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
343 # [bytecode read-only] |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
344 @source_util.writes_bytecode_files |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
345 def test_read_only_bytecode(self): |
|
58960
52823820dce1
Merged revisions 78242 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
57137
diff
changeset
|
346 # When bytecode is read-only but should be rewritten, fail silently. |
|
57137
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
347 with source_util.create_modules('_temp') as mapping: |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
348 # Create bytecode that will need to be re-created. |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
349 py_compile.compile(mapping['_temp']) |
| 60648 | 350 bytecode_path = imp.cache_from_source(mapping['_temp']) |
|
57137
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
351 with open(bytecode_path, 'r+b') as bytecode_file: |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
352 bytecode_file.seek(0) |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
353 bytecode_file.write(b'\x00\x00\x00\x00') |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
354 # Make the bytecode read-only. |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
355 os.chmod(bytecode_path, |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
356 stat.S_IRUSR | stat.S_IRGRP | stat.S_IROTH) |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
357 try: |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
358 # Should not raise IOError! |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
359 self.import_(mapping['_temp'], '_temp') |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
360 finally: |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
361 # Make writable for eventual clean-up. |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
362 os.chmod(bytecode_path, stat.S_IWUSR) |
|
e25288bb6faf
Merged revisions 76146 via svnmerge from
Brett Cannon <bcannon@gmail.com>
parents:
55593
diff
changeset
|
363 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
364 |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
365 class SourcelessLoaderBadBytecodeTest(BadBytecodeTest): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
366 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
367 loader = _bootstrap._SourcelessFileLoader |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
368 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
369 def test_empty_file(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
370 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
371 with self.assertRaises(ImportError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
372 self.import_(bytecode_path, name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
373 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
374 self._test_empty_file(test, del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
375 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
376 def test_partial_magic(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
377 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
378 with self.assertRaises(ImportError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
379 self.import_(bytecode_path, name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
380 self._test_partial_magic(test, del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
381 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
382 def test_magic_only(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
383 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
384 with self.assertRaises(EOFError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
385 self.import_(bytecode_path, name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
386 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
387 self._test_magic_only(test, del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
388 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
389 def test_bad_magic(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
390 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
391 with self.assertRaises(ImportError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
392 self.import_(bytecode_path, name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
393 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
394 self._test_bad_magic(test, del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
395 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
396 def test_partial_timestamp(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
397 def test(name, mapping, bytecode_path): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
398 with self.assertRaises(EOFError): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
399 self.import_(bytecode_path, name) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
400 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
401 self._test_partial_timestamp(test, del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
402 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
403 def test_no_marshal(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
404 self._test_no_marshal(del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
405 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
406 def test_non_code_marshal(self): |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
407 self._test_non_code_marshal(del_source=True) |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
408 |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
409 |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
410 def test_main(): |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
411 from test.support import run_unittest |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
412 run_unittest(SimpleTest, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
413 SourceLoaderBadBytecodeTest, |
|
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
61692
diff
changeset
|
414 SourcelessLoaderBadBytecodeTest |
|
64239
a412ceeef0f1
Make sure that no __pycache__ directory is needlessly left behind when testing
Brett Cannon <bcannon@gmail.com>
parents:
62760
diff
changeset
|
415 ) |
|
50845
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
416 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
417 |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
418 if __name__ == '__main__': |
|
aac51b70c74c
Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
419 test_main() |
