Mercurial > cpython-lru_cache-weakref
annotate Lib/importlib/test/source/test_abc_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 | 3c9232a684ae |
| children | f82ebf9b3a52 |
| rev | line source |
|---|---|
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
1 import importlib |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
2 from importlib import abc |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
3 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
4 from .. import abc as testing_abc |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
5 from .. import util |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
6 from . import util as source_util |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
7 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
8 import imp |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
9 import inspect |
|
62761
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
10 import io |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
11 import marshal |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
12 import os |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
13 import sys |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
14 import types |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
15 import unittest |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
16 import warnings |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
17 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
18 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
19 class SourceOnlyLoaderMock(abc.SourceLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
20 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
21 # Globals that should be defined for all modules. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
22 source = (b"_ = '::'.join([__name__, __file__, __cached__, __package__, " |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
23 b"repr(__loader__)])") |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
24 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
25 def __init__(self, path): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
26 self.path = path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
27 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
28 def get_data(self, path): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
29 assert self.path == path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
30 return self.source |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
31 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
32 def get_filename(self, fullname): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
33 return self.path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
34 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
35 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
36 class SourceLoaderMock(SourceOnlyLoaderMock): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
37 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
38 source_mtime = 1 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
39 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
40 def __init__(self, path, magic=imp.get_magic()): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
41 super().__init__(path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
42 self.bytecode_path = imp.cache_from_source(self.path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
43 data = bytearray(magic) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
44 data.extend(marshal._w_long(self.source_mtime)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
45 code_object = compile(self.source, self.path, 'exec', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
46 dont_inherit=True) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
47 data.extend(marshal.dumps(code_object)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
48 self.bytecode = bytes(data) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
49 self.written = {} |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
50 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
51 def get_data(self, path): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
52 if path == self.path: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
53 return super().get_data(path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
54 elif path == self.bytecode_path: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
55 return self.bytecode |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
56 else: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
57 raise IOError |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
58 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
59 def path_mtime(self, path): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
60 assert path == self.path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
61 return self.source_mtime |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
62 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
63 def set_data(self, path, data): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
64 self.written[path] = bytes(data) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
65 return path == self.bytecode_path |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
66 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
67 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
68 class PyLoaderMock(abc.PyLoader): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
69 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
70 # Globals that should be defined for all modules. |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
71 source = (b"_ = '::'.join([__name__, __file__, __package__, " |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
72 b"repr(__loader__)])") |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
73 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
74 def __init__(self, data): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
75 """Take a dict of 'module_name: path' pairings. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
76 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
77 Paths should have no file extension, allowing packages to be denoted by |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
78 ending in '__init__'. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
79 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
80 """ |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
81 self.module_paths = data |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
82 self.path_to_module = {val:key for key,val in data.items()} |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
83 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
84 def get_data(self, path): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
85 if path not in self.path_to_module: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
86 raise IOError |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
87 return self.source |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
88 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
89 def is_package(self, name): |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
90 filename = os.path.basename(self.get_filename(name)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
91 return os.path.splitext(filename)[0] == '__init__' |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
92 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
93 def source_path(self, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
94 try: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
95 return self.module_paths[name] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
96 except KeyError: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
97 raise ImportError |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
98 |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
99 def get_filename(self, name): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
100 """Silence deprecation warning.""" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
101 with warnings.catch_warnings(record=True) as w: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
102 warnings.simplefilter("always") |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
103 path = super().get_filename(name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
104 assert len(w) == 1 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
105 assert issubclass(w[0].category, PendingDeprecationWarning) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
106 return path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
107 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
108 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
109 class PyLoaderCompatMock(PyLoaderMock): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
110 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
111 """Mock that matches what is suggested to have a loader that is compatible |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
112 from Python 3.1 onwards.""" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
113 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
114 def get_filename(self, fullname): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
115 try: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
116 return self.module_paths[fullname] |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
117 except KeyError: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
118 raise ImportError |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
119 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
120 def source_path(self, fullname): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
121 try: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
122 return self.get_filename(fullname) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
123 except ImportError: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
124 return None |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
125 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
126 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
127 class PyPycLoaderMock(abc.PyPycLoader, PyLoaderMock): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
128 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
129 default_mtime = 1 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
130 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
131 def __init__(self, source, bc={}): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
132 """Initialize mock. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
133 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
134 'bc' is a dict keyed on a module's name. The value is dict with |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
135 possible keys of 'path', 'mtime', 'magic', and 'bc'. Except for 'path', |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
136 each of those keys control if any part of created bytecode is to |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
137 deviate from default values. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
138 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
139 """ |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
140 super().__init__(source) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
141 self.module_bytecode = {} |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
142 self.path_to_bytecode = {} |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
143 self.bytecode_to_path = {} |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
144 for name, data in bc.items(): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
145 self.path_to_bytecode[data['path']] = name |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
146 self.bytecode_to_path[name] = data['path'] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
147 magic = data.get('magic', imp.get_magic()) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
148 mtime = importlib._w_long(data.get('mtime', self.default_mtime)) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
149 if 'bc' in data: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
150 bc = data['bc'] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
151 else: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
152 bc = self.compile_bc(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
153 self.module_bytecode[name] = magic + mtime + bc |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
154 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
155 def compile_bc(self, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
156 source_path = self.module_paths.get(name, '<test>') or '<test>' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
157 code = compile(self.source, source_path, 'exec') |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
158 return marshal.dumps(code) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
159 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
160 def source_mtime(self, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
161 if name in self.module_paths: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
162 return self.default_mtime |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
163 elif name in self.module_bytecode: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
164 return None |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
165 else: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
166 raise ImportError |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
167 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
168 def bytecode_path(self, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
169 try: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
170 return self.bytecode_to_path[name] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
171 except KeyError: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
172 if name in self.module_paths: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
173 return None |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
174 else: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
175 raise ImportError |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
176 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
177 def write_bytecode(self, name, bytecode): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
178 self.module_bytecode[name] = bytecode |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
179 return True |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
180 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
181 def get_data(self, path): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
182 if path in self.path_to_module: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
183 return super().get_data(path) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
184 elif path in self.path_to_bytecode: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
185 name = self.path_to_bytecode[path] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
186 return self.module_bytecode[name] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
187 else: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
188 raise IOError |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
189 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
190 def is_package(self, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
191 try: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
192 return super().is_package(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
193 except TypeError: |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
194 return '__init__' in self.bytecode_to_path[name] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
195 |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
196 def get_code(self, name): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
197 with warnings.catch_warnings(record=True) as w: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
198 warnings.simplefilter("always") |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
199 code_object = super().get_code(name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
200 assert len(w) == 1 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
201 assert issubclass(w[0].category, PendingDeprecationWarning) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
202 return code_object |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
203 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
204 class PyLoaderTests(testing_abc.LoaderTests): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
205 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
206 """Tests for importlib.abc.PyLoader.""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
207 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
208 mocker = PyLoaderMock |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
209 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
210 def eq_attrs(self, ob, **kwargs): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
211 for attr, val in kwargs.items(): |
|
52734
759f43c97887
Give a more informative message on an importlib test upon failure.
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
212 found = getattr(ob, attr) |
|
759f43c97887
Give a more informative message on an importlib test upon failure.
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
213 self.assertEqual(found, val, |
|
759f43c97887
Give a more informative message on an importlib test upon failure.
Brett Cannon <bcannon@gmail.com>
parents:
52112
diff
changeset
|
214 "{} attribute: {} != {}".format(attr, found, val)) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
215 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
216 def test_module(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
217 name = '<module>' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
218 path = os.path.join('', 'path', 'to', 'module') |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
219 mock = self.mocker({name: path}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
220 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
221 module = mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
222 self.assertTrue(name in sys.modules) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
223 self.eq_attrs(module, __name__=name, __file__=path, __package__='', |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
224 __loader__=mock) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
225 self.assertTrue(not hasattr(module, '__path__')) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
226 return mock, name |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
227 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
228 def test_package(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
229 name = '<pkg>' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
230 path = os.path.join('path', 'to', name, '__init__') |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
231 mock = self.mocker({name: path}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
232 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
233 module = mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
234 self.assertTrue(name in sys.modules) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
235 self.eq_attrs(module, __name__=name, __file__=path, |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
236 __path__=[os.path.dirname(path)], __package__=name, |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
237 __loader__=mock) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
238 return mock, name |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
239 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
240 def test_lacking_parent(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
241 name = 'pkg.mod' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
242 path = os.path.join('path', 'to', 'pkg', 'mod') |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
243 mock = self.mocker({name: path}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
244 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
245 module = mock.load_module(name) |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
246 self.assertIn(name, sys.modules) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
247 self.eq_attrs(module, __name__=name, __file__=path, __package__='pkg', |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
248 __loader__=mock) |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
249 self.assertFalse(hasattr(module, '__path__')) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
250 return mock, name |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
251 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
252 def test_module_reuse(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
253 name = 'mod' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
254 path = os.path.join('path', 'to', 'mod') |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
255 module = imp.new_module(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
256 mock = self.mocker({name: path}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
257 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
258 sys.modules[name] = module |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
259 loaded_module = mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
260 self.assertTrue(loaded_module is module) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
261 self.assertTrue(sys.modules[name] is module) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
262 return mock, name |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
263 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
264 def test_state_after_failure(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
265 name = "mod" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
266 module = imp.new_module(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
267 module.blah = None |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
268 mock = self.mocker({name: os.path.join('path', 'to', 'mod')}) |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
269 mock.source = b"1/0" |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
270 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
271 sys.modules[name] = module |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
272 with self.assertRaises(ZeroDivisionError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
273 mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
274 self.assertTrue(sys.modules[name] is module) |
|
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
275 self.assertTrue(hasattr(module, 'blah')) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
276 return mock |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
277 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
278 def test_unloadable(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
279 name = "mod" |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
280 mock = self.mocker({name: os.path.join('path', 'to', 'mod')}) |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
281 mock.source = b"1/0" |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
282 with util.uncache(name): |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
283 with self.assertRaises(ZeroDivisionError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
284 mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
285 self.assertTrue(name not in sys.modules) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
286 return mock |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
287 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
288 |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
289 class PyLoaderCompatTests(PyLoaderTests): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
290 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
291 """Test that the suggested code to make a loader that is compatible from |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
292 Python 3.1 forward works.""" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
293 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
294 mocker = PyLoaderCompatMock |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
295 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
296 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
297 class PyLoaderInterfaceTests(unittest.TestCase): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
298 |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
299 """Tests for importlib.abc.PyLoader to make sure that when source_path() |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
300 doesn't return a path everything works as expected.""" |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
301 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
302 def test_no_source_path(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
303 # No source path should lead to ImportError. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
304 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
305 mock = PyLoaderMock({}) |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
306 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
307 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
308 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
309 def test_source_path_is_None(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
310 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
311 mock = PyLoaderMock({name: None}) |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
312 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
313 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
314 |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
315 def test_get_filename_with_source_path(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
316 # get_filename() should return what source_path() returns. |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
317 name = 'mod' |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
318 path = os.path.join('path', 'to', 'source') |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
319 mock = PyLoaderMock({name: path}) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
320 with util.uncache(name): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
321 self.assertEqual(mock.get_filename(name), path) |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
322 |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
323 def test_get_filename_no_source_path(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
324 # get_filename() should raise ImportError if source_path returns None. |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
325 name = 'mod' |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
326 mock = PyLoaderMock({name: None}) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
327 with util.uncache(name), self.assertRaises(ImportError): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
328 mock.get_filename(name) |
|
52112
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
329 |
|
03080fa08dfe
Implement get_source for importlib.abc.PyLoader using source_path and get_data.
Brett Cannon <bcannon@gmail.com>
parents:
52083
diff
changeset
|
330 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
331 class PyPycLoaderTests(PyLoaderTests): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
332 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
333 """Tests for importlib.abc.PyPycLoader.""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
334 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
335 mocker = PyPycLoaderMock |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
336 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
337 @source_util.writes_bytecode_files |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
338 def verify_bytecode(self, mock, name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
339 assert name in mock.module_paths |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
340 self.assertIn(name, mock.module_bytecode) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
341 magic = mock.module_bytecode[name][:4] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
342 self.assertEqual(magic, imp.get_magic()) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
343 mtime = importlib._r_long(mock.module_bytecode[name][4:8]) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
344 self.assertEqual(mtime, 1) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
345 bc = mock.module_bytecode[name][8:] |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
346 self.assertEqual(bc, mock.compile_bc(name)) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
347 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
348 def test_module(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
349 mock, name = super().test_module() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
350 self.verify_bytecode(mock, name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
351 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
352 def test_package(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
353 mock, name = super().test_package() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
354 self.verify_bytecode(mock, name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
355 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
356 def test_lacking_parent(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
357 mock, name = super().test_lacking_parent() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
358 self.verify_bytecode(mock, name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
359 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
360 def test_module_reuse(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
361 mock, name = super().test_module_reuse() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
362 self.verify_bytecode(mock, name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
363 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
364 def test_state_after_failure(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
365 super().test_state_after_failure() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
366 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
367 def test_unloadable(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
368 super().test_unloadable() |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
369 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
370 |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
371 class PyPycLoaderInterfaceTests(unittest.TestCase): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
372 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
373 """Test for the interface of importlib.abc.PyPycLoader.""" |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
374 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
375 def get_filename_check(self, src_path, bc_path, expect): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
376 name = 'mod' |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
377 mock = PyPycLoaderMock({name: src_path}, {name: {'path': bc_path}}) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
378 with util.uncache(name): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
379 assert mock.source_path(name) == src_path |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
380 assert mock.bytecode_path(name) == bc_path |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
381 self.assertEqual(mock.get_filename(name), expect) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
382 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
383 def test_filename_with_source_bc(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
384 # When source and bytecode paths present, return the source path. |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
385 self.get_filename_check('source_path', 'bc_path', 'source_path') |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
386 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
387 def test_filename_with_source_no_bc(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
388 # With source but no bc, return source path. |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
389 self.get_filename_check('source_path', None, 'source_path') |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
390 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
391 def test_filename_with_no_source_bc(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
392 # With not source but bc, return the bc path. |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
393 self.get_filename_check(None, 'bc_path', 'bc_path') |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
394 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
395 def test_filename_with_no_source_or_bc(self): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
396 # With no source or bc, raise ImportError. |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
397 name = 'mod' |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
398 mock = PyPycLoaderMock({name: None}, {name: {'path': None}}) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
399 with util.uncache(name), self.assertRaises(ImportError): |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
400 mock.get_filename(name) |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
401 |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
402 |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
403 class SkipWritingBytecodeTests(unittest.TestCase): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
404 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
405 """Test that bytecode is properly handled based on |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
406 sys.dont_write_bytecode.""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
407 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
408 @source_util.writes_bytecode_files |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
409 def run_test(self, dont_write_bytecode): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
410 name = 'mod' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
411 mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')}) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
412 sys.dont_write_bytecode = dont_write_bytecode |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
413 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
414 mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
415 self.assertTrue((name in mock.module_bytecode) is not |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
416 dont_write_bytecode) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
417 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
418 def test_no_bytecode_written(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
419 self.run_test(True) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
420 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
421 def test_bytecode_written(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
422 self.run_test(False) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
423 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
424 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
425 class RegeneratedBytecodeTests(unittest.TestCase): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
426 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
427 """Test that bytecode is regenerated as expected.""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
428 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
429 @source_util.writes_bytecode_files |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
430 def test_different_magic(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
431 # A different magic number should lead to new bytecode. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
432 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
433 bad_magic = b'\x00\x00\x00\x00' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
434 assert bad_magic != imp.get_magic() |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
435 mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')}, |
|
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
436 {name: {'path': os.path.join('path', 'to', |
|
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
437 'mod.bytecode'), |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
438 'magic': bad_magic}}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
439 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
440 mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
441 self.assertTrue(name in mock.module_bytecode) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
442 magic = mock.module_bytecode[name][:4] |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
443 self.assertEqual(magic, imp.get_magic()) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
444 |
|
53996
0495bc47cd08
Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents:
52737
diff
changeset
|
445 @source_util.writes_bytecode_files |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
446 def test_old_mtime(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
447 # Bytecode with an older mtime should be regenerated. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
448 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
449 old_mtime = PyPycLoaderMock.default_mtime - 1 |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
450 mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')}, |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
451 {name: {'path': 'path/to/mod.bytecode', 'mtime': old_mtime}}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
452 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
453 mock.load_module(name) |
|
55593
0bb232078b8c
Merged revisions 73715 via svnmerge from
Georg Brandl <georg@python.org>
parents:
55350
diff
changeset
|
454 self.assertTrue(name in mock.module_bytecode) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
455 mtime = importlib._r_long(mock.module_bytecode[name][4:8]) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
456 self.assertEqual(mtime, PyPycLoaderMock.default_mtime) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
457 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
458 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
459 class BadBytecodeFailureTests(unittest.TestCase): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
460 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
461 """Test import failures when there is no source and parts of the bytecode |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
462 is bad.""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
463 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
464 def test_bad_magic(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
465 # A bad magic number should lead to an ImportError. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
466 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
467 bad_magic = b'\x00\x00\x00\x00' |
|
55348
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
468 bc = {name: |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
469 {'path': os.path.join('path', 'to', 'mod'), |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
470 'magic': bad_magic}} |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
471 mock = PyPycLoaderMock({name: None}, bc) |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
472 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
473 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
474 |
|
55348
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
475 def test_no_bytecode(self): |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
476 # Missing code object bytecode should lead to an EOFError. |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
477 name = 'mod' |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
478 bc = {name: {'path': os.path.join('path', 'to', 'mod'), 'bc': b''}} |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
479 mock = PyPycLoaderMock({name: None}, bc) |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
480 with util.uncache(name), self.assertRaises(EOFError): |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
481 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
482 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
483 def test_bad_bytecode(self): |
|
55348
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
484 # Malformed code object bytecode should lead to a ValueError. |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
485 name = 'mod' |
|
62584
a9ced423dace
fix test with more obviously incorrect bytecode
Benjamin Peterson <benjamin@python.org>
parents:
62581
diff
changeset
|
486 bc = {name: {'path': os.path.join('path', 'to', 'mod'), 'bc': b'1234'}} |
|
55348
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
487 mock = PyPycLoaderMock({name: None}, bc) |
|
ae2c056c46d3
Some tests in importlib.test.source.test_abc_loader were testing what happens
Brett Cannon <bcannon@gmail.com>
parents:
55347
diff
changeset
|
488 with util.uncache(name), self.assertRaises(ValueError): |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
489 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
490 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
491 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
492 def raise_ImportError(*args, **kwargs): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
493 raise ImportError |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
494 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
495 class MissingPathsTests(unittest.TestCase): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
496 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
497 """Test what happens when a source or bytecode path does not exist (either |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
498 from *_path returning None or raising ImportError).""" |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
499 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
500 def test_source_path_None(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
501 # Bytecode should be used when source_path returns None, along with |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
502 # __file__ being set to the bytecode path. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
503 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
504 bytecode_path = 'path/to/mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
505 mock = PyPycLoaderMock({name: None}, {name: {'path': bytecode_path}}) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
506 with util.uncache(name): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
507 module = mock.load_module(name) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
508 self.assertEqual(module.__file__, bytecode_path) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
509 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
510 # Testing for bytecode_path returning None handled by all tests where no |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
511 # bytecode initially exists. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
512 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
513 def test_all_paths_None(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
514 # If all *_path methods return None, raise ImportError. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
515 name = 'mod' |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
516 mock = PyPycLoaderMock({name: None}) |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
517 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
518 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
519 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
520 def test_source_path_ImportError(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
521 # An ImportError from source_path should trigger an ImportError. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
522 name = 'mod' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
523 mock = PyPycLoaderMock({}, {name: {'path': os.path.join('path', 'to', |
|
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
524 'mod')}}) |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
525 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
526 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
527 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
528 def test_bytecode_path_ImportError(self): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
529 # An ImportError from bytecode_path should trigger an ImportError. |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
530 name = 'mod' |
|
52737
f81424972ad1
importlib.test.source.test_abc_loader was making a bad assumption that all file
Brett Cannon <bcannon@gmail.com>
parents:
52735
diff
changeset
|
531 mock = PyPycLoaderMock({name: os.path.join('path', 'to', 'mod')}) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
532 bad_meth = types.MethodType(raise_ImportError, mock) |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
533 mock.bytecode_path = bad_meth |
|
55347
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
534 with util.uncache(name), self.assertRaises(ImportError): |
|
eaf4a00484bd
Update importlib.test.source.test_abc_loader to new features added in Python 3.1.
Brett Cannon <bcannon@gmail.com>
parents:
55003
diff
changeset
|
535 mock.load_module(name) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
536 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
537 |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
538 class SourceLoaderTestHarness(unittest.TestCase): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
539 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
540 def setUp(self, *, is_package=True, **kwargs): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
541 self.package = 'pkg' |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
542 if is_package: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
543 self.path = os.path.join(self.package, '__init__.py') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
544 self.name = self.package |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
545 else: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
546 module_name = 'mod' |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
547 self.path = os.path.join(self.package, '.'.join(['mod', 'py'])) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
548 self.name = '.'.join([self.package, module_name]) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
549 self.cached = imp.cache_from_source(self.path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
550 self.loader = self.loader_mock(self.path, **kwargs) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
551 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
552 def verify_module(self, module): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
553 self.assertEqual(module.__name__, self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
554 self.assertEqual(module.__file__, self.path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
555 self.assertEqual(module.__cached__, self.cached) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
556 self.assertEqual(module.__package__, self.package) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
557 self.assertEqual(module.__loader__, self.loader) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
558 values = module._.split('::') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
559 self.assertEqual(values[0], self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
560 self.assertEqual(values[1], self.path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
561 self.assertEqual(values[2], self.cached) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
562 self.assertEqual(values[3], self.package) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
563 self.assertEqual(values[4], repr(self.loader)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
564 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
565 def verify_code(self, code_object): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
566 module = imp.new_module(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
567 module.__file__ = self.path |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
568 module.__cached__ = self.cached |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
569 module.__package__ = self.package |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
570 module.__loader__ = self.loader |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
571 module.__path__ = [] |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
572 exec(code_object, module.__dict__) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
573 self.verify_module(module) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
574 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
575 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
576 class SourceOnlyLoaderTests(SourceLoaderTestHarness): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
577 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
578 """Test importlib.abc.SourceLoader for source-only loading. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
579 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
580 Reload testing is subsumed by the tests for |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
581 importlib.util.module_for_loader. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
582 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
583 """ |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
584 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
585 loader_mock = SourceOnlyLoaderMock |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
586 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
587 def test_get_source(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
588 # Verify the source code is returned as a string. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
589 # If an IOError is raised by get_data then raise ImportError. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
590 expected_source = self.loader.source.decode('utf-8') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
591 self.assertEqual(self.loader.get_source(self.name), expected_source) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
592 def raise_IOError(path): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
593 raise IOError |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
594 self.loader.get_data = raise_IOError |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
595 with self.assertRaises(ImportError): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
596 self.loader.get_source(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
597 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
598 def test_is_package(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
599 # Properly detect when loading a package. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
600 self.setUp(is_package=True) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
601 self.assertTrue(self.loader.is_package(self.name)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
602 self.setUp(is_package=False) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
603 self.assertFalse(self.loader.is_package(self.name)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
604 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
605 def test_get_code(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
606 # Verify the code object is created. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
607 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
608 self.verify_code(code_object) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
609 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
610 def test_load_module(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
611 # Loading a module should set __name__, __loader__, __package__, |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
612 # __path__ (for packages), __file__, and __cached__. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
613 # The module should also be put into sys.modules. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
614 with util.uncache(self.name): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
615 module = self.loader.load_module(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
616 self.verify_module(module) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
617 self.assertEqual(module.__path__, [os.path.dirname(self.path)]) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
618 self.assertTrue(self.name in sys.modules) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
619 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
620 def test_package_settings(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
621 # __package__ needs to be set, while __path__ is set on if the module |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
622 # is a package. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
623 # Testing the values for a package are covered by test_load_module. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
624 self.setUp(is_package=False) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
625 with util.uncache(self.name): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
626 module = self.loader.load_module(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
627 self.verify_module(module) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
628 self.assertTrue(not hasattr(module, '__path__')) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
629 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
630 def test_get_source_encoding(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
631 # Source is considered encoded in UTF-8 by default unless otherwise |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
632 # specified by an encoding line. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
633 source = "_ = 'ü'" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
634 self.loader.source = source.encode('utf-8') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
635 returned_source = self.loader.get_source(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
636 self.assertEqual(returned_source, source) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
637 source = "# coding: latin-1\n_ = ü" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
638 self.loader.source = source.encode('latin-1') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
639 returned_source = self.loader.get_source(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
640 self.assertEqual(returned_source, source) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
641 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
642 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
643 @unittest.skipIf(sys.dont_write_bytecode, "sys.dont_write_bytecode is true") |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
644 class SourceLoaderBytecodeTests(SourceLoaderTestHarness): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
645 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
646 """Test importlib.abc.SourceLoader's use of bytecode. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
647 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
648 Source-only testing handled by SourceOnlyLoaderTests. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
649 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
650 """ |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
651 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
652 loader_mock = SourceLoaderMock |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
653 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
654 def verify_code(self, code_object, *, bytecode_written=False): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
655 super().verify_code(code_object) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
656 if bytecode_written: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
657 self.assertIn(self.cached, self.loader.written) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
658 data = bytearray(imp.get_magic()) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
659 data.extend(marshal._w_long(self.loader.source_mtime)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
660 data.extend(marshal.dumps(code_object)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
661 self.assertEqual(self.loader.written[self.cached], bytes(data)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
662 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
663 def test_code_with_everything(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
664 # When everything should work. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
665 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
666 self.verify_code(code_object) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
667 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
668 def test_no_bytecode(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
669 # If no bytecode exists then move on to the source. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
670 self.loader.bytecode_path = "<does not exist>" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
671 # Sanity check |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
672 with self.assertRaises(IOError): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
673 bytecode_path = imp.cache_from_source(self.path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
674 self.loader.get_data(bytecode_path) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
675 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
676 self.verify_code(code_object, bytecode_written=True) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
677 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
678 def test_code_bad_timestamp(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
679 # Bytecode is only used when the timestamp matches the source EXACTLY. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
680 for source_mtime in (0, 2): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
681 assert source_mtime != self.loader.source_mtime |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
682 original = self.loader.source_mtime |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
683 self.loader.source_mtime = source_mtime |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
684 # If bytecode is used then EOFError would be raised by marshal. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
685 self.loader.bytecode = self.loader.bytecode[8:] |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
686 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
687 self.verify_code(code_object, bytecode_written=True) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
688 self.loader.source_mtime = original |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
689 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
690 def test_code_bad_magic(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
691 # Skip over bytecode with a bad magic number. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
692 self.setUp(magic=b'0000') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
693 # If bytecode is used then EOFError would be raised by marshal. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
694 self.loader.bytecode = self.loader.bytecode[8:] |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
695 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
696 self.verify_code(code_object, bytecode_written=True) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
697 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
698 def test_dont_write_bytecode(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
699 # Bytecode is not written if sys.dont_write_bytecode is true. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
700 # Can assume it is false already thanks to the skipIf class decorator. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
701 try: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
702 sys.dont_write_bytecode = True |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
703 self.loader.bytecode_path = "<does not exist>" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
704 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
705 self.assertNotIn(self.cached, self.loader.written) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
706 finally: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
707 sys.dont_write_bytecode = False |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
708 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
709 def test_no_set_data(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
710 # If set_data is not defined, one can still read bytecode. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
711 self.setUp(magic=b'0000') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
712 original_set_data = self.loader.__class__.set_data |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
713 try: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
714 del self.loader.__class__.set_data |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
715 code_object = self.loader.get_code(self.name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
716 self.verify_code(code_object) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
717 finally: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
718 self.loader.__class__.set_data = original_set_data |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
719 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
720 def test_set_data_raises_exceptions(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
721 # Raising NotImplementedError or IOError is okay for set_data. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
722 def raise_exception(exc): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
723 def closure(*args, **kwargs): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
724 raise exc |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
725 return closure |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
726 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
727 self.setUp(magic=b'0000') |
|
62587
b07b2c954a80
Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
62584
diff
changeset
|
728 self.loader.set_data = raise_exception(NotImplementedError) |
|
b07b2c954a80
Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
62584
diff
changeset
|
729 code_object = self.loader.get_code(self.name) |
|
b07b2c954a80
Move importlib.abc.SourceLoader to _bootstrap.
Brett Cannon <bcannon@gmail.com>
parents:
62584
diff
changeset
|
730 self.verify_code(code_object) |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
731 |
|
62761
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
732 |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
733 class SourceLoaderGetSourceTests(unittest.TestCase): |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
734 |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
735 """Tests for importlib.abc.SourceLoader.get_source().""" |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
736 |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
737 def test_default_encoding(self): |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
738 # Should have no problems with UTF-8 text. |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
739 name = 'mod' |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
740 mock = SourceOnlyLoaderMock('mod.file') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
741 source = 'x = "ü"' |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
742 mock.source = source.encode('utf-8') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
743 returned_source = mock.get_source(name) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
744 self.assertEqual(returned_source, source) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
745 |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
746 def test_decoded_source(self): |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
747 # Decoding should work. |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
748 name = 'mod' |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
749 mock = SourceOnlyLoaderMock("mod.file") |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
750 source = "# coding: Latin-1\nx='ü'" |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
751 assert source.encode('latin-1') != source.encode('utf-8') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
752 mock.source = source.encode('latin-1') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
753 returned_source = mock.get_source(name) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
754 self.assertEqual(returned_source, source) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
755 |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
756 def test_universal_newlines(self): |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
757 # PEP 302 says universal newlines should be used. |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
758 name = 'mod' |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
759 mock = SourceOnlyLoaderMock('mod.file') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
760 source = "x = 42\r\ny = -13\r\n" |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
761 mock.source = source.encode('utf-8') |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
762 expect = io.IncrementalNewlineDecoder(None, True).decode(source) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
763 self.assertEqual(mock.get_source(name), expect) |
|
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
764 |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
765 class AbstractMethodImplTests(unittest.TestCase): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
766 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
767 """Test the concrete abstractmethod implementations.""" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
768 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
769 class Loader(abc.Loader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
770 def load_module(self, fullname): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
771 super().load_module(fullname) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
772 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
773 class Finder(abc.Finder): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
774 def find_module(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
775 super().find_module(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
776 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
777 class ResourceLoader(Loader, abc.ResourceLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
778 def get_data(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
779 super().get_data(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
780 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
781 class InspectLoader(Loader, abc.InspectLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
782 def is_package(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
783 super().is_package(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
784 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
785 def get_code(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
786 super().get_code(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
787 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
788 def get_source(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
789 super().get_source(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
790 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
791 class ExecutionLoader(InspectLoader, abc.ExecutionLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
792 def get_filename(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
793 super().get_filename(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
794 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
795 class SourceLoader(ResourceLoader, ExecutionLoader, abc.SourceLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
796 pass |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
797 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
798 class PyLoader(ResourceLoader, InspectLoader, abc.PyLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
799 def source_path(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
800 super().source_path(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
801 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
802 class PyPycLoader(PyLoader, abc.PyPycLoader): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
803 def bytecode_path(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
804 super().bytecode_path(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
805 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
806 def source_mtime(self, _): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
807 super().source_mtime(_) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
808 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
809 def write_bytecode(self, _, _2): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
810 super().write_bytecode(_, _2) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
811 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
812 def raises_NotImplementedError(self, ins, *args): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
813 for method_name in args: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
814 method = getattr(ins, method_name) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
815 arg_count = len(inspect.getfullargspec(method)[0]) - 1 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
816 args = [''] * arg_count |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
817 try: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
818 method(*args) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
819 except NotImplementedError: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
820 pass |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
821 else: |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
822 msg = "{}.{} did not raise NotImplementedError" |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
823 self.fail(msg.format(ins.__class__.__name__, method_name)) |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
824 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
825 def test_Loader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
826 self.raises_NotImplementedError(self.Loader(), 'load_module') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
827 |
|
62756
34d827249ce0
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62587
diff
changeset
|
828 # XXX misplaced; should be somewhere else |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
829 def test_Finder(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
830 self.raises_NotImplementedError(self.Finder(), 'find_module') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
831 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
832 def test_ResourceLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
833 self.raises_NotImplementedError(self.ResourceLoader(), 'load_module', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
834 'get_data') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
835 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
836 def test_InspectLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
837 self.raises_NotImplementedError(self.InspectLoader(), 'load_module', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
838 'is_package', 'get_code', 'get_source') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
839 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
840 def test_ExecutionLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
841 self.raises_NotImplementedError(self.ExecutionLoader(), 'load_module', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
842 'is_package', 'get_code', 'get_source', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
843 'get_filename') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
844 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
845 def test_SourceLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
846 ins = self.SourceLoader() |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
847 # Required abstractmethods. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
848 self.raises_NotImplementedError(ins, 'get_filename', 'get_data') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
849 # Optional abstractmethods. |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
850 self.raises_NotImplementedError(ins,'path_mtime', 'set_data') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
851 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
852 def test_PyLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
853 self.raises_NotImplementedError(self.PyLoader(), 'source_path', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
854 'get_data', 'is_package') |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
855 |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
856 def test_PyPycLoader(self): |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
857 self.raises_NotImplementedError(self.PyPycLoader(), 'source_path', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
858 'source_mtime', 'bytecode_path', |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
859 'write_bytecode') |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
860 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
861 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
862 def test_main(): |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
863 from test.support import run_unittest |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
864 run_unittest(PyLoaderTests, PyLoaderCompatTests, |
|
62761
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
865 PyLoaderInterfaceTests, |
|
55355
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
866 PyPycLoaderTests, PyPycLoaderInterfaceTests, |
|
dfaf6de2c8ce
Implement the PEP 302 protocol for get_filename() as
Brett Cannon <bcannon@gmail.com>
parents:
55348
diff
changeset
|
867 SkipWritingBytecodeTests, RegeneratedBytecodeTests, |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
868 BadBytecodeFailureTests, MissingPathsTests, |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
869 SourceOnlyLoaderTests, |
|
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
870 SourceLoaderBytecodeTests, |
|
62761
32682b253ffc
Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents:
62756
diff
changeset
|
871 SourceLoaderGetSourceTests, |
|
62581
dd30bc5d184e
Implement importlib.abc.SourceLoader and deprecate PyLoader and PyPycLoader.
Brett Cannon <bcannon@gmail.com>
parents:
55355
diff
changeset
|
872 AbstractMethodImplTests) |
|
52083
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
873 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
874 |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
875 if __name__ == '__main__': |
|
c06f02a5e962
Introduce importlib.abc. The module contains various ABCs related to imports
Brett Cannon <bcannon@gmail.com>
parents:
diff
changeset
|
876 test_main() |
