root / env / lib / python2.7 / site-packages / django / utils / module_loading.py @ 1a305335
History | View | Annotate | Download (2.51 KB)
1 | 1a305335 | officers | import imp |
---|---|---|---|
2 | import os |
||
3 | import sys |
||
4 | |||
5 | |||
6 | def module_has_submodule(package, module_name): |
||
7 | """See if 'module' is in 'package'."""
|
||
8 | name = ".".join([package.__name__, module_name])
|
||
9 | try:
|
||
10 | # None indicates a cached miss; see mark_miss() in Python/import.c.
|
||
11 | return sys.modules[name] is not None |
||
12 | except KeyError: |
||
13 | pass
|
||
14 | try:
|
||
15 | package_path = package.__path__ # No __path__, then not a package.
|
||
16 | except AttributeError: |
||
17 | # Since the remainder of this function assumes that we're dealing with
|
||
18 | # a package (module with a __path__), so if it's not, then bail here.
|
||
19 | return False |
||
20 | for finder in sys.meta_path: |
||
21 | if finder.find_module(name, package_path):
|
||
22 | return True |
||
23 | for entry in package_path: |
||
24 | try:
|
||
25 | # Try the cached finder.
|
||
26 | finder = sys.path_importer_cache[entry] |
||
27 | if finder is None: |
||
28 | # Implicit import machinery should be used.
|
||
29 | try:
|
||
30 | file_, _, _ = imp.find_module(module_name, [entry]) |
||
31 | if file_:
|
||
32 | file_.close() |
||
33 | return True |
||
34 | except ImportError: |
||
35 | continue
|
||
36 | # Else see if the finder knows of a loader.
|
||
37 | elif finder.find_module(name):
|
||
38 | return True |
||
39 | else:
|
||
40 | continue
|
||
41 | except KeyError: |
||
42 | # No cached finder, so try and make one.
|
||
43 | for hook in sys.path_hooks: |
||
44 | try:
|
||
45 | finder = hook(entry) |
||
46 | # XXX Could cache in sys.path_importer_cache
|
||
47 | if finder.find_module(name):
|
||
48 | return True |
||
49 | else:
|
||
50 | # Once a finder is found, stop the search.
|
||
51 | break
|
||
52 | except ImportError: |
||
53 | # Continue the search for a finder.
|
||
54 | continue
|
||
55 | else:
|
||
56 | # No finder found.
|
||
57 | # Try the implicit import machinery if searching a directory.
|
||
58 | if os.path.isdir(entry):
|
||
59 | try:
|
||
60 | file_, _, _ = imp.find_module(module_name, [entry]) |
||
61 | if file_:
|
||
62 | file_.close() |
||
63 | return True |
||
64 | except ImportError: |
||
65 | pass
|
||
66 | # XXX Could insert None or NullImporter
|
||
67 | else:
|
||
68 | # Exhausted the search, so the module cannot be found.
|
||
69 | return False |