annotate Lib/importlib/__init__.py @ 78069:a8aa77458e12 3.2

Add c_size_t and c_ssize_t to the ctypes summary table.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 12 Jul 2012 20:31:50 +0200
parents 34d827249ce0
children e4d44c2e8e81
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
1 """A pure Python implementation of import.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
2
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
3 References on import:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
4
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
5 * Language reference
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
6 http://docs.python.org/ref/import.html
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
7 * __import__ function
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
8 http://docs.python.org/lib/built-in-funcs.html
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
9 * Packages
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
10 http://www.python.org/doc/essays/packages.html
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
11 * PEP 235: Import on Case-Insensitive Platforms
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
12 http://www.python.org/dev/peps/pep-0235
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
13 * PEP 275: Import Modules from Zip Archives
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
14 http://www.python.org/dev/peps/pep-0273
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
15 * PEP 302: New Import Hooks
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
16 http://www.python.org/dev/peps/pep-0302/
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
17 * PEP 328: Imports: Multi-line and Absolute/Relative
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
18 http://www.python.org/dev/peps/pep-0328
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
19
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
20 """
52138
6b024ff5b58f Define importlib.__init__.__all__.
Brett Cannon <bcannon@gmail.com>
parents: 51991
diff changeset
21 __all__ = ['__import__', 'import_module']
6b024ff5b58f Define importlib.__init__.__all__.
Brett Cannon <bcannon@gmail.com>
parents: 51991
diff changeset
22
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
23 from . import _bootstrap
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
24
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
25 import os
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
26 import re
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
27 import tokenize
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
28
50984
dd638b7e747e Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents: 50922
diff changeset
29 # Bootstrap help #####################################################
dd638b7e747e Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents: 50922
diff changeset
30
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
31 def _case_ok(directory, check):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
32 """Check if the directory contains something matching 'check'.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
33
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
34 No check is done if the file/directory exists or not.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
35
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
36 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
37 if 'PYTHONCASEOK' in os.environ:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
38 return True
62756
34d827249ce0 Make importlib.abc.SourceLoader the primary mechanism for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52165
diff changeset
39 elif check in os.listdir(directory if directory else os.getcwd()):
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
40 return True
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
41 return False
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
42
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
43
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
44 def _w_long(x):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
45 """Convert a 32-bit integer to little-endian.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
46
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
47 XXX Temporary until marshal's long functions are exposed.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
48
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
49 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
50 x = int(x)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
51 int_bytes = []
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
52 int_bytes.append(x & 0xFF)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
53 int_bytes.append((x >> 8) & 0xFF)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
54 int_bytes.append((x >> 16) & 0xFF)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
55 int_bytes.append((x >> 24) & 0xFF)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
56 return bytearray(int_bytes)
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
57
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
58
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
59 def _r_long(int_bytes):
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
60 """Convert 4 bytes in little-endian to an integer.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
61
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
62 XXX Temporary until marshal's long function are exposed.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
63
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
64 """
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
65 x = int_bytes[0]
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
66 x |= int_bytes[1] << 8
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
67 x |= int_bytes[2] << 16
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
68 x |= int_bytes[3] << 24
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
69 return x
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
70
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
71
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
72 # Required built-in modules.
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
73 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
74 import posix as _os
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
75 except ImportError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
76 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
77 import nt as _os
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
78 except ImportError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
79 try:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
80 import os2 as _os
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
81 except ImportError:
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
82 raise ImportError('posix, nt, or os2 module required for importlib')
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
83 _bootstrap._os = _os
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51426
diff changeset
84 import imp, sys, marshal, errno, _io
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
85 _bootstrap.imp = imp
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
86 _bootstrap.sys = sys
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
87 _bootstrap.marshal = marshal
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
88 _bootstrap.errno = errno
51991
3049ac17e256 merge the io-c branch: C implementation of the io module
Benjamin Peterson <benjamin@python.org>
parents: 51426
diff changeset
89 _bootstrap._io = _io
50845
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
90 import _warnings
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
91 _bootstrap._warnings = _warnings
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
92
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
93
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
94 from os import sep
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
95 # For os.path.join replacement; pull from Include/osdefs.h:SEP .
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
96 _bootstrap.path_sep = sep
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
97
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
98 _bootstrap._case_ok = _case_ok
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
99 marshal._w_long = _w_long
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
100 marshal._r_long = _r_long
aac51b70c74c Add initial implementation of importlib. See the NOTES files for what is
Brett Cannon <bcannon@gmail.com>
parents:
diff changeset
101
50922
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
102
50984
dd638b7e747e Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents: 50922
diff changeset
103 # Public API #########################################################
dd638b7e747e Add some comment markers to more clearly delineate what different parts of the
Brett Cannon <bcannon@gmail.com>
parents: 50922
diff changeset
104
52165
62f9dad628de A few more docstring/API cleanups for importlib.
Brett Cannon <bcannon@gmail.com>
parents: 52142
diff changeset
105 from ._bootstrap import __import__
50922
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
106
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
107
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
108 def import_module(name, package=None):
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
109 """Import a module.
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
110
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
111 The 'package' argument is required when performing a relative import. It
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
112 specifies the package to use as the anchor point from which to resolve the
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
113 relative import to an absolute import.
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
114
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
115 """
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 50984
diff changeset
116 level = 0
50922
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
117 if name.startswith('.'):
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
118 if not package:
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
119 raise TypeError("relative imports require the 'package' argument")
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
120 for character in name:
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
121 if character != '.':
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
122 break
a63f7cf3793a Document the (very small) public API for importlib. As time goes on and some
Brett Cannon <bcannon@gmail.com>
parents: 50845
diff changeset
123 level += 1
51426
7a1356d2aeef Rewrite the code implementing __import__ for importlib. Now it is much simpler
Brett Cannon <bcannon@gmail.com>
parents: 50984
diff changeset
124 return _bootstrap._gcd_import(name[level:], package, level)