annotate Lib/importlib/test/util.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 d2504d30f259
children 2dd046be2c88
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 from contextlib import contextmanager
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2 import imp
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 import os.path
52728
53916aaccf2c Check that on a platform that is expected to have a case-insensitive filesystem
Brett Cannon <bcannon@gmail.com>
parents: 51432
diff changeset
4 from test import support
51432
bc58a61f817b Factor out helper code from importlib.test.extension.test_path_hook.
Brett Cannon <bcannon@gmail.com>
parents: 51237
diff changeset
5 import unittest
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6 import sys
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8
55351
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
9 CASE_INSENSITIVE_FS = True
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
10 # Windows is the only OS that is *always* case-insensitive
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
11 # (OS X *can* be case-sensitive).
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
12 if sys.platform not in ('win32', 'cygwin'):
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
13 changed_name = __file__.upper()
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
14 if changed_name == __file__:
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
15 changed_name = __file__.lower()
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
16 if not os.path.exists(changed_name):
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
17 CASE_INSENSITIVE_FS = False
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
18
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
19
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
20 def case_insensitive_tests(test):
53996
0495bc47cd08 Tests for case-senstivity were not being skipped for darwin when installed on a
Brett Cannon <bcannon@gmail.com>
parents: 52788
diff changeset
21 """Class decorator that nullifies tests requiring a case-insensitive
50858
c86347ac6663 Tests of case-sensitivity were being executed on OSs which did not have a
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
22 file system."""
55351
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
23 return unittest.skipIf(not CASE_INSENSITIVE_FS,
5cf68d3b6532 Remove custom test-skipping code in importlib tests for unittest code.
Brett Cannon <bcannon@gmail.com>
parents: 53996
diff changeset
24 "requires a case-insensitive filesystem")(test)
50858
c86347ac6663 Tests of case-sensitivity were being executed on OSs which did not have a
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
25
c86347ac6663 Tests of case-sensitivity were being executed on OSs which did not have a
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
26
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 @contextmanager
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28 def uncache(*names):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
29 """Uncache a module from sys.modules.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
30
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 A basic sanity check is performed to prevent uncaching modules that either
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32 cannot/shouldn't be uncached.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35 for name in names:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
36 if name in ('sys', 'marshal', 'imp'):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37 raise ValueError(
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38 "cannot uncache {0} as it will break _importlib".format(name))
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
39 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 del sys.modules[name]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 except KeyError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42 pass
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 yield
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45 finally:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
46 for name in names:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
47 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48 del sys.modules[name]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49 except KeyError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
50 pass
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
51
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
52 @contextmanager
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
53 def import_state(**kwargs):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
54 """Context manager to manage the various importers and stored state in the
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
55 sys module.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
56
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
57 The 'modules' attribute is not supported as the interpreter state stores a
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58 pointer to the dict that the interpreter uses internally;
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59 reassigning to sys.modules does not have the desired effect.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
62 originals = {}
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
63 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64 for attr, default in (('meta_path', []), ('path', []),
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 ('path_hooks', []),
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
66 ('path_importer_cache', {})):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
67 originals[attr] = getattr(sys, attr)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
68 if attr in kwargs:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
69 new_value = kwargs[attr]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
70 del kwargs[attr]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
71 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
72 new_value = default
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
73 setattr(sys, attr, new_value)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
74 if len(kwargs):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
75 raise ValueError(
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
76 'unrecognized arguments: {0}'.format(kwargs.keys()))
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
77 yield
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
78 finally:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
79 for attr, value in originals.items():
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
80 setattr(sys, attr, value)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
81
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
82
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
83 class mock_modules:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
84
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
85 """A mock importer/loader."""
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
86
73973
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
87 def __init__(self, *names, module_code={}):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
88 self.modules = {}
73973
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
89 self.module_code = {}
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
90 for name in names:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
91 if not name.endswith('.__init__'):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
92 import_name = name
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
93 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
94 import_name = name[:-len('.__init__')]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
95 if '.' not in name:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
96 package = None
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
97 elif import_name == name:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
98 package = name.rsplit('.', 1)[0]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
99 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
100 package = import_name
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
101 module = imp.new_module(import_name)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
102 module.__loader__ = self
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
103 module.__file__ = '<mock __file__>'
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
104 module.__package__ = package
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
105 module.attr = name
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
106 if import_name != name:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
107 module.__path__ = ['<mock __path__>']
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
108 self.modules[import_name] = module
73973
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
109 if import_name in module_code:
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
110 self.module_code[import_name] = module_code[import_name]
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
111
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
112 def __getitem__(self, name):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
113 return self.modules[name]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
114
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
115 def find_module(self, fullname, path=None):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
116 if fullname not in self.modules:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
117 return None
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
118 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
119 return self
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
120
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
121 def load_module(self, fullname):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
122 if fullname not in self.modules:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
123 raise ImportError
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
124 else:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
125 sys.modules[fullname] = self.modules[fullname]
73973
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
126 if fullname in self.module_code:
d2504d30f259 Issue #13591: import_module potentially imports a module twice.
Meador Inge <meadori@gmail.com>
parents: 55351
diff changeset
127 self.module_code[fullname]()
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
128 return self.modules[fullname]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
129
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
130 def __enter__(self):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
131 self._uncache = uncache(*self.modules.keys())
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
132 self._uncache.__enter__()
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
133 return self
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
134
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
135 def __exit__(self, *exc_info):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
136 self._uncache.__exit__(None, None, None)