source: trunk/essentials/dev-lang/python/Doc/api/init.tex@ 3226

Last change on this file since 3226 was 3225, checked in by bird, 19 years ago

Python 2.5

File size: 40.1 KB
Line 
1\chapter{Initialization, Finalization, and Threads
2 \label{initialization}}
3
4\begin{cfuncdesc}{void}{Py_Initialize}{}
5 Initialize the Python interpreter. In an application embedding
6 Python, this should be called before using any other Python/C API
7 functions; with the exception of
8 \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()},
9 \cfunction{PyEval_InitThreads()}\ttindex{PyEval_InitThreads()},
10 \cfunction{PyEval_ReleaseLock()}\ttindex{PyEval_ReleaseLock()},
11 and \cfunction{PyEval_AcquireLock()}\ttindex{PyEval_AcquireLock()}.
12 This initializes the table of loaded modules (\code{sys.modules}),
13 and\withsubitem{(in module sys)}{\ttindex{modules}\ttindex{path}}
14 creates the fundamental modules
15 \module{__builtin__}\refbimodindex{__builtin__},
16 \module{__main__}\refbimodindex{__main__} and
17 \module{sys}\refbimodindex{sys}. It also initializes the module
18 search\indexiii{module}{search}{path} path (\code{sys.path}).
19 It does not set \code{sys.argv}; use
20 \cfunction{PySys_SetArgv()}\ttindex{PySys_SetArgv()} for that. This
21 is a no-op when called for a second time (without calling
22 \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} first). There is
23 no return value; it is a fatal error if the initialization fails.
24\end{cfuncdesc}
25
26\begin{cfuncdesc}{void}{Py_InitializeEx}{int initsigs}
27 This function works like \cfunction{Py_Initialize()} if
28 \var{initsigs} is 1. If \var{initsigs} is 0, it skips
29 initialization registration of signal handlers, which
30 might be useful when Python is embedded. \versionadded{2.4}
31\end{cfuncdesc}
32
33\begin{cfuncdesc}{int}{Py_IsInitialized}{}
34 Return true (nonzero) when the Python interpreter has been
35 initialized, false (zero) if not. After \cfunction{Py_Finalize()}
36 is called, this returns false until \cfunction{Py_Initialize()} is
37 called again.
38\end{cfuncdesc}
39
40\begin{cfuncdesc}{void}{Py_Finalize}{}
41 Undo all initializations made by \cfunction{Py_Initialize()} and
42 subsequent use of Python/C API functions, and destroy all
43 sub-interpreters (see \cfunction{Py_NewInterpreter()} below) that
44 were created and not yet destroyed since the last call to
45 \cfunction{Py_Initialize()}. Ideally, this frees all memory
46 allocated by the Python interpreter. This is a no-op when called
47 for a second time (without calling \cfunction{Py_Initialize()} again
48 first). There is no return value; errors during finalization are
49 ignored.
50
51 This function is provided for a number of reasons. An embedding
52 application might want to restart Python without having to restart
53 the application itself. An application that has loaded the Python
54 interpreter from a dynamically loadable library (or DLL) might want
55 to free all memory allocated by Python before unloading the
56 DLL. During a hunt for memory leaks in an application a developer
57 might want to free all memory allocated by Python before exiting
58 from the application.
59
60 \strong{Bugs and caveats:} The destruction of modules and objects in
61 modules is done in random order; this may cause destructors
62 (\method{__del__()} methods) to fail when they depend on other
63 objects (even functions) or modules. Dynamically loaded extension
64 modules loaded by Python are not unloaded. Small amounts of memory
65 allocated by the Python interpreter may not be freed (if you find a
66 leak, please report it). Memory tied up in circular references
67 between objects is not freed. Some memory allocated by extension
68 modules may not be freed. Some extensions may not work properly if
69 their initialization routine is called more than once; this can
70 happen if an application calls \cfunction{Py_Initialize()} and
71 \cfunction{Py_Finalize()} more than once.
72\end{cfuncdesc}
73
74\begin{cfuncdesc}{PyThreadState*}{Py_NewInterpreter}{}
75 Create a new sub-interpreter. This is an (almost) totally separate
76 environment for the execution of Python code. In particular, the
77 new interpreter has separate, independent versions of all imported
78 modules, including the fundamental modules
79 \module{__builtin__}\refbimodindex{__builtin__},
80 \module{__main__}\refbimodindex{__main__} and
81 \module{sys}\refbimodindex{sys}. The table of loaded modules
82 (\code{sys.modules}) and the module search path (\code{sys.path})
83 are also separate. The new environment has no \code{sys.argv}
84 variable. It has new standard I/O stream file objects
85 \code{sys.stdin}, \code{sys.stdout} and \code{sys.stderr} (however
86 these refer to the same underlying \ctype{FILE} structures in the C
87 library).
88 \withsubitem{(in module sys)}{
89 \ttindex{stdout}\ttindex{stderr}\ttindex{stdin}}
90
91 The return value points to the first thread state created in the new
92 sub-interpreter. This thread state is made in the current thread
93 state. Note that no actual thread is created; see the discussion of
94 thread states below. If creation of the new interpreter is
95 unsuccessful, \NULL{} is returned; no exception is set since the
96 exception state is stored in the current thread state and there may
97 not be a current thread state. (Like all other Python/C API
98 functions, the global interpreter lock must be held before calling
99 this function and is still held when it returns; however, unlike
100 most other Python/C API functions, there needn't be a current thread
101 state on entry.)
102
103 Extension modules are shared between (sub-)interpreters as follows:
104 the first time a particular extension is imported, it is initialized
105 normally, and a (shallow) copy of its module's dictionary is
106 squirreled away. When the same extension is imported by another
107 (sub-)interpreter, a new module is initialized and filled with the
108 contents of this copy; the extension's \code{init} function is not
109 called. Note that this is different from what happens when an
110 extension is imported after the interpreter has been completely
111 re-initialized by calling
112 \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} and
113 \cfunction{Py_Initialize()}\ttindex{Py_Initialize()}; in that case,
114 the extension's \code{init\var{module}} function \emph{is} called
115 again.
116
117 \strong{Bugs and caveats:} Because sub-interpreters (and the main
118 interpreter) are part of the same process, the insulation between
119 them isn't perfect --- for example, using low-level file operations
120 like \withsubitem{(in module os)}{\ttindex{close()}}
121 \function{os.close()} they can (accidentally or maliciously) affect
122 each other's open files. Because of the way extensions are shared
123 between (sub-)interpreters, some extensions may not work properly;
124 this is especially likely when the extension makes use of (static)
125 global variables, or when the extension manipulates its module's
126 dictionary after its initialization. It is possible to insert
127 objects created in one sub-interpreter into a namespace of another
128 sub-interpreter; this should be done with great care to avoid
129 sharing user-defined functions, methods, instances or classes
130 between sub-interpreters, since import operations executed by such
131 objects may affect the wrong (sub-)interpreter's dictionary of
132 loaded modules. (XXX This is a hard-to-fix bug that will be
133 addressed in a future release.)
134
135 Also note that the use of this functionality is incompatible with
136 extension modules such as PyObjC and ctypes that use the
137 \cfunction{PyGILState_*} APIs (and this is inherent in the way the
138 \cfunction{PyGILState_*} functions work). Simple things may work,
139 but confusing behavior will always be near.
140\end{cfuncdesc}
141
142\begin{cfuncdesc}{void}{Py_EndInterpreter}{PyThreadState *tstate}
143 Destroy the (sub-)interpreter represented by the given thread state.
144 The given thread state must be the current thread state. See the
145 discussion of thread states below. When the call returns, the
146 current thread state is \NULL. All thread states associated with
147 this interpreter are destroyed. (The global interpreter lock must
148 be held before calling this function and is still held when it
149 returns.) \cfunction{Py_Finalize()}\ttindex{Py_Finalize()} will
150 destroy all sub-interpreters that haven't been explicitly destroyed
151 at that point.
152\end{cfuncdesc}
153
154\begin{cfuncdesc}{void}{Py_SetProgramName}{char *name}
155 This function should be called before
156 \cfunction{Py_Initialize()}\ttindex{Py_Initialize()} is called
157 for the first time, if it is called at all. It tells the
158 interpreter the value of the \code{argv[0]} argument to the
159 \cfunction{main()}\ttindex{main()} function of the program. This is
160 used by \cfunction{Py_GetPath()}\ttindex{Py_GetPath()} and some
161 other functions below to find the Python run-time libraries relative
162 to the interpreter executable. The default value is
163 \code{'python'}. The argument should point to a zero-terminated
164 character string in static storage whose contents will not change
165 for the duration of the program's execution. No code in the Python
166 interpreter will change the contents of this storage.
167\end{cfuncdesc}
168
169\begin{cfuncdesc}{char*}{Py_GetProgramName}{}
170 Return the program name set with
171 \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()}, or the
172 default. The returned string points into static storage; the caller
173 should not modify its value.
174\end{cfuncdesc}
175
176\begin{cfuncdesc}{char*}{Py_GetPrefix}{}
177 Return the \emph{prefix} for installed platform-independent files.
178 This is derived through a number of complicated rules from the
179 program name set with \cfunction{Py_SetProgramName()} and some
180 environment variables; for example, if the program name is
181 \code{'/usr/local/bin/python'}, the prefix is \code{'/usr/local'}.
182 The returned string points into static storage; the caller should
183 not modify its value. This corresponds to the \makevar{prefix}
184 variable in the top-level \file{Makefile} and the
185 \longprogramopt{prefix} argument to the \program{configure} script
186 at build time. The value is available to Python code as
187 \code{sys.prefix}. It is only useful on \UNIX{}. See also the next
188 function.
189\end{cfuncdesc}
190
191\begin{cfuncdesc}{char*}{Py_GetExecPrefix}{}
192 Return the \emph{exec-prefix} for installed
193 platform-\emph{de}pendent files. This is derived through a number
194 of complicated rules from the program name set with
195 \cfunction{Py_SetProgramName()} and some environment variables; for
196 example, if the program name is \code{'/usr/local/bin/python'}, the
197 exec-prefix is \code{'/usr/local'}. The returned string points into
198 static storage; the caller should not modify its value. This
199 corresponds to the \makevar{exec_prefix} variable in the top-level
200 \file{Makefile} and the \longprogramopt{exec-prefix} argument to the
201 \program{configure} script at build time. The value is available
202 to Python code as \code{sys.exec_prefix}. It is only useful on
203 \UNIX.
204
205 Background: The exec-prefix differs from the prefix when platform
206 dependent files (such as executables and shared libraries) are
207 installed in a different directory tree. In a typical installation,
208 platform dependent files may be installed in the
209 \file{/usr/local/plat} subtree while platform independent may be
210 installed in \file{/usr/local}.
211
212 Generally speaking, a platform is a combination of hardware and
213 software families, e.g. Sparc machines running the Solaris 2.x
214 operating system are considered the same platform, but Intel
215 machines running Solaris 2.x are another platform, and Intel
216 machines running Linux are yet another platform. Different major
217 revisions of the same operating system generally also form different
218 platforms. Non-\UNIX{} operating systems are a different story; the
219 installation strategies on those systems are so different that the
220 prefix and exec-prefix are meaningless, and set to the empty string.
221 Note that compiled Python bytecode files are platform independent
222 (but not independent from the Python version by which they were
223 compiled!).
224
225 System administrators will know how to configure the \program{mount}
226 or \program{automount} programs to share \file{/usr/local} between
227 platforms while having \file{/usr/local/plat} be a different
228 filesystem for each platform.
229\end{cfuncdesc}
230
231\begin{cfuncdesc}{char*}{Py_GetProgramFullPath}{}
232 Return the full program name of the Python executable; this is
233 computed as a side-effect of deriving the default module search path
234 from the program name (set by
235 \cfunction{Py_SetProgramName()}\ttindex{Py_SetProgramName()} above).
236 The returned string points into static storage; the caller should
237 not modify its value. The value is available to Python code as
238 \code{sys.executable}.
239 \withsubitem{(in module sys)}{\ttindex{executable}}
240\end{cfuncdesc}
241
242\begin{cfuncdesc}{char*}{Py_GetPath}{}
243 \indexiii{module}{search}{path}
244 Return the default module search path; this is computed from the
245 program name (set by \cfunction{Py_SetProgramName()} above) and some
246 environment variables. The returned string consists of a series of
247 directory names separated by a platform dependent delimiter
248 character. The delimiter character is \character{:} on \UNIX and Mac OS X,
249 \character{;} on Windows. The returned string points into
250 static storage; the caller should not modify its value. The value
251 is available to Python code as the list
252 \code{sys.path}\withsubitem{(in module sys)}{\ttindex{path}}, which
253 may be modified to change the future search path for loaded
254 modules.
255
256 % XXX should give the exact rules
257\end{cfuncdesc}
258
259\begin{cfuncdesc}{const char*}{Py_GetVersion}{}
260 Return the version of this Python interpreter. This is a string
261 that looks something like
262
263\begin{verbatim}
264"1.5 (#67, Dec 31 1997, 22:34:28) [GCC 2.7.2.2]"
265\end{verbatim}
266
267 The first word (up to the first space character) is the current
268 Python version; the first three characters are the major and minor
269 version separated by a period. The returned string points into
270 static storage; the caller should not modify its value. The value
271 is available to Python code as \code{sys.version}.
272 \withsubitem{(in module sys)}{\ttindex{version}}
273\end{cfuncdesc}
274
275\begin{cfuncdesc}{const char*}{Py_GetBuildNumber}{}
276 Return a string representing the Subversion revision that this Python
277 executable was built from. This number is a string because it may contain a
278 trailing 'M' if Python was built from a mixed revision source tree.
279 \versionadded{2.5}
280\end{cfuncdesc}
281
282\begin{cfuncdesc}{const char*}{Py_GetPlatform}{}
283 Return the platform identifier for the current platform. On \UNIX,
284 this is formed from the ``official'' name of the operating system,
285 converted to lower case, followed by the major revision number;
286 e.g., for Solaris 2.x, which is also known as SunOS 5.x, the value
287 is \code{'sunos5'}. On Mac OS X, it is \code{'darwin'}. On Windows,
288 it is \code{'win'}. The returned string points into static storage;
289 the caller should not modify its value. The value is available to
290 Python code as \code{sys.platform}.
291 \withsubitem{(in module sys)}{\ttindex{platform}}
292\end{cfuncdesc}
293
294\begin{cfuncdesc}{const char*}{Py_GetCopyright}{}
295 Return the official copyright string for the current Python version,
296 for example
297
298 \code{'Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam'}
299
300 The returned string points into static storage; the caller should
301 not modify its value. The value is available to Python code as
302 \code{sys.copyright}.
303 \withsubitem{(in module sys)}{\ttindex{copyright}}
304\end{cfuncdesc}
305
306\begin{cfuncdesc}{const char*}{Py_GetCompiler}{}
307 Return an indication of the compiler used to build the current
308 Python version, in square brackets, for example:
309
310\begin{verbatim}
311"[GCC 2.7.2.2]"
312\end{verbatim}
313
314 The returned string points into static storage; the caller should
315 not modify its value. The value is available to Python code as part
316 of the variable \code{sys.version}.
317 \withsubitem{(in module sys)}{\ttindex{version}}
318\end{cfuncdesc}
319
320\begin{cfuncdesc}{const char*}{Py_GetBuildInfo}{}
321 Return information about the sequence number and build date and time
322 of the current Python interpreter instance, for example
323
324\begin{verbatim}
325"#67, Aug 1 1997, 22:34:28"
326\end{verbatim}
327
328 The returned string points into static storage; the caller should
329 not modify its value. The value is available to Python code as part