Merged release21-maint changes.
[python/dscho.git] / Doc / lib / libsys.tex
blob9f9cb0e39398d39680351c04f359f98995df0f57
1 \section{\module{sys} ---
2 System-specific parameters and functions}
4 \declaremodule{builtin}{sys}
5 \modulesynopsis{Access system-specific parameters and functions.}
7 This module provides access to some variables used or maintained by the
8 interpreter and to functions that interact strongly with the interpreter.
9 It is always available.
12 \begin{datadesc}{argv}
13 The list of command line arguments passed to a Python script.
14 \code{argv[0]} is the script name (it is operating system dependent
15 whether this is a full pathname or not). If the command was
16 executed using the \programopt{-c} command line option to the
17 interpreter, \code{argv[0]} is set to the string \code{'-c'}. If no
18 script name was passed to the Python interpreter, \code{argv} has
19 zero length.
20 \end{datadesc}
22 \begin{datadesc}{byteorder}
23 An indicator of the native byte order. This will have the value
24 \code{'big'} on big-endian (most-signigicant byte first) platforms,
25 and \code{'little'} on little-endian (least-significant byte first)
26 platforms.
27 \versionadded{2.0}
28 \end{datadesc}
30 \begin{datadesc}{builtin_module_names}
31 A tuple of strings giving the names of all modules that are compiled
32 into this Python interpreter. (This information is not available in
33 any other way --- \code{modules.keys()} only lists the imported
34 modules.)
35 \end{datadesc}
37 \begin{datadesc}{copyright}
38 A string containing the copyright pertaining to the Python
39 interpreter.
40 \end{datadesc}
42 \begin{datadesc}{dllhandle}
43 Integer specifying the handle of the Python DLL.
44 Availability: Windows.
45 \end{datadesc}
47 \begin{funcdesc}{displayhook}{\var{value}}
48 If \var{value} is not \code{None}, this function prints it to
49 \code{sys.stdout}, and saves it in \code{__builtin__._}.
51 \code{sys.displayhook} is called on the result of evaluating an
52 expression entered in an interactive Python session. The display of
53 these values can be customized by assigning another one-argument
54 function to \code{sys.displayhook}.
55 \end{funcdesc}
57 \begin{funcdesc}{excepthook}{\var{type}, \var{value}, \var{traceback}}
58 This function prints out a given traceback and exception to
59 \code{sys.stderr}.
61 When an exception is raised and uncaught, the interpreter calls
62 \code{sys.excepthook} with three arguments, the exception class,
63 exception instance, and a traceback object. In an interactive
64 session this happens just before control is returned to the prompt;
65 in a Python program this happens just before the program exits. The
66 handling of such top-level exceptions can be customized by assigning
67 another three-argument function to \code{sys.excepthook}.
68 \end{funcdesc}
70 \begin{datadesc}{__displayhook__}
71 \dataline{__excepthook__}
72 These objects contain the original values of \code{displayhook} and
73 \code{excepthook} at the start of the program. They are saved so
74 that \code{displayhook} and \code{excepthook} can be restored in
75 case they happen to get replaced with broken objects.
76 \end{datadesc}
78 \begin{funcdesc}{exc_info}{}
79 This function returns a tuple of three values that give information
80 about the exception that is currently being handled. The
81 information returned is specific both to the current thread and to
82 the current stack frame. If the current stack frame is not handling
83 an exception, the information is taken from the calling stack frame,
84 or its caller, and so on until a stack frame is found that is
85 handling an exception. Here, ``handling an exception'' is defined
86 as ``executing or having executed an except clause.'' For any stack
87 frame, only information about the most recently handled exception is
88 accessible.
90 If no exception is being handled anywhere on the stack, a tuple
91 containing three \code{None} values is returned. Otherwise, the
92 values returned are \code{(\var{type}, \var{value},
93 \var{traceback})}. Their meaning is: \var{type} gets the exception
94 type of the exception being handled (a string or class object);
95 \var{value} gets the exception parameter (its \dfn{associated value}
96 or the second argument to \keyword{raise}, which is always a class
97 instance if the exception type is a class object); \var{traceback}
98 gets a traceback object (see the Reference Manual) which
99 encapsulates the call stack at the point where the exception
100 originally occurred. \obindex{traceback}
102 \strong{Warning:} assigning the \var{traceback} return value to a
103 local variable in a function that is handling an exception will
104 cause a circular reference. This will prevent anything referenced
105 by a local variable in the same function or by the traceback from
106 being garbage collected. Since most functions don't need access to
107 the traceback, the best solution is to use something like
108 \code{type, value = sys.exc_info()[:2]} to extract only the
109 exception type and value. If you do need the traceback, make sure
110 to delete it after use (best done with a \keyword{try}
111 ... \keyword{finally} statement) or to call \function{exc_info()} in
112 a function that does not itself handle an exception.
113 \end{funcdesc}
115 \begin{datadesc}{exc_type}
116 \dataline{exc_value}
117 \dataline{exc_traceback}
118 \deprecated {1.5}
119 {Use \function{exc_info()} instead.}
120 Since they are global variables, they are not specific to the
121 current thread, so their use is not safe in a multi-threaded
122 program. When no exception is being handled, \code{exc_type} is set
123 to \code{None} and the other two are undefined.
124 \end{datadesc}
126 \begin{datadesc}{exec_prefix}
127 A string giving the site-specific directory prefix where the
128 platform-dependent Python files are installed; by default, this is
129 also \code{'/usr/local'}. This can be set at build time with the
130 \longprogramopt{exec-prefix} argument to the \program{configure}
131 script. Specifically, all configuration files (e.g. the
132 \file{pyconfig.h} header file) are installed in the directory
133 \code{exec_prefix + '/lib/python\var{version}/config'}, and shared
134 library modules are installed in \code{exec_prefix +
135 '/lib/python\var{version}/lib-dynload'}, where \var{version} is
136 equal to \code{version[:3]}.
137 \end{datadesc}
139 \begin{datadesc}{executable}
140 A string giving the name of the executable binary for the Python
141 interpreter, on systems where this makes sense.
142 \end{datadesc}
144 \begin{funcdesc}{exit}{\optional{arg}}
145 Exit from Python. This is implemented by raising the
146 \exception{SystemExit} exception, so cleanup actions specified by
147 finally clauses of \keyword{try} statements are honored, and it is
148 possible to intercept the exit attempt at an outer level. The
149 optional argument \var{arg} can be an integer giving the exit status
150 (defaulting to zero), or another type of object. If it is an
151 integer, zero is considered ``successful termination'' and any
152 nonzero value is considered ``abnormal termination'' by shells and
153 the like. Most systems require it to be in the range 0-127, and
154 produce undefined results otherwise. Some systems have a convention
155 for assigning specific meanings to specific exit codes, but these
156 are generally underdeveloped; Unix programs generally use 2 for
157 command line syntax errors and 1 for all other kind of errors. If
158 another type of object is passed, \code{None} is equivalent to
159 passing zero, and any other object is printed to \code{sys.stderr}
160 and results in an exit code of 1. In particular,
161 \code{sys.exit("some error message")} is a quick way to exit a
162 program when an error occurs.
163 \end{funcdesc}
165 \begin{datadesc}{exitfunc}
166 This value is not actually defined by the module, but can be set by
167 the user (or by a program) to specify a clean-up action at program
168 exit. When set, it should be a parameterless function. This
169 function will be called when the interpreter exits. Only one
170 function may be installed in this way; to allow multiple functions
171 which will be called at termination, use the \refmodule{atexit}
172 module. Note: the exit function is not called when the program is
173 killed by a signal, when a Python fatal internal error is detected,
174 or when \code{os._exit()} is called.
175 \end{datadesc}
177 \begin{funcdesc}{getdefaultencoding}{}
178 Return the name of the current default string encoding used by the
179 Unicode implementation.
180 \versionadded{2.0}
181 \end{funcdesc}
183 \begin{funcdesc}{getdlopenflags}{}
184 Return the current value of the flags that are used for
185 \cfunction{dlopen()} calls. The flag constants are defined in the
186 \refmodule{dl} and \module{DLFCN} modules.
187 Availability: \UNIX.
188 \versionadded{2.2}
189 \end{funcdesc}
191 \begin{funcdesc}{getrefcount}{object}
192 Return the reference count of the \var{object}. The count returned
193 is generally one higher than you might expect, because it includes
194 the (temporary) reference as an argument to
195 \function{getrefcount()}.
196 \end{funcdesc}
198 \begin{funcdesc}{getrecursionlimit}{}
199 Return the current value of the recursion limit, the maximum depth
200 of the Python interpreter stack. This limit prevents infinite
201 recursion from causing an overflow of the C stack and crashing
202 Python. It can be set by \function{setrecursionlimit()}.
203 \end{funcdesc}
205 \begin{funcdesc}{_getframe}{\optional{depth}}
206 Return a frame object from the call stack. If optional integer
207 \var{depth} is given, return the frame object that many calls below
208 the top of the stack. If that is deeper than the call stack,
209 \exception{ValueError} is raised. The default for \var{depth} is
210 zero, returning the frame at the top of the call stack.
212 This function should be used for internal and specialized purposes
213 only.
214 \end{funcdesc}
216 \begin{datadesc}{hexversion}
217 The version number encoded as a single integer. This is guaranteed
218 to increase with each version, including proper support for
219 non-production releases. For example, to test that the Python
220 interpreter is at least version 1.5.2, use:
222 \begin{verbatim}
223 if sys.hexversion >= 0x010502F0:
224 # use some advanced feature
226 else:
227 # use an alternative implementation or warn the user
229 \end{verbatim}
231 This is called \samp{hexversion} since it only really looks
232 meaningful when viewed as the result of passing it to the built-in
233 \function{hex()} function. The \code{version_info} value may be
234 used for a more human-friendly encoding of the same information.
235 \versionadded{1.5.2}
236 \end{datadesc}
238 \begin{datadesc}{last_type}
239 \dataline{last_value}
240 \dataline{last_traceback}
241 These three variables are not always defined; they are set when an
242 exception is not handled and the interpreter prints an error message
243 and a stack traceback. Their intended use is to allow an
244 interactive user to import a debugger module and engage in
245 post-mortem debugging without having to re-execute the command that
246 caused the error. (Typical use is \samp{import pdb; pdb.pm()} to
247 enter the post-mortem debugger; see chapter \ref{debugger}, ``The
248 Python Debugger,'' for more information.)
250 The meaning of the variables is the same as that of the return
251 values from \function{exc_info()} above. (Since there is only one
252 interactive thread, thread-safety is not a concern for these
253 variables, unlike for \code{exc_type} etc.)
254 \end{datadesc}
256 \begin{datadesc}{maxint}
257 The largest positive integer supported by Python's regular integer
258 type. This is at least 2**31-1. The largest negative integer is
259 \code{-maxint-1} -- the asymmetry results from the use of 2's
260 complement binary arithmetic.
261 \end{datadesc}
263 \begin{datadesc}{modules}
264 This is a dictionary that maps module names to modules which have
265 already been loaded. This can be manipulated to force reloading of
266 modules and other tricks. Note that removing a module from this
267 dictionary is \emph{not} the same as calling
268 \function{reload()}\bifuncindex{reload} on the corresponding module
269 object.
270 \end{datadesc}
272 \begin{datadesc}{path}
273 \indexiii{module}{search}{path}
274 A list of strings that specifies the search path for modules.
275 Initialized from the environment variable \envvar{PYTHONPATH}, or an
276 installation-dependent default.
278 The first item of this list, \code{path[0]}, is the directory
279 containing the script that was used to invoke the Python
280 interpreter. If the script directory is not available (e.g. if the
281 interpreter is invoked interactively or if the script is read from
282 standard input), \code{path[0]} is the empty string, which directs
283 Python to search modules in the current directory first. Notice
284 that the script directory is inserted \emph{before} the entries
285 inserted as a result of \envvar{PYTHONPATH}.
286 \end{datadesc}
288 \begin{datadesc}{platform}
289 This string contains a platform identifier, e.g. \code{'sunos5'} or
290 \code{'linux1'}. This can be used to append platform-specific
291 components to \code{path}, for instance.
292 \end{datadesc}
294 \begin{datadesc}{prefix}
295 A string giving the site-specific directory prefix where the
296 platform independent Python files are installed; by default, this is
297 the string \code{'/usr/local'}. This can be set at build time with
298 the \longprogramopt{prefix} argument to the \program{configure}
299 script. The main collection of Python library modules is installed
300 in the directory \code{prefix + '/lib/python\var{version}'} while
301 the platform independent header files (all except \file{pyconfig.h})
302 are stored in \code{prefix + '/include/python\var{version}'}, where
303 \var{version} is equal to \code{version[:3]}.
304 \end{datadesc}
306 \begin{datadesc}{ps1}
307 \dataline{ps2}
308 \index{interpreter prompts}
309 \index{prompts, interpreter}
310 Strings specifying the primary and secondary prompt of the
311 interpreter. These are only defined if the interpreter is in
312 interactive mode. Their initial values in this case are
313 \code{'>\code{>}> '} and \code{'... '}. If a non-string object is
314 assigned to either variable, its \function{str()} is re-evaluated
315 each time the interpreter prepares to read a new interactive
316 command; this can be used to implement a dynamic prompt.
317 \end{datadesc}
319 \begin{funcdesc}{setcheckinterval}{interval}
320 Set the interpreter's ``check interval''. This integer value
321 determines how often the interpreter checks for periodic things such
322 as thread switches and signal handlers. The default is \code{10},
323 meaning the check is performed every 10 Python virtual instructions.
324 Setting it to a larger value may increase performance for programs
325 using threads. Setting it to a value \code{<=} 0 checks every
326 virtual instruction, maximizing responsiveness as well as overhead.
327 \end{funcdesc}
329 \begin{funcdesc}{setdefaultencoding}{name}
330 Set the current default string encoding used by the Unicode
331 implementation. If \var{name} does not match any available
332 encoding, \exception{LookupError} is raised. This function is only
333 intended to be used by the \refmodule{site} module implementation
334 and, where needed, by \module{sitecustomize}. Once used by the
335 \refmodule{site} module, it is removed from the \module{sys}
336 module's namespace.
337 % Note that \refmodule{site} is not imported if
338 % the \programopt{-S} option is passed to the interpreter, in which
339 % case this function will remain available.
340 \versionadded{2.0}
341 \end{funcdesc}
343 \begin{funcdesc}{setdlopenflags}{n}
344 Set the flags used by the interpreter for \cfunction{dlopen()}
345 calls, such as when the interpreter loads extension modules. Among
346 other things, this will enable a lazy resolving of symbols when
347 importing a module, if called as \code{sys.setdlopenflags(0)}. To
348 share symbols across extension modules, call as
349 \code{sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)}. Symbolic
350 names for the flag modules can be either found in the \refmodule{dl}
351 module, or in the \module{DLFCN} module. If \module{DLFCN} is not
352 available, it can be generated from \file{/usr/include/dlfcn.h}
353 using the \program{h2py} script.
354 Availability: \UNIX.
355 \versionadded{2.2}
356 \end{funcdesc}
358 \begin{funcdesc}{setprofile}{profilefunc}
359 Set the system's profile function,\index{profile function} which
360 allows you to implement a Python source code profiler in
361 Python.\index{profiler} See chapter \ref{profile} for more
362 information on the Python profiler. The system's profile function
363 is called similarly to the system's trace function (see
364 \function{settrace()}), but it isn't called for each executed line
365 of code (only on call and return and when an exception occurs).
366 Also, its return value is not used, so it can simply return
367 \code{None}.
368 \end{funcdesc}
370 \begin{funcdesc}{setrecursionlimit}{limit}
371 Set the maximum depth of the Python interpreter stack to
372 \var{limit}. This limit prevents infinite recursion from causing an
373 overflow of the C stack and crashing Python.
375 The highest possible limit is platform-dependent. A user may need
376 to set the limit higher when she has a program that requires deep
377 recursion and a platform that supports a higher limit. This should
378 be done with care, because a too-high limit can lead to a crash.
379 \end{funcdesc}
381 \begin{funcdesc}{settrace}{tracefunc}
382 Set the system's trace function,\index{trace function} which allows
383 you to implement a Python source code debugger in Python. See
384 section \ref{debugger-hooks}, ``How It Works,'' in the chapter on
385 the Python debugger.\index{debugger}
386 \end{funcdesc}
388 \begin{datadesc}{stdin}
389 \dataline{stdout}
390 \dataline{stderr}
391 File objects corresponding to the interpreter's standard input,
392 output and error streams. \code{stdin} is used for all interpreter
393 input except for scripts but including calls to
394 \function{input()}\bifuncindex{input} and
395 \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is
396 used for the output of \keyword{print} and expression statements and
397 for the prompts of \function{input()} and \function{raw_input()}.
398 The interpreter's own prompts and (almost all of) its error messages
399 go to \code{stderr}. \code{stdout} and \code{stderr} needn't be
400 built-in file objects: any object is acceptable as long as it has a
401 \method{write()} method that takes a string argument. (Changing
402 these objects doesn't affect the standard I/O streams of processes
403 executed by \function{os.popen()}, \function{os.system()} or the
404 \function{exec*()} family of functions in the \refmodule{os}
405 module.)
406 \end{datadesc}
408 \begin{datadesc}{__stdin__}
409 \dataline{__stdout__}
410 \dataline{__stderr__}
411 These objects contain the original values of \code{stdin},
412 \code{stderr} and \code{stdout} at the start of the program. They
413 are used during finalization, and could be useful to restore the
414 actual files to known working file objects in case they have been
415 overwritten with a broken object.
416 \end{datadesc}
418 \begin{datadesc}{tracebacklimit}
419 When this variable is set to an integer value, it determines the
420 maximum number of levels of traceback information printed when an
421 unhandled exception occurs. The default is \code{1000}. When set
422 to \code{0} or less, all traceback information is suppressed and
423 only the exception type and value are printed.
424 \end{datadesc}
426 \begin{datadesc}{version}
427 A string containing the version number of the Python interpreter
428 plus additional information on the build number and compiler used.
429 It has a value of the form \code{'\var{version}
430 (\#\var{build_number}, \var{build_date}, \var{build_time})
431 [\var{compiler}]'}. The first three characters are used to identify
432 the version in the installation directories (where appropriate on
433 each platform). An example:
435 \begin{verbatim}
436 >>> import sys
437 >>> sys.version
438 '1.5.2 (#0 Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]'
439 \end{verbatim}
440 \end{datadesc}
442 \begin{datadesc}{version_info}
443 A tuple containing the five components of the version number:
444 \var{major}, \var{minor}, \var{micro}, \var{releaselevel}, and
445 \var{serial}. All values except \var{releaselevel} are integers;
446 the release level is \code{'alpha'}, \code{'beta'},
447 \code{'candidate'}, or \code{'final'}. The \code{version_info}
448 value corresponding to the Python version 2.0 is \code{(2, 0, 0,
449 'final', 0)}.
450 \versionadded{2.0}
451 \end{datadesc}
453 \begin{datadesc}{winver}
454 The version number used to form registry keys on Windows platforms.
455 This is stored as string resource 1000 in the Python DLL. The value
456 is normally the first three characters of \constant{version}. It is
457 provided in the \module{sys} module for informational purposes;
458 modifying this value has no effect on the registry keys used by
459 Python.
460 Availability: Windows.
461 \end{datadesc}