annotate Lib/imputil.py @ 79675:c0a2294de787 2.7

Issue #16199: Backport 2to3 typo fix from 3.2.
author Chris Jerdonek <chris.jerdonek@gmail.com>
date Thu, 11 Oct 2012 16:15:46 -0700
parents b36cb4602e21
children
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):
56435
57ee0793e462 replace callable()
Benjamin Peterson <benjamin@python.org>
parents: 56812
diff changeset
54 assert hasattr(importFunc, '__call__')
13636
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
63528
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56812
diff changeset
284 def _process_result(self, result, fqname):
751c6dd71b5e Merged revisions 77310-77311 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents: 56812
diff changeset
285 ispkg, code, values = result
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
286 # 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
287 is_module = isinstance(code, _ModuleType)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
288
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
289 # 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
290 if is_module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
291 module = code
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
292 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
293 module = imp.new_module(fqname)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
294
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
295 ### 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
296 module.__importer__ = self
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
297 module.__ispkg__ = ispkg
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
298
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
299 # 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
300 module.__dict__.update(values)
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
301
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
302 # 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
303 sys.modules[fqname] = module
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
304
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
305 # 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
306 if not is_module:
32946
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
307 try:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
308 exec code in module.__dict__
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
309 except:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
310 if fqname in sys.modules:
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
311 del sys.modules[fqname]
db38c1e83965 ihooks FancyModuleLoader.load_module()
Tim Peters <tim.peters@gmail.com>
parents: 24041
diff changeset
312 raise
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
313
16564
d3484ca40b8a This change makes imputil more closely emulate the standard import
Thomas Heller <theller@ctypes.org>
parents: 16198
diff changeset
314 # 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
315 # 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
316 # 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
317 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
318 module.__name__ = fqname
d1669af9d0ec Patch #443337: Fix incompatibilities in imputil's behavior.
Martin v. Löwis <martin@v.loewis.de>
parents: 18856
diff changeset
319 return module
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
320
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
321 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
322 """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
323
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
324 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
325 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
326 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
327 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
328 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
329 if not m:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
330 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
331 return m
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
332
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
333 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
334 'Import any sub-modules in the "from" list.'
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
335
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
336 # 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
337 # 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
338 if '*' in fromlist:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
339 fromlist = list(fromlist) + \
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
340 list(package.__dict__.get('__all__', []))
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
341
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
342 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
343 # 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
344 # 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
345 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
346 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
347 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
348 if not submod:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
349 raise ImportError, "cannot import name " + subname
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
350
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
351 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
352 """Attempt to import the module relative to parent.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
353
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
354 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
355 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
356 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
357 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
358 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
359 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
360 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
361 # 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
362 return None
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
363
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
364 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
365
13636
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 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
368 # METHODS TO OVERRIDE
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
369 #
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
370 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
371 """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
372
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
373 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
374 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
375
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
376 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
377
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
378 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
379 (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
380 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
381 If there is no parent, then modname==fqname.
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
382
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
383 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
384
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
385 * 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
386
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
387 * 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
388 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
389
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
390 * 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
391 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
392 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
393
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
394 * 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
395 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
396 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
397 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
398 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
399 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
400 """
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
401 raise RuntimeError, "get_code not implemented"
11445
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 ######################################################################
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
405 #
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
406 # 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
407 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
408
11819
bf1501cf5f08 shift demo importers to importers.py (destined for Demo/ ?)
Greg Stein <gstein@lyra.org>
parents: 11641
diff changeset
409 # 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
410 _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
411
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
412 # byte-compiled file suffix
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
413 _suffix = '.py' + _suffix_char
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
414
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
415 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
416 """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
417
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
418 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
419 returned.
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
420
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
421 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
422 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
423 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
424 """
24041
af0532f23ab3 Close SF patch 523944: importing modules with foreign newlines.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 23625
diff changeset
425 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
426 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
427 codestring = codestring + '\n'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
428 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
429
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
430 # 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
431 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
432 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
433 except IOError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
434 pass
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
435 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
436 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
437 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
438 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
439 f.flush()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
440 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
441 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
442 f.close()
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
443
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
444 return code
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
445
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
446 _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
447 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
448 "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
449
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
450 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
451
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
452 join = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
453 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
454 sep = '/'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
455 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
456 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
457 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
458 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
459 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
460 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
461 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
462 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
463 sep = '\\'
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
464 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
465 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
466 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
467
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
468 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
469 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
470 if a == '':
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
471 return b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
472 lastchar = a[-1:]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
473 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
474 return a + b
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
475 return a + sep + b
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 global _os_stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
478 _os_stat = stat
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
479
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
480 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
481 _os_path_join = join
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
482
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
483 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
484 "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
485 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
486 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
487 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
488 return None
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23613
diff changeset
489 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
490
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
491 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
492 "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
493 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
494 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
495 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
496 return None
23625
65cf478950ff Replaced obsolete stat module constants with equivalent attributes
Raymond Hettinger <python@rcn.com>
parents: 23613
diff changeset
497 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
498
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 ######################################################################
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
501 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
502 # 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
503 #
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
504 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
505 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
506 if parent:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
507 # 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
508 return None
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
509
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
510 # 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
511 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
512 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
513 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
514 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
515 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
516 # not found
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 # 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
520 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
521 return 0, module, { }
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
522
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
523
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
524 ######################################################################
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
525 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
526 # Internal importer used for importing from the filesystem
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
527 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
528 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
529 def __init__(self):
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
530 self.suffixes = [ ]
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
531
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
532 def add_suffix(self, suffix, importFunc):
56435
57ee0793e462 replace callable()
Benjamin Peterson <benjamin@python.org>
parents: 56812
diff changeset
533 assert hasattr(importFunc, '__call__')
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
534 self.suffixes.append((suffix, importFunc))
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
535
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
536 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
537 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
538 if result:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
539 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
540 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
541
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
542 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
543 # 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
544 # 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
545 # 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
546 # 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
547 assert parent
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
548
40831
81d68dd84aa0 Patch #685268: Consider a package's __path__ in imputil.
Martin v. Löwis <martin@v.loewis.de>
parents: 38109
diff changeset
549 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
550 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
551 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
552 return code
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
553 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
554 fqname)
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
555
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
556 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
557 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
558 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
559 fqname)
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
560 if result:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
561 values = result[2]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
562 values['__pkgdir__'] = pathname
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
563 values['__path__'] = [ pathname ]
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
564 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
565 return None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
566
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
567 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
568 filename = pathname + suffix
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
569 try:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
570 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
571 except OSError:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
572 pass
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
573 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
574 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
575 return None
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
576
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
577 ######################################################################
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
578 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
579 # SUFFIX-BASED IMPORTERS
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
580 #
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
581
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
582 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
583 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
584 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
585 t_pyc = _timestamp(file)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
586
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
587 code = None
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
588 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
589 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
590 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
591 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
592 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
593 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
594 f.close()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
595 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
596 file = filename
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
597 code = _compile(file, t_py)
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
598
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
599 return 0, code, { '__file__' : file }
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
600
11820
be9500e6477f reduce fsimp coupling: provide fs_imp param to ImportManager constructor,
Greg Stein <gstein@lyra.org>
parents: 11819
diff changeset
601 class DynLoadSuffixImporter:
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
602 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
603 self.desc = desc
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
604
13636
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
605 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
606 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
607 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
608 module.__file__ = filename
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
609 return 0, module, { }
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
610
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
611
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
612 ######################################################################
11489
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
613
7034b75aad03 shift code from DirectoryImporter out to a common area.
Greg Stein <gstein@lyra.org>
parents: 11446
diff changeset
614 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
615 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
616 items.sort()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
617 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
618 if module:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
619 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
620 else:
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
621 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
622
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
623 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
624 ImportManager().install()
b5120f58c5da no changes other than indentation level (now 4) and comment reflow.
Greg Stein <gstein@lyra.org>
parents: 13021
diff changeset
625 sys.path.insert(0, BuiltinImporter())
11641
a7df7d523d29 redesign/rebuild around the ImportManager concept.
Greg Stein <gstein@lyra.org>
parents: 11496
diff changeset
626
11445
3a506ab1c138 initial checkin for my Python stuff.
Greg Stein <gstein@lyra.org>
parents:
diff changeset
627 ######################################################################
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
628
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
629 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
630 # TODO
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
631 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
632 # 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
633 # 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
634 # 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
635 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
636 # given foo.py of:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
637 # import sys
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
638 # sys.modules['foo'] = sys
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
639 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
640 # ---- standard import mechanism
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
641 # >>> import foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
642 # >>> foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
643 # <module 'sys' (built-in)>
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 # ---- revamped import mechanism
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
646 # >>> import imputil
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
647 # >>> imputil._test_revamp()
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
648 # >>> import foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
649 # >>> foo
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
650 # <module 'foo' from 'foo.py'>
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
651 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
652 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
653 # from MAL:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
654 # should BuiltinImporter exist in sys.path or hard-wired in ImportManager?
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
655 # need __path__ processing
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
656 # performance
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
657 # move chaining to a subclass [gjs: it's been nuked]
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
658 # deinstall should be possible
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
659 # query mechanism needed: is a specific Importer installed?
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
660 # py/pyc/pyo piping hooks to filter/process these files
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
661 # wish list:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
662 # distutils importer hooked to list of standard Internet repositories
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
663 # module->file location mapper to speed FS-based imports
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
664 # relative imports
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
665 # 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
666 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
667 # from Gordon:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
668 # 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
669 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
670 # from Guido:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
671 # need to change sys.* references for rexec environs
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
672 # 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
673 # watch out for sys.modules[...] is None
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
674 # flag to force absolute imports? (speeds _determine_import_context and
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
675 # checking for a relative module)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
676 # insert names of archives into sys.path (see quote below)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
677 # note: reload does NOT blast module dict
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
678 # shift import mechanisms and policies around; provide for hooks, overrides
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
679 # (see quote below)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
680 # add get_source stuff
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
681 # get_topcode and get_subcode
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
682 # CRLF handling in _compile
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
683 # race condition in _compile
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
684 # refactoring of os.py to deal with _os_bootstrap problem
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
685 # 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
686 # (e.g. clean up the traceback)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
687 # implement "domain" for path-type functionality using pkg namespace
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
688 # (rather than FS-names like __path__)
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
689 # don't use the word "private"... maybe "internal"
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
690 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
691 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
692 # Guido's comments on sys.path caching:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
693 #
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
694 # 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
695 # cache dict mapping pathnames to importer objects, and a separate
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
696 # 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
697 # 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
698 # 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
699 # 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
700 # 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
701 # The cache is independent from sys.path (but maintained per
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
702 # ImportManager instance) so that rearrangements of sys.path do the
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
703 # 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
704 # cache entry is simply no longer used.
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
705 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
706 # My/Guido's comments on factoring ImportManager and Importer:
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
707 #
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
708 # > However, we still have a tension occurring here:
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
709 # >
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
710 # > 1) implementing policy in ImportManager assists in single-point policy
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
711 # > changes for app/rexec situations
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
712 # > 2) implementing policy in Importer assists in package-private policy
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
713 # > changes for normal, operating conditions
15868
3060cbfa01be Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 15543
diff changeset
714 # >
11823
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
715 # > 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
716 # > implement the methods (which can be overridden to change policy) by
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
717 # > delegating to ImportManager.
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 # 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
720 # 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
721 # 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
722 # working given the calling conventions for the __import__ hook: whether
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
723 # 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
724 # fromlist" stuff.
8878c096fa19 add TODO section
Greg Stein <gstein@lyra.org>
parents: 11820
diff changeset
725 #