annotate Doc/lib/libimp.tex @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents e1423ec9e6ec
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8918
acfffe14628e New section header style.
Fred Drake <fdrake@acm.org>
parents: 8716
diff changeset
1 \section{\module{imp} ---
10699
aba1cd0d13ea Shorten the section title.
Fred Drake <fdrake@acm.org>
parents: 8935
diff changeset
2 Access the \keyword{import} internals}
aba1cd0d13ea Shorten the section title.
Fred Drake <fdrake@acm.org>
parents: 8935
diff changeset
3
8716
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
4 \declaremodule{builtin}{imp}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
5 \modulesynopsis{Access the implementation of the \keyword{import} statement.}
760dcf197048 Adjusted to use the new module synopsis support macros.
Fred Drake <fdrake@acm.org>
parents: 7877
diff changeset
6
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
7
10966
259237b25b93 Improve index entry for "import"; responding to comments by Stefan
Fred Drake <fdrake@acm.org>
parents: 10699
diff changeset
8 This\stindex{import} module provides an interface to the mechanisms
10699
aba1cd0d13ea Shorten the section title.
Fred Drake <fdrake@acm.org>
parents: 8935
diff changeset
9 used to implement the \keyword{import} statement. It defines the
aba1cd0d13ea Shorten the section title.
Fred Drake <fdrake@acm.org>
parents: 8935
diff changeset
10 following constants and functions:
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
11
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 \begin{funcdesc}{get_magic}{}
8935
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
14 \indexii{file}{byte-code}
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
15 Return the magic string value used to recognize byte-compiled code
8935
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
16 files (\file{.pyc} files). (This value may be different for each
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
17 Python version.)
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 \begin{funcdesc}{get_suffixes}{}
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
21 Return a list of triples, each describing a particular type of module.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 Each triple has the form \code{(\var{suffix}, \var{mode},
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 \var{type})}, where \var{suffix} is a string to be appended to the
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 module name to form the filename to search for, \var{mode} is the mode
8935
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
25 string to pass to the built-in \function{open()} function to open the
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
26 file (this can be \code{'r'} for text files or \code{'rb'} for binary
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 files), and \var{type} is the file type, which has one of the values
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
28 \constant{PY_SOURCE}, \constant{PY_COMPILED}, or
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
29 \constant{C_EXTENSION}, described below.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
31
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
32 \begin{funcdesc}{find_module}{name\optional{, path}}
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
33 Try to find the module \var{name} on the search path \var{path}. If
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
34 \var{path} is a list of directory names, each directory is searched
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
35 for files with any of the suffixes returned by \function{get_suffixes()}
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
36 above. Invalid names in the list are silently ignored (but all list
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
37 items must be strings). If \var{path} is omitted or \code{None}, the
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
38 list of directory names given by \code{sys.path} is searched, but
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
39 first it searches a few special places: it tries to find a built-in
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
40 module with the given name (\constant{C_BUILTIN}), then a frozen module
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
41 (\constant{PY_FROZEN}), and on some systems some other places are looked
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
42 in as well (on the Mac, it looks for a resource (\constant{PY_RESOURCE});
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
43 on Windows, it looks in the registry which may point to a specific
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
44 file).
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
45
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
46 If search is successful, the return value is a triple
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 \code{(\var{file}, \var{pathname}, \var{description})} where
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
48 \var{file} is an open file object positioned at the beginning,
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
49 \var{pathname} is the pathname of the
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 file found, and \var{description} is a triple as contained in the list
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
51 returned by \function{get_suffixes()} describing the kind of module found.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
52 If the module does not live in a file, the returned \var{file} is
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
53 \code{None}, \var{filename} is the empty string, and the
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
54 \var{description} tuple contains empty strings for its suffix and
13625
fdfd1049f36e Rob W. W. Hooft's spelling fixes for the Library Reference. I hope
Thomas Wouters <thomas@python.org>
parents: 10966
diff changeset
55 mode; the module type is as indicate in parentheses above. If the
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
56 search is unsuccessful, \exception{ImportError} is raised. Other
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
57 exceptions indicate problems with the arguments or environment.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
58
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
59 This function does not handle hierarchical module names (names
18411
eeb2e77c5bad Fix up a few style nits -- avoid "e.g." and "i.e." -- these make
Fred Drake <fdrake@acm.org>
parents: 15928
diff changeset
60 containing dots). In order to find \var{P}.\var{M}, that is, submodule
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
61 \var{M} of package \var{P}, use \function{find_module()} and
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
62 \function{load_module()} to find and load package \var{P}, and then use
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
63 \function{find_module()} with the \var{path} argument set to
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
64 \code{\var{P}.__path__}. When \var{P} itself has a dotted name, apply
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
65 this recipe recursively.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
66 \end{funcdesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
67
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
68 \begin{funcdesc}{load_module}{name, file, filename, description}
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
69 Load a module that was previously found by \function{find_module()} (or by
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
70 an otherwise conducted search yielding compatible results). This
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
71 function does more than importing the module: if the module was
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
72 already imported, it is equivalent to a
15149
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
73 \function{reload()}\bifuncindex{reload}! The \var{name} argument
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
74 indicates the full module name (including the package name, if this is
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
75 a submodule of a package). The \var{file} argument is an open file,
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
76 and \var{filename} is the corresponding file name; these can be
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
77 \code{None} and \code{''}, respectively, when the module is not being
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
78 loaded from a file. The \var{description} argument is a tuple, as
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
79 would be returned by \function{get_suffixes()}, describing what kind
fdc2abdf4777 Fixed a large number of small problems, mostly noted by Detlef Lannert
Fred Drake <fdrake@acm.org>
parents: 13625
diff changeset
80 of module must be loaded.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
81
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
82 If the load is successful, the return value is the module object;
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
83 otherwise, an exception (usually \exception{ImportError}) is raised.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
84
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
85 \strong{Important:} the caller is responsible for closing the
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
86 \var{file} argument, if it was not \code{None}, even when an exception
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
87 is raised. This is best done using a \keyword{try}
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
88 ... \keyword{finally} statement.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
90
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
91 \begin{funcdesc}{new_module}{name}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
92 Return a new empty module object called \var{name}. This object is
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
93 \emph{not} inserted in \code{sys.modules}.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
94 \end{funcdesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
95
19596
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
96 \begin{funcdesc}{lock_held}{}
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
97 Return 1 if the import lock is currently held, else 0.
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
98 On platforms without threads, always return 0.
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
99
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
100 On platforms with threads, a thread executing an import holds an internal
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
101 lock until the import is complete.
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
102 This lock blocks other threads from doing an import until the original
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
103 import completes, which in turn prevents other threads from seeing
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
104 incomplete module objects constructed by the original thread while in
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
105 the process of completing its import (and the imports, if any,
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
106 triggered by that).
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
107 \end{funcdesc}
1be18ef869a4 Add a new function imp.lock_held(), and use it to skip test_threaded_import
Tim Peters <tim.peters@gmail.com>
parents: 18411
diff changeset
108
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
109 The following constants with integer values, defined in this module,
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
110 are used to indicate the search result of \function{find_module()}.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
111
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
112 \begin{datadesc}{PY_SOURCE}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
113 The module was found as a source file.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
114 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
115
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
116 \begin{datadesc}{PY_COMPILED}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
117 The module was found as a compiled code object file.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
118 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
119
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
120 \begin{datadesc}{C_EXTENSION}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
121 The module was found as dynamically loadable shared library.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
122 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
123
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
124 \begin{datadesc}{PY_RESOURCE}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
125 The module was found as a Macintosh resource. This value can only be
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
126 returned on a Macintosh.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
127 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
128
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
129 \begin{datadesc}{PKG_DIRECTORY}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
130 The module was found as a package directory.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
131 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
132
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
133 \begin{datadesc}{C_BUILTIN}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
134 The module was found as a built-in module.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
135 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
136
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
137 \begin{datadesc}{PY_FROZEN}
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
138 The module was found as a frozen module (see \function{init_frozen()}).
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
139 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
140
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
141 The following constant and functions are obsolete; their functionality
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
142 is available through \function{find_module()} or \function{load_module()}.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
143 They are kept around for backward compatibility:
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
144
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
145 \begin{datadesc}{SEARCH_ERROR}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
146 Unused.
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
147 \end{datadesc}
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
148
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 \begin{funcdesc}{init_builtin}{name}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
150 Initialize the built-in module called \var{name} and return its module
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
151 object. If the module was already initialized, it will be initialized
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
152 \emph{again}. A few modules cannot be initialized twice --- attempting
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
153 to initialize these again will raise an \exception{ImportError}
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
154 exception. If there is no
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
155 built-in module called \var{name}, \code{None} is returned.
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
156 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
157
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
158 \begin{funcdesc}{init_frozen}{name}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
159 Initialize the frozen module called \var{name} and return its module
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
160 object. If the module was already initialized, it will be initialized
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
161 \emph{again}. If there is no frozen module called \var{name},
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 \code{None} is returned. (Frozen modules are modules written in
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 Python whose compiled byte-code object is incorporated into a
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
164 custom-built Python interpreter by Python's \program{freeze} utility.
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
165 See \file{Tools/freeze/} for now.)
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
167
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 \begin{funcdesc}{is_builtin}{name}
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
169 Return \code{1} if there is a built-in module called \var{name} which
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
170 can be initialized again. Return \code{-1} if there is a built-in
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
171 module called \var{name} which cannot be initialized again (see
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
172 \function{init_builtin()}). Return \code{0} if there is no built-in
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
173 module called \var{name}.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
174 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
175
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
176 \begin{funcdesc}{is_frozen}{name}
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
177 Return \code{1} if there is a frozen module (see
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
178 \function{init_frozen()}) called \var{name}, or \code{0} if there is
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
179 no such module.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
181
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
182 \begin{funcdesc}{load_compiled}{name, pathname, file}
8935
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
183 \indexii{file}{byte-code}
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 Load and initialize a module implemented as a byte-compiled code file
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 and return its module object. If the module was already initialized,
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
186 it will be initialized \emph{again}. The \var{name} argument is used
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
187 to create or access a module object. The \var{pathname} argument
3709
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
188 points to the byte-compiled code file. The \var{file}
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 argument is the byte-compiled code file, open for reading in binary
3709
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
190 mode, from the beginning.
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
191 It must currently be a real file object, not a
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 user-defined class emulating a file.
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
194
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
195 \begin{funcdesc}{load_dynamic}{name, pathname\optional{, file}}
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 Load and initialize a module implemented as a dynamically loadable
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 shared library and return its module object. If the module was
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
198 already initialized, it will be initialized \emph{again}. Some modules
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 don't like that and may raise an exception. The \var{pathname}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 argument must point to the shared library. The \var{name} argument is
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 used to construct the name of the initialization function: an external
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
202 C function called \samp{init\var{name}()} in the shared library is
13625
fdfd1049f36e Rob W. W. Hooft's spelling fixes for the Library Reference. I hope
Thomas Wouters <thomas@python.org>
parents: 10966
diff changeset
203 called. The optional \var{file} argument is ignored. (Note: using
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 shared libraries is highly system dependent, and not all systems
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 support it.)
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
207
7712
0cbc9df882b6 Change "\," to just "," in function signatures. This is easier to maintain,
Fred Drake <fdrake@acm.org>
parents: 7606
diff changeset
208 \begin{funcdesc}{load_source}{name, pathname, file}
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 Load and initialize a module implemented as a Python source file and
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 return its module object. If the module was already initialized, it
7038
49ce18a1699f Normalize some markup.
Fred Drake <fdrake@acm.org>
parents: 6956
diff changeset
211 will be initialized \emph{again}. The \var{name} argument is used to
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 create or access a module object. The \var{pathname} argument points
3709
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
213 to the source file. The \var{file} argument is the source
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
214 file, open for reading as text, from the beginning.
dad4d43363d8 Slight update; file args are no longer optional.
Guido van Rossum <guido@python.org>
parents: 2943
diff changeset
215 It must currently be a real file
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
216 object, not a user-defined class emulating a file. Note that if a
8935
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
217 properly matching byte-compiled file (with suffix \file{.pyc} or
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
218 \file{.pyo}) exists, it will be used instead of parsing the given
2f936b7a09f9 Added index entries.
Fred Drake <fdrake@acm.org>
parents: 8918
diff changeset
219 source file.
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
220 \end{funcdesc}
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
221
7400
685c510e14ce Added \label{} for logical addressing.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
222
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
223 \subsection{Examples}
7400
685c510e14ce Added \label{} for logical addressing.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
224 \label{examples-imp}
685c510e14ce Added \label{} for logical addressing.
Fred Drake <fdrake@acm.org>
parents: 7277
diff changeset
225
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
226 The following function emulates what was the standard import statement
18411
eeb2e77c5bad Fix up a few style nits -- avoid "e.g." and "i.e." -- these make
Fred Drake <fdrake@acm.org>
parents: 15928
diff changeset
227 up to Python 1.4 (no hierarchical module names). (This
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
228 \emph{implementation} wouldn't work in that version, since
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
229 \function{find_module()} has been extended and
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
230 \function{load_module()} has been added in 1.4.)
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
231
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7038
diff changeset
232 \begin{verbatim}
15928
1872e609a634 Add a missing newline in an example; caught by Chris Ryland
Fred Drake <fdrake@acm.org>
parents: 15149
diff changeset
233 import imp
1872e609a634 Add a missing newline in an example; caught by Chris Ryland
Fred Drake <fdrake@acm.org>
parents: 15149
diff changeset
234 import sys
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
235
2345
9552edb4eabf make __import__ 4-args
Guido van Rossum <guido@python.org>
parents: 2110
diff changeset
236 def __import__(name, globals=None, locals=None, fromlist=None):
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
237 # Fast path: see if the module has already been imported.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
238 try:
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
239 return sys.modules[name]
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
240 except KeyError:
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
241 pass
2110
6c4905ffff01 new module
Guido van Rossum <guido@python.org>
parents:
diff changeset
242
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
243 # If any of the following calls raises an exception,
2647
9b7e6d9a528d typos, layout and other small things
Guido van Rossum <guido@python.org>
parents: 2609
diff changeset
244 # there's a problem we can't handle -- let the caller handle it.
2576
f47bc5554418 mass changes; fix titles; add examples; correct typos; clarifications;
Guido van Rossum <guido@python.org>
parents: 2527
diff changeset
245
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
246 fp, pathname, description = imp.find_module(name)
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
247
2818
c9b7297a0758 fix bug in example (should close file at all times)
Guido van Rossum <guido@python.org>
parents: 2647
diff changeset
248 try:
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
249 return imp.load_module(name, fp, pathname, description)
2818
c9b7297a0758 fix bug in example (should close file at all times)
Guido van Rossum <guido@python.org>
parents: 2647
diff changeset
250 finally:
c9b7297a0758 fix bug in example (should close file at all times)
Guido van Rossum <guido@python.org>
parents: 2647
diff changeset
251 # Since we may exit via an exception, close fp explicitly.
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
252 if fp:
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
253 fp.close()
7277
bf4e790bd61a Remove all \bcode / \ecode cruft; this is no longer needed. See previous
Fred Drake <fdrake@acm.org>
parents: 7038
diff changeset
254 \end{verbatim}
6303
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
255
cf0effc3f585 Update the description and the example to the new functionality, which
Guido van Rossum <guido@python.org>
parents: 5818
diff changeset
256 A more complete example that implements hierarchical module names and
7606
c0c02e9269f3 Logical markup.
Fred Drake <fdrake@acm.org>
parents: 7400
diff changeset
257 includes a \function{reload()}\bifuncindex{reload} function can be
22879
e1423ec9e6ec Do not call "knee" a standard module.
Fred Drake <fdrake@acm.org>
parents: 19596
diff changeset
258 found in the module \module{knee}\refmodindex{knee}.