Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / html / python / lib / module-imp.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-zipimport.html" />
13<link rel="prev" href="module-warnings.html" />
14<link rel="parent" href="python.html" />
15<link rel="next" href="examples-imp.html" />
16<meta name='aesop' content='information' />
17<title>3.21 imp -- Access the import internals</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="3.20.3 Available Functions"
25 href="warning-functions.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
28 href="python.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="3.21.1 Examples"
31 href="examples-imp.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="warning-functions.html">3.20.3 Available Functions</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="examples-imp.html">3.21.1 Examples</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0052100000000000000000">
563.21 <tt class="module">imp</tt> --
57 Access the <tt class="keyword">import</tt> internals</A>
58</H1>
59
60<P>
61<A NAME="module-imp"></A>
62
63<P>
64This<a id='l2h-696' xml:id='l2h-696'></a> module provides an interface to the mechanisms
65used to implement the <tt class="keyword">import</tt> statement. It defines the
66following constants and functions:
67
68<P>
69<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
70 <td><nobr><b><tt id='l2h-697' xml:id='l2h-697' class="function">get_magic</tt></b>(</nobr></td>
71 <td><var></var>)</td></tr></table></dt>
72<dd>
73<a id='l2h-698' xml:id='l2h-698'></a>Return the magic string value used to recognize byte-compiled code
74files (<span class="file">.pyc</span> files). (This value may be different for each
75Python version.)
76</dl>
77
78<P>
79<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
80 <td><nobr><b><tt id='l2h-699' xml:id='l2h-699' class="function">get_suffixes</tt></b>(</nobr></td>
81 <td><var></var>)</td></tr></table></dt>
82<dd>
83Return a list of triples, each describing a particular type of module.
84Each triple has the form <code>(<var>suffix</var>, <var>mode</var>,
85<var>type</var>)</code>, where <var>suffix</var> is a string to be appended to the
86module name to form the filename to search for, <var>mode</var> is the mode
87string to pass to the built-in <tt class="function">open()</tt> function to open the
88file (this can be <code>'r'</code> for text files or <code>'rb'</code> for binary
89files), and <var>type</var> is the file type, which has one of the values
90<tt class="constant">PY_SOURCE</tt>, <tt class="constant">PY_COMPILED</tt>, or
91<tt class="constant">C_EXTENSION</tt>, described below.
92</dl>
93
94<P>
95<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
96 <td><nobr><b><tt id='l2h-700' xml:id='l2h-700' class="function">find_module</tt></b>(</nobr></td>
97 <td><var>name</var><big>[</big><var>, path</var><big>]</big><var></var>)</td></tr></table></dt>
98<dd>
99Try to find the module <var>name</var> on the search path <var>path</var>. If
100<var>path</var> is a list of directory names, each directory is searched
101for files with any of the suffixes returned by <tt class="function">get_suffixes()</tt>
102above. Invalid names in the list are silently ignored (but all list
103items must be strings). If <var>path</var> is omitted or <code>None</code>, the
104list of directory names given by <code>sys.path</code> is searched, but
105first it searches a few special places: it tries to find a built-in
106module with the given name (<tt class="constant">C_BUILTIN</tt>), then a frozen module
107(<tt class="constant">PY_FROZEN</tt>), and on some systems some other places are looked
108in as well (on the Mac, it looks for a resource (<tt class="constant">PY_RESOURCE</tt>);
109on Windows, it looks in the registry which may point to a specific
110file).
111
112<P>
113If search is successful, the return value is a triple
114<code>(<var>file</var>, <var>pathname</var>, <var>description</var>)</code> where
115<var>file</var> is an open file object positioned at the beginning,
116<var>pathname</var> is the pathname of the
117file found, and <var>description</var> is a triple as contained in the list
118returned by <tt class="function">get_suffixes()</tt> describing the kind of module found.
119If the module does not live in a file, the returned <var>file</var> is
120<code>None</code>, <var>filename</var> is the empty string, and the
121<var>description</var> tuple contains empty strings for its suffix and
122mode; the module type is as indicate in parentheses above. If the
123search is unsuccessful, <tt class="exception">ImportError</tt> is raised. Other
124exceptions indicate problems with the arguments or environment.
125
126<P>
127This function does not handle hierarchical module names (names
128containing dots). In order to find <var>P</var>.<var>M</var>, that is, submodule
129<var>M</var> of package <var>P</var>, use <tt class="function">find_module()</tt> and
130<tt class="function">load_module()</tt> to find and load package <var>P</var>, and then use
131<tt class="function">find_module()</tt> with the <var>path</var> argument set to
132<code><var>P</var>.__path__</code>. When <var>P</var> itself has a dotted name, apply
133this recipe recursively.
134</dl>
135
136<P>
137<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
138 <td><nobr><b><tt id='l2h-701' xml:id='l2h-701' class="function">load_module</tt></b>(</nobr></td>
139 <td><var>name, file, filename, description</var>)</td></tr></table></dt>
140<dd>
141Load a module that was previously found by <tt class="function">find_module()</tt> (or by
142an otherwise conducted search yielding compatible results). This
143function does more than importing the module: if the module was
144already imported, it is equivalent to a
145<tt class="function">reload()</tt><a id='l2h-702' xml:id='l2h-702'></a>! The <var>name</var> argument
146indicates the full module name (including the package name, if this is
147a submodule of a package). The <var>file</var> argument is an open file,
148and <var>filename</var> is the corresponding file name; these can be
149<code>None</code> and <code>''</code>, respectively, when the module is not being
150loaded from a file. The <var>description</var> argument is a tuple, as
151would be returned by <tt class="function">get_suffixes()</tt>, describing what kind
152of module must be loaded.
153
154<P>
155If the load is successful, the return value is the module object;
156otherwise, an exception (usually <tt class="exception">ImportError</tt>) is raised.
157
158<P>
159<strong>Important:</strong> the caller is responsible for closing the
160<var>file</var> argument, if it was not <code>None</code>, even when an exception
161is raised. This is best done using a <tt class="keyword">try</tt>
162... <tt class="keyword">finally</tt> statement.
163</dl>
164
165<P>
166<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
167 <td><nobr><b><tt id='l2h-703' xml:id='l2h-703' class="function">new_module</tt></b>(</nobr></td>
168 <td><var>name</var>)</td></tr></table></dt>
169<dd>
170Return a new empty module object called <var>name</var>. This object is
171<em>not</em> inserted in <code>sys.modules</code>.
172</dl>
173
174<P>
175<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
176 <td><nobr><b><tt id='l2h-704' xml:id='l2h-704' class="function">lock_held</tt></b>(</nobr></td>
177 <td><var></var>)</td></tr></table></dt>
178<dd>
179Return <code>True</code> if the import lock is currently held, else <code>False</code>.
180On platforms without threads, always return <code>False</code>.
181
182<P>
183On platforms with threads, a thread executing an import holds an internal
184lock until the import is complete.
185This lock blocks other threads from doing an import until the original
186import completes, which in turn prevents other threads from seeing
187incomplete module objects constructed by the original thread while in
188the process of completing its import (and the imports, if any,
189triggered by that).
190</dl>
191
192<P>
193<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
194 <td><nobr><b><tt id='l2h-705' xml:id='l2h-705' class="function">acquire_lock</tt></b>(</nobr></td>
195 <td><var></var>)</td></tr></table></dt>
196<dd>
197Acquires the interpreter's import lock for the current thread. This lock
198should be used by import hooks to ensure thread-safety when importing modules.
199On platforms without threads, this function does nothing.
200
201<span class="versionnote">New in version 2.3.</span>
202
203</dl>
204
205<P>
206<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
207 <td><nobr><b><tt id='l2h-706' xml:id='l2h-706' class="function">release_lock</tt></b>(</nobr></td>
208 <td><var></var>)</td></tr></table></dt>
209<dd>
210Release the interpreter's import lock.
211On platforms without threads, this function does nothing.
212
213<span class="versionnote">New in version 2.3.</span>
214
215</dl>
216
217<P>
218The following constants with integer values, defined in this module,
219are used to indicate the search result of <tt class="function">find_module()</tt>.
220
221<P>
222<dl><dt><b><tt id='l2h-707' xml:id='l2h-707'>PY_SOURCE</tt></b></dt>
223<dd>
224The module was found as a source file.
225</dd></dl>
226
227<P>
228<dl><dt><b><tt id='l2h-708' xml:id='l2h-708'>PY_COMPILED</tt></b></dt>
229<dd>
230The module was found as a compiled code object file.
231</dd></dl>
232
233<P>
234<dl><dt><b><tt id='l2h-709' xml:id='l2h-709'>C_EXTENSION</tt></b></dt>
235<dd>
236The module was found as dynamically loadable shared library.
237</dd></dl>
238
239<P>
240<dl><dt><b><tt id='l2h-710' xml:id='l2h-710'>PY_RESOURCE</tt></b></dt>
241<dd>
242The module was found as a Mac OS 9 resource. This value can only be
243returned on a Mac OS 9 or earlier Macintosh.
244</dd></dl>
245
246<P>
247<dl><dt><b><tt id='l2h-711' xml:id='l2h-711'>PKG_DIRECTORY</tt></b></dt>
248<dd>
249The module was found as a package directory.
250</dd></dl>
251
252<P>
253<dl><dt><b><tt id='l2h-712' xml:id='l2h-712'>C_BUILTIN</tt></b></dt>
254<dd>
255The module was found as a built-in module.
256</dd></dl>
257
258<P>
259<dl><dt><b><tt id='l2h-713' xml:id='l2h-713'>PY_FROZEN</tt></b></dt>
260<dd>
261The module was found as a frozen module (see <tt class="function">init_frozen()</tt>).
262</dd></dl>
263
264<P>
265The following constant and functions are obsolete; their functionality
266is available through <tt class="function">find_module()</tt> or <tt class="function">load_module()</tt>.
267They are kept around for backward compatibility:
268
269<P>
270<dl><dt><b><tt id='l2h-714' xml:id='l2h-714'>SEARCH_ERROR</tt></b></dt>
271<dd>
272Unused.
273</dd></dl>
274
275<P>
276<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
277 <td><nobr><b><tt id='l2h-715' xml:id='l2h-715' class="function">init_builtin</tt></b>(</nobr></td>
278 <td><var>name</var>)</td></tr></table></dt>
279<dd>
280Initialize the built-in module called <var>name</var> and return its module
281object. If the module was already initialized, it will be initialized
282<em>again</em>. A few modules cannot be initialized twice -- attempting
283to initialize these again will raise an <tt class="exception">ImportError</tt>
284exception. If there is no
285built-in module called <var>name</var>, <code>None</code> is returned.
286</dl>
287
288<P>
289<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
290 <td><nobr><b><tt id='l2h-716' xml:id='l2h-716' class="function">init_frozen</tt></b>(</nobr></td>
291 <td><var>name</var>)</td></tr></table></dt>
292<dd>
293Initialize the frozen module called <var>name</var> and return its module
294object. If the module was already initialized, it will be initialized
295<em>again</em>. If there is no frozen module called <var>name</var>,
296<code>None</code> is returned. (Frozen modules are modules written in
297Python whose compiled byte-code object is incorporated into a
298custom-built Python interpreter by Python's <b class="program">freeze</b> utility.
299See <span class="file">Tools/freeze/</span> for now.)
300</dl>
301
302<P>
303<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
304 <td><nobr><b><tt id='l2h-717' xml:id='l2h-717' class="function">is_builtin</tt></b>(</nobr></td>
305 <td><var>name</var>)</td></tr></table></dt>
306<dd>
307Return <code>1</code> if there is a built-in module called <var>name</var> which
308can be initialized again. Return <code>-1</code> if there is a built-in
309module called <var>name</var> which cannot be initialized again (see
310<tt class="function">init_builtin()</tt>). Return <code>0</code> if there is no built-in
311module called <var>name</var>.
312</dl>
313
314<P>
315<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
316 <td><nobr><b><tt id='l2h-718' xml:id='l2h-718' class="function">is_frozen</tt></b>(</nobr></td>
317 <td><var>name</var>)</td></tr></table></dt>
318<dd>
319Return <code>True</code> if there is a frozen module (see
320<tt class="function">init_frozen()</tt>) called <var>name</var>, or <code>False</code> if there is
321no such module.
322</dl>
323
324<P>
325<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
326 <td><nobr><b><tt id='l2h-719' xml:id='l2h-719' class="function">load_compiled</tt></b>(</nobr></td>
327 <td><var>name, pathname, </var><big>[</big><var>file</var><big>]</big><var></var>)</td></tr></table></dt>
328<dd>
329<a id='l2h-720' xml:id='l2h-720'></a>Load and initialize a module implemented as a byte-compiled code file
330and return its module object. If the module was already initialized,
331it will be initialized <em>again</em>. The <var>name</var> argument is used
332to create or access a module object. The <var>pathname</var> argument
333points to the byte-compiled code file. The <var>file</var>
334argument is the byte-compiled code file, open for reading in binary
335mode, from the beginning.
336It must currently be a real file object, not a
337user-defined class emulating a file.
338</dl>
339
340<P>
341<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
342 <td><nobr><b><tt id='l2h-721' xml:id='l2h-721' class="function">load_dynamic</tt></b>(</nobr></td>
343 <td><var>name, pathname</var><big>[</big><var>, file</var><big>]</big><var></var>)</td></tr></table></dt>
344<dd>
345Load and initialize a module implemented as a dynamically loadable
346shared library and return its module object. If the module was
347already initialized, it will be initialized <em>again</em>. Some modules
348don't like that and may raise an exception. The <var>pathname</var>
349argument must point to the shared library. The <var>name</var> argument is
350used to construct the name of the initialization function: an external
351C function called "<tt class="samp">init<var>name</var>()</tt>" in the shared library is
352called. The optional <var>file</var> argument is ignored. (Note: using
353shared libraries is highly system dependent, and not all systems
354support it.)
355</dl>
356
357<P>
358<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
359 <td><nobr><b><tt id='l2h-722' xml:id='l2h-722' class="function">load_source</tt></b>(</nobr></td>
360 <td><var>name, pathname</var><big>[</big><var>, file</var><big>]</big><var></var>)</td></tr></table></dt>
361<dd>
362Load and initialize a module implemented as a Python source file and
363return its module object. If the module was already initialized, it
364will be initialized <em>again</em>. The <var>name</var> argument is used to
365create or access a module object. The <var>pathname</var> argument points
366to the source file. The <var>file</var> argument is the source
367file, open for reading as text, from the beginning.
368It must currently be a real file
369object, not a user-defined class emulating a file. Note that if a
370properly matching byte-compiled file (with suffix <span class="file">.pyc</span> or
371<span class="file">.pyo</span>) exists, it will be used instead of parsing the given
372source file.
373</dl>
374
375<P>
376
377<p><br /></p><hr class='online-navigation' />
378<div class='online-navigation'>
379<!--Table of Child-Links-->
380<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
381
382<UL CLASS="ChildLinks">
383<LI><A href="examples-imp.html">3.21.1 Examples</a>
384</ul>
385<!--End of Table of Child-Links-->
386</div>
387
388<DIV CLASS="navigation">
389<div class='online-navigation'>
390<p></p><hr />
391<table align="center" width="100%" cellpadding="0" cellspacing="2">
392<tr>
393<td class='online-navigation'><a rel="prev" title="3.20.3 Available Functions"
394 href="warning-functions.html"><img src='../icons/previous.png'
395 border='0' height='32' alt='Previous Page' width='32' /></A></td>
396<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
397 href="python.html"><img src='../icons/up.png'
398 border='0' height='32' alt='Up One Level' width='32' /></A></td>
399<td class='online-navigation'><a rel="next" title="3.21.1 Examples"
400 href="examples-imp.html"><img src='../icons/next.png'
401 border='0' height='32' alt='Next Page' width='32' /></A></td>
402<td align="center" width="100%">Python Library Reference</td>
403<td class='online-navigation'><a rel="contents" title="Table of Contents"
404 href="contents.html"><img src='../icons/contents.png'
405 border='0' height='32' alt='Contents' width='32' /></A></td>
406<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
407 border='0' height='32' alt='Module Index' width='32' /></a></td>
408<td class='online-navigation'><a rel="index" title="Index"
409 href="genindex.html"><img src='../icons/index.png'
410 border='0' height='32' alt='Index' width='32' /></A></td>
411</tr></table>
412<div class='online-navigation'>
413<b class="navlabel">Previous:</b>
414<a class="sectref" rel="prev" href="warning-functions.html">3.20.3 Available Functions</A>
415<b class="navlabel">Up:</b>
416<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
417<b class="navlabel">Next:</b>
418<a class="sectref" rel="next" href="examples-imp.html">3.21.1 Examples</A>
419</div>
420</div>
421<hr />
422<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
423</DIV>
424<!--End of Navigation Panel-->
425<ADDRESS>
426See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
427</ADDRESS>
428</BODY>
429</HTML>