annotate Lib/importlib/__init__.py @ 80661:94de77bd0b4b 2.7

Issue #16194: document imp.load_dynamic problems when read several extension modules from the same binary object file.
author Andrew Svetlov <andrew.svetlov@gmail.com>
date Thu, 29 Nov 2012 19:51:28 +0200
parents 58db4deb7ab1
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """Backport of importlib.import_module from 3.x."""
51310
1e7a06a7f5e7 Make importlib backwards-compatible to Python 2.2 (but this is not promised to
Brett Cannon <bcannon@gmail.com>
parents: 51306
diff changeset
2 # While not critical (and in no way guaranteed!), it would be nice to keep this
1e7a06a7f5e7 Make importlib backwards-compatible to Python 2.2 (but this is not promised to
Brett Cannon <bcannon@gmail.com>
parents: 51306
diff changeset
3 # code compatible with Python 2.3.
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4 import sys
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6 def _resolve_name(name, package, level):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7 """Return the absolute name of the module to be imported."""
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
8 if not hasattr(package, 'rindex'):
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
9 raise ValueError("'package' not set to a string")
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
10 dot = len(package)
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
11 for x in xrange(level, 1, -1):
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
12 try:
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
13 dot = package.rindex('.', 0, dot)
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
14 except ValueError:
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 raise ValueError("attempted relative import beyond top-level "
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 "package")
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
17 return "%s.%s" % (package[:dot], name)
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
18
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
20 def import_module(name, package=None):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
21 """Import a module.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
22
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
23 The 'package' argument is required when performing a relative import. It
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24 specifies the package to use as the anchor point from which to resolve the
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
25 relative import to an absolute import.
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 """
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28 if name.startswith('.'):
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
29 if not package:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
30 raise TypeError("relative imports require the 'package' argument")
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 level = 0
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32 for character in name:
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33 if character != '.':
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 break
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35 level += 1
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
36 name = _resolve_name(name[level:], package, level)
51992
58db4deb7ab1 Fix some more bugs caused by the backport from 3.x for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 51887
diff changeset
37 __import__(name)
51055
b4ace2b6c28f Backport importlib in the form of providing importlib.import_module(). This has
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38 return sys.modules[name]