annotate Lib/imputil.py @ 58925:eecf1bef052d 2.6

Merged revisions 78207 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line #7930: fix stripid ........
author Ezio Melotti <ezio.melotti@gmail.com>
date Tue, 16 Feb 2010 23:29:44 +0000
parents a1b0936b027c
children 751c6dd71b5e 57ee0793e462
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
19690
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
1 """
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
2 Import utilities
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
3
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
4 Exported classes:
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
5 ImportManager Manage the import process
13021
30a4e1395211 now that imputil is in the main Python repository, clean up the header
Greg Stein <gstein@lyra.org>
parents: 11823
diff changeset
6
19690
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
7 Importer Base class for replacing standard import functions
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
8 BuiltinImporter Emulate the import mechanism for builtin and frozen modules
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
9
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
10 DynLoadSuffixImporter
6b82853df2ed Added docstring by Neal Norwitz. This closes SF bug #450979.
Fred Drake <fdrake@acm.org>
parents: 18857
diff changeset
11 """
46749
2028fa2fc1dd Deprecate the imputil module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 40831
diff changeset
12 from warnings import warnpy3k
2028fa2fc1dd Deprecate the imputil module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 40831
diff changeset
13 warnpy3k("the imputil module has been removed in Python 3.0", stacklevel=2)
2028fa2fc1dd Deprecate the imputil module for removal in 3.0.
Brett Cannon <bcannon@gmail.com>
parents: 40831
diff changeset
14 del warnpy3k
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
15
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
16 # note: avoid importing non-builtin modules
56812
a1b0936b027c Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46749
diff changeset
17 import imp ### not available in Jython?
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
18 import sys
11492
ceb06de23faa turn SysPathImporter into PathImporter.
Greg Stein <gstein@lyra.org>
parents: 11491
diff changeset
19 import __builtin__
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
20
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
21 # for the DirectoryImporter
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
22 import struct
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
23 import marshal
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
24
16198
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
25 __all__ = ["ImportManager","Importer","BuiltinImporter"]
32755b456369 a few more modules get __all__
Skip Montanaro <skip@pobox.com>
parents: 15868
diff changeset
26
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
27 _StringType = type('')
56812
a1b0936b027c Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46749
diff changeset
28 _ModuleType = type(sys) ### doesn't work in Jython...
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
29
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
30 class ImportManager:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
31 "Manage the import process."
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
32
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
33 def install(self, namespace=vars(__builtin__)):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
34 "Install this ImportManager into the specified namespace."
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
35
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
36 if isinstance(namespace, _ModuleType):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
37 namespace = vars(namespace)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
38
17335
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
39 # Note: we have no notion of "chaining"
11819
bf1501cf5f08 shift demo importers to importers.py (destined for Demo/ ?)
Greg Stein <gstein@lyra.org>
parents: 11641
diff changeset
40
17335
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
41 # Record the previous import hook, then install our own.
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
42 self.previous_importer = namespace['__import__']
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
43 self.namespace = namespace
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
44 namespace['__import__'] = self._import_hook
17335
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
45
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
46 ### fix this
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
47 #namespace['reload'] = self._reload_hook
11819
bf1501cf5f08 shift demo importers to importers.py (destined for Demo/ ?)
Greg Stein <gstein@lyra.org>
parents: 11641
diff changeset
48
17335
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
49 def uninstall(self):
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
50 "Restore the previous import mechanism."
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
51 self.namespace['__import__'] = self.previous_importer
ccae412bbc3e Add an uninstall method to the ImportManager.
Greg Stein <gstein@lyra.org>
parents: 16564
diff changeset
52
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
53 def add_suffix(self, suffix, importFunc):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
54 assert callable(importFunc)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
55 self.fs_imp.add_suffix(suffix, importFunc)
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
56
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
57 ######################################################################
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
58 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
59 # PRIVATE METHODS
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
60 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
61
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
62 clsFilesystemImporter = None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
63
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
64 def __init__(self, fs_imp=None):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
65 # we're definitely going to be importing something in the future,
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
66 # so let's just load the OS-related facilities.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
67 if not _os_stat:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
68 _os_bootstrap()
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
69
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
70 # This is the Importer that we use for grabbing stuff from the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
71 # filesystem. It defines one more method (import_from_dir) for our use.
23613
4a1f5bd486ff Replaced boolean test with is None
Raymond Hettinger <python@rcn.com>
parents: 21919
diff changeset
72 if fs_imp is None:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
73 cls = self.clsFilesystemImporter or _FilesystemImporter
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
74 fs_imp = cls()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
75 self.fs_imp = fs_imp
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
76
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
77 # Initialize the set of suffixes that we recognize and import.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
78 # The default will import dynamic-load modules first, followed by
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
79 # .py files (or a .py file's cached bytecode)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
80 for desc in imp.get_suffixes():
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
81 if desc[2] == imp.C_EXTENSION:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
82 self.add_suffix(desc[0],
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
83 DynLoadSuffixImporter(desc).import_file)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
84 self.add_suffix('.py', py_suffix_importer)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
85
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
86 def _import_hook(self, fqname, globals=None, locals=None, fromlist=None):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
87 """Python calls this hook to locate and import a module."""
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
88
18856
4ee1fee1d4ad Remove usage of strop module.
Martin v. Löwis <martin@v.loewis.de>
parents: 17335
diff changeset
89 parts = fqname.split('.')
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
90
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
91 # determine the context of this import
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
92 parent = self._determine_import_context(globals)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
93
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
94 # if there is a parent, then its importer should manage this import
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
95 if parent:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
96 module = parent.__importer__._do_import(parent, parts, fromlist)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
97 if module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
98 return module
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
99
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
100 # has the top module already been imported?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
101 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
102 top_module = sys.modules[parts[0]]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
103 except KeyError:
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
104
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
105 # look for the topmost module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
106 top_module = self._import_top_module(parts[0])
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
107 if not top_module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
108 # the topmost module wasn't found at all.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
109 raise ImportError, 'No module named ' + fqname
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
110
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
111 # fast-path simple imports
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
112 if len(parts) == 1:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
113 if not fromlist:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
114 return top_module
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
115
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
116 if not top_module.__dict__.get('__ispkg__'):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
117 # __ispkg__ isn't defined (the module was not imported by us),
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
118 # or it is zero.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
119 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
120 # In the former case, there is no way that we could import
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
121 # sub-modules that occur in the fromlist (but we can't raise an
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
122 # error because it may just be names) because we don't know how
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
123 # to deal with packages that were imported by other systems.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
124 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
125 # In the latter case (__ispkg__ == 0), there can't be any sub-
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
126 # modules present, so we can just return.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
127 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
128 # In both cases, since len(parts) == 1, the top_module is also
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
129 # the "bottom" which is the defined return when a fromlist
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
130 # exists.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
131 return top_module
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
132
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
133 importer = top_module.__dict__.get('__importer__')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
134 if importer:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
135 return importer._finish_import(top_module, parts[1:], fromlist)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
136
38109
91a6436f0a58 Bug #1481530: allow "from os.path import ..." with imputil
Georg Brandl <georg@python.org>
parents: 32946
diff changeset
137 # Grrr, some people "import os.path" or do "from os.path import ..."
18857
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
138 if len(parts) == 2 and hasattr(top_module, parts[1]):
38109
91a6436f0a58 Bug #1481530: allow "from os.path import ..." with imputil
Georg Brandl <georg@python.org>
parents: 32946
diff changeset
139 if fromlist:
91a6436f0a58 Bug #1481530: allow "from os.path import ..." with imputil
Georg Brandl <georg@python.org>
parents: 32946
diff changeset
140 return getattr(top_module, parts[1])
91a6436f0a58 Bug #1481530: allow "from os.path import ..." with imputil
Georg Brandl <georg@python.org>
parents: 32946
diff changeset
141 else:
91a6436f0a58 Bug #1481530: allow "from os.path import ..." with imputil
Georg Brandl <georg@python.org>
parents: 32946
diff changeset
142 return top_module
18857
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
143
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
144 # If the importer does not exist, then we have to bail. A missing
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
145 # importer means that something else imported the module, and we have
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
146 # no knowledge of how to get sub-modules out of the thing.
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
147 raise ImportError, 'No module named ' + fqname
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
148
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
149 def _determine_import_context(self, globals):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
150 """Returns the context in which a module should be imported.
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
151
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
152 The context could be a loaded (package) module and the imported module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
153 will be looked for within that package. The context could also be None,
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
154 meaning there is no context -- the module should be looked for as a
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
155 "top-level" module.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
156 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
157
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
158 if not globals or not globals.get('__importer__'):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
159 # globals does not refer to one of our modules or packages. That
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
160 # implies there is no relative import context (as far as we are
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
161 # concerned), and it should just pick it off the standard path.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
162 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
163
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
164 # The globals refer to a module or package of ours. It will define
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
165 # the context of the new import. Get the module/package fqname.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
166 parent_fqname = globals['__name__']
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
167
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
168 # if a package is performing the import, then return itself (imports
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
169 # refer to pkg contents)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
170 if globals['__ispkg__']:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
171 parent = sys.modules[parent_fqname]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
172 assert globals is parent.__dict__
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
173 return parent
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
174
18856
4ee1fee1d4ad Remove usage of strop module.
Martin v. Löwis <martin@v.loewis.de>
parents: 17335
diff changeset
175 i = parent_fqname.rfind('.')
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
176
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
177 # a module outside of a package has no particular import context
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
178 if i == -1:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
179 return None
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
180
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
181 # if a module in a package is performing the import, then return the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
182 # package (imports refer to siblings)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
183 parent_fqname = parent_fqname[:i]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
184 parent = sys.modules[parent_fqname]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
185 assert parent.__name__ == parent_fqname
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
186 return parent
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
187
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
188 def _import_top_module(self, name):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
189 # scan sys.path looking for a location in the filesystem that contains
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
190 # the module, or an Importer object that can import the module.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
191 for item in sys.path:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
192 if isinstance(item, _StringType):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
193 module = self.fs_imp.import_from_dir(item, name)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
194 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
195 module = item.import_top(name)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
196 if module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
197 return module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
198 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
199
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
200 def _reload_hook(self, module):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
201 "Python calls this hook to reload a module."
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
202
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
203 # reloading of a module may or may not be possible (depending on the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
204 # importer), but at least we can validate that it's ours to reload
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
205 importer = module.__dict__.get('__importer__')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
206 if not importer:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
207 ### oops. now what...
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
208 pass
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
209
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
210 # okay. it is using the imputil system, and we must delegate it, but
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
211 # we don't know what to do (yet)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
212 ### we should blast the module dict and do another get_code(). need to
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
213 ### flesh this out and add proper docco...
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
214 raise SystemError, "reload not yet implemented"
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
215
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
216
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
217 class Importer:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
218 "Base class for replacing standard import functions."
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
219
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
220 def import_top(self, name):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
221 "Import a top-level module."
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
222 return self._import_one(None, name, name)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
223
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
224 ######################################################################
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
225 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
226 # PRIVATE METHODS
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
227 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
228 def _finish_import(self, top, parts, fromlist):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
229 # if "a.b.c" was provided, then load the ".b.c" portion down from
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
230 # below the top-level module.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
231 bottom = self._load_tail(top, parts)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
232
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
233 # if the form is "import a.b.c", then return "a"
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
234 if not fromlist:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
235 # no fromlist: return the top of the import tree
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
236 return top
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
237
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
238 # the top module was imported by self.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
239 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
240 # this means that the bottom module was also imported by self (just
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
241 # now, or in the past and we fetched it from sys.modules).
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
242 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
243 # since we imported/handled the bottom module, this means that we can
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
244 # also handle its fromlist (and reliably use __ispkg__).
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
245
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
246 # if the bottom node is a package, then (potentially) import some
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
247 # modules.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
248 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
249 # note: if it is not a package, then "fromlist" refers to names in
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
250 # the bottom module rather than modules.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
251 # note: for a mix of names and modules in the fromlist, we will
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
252 # import all modules and insert those into the namespace of
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
253 # the package module. Python will pick up all fromlist names
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
254 # from the bottom (package) module; some will be modules that
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
255 # we imported and stored in the namespace, others are expected
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
256 # to be present already.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
257 if bottom.__ispkg__:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
258 self._import_fromlist(bottom, fromlist)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
259
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
260 # if the form is "from a.b import c, d" then return "b"
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
261 return bottom
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
262
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
263 def _import_one(self, parent, modname, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
264 "Import a single module."
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
265
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
266 # has the module already been imported?
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
267 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
268 return sys.modules[fqname]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
269 except KeyError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
270 pass
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
271
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
272 # load the module's code, or fetch the module itself
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
273 result = self.get_code(parent, modname, fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
274 if result is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
275 return None
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
276
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
277 module = self._process_result(result, fqname)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
278
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
279 # insert the module into its parent
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
280 if parent:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
281 setattr(parent, modname, module)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
282 return module
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
283
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
284 def _process_result(self, (ispkg, code, values), fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
285 # did get_code() return an actual module? (rather than a code object)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
286 is_module = isinstance(code, _ModuleType)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
287
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
288 # use the returned module, or create a new one to exec code into
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
289 if is_module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
290 module = code
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
291 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
292 module = imp.new_module(fqname)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
293
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
294 ### record packages a bit differently??
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
295 module.__importer__ = self
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
296 module.__ispkg__ = ispkg
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
297
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
298 # insert additional values into the module (before executing the code)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
299 module.__dict__.update(values)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
300
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
301 # the module is almost ready... make it visible
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
302 sys.modules[fqname] = module
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
303
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
304 # execute the code within the module's namespace
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
305 if not is_module:
32946
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
306 try:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
307 exec code in module.__dict__
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
308 except:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
309 if fqname in sys.modules:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
310 del sys.modules[fqname]
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
311 raise
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
312
16564
d3484ca40b8a This change makes imputil more closely emulate the standard import
Thomas Heller <theller@ctypes.org>
parents: 16198
diff changeset
313 # fetch from sys.modules instead of returning module directly.
18857
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
314 # also make module's __name__ agree with fqname, in case
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
315 # the "exec code in module.__dict__" played games on us.
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
316 module = sys.modules[fqname]
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
317 module.__name__ = fqname
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
318 return module
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
319
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
320 def _load_tail(self, m, parts):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
321 """Import the rest of the modules, down from the top-level module.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
322
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
323 Returns the last module in the dotted list of modules.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
324 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
325 for part in parts:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
326 fqname = "%s.%s" % (m.__name__, part)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
327 m = self._import_one(m, part, fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
328 if not m:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
329 raise ImportError, "No module named " + fqname
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
330 return m
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
331
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
332 def _import_fromlist(self, package, fromlist):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
333 'Import any sub-modules in the "from" list.'
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
334
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
335 # if '*' is present in the fromlist, then look for the '__all__'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
336 # variable to find additional items (modules) to import.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
337 if '*' in fromlist:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
338 fromlist = list(fromlist) + \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
339 list(package.__dict__.get('__all__', []))
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
340
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
341 for sub in fromlist:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
342 # if the name is already present, then don't try to import it (it
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
343 # might not be a module!).
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
344 if sub != '*' and not hasattr(package, sub):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
345 subname = "%s.%s" % (package.__name__, sub)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
346 submod = self._import_one(package, sub, subname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
347 if not submod:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
348 raise ImportError, "cannot import name " + subname
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
349
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
350 def _do_import(self, parent, parts, fromlist):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
351 """Attempt to import the module relative to parent.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
352
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
353 This method is used when the import context specifies that <self>
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
354 imported the parent module.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
355 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
356 top_name = parts[0]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
357 top_fqname = parent.__name__ + '.' + top_name
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
358 top_module = self._import_one(parent, top_name, top_fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
359 if not top_module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
360 # this importer and parent could not find the module (relatively)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
361 return None
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
362
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
363 return self._finish_import(top_module, parts[1:], fromlist)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
364
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
365 ######################################################################
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
366 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
367 # METHODS TO OVERRIDE
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
368 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
369 def get_code(self, parent, modname, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
370 """Find and retrieve the code for the given module.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
371
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
372 parent specifies a parent module to define a context for importing. It
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
373 may be None, indicating no particular context for the search.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
374
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
375 modname specifies a single module (not dotted) within the parent.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
376
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
377 fqname specifies the fully-qualified module name. This is a
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
378 (potentially) dotted name from the "root" of the module namespace
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
379 down to the modname.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
380 If there is no parent, then modname==fqname.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
381
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
382 This method should return None, or a 3-tuple.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
383
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
384 * If the module was not found, then None should be returned.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
385
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
386 * The first item of the 2- or 3-tuple should be the integer 0 or 1,
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
387 specifying whether the module that was found is a package or not.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
388
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
389 * The second item is the code object for the module (it will be
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
390 executed within the new module's namespace). This item can also
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
391 be a fully-loaded module object (e.g. loaded from a shared lib).
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
392
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
393 * The third item is a dictionary of name/value pairs that will be
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
394 inserted into new module before the code object is executed. This
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
395 is provided in case the module's code expects certain values (such
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
396 as where the module was found). When the second item is a module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
397 object, then these names/values will be inserted *after* the module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
398 has been loaded/initialized.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
399 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
400 raise RuntimeError, "get_code not implemented"
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
401
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
402
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
403 ######################################################################
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
404 #
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
405 # Some handy stuff for the Importers
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
406 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
407
11819
bf1501cf5f08 shift demo importers to importers.py (destined for Demo/ ?)
Greg Stein <gstein@lyra.org>
parents: 11641
diff changeset
408 # byte-compiled file suffix character
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
409 _suffix_char = __debug__ and 'c' or 'o'
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
410
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
411 # byte-compiled file suffix
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
412 _suffix = '.py' + _suffix_char
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
413
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
414 def _compile(pathname, timestamp):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
415 """Compile (and cache) a Python source file.
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
416
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
417 The file specified by <pathname> is compiled to a code object and
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
418 returned.
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
419
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
420 Presuming the appropriate privileges exist, the bytecodes will be
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
421 saved back to the filesystem for future imports. The source file's
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
422 modification timestamp must be provided as a Long value.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
423 """
24041
af0532f23ab3 Close SF patch 523944: importing modules with foreign newlines.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23625
diff changeset
424 codestring = open(pathname, 'rU').read()
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
425 if codestring and codestring[-1] != '\n':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
426 codestring = codestring + '\n'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
427 code = __builtin__.compile(codestring, pathname, 'exec')
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
428
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
429 # try to cache the compiled code
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
430 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
431 f = open(pathname + _suffix_char, 'wb')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
432 except IOError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
433 pass
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
434 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
435 f.write('\0\0\0\0')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
436 f.write(struct.pack('<I', timestamp))
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
437 marshal.dump(code, f)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
438 f.flush()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
439 f.seek(0, 0)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
440 f.write(imp.get_magic())
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
441 f.close()
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
442
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
443 return code
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
444
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
445 _os_stat = _os_path_join = None
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
446 def _os_bootstrap():
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
447 "Set up 'os' module replacement functions for use during import bootstrap."
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
448
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
449 names = sys.builtin_module_names
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
450
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
451 join = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
452 if 'posix' in names:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
453 sep = '/'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
454 from posix import stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
455 elif 'nt' in names:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
456 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
457 from nt import stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
458 elif 'dos' in names:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
459 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
460 from dos import stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
461 elif 'os2' in names:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
462 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
463 from os2 import stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
464 elif 'mac' in names:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
465 from mac import stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
466 def join(a, b):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
467 if a == '':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
468 return b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
469 if ':' not in a:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
470 a = ':' + a
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 13636
diff changeset
471 if a[-1:] != ':':
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
472 a = a + ':'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
473 return a + b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
474 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
475 raise ImportError, 'no os specific module found'
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
476
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
477 if join is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
478 def join(a, b, sep=sep):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
479 if a == '':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
480 return b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
481 lastchar = a[-1:]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
482 if lastchar == '/' or lastchar == sep:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
483 return a + b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
484 return a + sep + b
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
485
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
486 global _os_stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
487 _os_stat = stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
488
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
489 global _os_path_join
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
490 _os_path_join = join
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
491
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
492 def _os_path_isdir(pathname):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
493 "Local replacement for os.path.isdir()."
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
494 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
495 s = _os_stat(pathname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
496 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
497 return None
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23613
diff changeset
498 return (s.st_mode & 0170000) == 0040000
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
499
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
500 def _timestamp(pathname):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
501 "Return the file modification time as a Long."
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
502 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
503 s = _os_stat(pathname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
504 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
505 return None
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23613
diff changeset
506 return long(s.st_mtime)
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
507
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
508
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
509 ######################################################################
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
510 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
511 # Emulate the import mechanism for builtin and frozen modules
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
512 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
513 class BuiltinImporter(Importer):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
514 def get_code(self, parent, modname, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
515 if parent:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
516 # these modules definitely do not occur within a package context
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
517 return None
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
518
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
519 # look for the module
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
520 if imp.is_builtin(modname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
521 type = imp.C_BUILTIN
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
522 elif imp.is_frozen(modname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
523 type = imp.PY_FROZEN
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
524 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
525 # not found
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
526 return None
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
527
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
528 # got it. now load and return it.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
529 module = imp.load_module(modname, None, modname, ('', '', type))
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
530 return 0, module, { }
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
531
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
532
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
533 ######################################################################
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
534 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
535 # Internal importer used for importing from the filesystem
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
536 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
537 class _FilesystemImporter(Importer):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
538 def __init__(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
539 self.suffixes = [ ]
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
540
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
541 def add_suffix(self, suffix, importFunc):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
542 assert callable(importFunc)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
543 self.suffixes.append((suffix, importFunc))
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
544
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
545 def import_from_dir(self, dir, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
546 result = self._import_pathname(_os_path_join(dir, fqname), fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
547 if result:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
548 return self._process_result(result, fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
549 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
550
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
551 def get_code(self, parent, modname, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
552 # This importer is never used with an empty parent. Its existence is
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
553 # private to the ImportManager. The ImportManager uses the
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
554 # import_from_dir() method to import top-level modules/packages.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
555 # This method is only used when we look for a module within a package.
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
556 assert parent
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
557
40831
81d68dd84aa0 Patch #685268: Consider a package's __path__ in imputil.
Martin v. Löwis <martin@v.loewis.de>
parents: 38109
diff changeset
558 for submodule_path in parent.__path__:
81d68dd84aa0 Patch #685268: Consider a package's __path__ in imputil.
Martin v. Löwis <martin@v.loewis.de>
parents: 38109
diff changeset
559 code = self._import_pathname(_os_path_join(submodule_path, modname), fqname)
81d68dd84aa0 Patch #685268: Consider a package's __path__ in imputil.
Martin v. Löwis <martin@v.loewis.de>
parents: 38109
diff changeset
560 if code is not None:
81d68dd84aa0 Patch #685268: Consider a package's __path__ in imputil.
Martin v. Löwis <martin@v.loewis.de>
parents: 38109
diff changeset
561 return code
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
562 return self._import_pathname(_os_path_join(parent.__pkgdir__, modname),
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
563 fqname)
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
564
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
565 def _import_pathname(self, pathname, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
566 if _os_path_isdir(pathname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
567 result = self._import_pathname(_os_path_join(pathname, '__init__'),
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
568 fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
569 if result:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
570 values = result[2]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
571 values['__pkgdir__'] = pathname
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
572 values['__path__'] = [ pathname ]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
573 return 1, result[1], values
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
574 return None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
575
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
576 for suffix, importFunc in self.suffixes:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
577 filename = pathname + suffix
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
578 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
579 finfo = _os_stat(filename)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
580 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
581 pass
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
582 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
583 return importFunc(filename, finfo, fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
584 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
585
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
586 ######################################################################
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
587 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
588 # SUFFIX-BASED IMPORTERS
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
589 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
590
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
591 def py_suffix_importer(filename, finfo, fqname):
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
592 file = filename[:-3] + _suffix
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
593 t_py = long(finfo[8])
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
594 t_pyc = _timestamp(file)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
595
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
596 code = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
597 if t_pyc is not None and t_pyc >= t_py:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
598 f = open(file, 'rb')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
599 if f.read(4) == imp.get_magic():
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
600 t = struct.unpack('<I', f.read(4))[0]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
601 if t == t_py:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
602 code = marshal.load(f)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
603 f.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
604 if code is None:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
605 file = filename
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
606 code = _compile(file, t_py)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
607
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
608 return 0, code, { '__file__' : file }
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
609
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
610 class DynLoadSuffixImporter:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
611 def __init__(self, desc):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
612 self.desc = desc
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
613
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
614 def import_file(self, filename, finfo, fqname):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
615 fp = open(filename, self.desc[1])
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
616 module = imp.load_module(fqname, fp, filename, self.desc)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
617 module.__file__ = filename
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
618 return 0, module, { }
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
619
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
620
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
621 ######################################################################
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
622
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
623 def _print_importers():
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
624 items = sys.modules.items()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
625 items.sort()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
626 for name, module in items:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
627 if module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
628 print name, module.__dict__.get('__importer__', '-- no importer')
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
629 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
630 print name, '-- non-existent module'
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
631
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
632 def _test_revamp():
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
633 ImportManager().install()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
634 sys.path.insert(0, BuiltinImporter())
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
635
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
636 ######################################################################
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
637
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
638 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
639 # TODO
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
640 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
641 # from Finn Bock:
56812
a1b0936b027c Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46749
diff changeset
642 # type(sys) is not a module in Jython. what to use instead?
a1b0936b027c Merged revisions 72558,72745,72750,72876,73042,73045-73048,73069,73089,73163,73186,73213,73215,73217,73257-73258,73260 via svnmerge from
Georg Brandl <georg@python.org>
parents: 46749
diff changeset
643 # imp.C_EXTENSION is not in Jython. same for get_suffixes and new_module
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
644 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
645 # given foo.py of:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
646 # import sys
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
647 # sys.modules['foo'] = sys
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
648 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
649 # ---- standard import mechanism
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
650 # >>> import foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
651 # >>> foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
652 # <module 'sys' (built-in)>
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
653 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
654 # ---- revamped import mechanism
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
655 # >>> import imputil
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
656 # >>> imputil._test_revamp()
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
657 # >>> import foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
658 # >>> foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
659 # <module 'foo' from 'foo.py'>
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
660 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
661 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
662 # from MAL:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
663 # should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
664 # need __path__ processing
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
665 # performance
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
666 # move chaining to a subclass [gjs: it's been nuked]
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
667 # deinstall should be possible
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
668 # query mechanism needed: is a specific Importer installed?
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
669 # py/pyc/pyo piping hooks to filter/process these files
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
670 # wish list:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
671 # distutils importer hooked to list of standard Internet repositories
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
672 # module->file location mapper to speed FS-based imports
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
673 # relative imports
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
674 # keep chaining so that it can play nice with other import hooks
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
675 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
676 # from Gordon:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
677 # push MAL's mapper into sys.path[0] as a cache (hard-coded for apps)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
678 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
679 # from Guido:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
680 # need to change sys.* references for rexec environs
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
681 # need hook for MAL's walk-me-up import strategy, or Tim's absolute strategy
15543
785155e2cfca Update the code to better reflect recommended style:
Fred Drake <fdrake@acm.org>
parents: 13636
diff changeset
682 # watch out for sys.modules[...] is None
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
683 # flag to force absolute imports? (speeds _determine_import_context and
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
684 # checking for a relative module)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
685 # insert names of archives into sys.path (see quote below)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
686 # note: reload does NOT blast module dict
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
687 # shift import mechanisms and policies around; provide for hooks, overrides
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
688 # (see quote below)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
689 # add get_source stuff
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
690 # get_topcode and get_subcode
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
691 # CRLF handling in _compile
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
692 # race condition in _compile
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
693 # refactoring of os.py to deal with _os_bootstrap problem
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
694 # any special handling to do for importing a module with a SyntaxError?
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
695 # (e.g. clean up the traceback)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
696 # implement "domain" for path-type functionality using pkg namespace
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
697 # (rather than FS-names like __path__)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
698 # don't use the word "private"... maybe "internal"
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
699 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
700 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
701 # Guido's comments on sys.path caching:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
702 #
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
703 # We could cache this in a dictionary: the ImportManager can have a
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
704 # cache dict mapping pathnames to importer objects, and a separate
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
705 # method for coming up with an importer given a pathname that's not yet
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
706 # in the cache. The method should do a stat and/or look at the
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
707 # extension to decide which importer class to use; you can register new
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
708 # importer classes by registering a suffix or a Boolean function, plus a
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
709 # class. If you register a new importer class, the cache is zapped.
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
710 # The cache is independent from sys.path (but maintained per
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
711 # ImportManager instance) so that rearrangements of sys.path do the
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
712 # right thing. If a path is dropped from sys.path the corresponding
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
713 # cache entry is simply no longer used.
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
714 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
715 # My/Guido's comments on factoring ImportManager and Importer:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
716 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
717 # > However, we still have a tension occurring here:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
718 # >
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
719 # > 1) implementing policy in ImportManager assists in single-point policy
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
720 # > changes for app/rexec situations
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
721 # > 2) implementing policy in Importer assists in package-private policy
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
722 # > changes for normal, operating conditions
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
723 # >
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
724 # > I'll see if I can sort out a way to do this. Maybe the Importer class will
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
725 # > implement the methods (which can be overridden to change policy) by
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
726 # > delegating to ImportManager.
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
727 #
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
728 # Maybe also think about what kind of policies an Importer would be
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
729 # likely to want to change. I have a feeling that a lot of the code
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
730 # there is actually not so much policy but a *necessity* to get things
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
731 # working given the calling conventions for the __import__ hook: whether
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
732 # to return the head or tail of a dotted name, or when to do the "finish
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
733 # fromlist" stuff.
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
734 #