blob: 7ffa35d9bdbba966125bb4b40422de533a18364c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{sys} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 System-specific parameters and functions}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{builtin}{sys}
Fred Drake295da241998-08-10 19:42:37 +00005\modulesynopsis{Access system-specific parameters and functions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00007This module provides access to some variables used or maintained by the
8interpreter and to functions that interact strongly with the interpreter.
9It is always available.
10
Guido van Rossum470be141995-03-17 16:07:09 +000011
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000012\begin{datadesc}{argv}
13 The list of command line arguments passed to a Python script.
Fred Drake72182022001-07-18 17:52:58 +000014 \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.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\end{datadesc}
21
Fred Drakea2b6ad62000-08-15 04:24:43 +000022\begin{datadesc}{byteorder}
Fred Drake68e29152000-08-14 15:47:30 +000023 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}
29
Barry Warsaw2a38a862005-12-18 01:27:35 +000030\begin{datadesc}{build_number}
31 A string representing the Subversion revision that this Python executable
32 was built from. This number is a string because it may contain a trailing
33 'M' if Python was built from a mixed revision source tree.
34\end{datadesc}
35
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000036\begin{datadesc}{builtin_module_names}
Guido van Rossum0d2971b1997-01-06 23:01:02 +000037 A tuple of strings giving the names of all modules that are compiled
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038 into this Python interpreter. (This information is not available in
Fred Drake0fd72ee1998-03-08 05:43:51 +000039 any other way --- \code{modules.keys()} only lists the imported
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000040 modules.)
41\end{datadesc}
42
Guido van Rossum3e5fe421998-06-10 17:57:44 +000043\begin{datadesc}{copyright}
Fred Drake72182022001-07-18 17:52:58 +000044 A string containing the copyright pertaining to the Python
45 interpreter.
Guido van Rossum3e5fe421998-06-10 17:57:44 +000046\end{datadesc}
47
Fred Drake38e5d272000-04-03 20:13:55 +000048\begin{datadesc}{dllhandle}
Fred Drake72182022001-07-18 17:52:58 +000049 Integer specifying the handle of the Python DLL.
50 Availability: Windows.
Fred Drake38e5d272000-04-03 20:13:55 +000051\end{datadesc}
52
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000053\begin{funcdesc}{displayhook}{\var{value}}
Fred Drake72182022001-07-18 17:52:58 +000054 If \var{value} is not \code{None}, this function prints it to
55 \code{sys.stdout}, and saves it in \code{__builtin__._}.
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000056
Fred Drake72182022001-07-18 17:52:58 +000057 \code{sys.displayhook} is called on the result of evaluating an
58 expression entered in an interactive Python session. The display of
59 these values can be customized by assigning another one-argument
60 function to \code{sys.displayhook}.
Moshe Zadkaf68f2fe2001-01-11 05:41:27 +000061\end{funcdesc}
62
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +000063\begin{funcdesc}{excepthook}{\var{type}, \var{value}, \var{traceback}}
Fred Drake72182022001-07-18 17:52:58 +000064 This function prints out a given traceback and exception to
65 \code{sys.stderr}.
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +000066
Fred Drake72182022001-07-18 17:52:58 +000067 When an exception is raised and uncaught, the interpreter calls
68 \code{sys.excepthook} with three arguments, the exception class,
69 exception instance, and a traceback object. In an interactive
70 session this happens just before control is returned to the prompt;
71 in a Python program this happens just before the program exits. The
72 handling of such top-level exceptions can be customized by assigning
73 another three-argument function to \code{sys.excepthook}.
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +000074\end{funcdesc}
75
76\begin{datadesc}{__displayhook__}
77\dataline{__excepthook__}
Fred Drake72182022001-07-18 17:52:58 +000078 These objects contain the original values of \code{displayhook} and
79 \code{excepthook} at the start of the program. They are saved so
80 that \code{displayhook} and \code{excepthook} can be restored in
81 case they happen to get replaced with broken objects.
Ka-Ping Yeeb5c51322001-03-23 02:46:52 +000082\end{datadesc}
83
Guido van Rossum871cf161997-10-20 22:38:43 +000084\begin{funcdesc}{exc_info}{}
Fred Drake72182022001-07-18 17:52:58 +000085 This function returns a tuple of three values that give information
86 about the exception that is currently being handled. The
87 information returned is specific both to the current thread and to
88 the current stack frame. If the current stack frame is not handling
89 an exception, the information is taken from the calling stack frame,
90 or its caller, and so on until a stack frame is found that is
91 handling an exception. Here, ``handling an exception'' is defined
92 as ``executing or having executed an except clause.'' For any stack
93 frame, only information about the most recently handled exception is
94 accessible.
Guido van Rossum871cf161997-10-20 22:38:43 +000095
Fred Drake72182022001-07-18 17:52:58 +000096 If no exception is being handled anywhere on the stack, a tuple
97 containing three \code{None} values is returned. Otherwise, the
98 values returned are \code{(\var{type}, \var{value},
99 \var{traceback})}. Their meaning is: \var{type} gets the exception
Neal Norwitz847207a2003-05-29 02:17:23 +0000100 type of the exception being handled (a class object);
Fred Drake72182022001-07-18 17:52:58 +0000101 \var{value} gets the exception parameter (its \dfn{associated value}
102 or the second argument to \keyword{raise}, which is always a class
103 instance if the exception type is a class object); \var{traceback}
104 gets a traceback object (see the Reference Manual) which
105 encapsulates the call stack at the point where the exception
106 originally occurred. \obindex{traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +0000107
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000108 If \function{exc_clear()} is called, this function will return three
109 \code{None} values until either another exception is raised in the
110 current thread or the execution stack returns to a frame where
111 another exception is being handled.
112
Fred Drake0aa811c2001-10-20 04:24:09 +0000113 \warning{Assigning the \var{traceback} return value to a
Fred Drake72182022001-07-18 17:52:58 +0000114 local variable in a function that is handling an exception will
115 cause a circular reference. This will prevent anything referenced
116 by a local variable in the same function or by the traceback from
117 being garbage collected. Since most functions don't need access to
118 the traceback, the best solution is to use something like
Fred Drake7731ed42002-01-05 04:00:03 +0000119 \code{exctype, value = sys.exc_info()[:2]} to extract only the
Fred Drake72182022001-07-18 17:52:58 +0000120 exception type and value. If you do need the traceback, make sure
121 to delete it after use (best done with a \keyword{try}
122 ... \keyword{finally} statement) or to call \function{exc_info()} in
Tim Peters98791af2001-10-23 01:59:54 +0000123 a function that does not itself handle an exception.} \note{Beginning
124 with Python 2.2, such cycles are automatically reclaimed when garbage
125 collection is enabled and they become unreachable, but it remains more
126 efficient to avoid creating cycles.}
Guido van Rossum871cf161997-10-20 22:38:43 +0000127\end{funcdesc}
128
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000129\begin{funcdesc}{exc_clear}{}
130 This function clears all information relating to the current or last
Johannes Gijsbersd3452252004-09-11 16:50:06 +0000131 exception that occurred in the current thread. After calling this
Guido van Rossum46d3dc32003-03-01 03:20:41 +0000132 function, \function{exc_info()} will return three \code{None} values until
133 another exception is raised in the current thread or the execution stack
134 returns to a frame where another exception is being handled.
135
136 This function is only needed in only a few obscure situations. These
137 include logging and error handling systems that report information on the
138 last or current exception. This function can also be used to try to free
139 resources and trigger object finalization, though no guarantee is made as
140 to what objects will be freed, if any.
141\versionadded{2.3}
142\end{funcdesc}
143
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000144\begin{datadesc}{exc_type}
145\dataline{exc_value}
146\dataline{exc_traceback}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000147\deprecated {1.5}
148 {Use \function{exc_info()} instead.}
Fred Drake72182022001-07-18 17:52:58 +0000149 Since they are global variables, they are not specific to the
150 current thread, so their use is not safe in a multi-threaded
151 program. When no exception is being handled, \code{exc_type} is set
152 to \code{None} and the other two are undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000153\end{datadesc}
154
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000155\begin{datadesc}{exec_prefix}
Fred Drake72182022001-07-18 17:52:58 +0000156 A string giving the site-specific directory prefix where the
157 platform-dependent Python files are installed; by default, this is
158 also \code{'/usr/local'}. This can be set at build time with the
159 \longprogramopt{exec-prefix} argument to the \program{configure}
160 script. Specifically, all configuration files (e.g. the
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000161 \file{pyconfig.h} header file) are installed in the directory
Fred Drake72182022001-07-18 17:52:58 +0000162 \code{exec_prefix + '/lib/python\var{version}/config'}, and shared
163 library modules are installed in \code{exec_prefix +
164 '/lib/python\var{version}/lib-dynload'}, where \var{version} is
165 equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000166\end{datadesc}
167
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000168\begin{datadesc}{executable}
Fred Drake72182022001-07-18 17:52:58 +0000169 A string giving the name of the executable binary for the Python
170 interpreter, on systems where this makes sense.
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000171\end{datadesc}
172
Guido van Rossum04307ce1998-11-23 17:49:53 +0000173\begin{funcdesc}{exit}{\optional{arg}}
Fred Drake72182022001-07-18 17:52:58 +0000174 Exit from Python. This is implemented by raising the
175 \exception{SystemExit} exception, so cleanup actions specified by
176 finally clauses of \keyword{try} statements are honored, and it is
177 possible to intercept the exit attempt at an outer level. The
178 optional argument \var{arg} can be an integer giving the exit status
179 (defaulting to zero), or another type of object. If it is an
180 integer, zero is considered ``successful termination'' and any
181 nonzero value is considered ``abnormal termination'' by shells and
182 the like. Most systems require it to be in the range 0-127, and
183 produce undefined results otherwise. Some systems have a convention
184 for assigning specific meanings to specific exit codes, but these
Fred Drakec37b65e2001-11-28 07:26:15 +0000185 are generally underdeveloped; \UNIX{} programs generally use 2 for
Fred Drake72182022001-07-18 17:52:58 +0000186 command line syntax errors and 1 for all other kind of errors. If
187 another type of object is passed, \code{None} is equivalent to
188 passing zero, and any other object is printed to \code{sys.stderr}
189 and results in an exit code of 1. In particular,
190 \code{sys.exit("some error message")} is a quick way to exit a
191 program when an error occurs.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000192\end{funcdesc}
193
194\begin{datadesc}{exitfunc}
195 This value is not actually defined by the module, but can be set by
196 the user (or by a program) to specify a clean-up action at program
Fred Drake72182022001-07-18 17:52:58 +0000197 exit. When set, it should be a parameterless function. This
198 function will be called when the interpreter exits. Only one
199 function may be installed in this way; to allow multiple functions
200 which will be called at termination, use the \refmodule{atexit}
Fred Drake0aa811c2001-10-20 04:24:09 +0000201 module. \note{The exit function is not called when the program is
Fred Drake72182022001-07-18 17:52:58 +0000202 killed by a signal, when a Python fatal internal error is detected,
Fred Drake0aa811c2001-10-20 04:24:09 +0000203 or when \code{os._exit()} is called.}
Raymond Hettinger01884d52004-08-18 02:50:00 +0000204 \deprecated{2.4}{Use \refmodule{atexit} instead.}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{datadesc}
206
Tim Peterse5e065b2003-07-06 18:36:54 +0000207\begin{funcdesc}{getcheckinterval}{}
208 Return the interpreter's ``check interval'';
209 see \function{setcheckinterval()}.
Neal Norwitz7cb229d2003-07-07 14:11:53 +0000210 \versionadded{2.3}
Tim Peterse5e065b2003-07-06 18:36:54 +0000211\end{funcdesc}
212
Fred Drake8940faf2000-10-25 21:02:55 +0000213\begin{funcdesc}{getdefaultencoding}{}
214 Return the name of the current default string encoding used by the
215 Unicode implementation.
216 \versionadded{2.0}
217\end{funcdesc}
218
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000219\begin{funcdesc}{getdlopenflags}{}
Fred Drake5d808fb2001-07-18 16:35:05 +0000220 Return the current value of the flags that are used for
221 \cfunction{dlopen()} calls. The flag constants are defined in the
222 \refmodule{dl} and \module{DLFCN} modules.
223 Availability: \UNIX.
224 \versionadded{2.2}
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000225\end{funcdesc}
226
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000227\begin{funcdesc}{getfilesystemencoding}{}
228 Return the name of the encoding used to convert Unicode filenames
229 into system file names, or \code{None} if the system default encoding
230 is used. The result value depends on the operating system:
231\begin{itemize}
232\item On Windows 9x, the encoding is ``mbcs''.
233\item On Mac OS X, the encoding is ``utf-8''.
234\item On Unix, the encoding is the user's preference
235 according to the result of nl_langinfo(CODESET), or None if
236 the nl_langinfo(CODESET) failed.
237\item On Windows NT+, file names are Unicode natively, so no conversion
Martin v. Löwis64af6c52004-06-16 04:53:46 +0000238 is performed. \code{getfilesystemencoding} still returns ``mbcs'',
239 as this is the encoding that applications should use when they
240 explicitly want to convert Unicode strings to byte strings that
241 are equivalent when used as file names.
Martin v. Löwis73d538b2003-03-05 15:13:47 +0000242\end{itemize}
243 \versionadded{2.3}
244\end{funcdesc}
245
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000246\begin{funcdesc}{getrefcount}{object}
Fred Drake72182022001-07-18 17:52:58 +0000247 Return the reference count of the \var{object}. The count returned
248 is generally one higher than you might expect, because it includes
249 the (temporary) reference as an argument to
250 \function{getrefcount()}.
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000251\end{funcdesc}
252
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000253\begin{funcdesc}{getrecursionlimit}{}
Fred Drake72182022001-07-18 17:52:58 +0000254 Return the current value of the recursion limit, the maximum depth
255 of the Python interpreter stack. This limit prevents infinite
256 recursion from causing an overflow of the C stack and crashing
257 Python. It can be set by \function{setrecursionlimit()}.
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000258\end{funcdesc}
259
Barry Warsawb6a54d22000-12-06 21:47:46 +0000260\begin{funcdesc}{_getframe}{\optional{depth}}
Fred Drake72182022001-07-18 17:52:58 +0000261 Return a frame object from the call stack. If optional integer
262 \var{depth} is given, return the frame object that many calls below
263 the top of the stack. If that is deeper than the call stack,
264 \exception{ValueError} is raised. The default for \var{depth} is
265 zero, returning the frame at the top of the call stack.
Barry Warsawb6a54d22000-12-06 21:47:46 +0000266
Fred Drake72182022001-07-18 17:52:58 +0000267 This function should be used for internal and specialized purposes
268 only.
Barry Warsawb6a54d22000-12-06 21:47:46 +0000269\end{funcdesc}
270
Mark Hammond8696ebc2002-10-08 02:44:31 +0000271\begin{funcdesc}{getwindowsversion}{}
272 Return a tuple containing five components, describing the Windows
273 version currently running. The elements are \var{major}, \var{minor},
274 \var{build}, \var{platform}, and \var{text}. \var{text} contains
275 a string while all other values are integers.
276
277 \var{platform} may be one of the following values:
Fred Drake8efc80a2004-11-11 04:39:56 +0000278
279 \begin{tableii}{l|l}{constant}{Constant}{Platform}
280 \lineii{VER_PLATFORM_WIN32s} {Win32s on Windows 3.1}
281 \lineii{VER_PLATFORM_WIN32_WINDOWS}{Windows 95/98/ME}
282 \lineii{VER_PLATFORM_WIN32_NT} {Windows NT/2000/XP}
283 \lineii{VER_PLATFORM_WIN32_CE} {Windows CE}
284 \end{tableii}
285
286 This function wraps the Win32 \cfunction{GetVersionEx()} function;
287 see the Microsoft documentation for more information about these
Mark Hammond8696ebc2002-10-08 02:44:31 +0000288 fields.
289
290 Availability: Windows.
291 \versionadded{2.3}
292\end{funcdesc}
293
Fred Drake4d65d732000-04-13 16:54:17 +0000294\begin{datadesc}{hexversion}
Fred Drake72182022001-07-18 17:52:58 +0000295 The version number encoded as a single integer. This is guaranteed
296 to increase with each version, including proper support for
297 non-production releases. For example, to test that the Python
298 interpreter is at least version 1.5.2, use:
Fred Drake4d65d732000-04-13 16:54:17 +0000299
300\begin{verbatim}
301if sys.hexversion >= 0x010502F0:
302 # use some advanced feature
303 ...
304else:
305 # use an alternative implementation or warn the user
306 ...
307\end{verbatim}
308
Fred Drake72182022001-07-18 17:52:58 +0000309 This is called \samp{hexversion} since it only really looks
310 meaningful when viewed as the result of passing it to the built-in
311 \function{hex()} function. The \code{version_info} value may be
312 used for a more human-friendly encoding of the same information.
313 \versionadded{1.5.2}
Fred Drake4d65d732000-04-13 16:54:17 +0000314\end{datadesc}
315
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000316\begin{datadesc}{last_type}
317\dataline{last_value}
318\dataline{last_traceback}
Fred Drake72182022001-07-18 17:52:58 +0000319 These three variables are not always defined; they are set when an
320 exception is not handled and the interpreter prints an error message
321 and a stack traceback. Their intended use is to allow an
322 interactive user to import a debugger module and engage in
323 post-mortem debugging without having to re-execute the command that
324 caused the error. (Typical use is \samp{import pdb; pdb.pm()} to
Fred Drake8efc80a2004-11-11 04:39:56 +0000325 enter the post-mortem debugger; see chapter~\ref{debugger}, ``The
Fred Drake72182022001-07-18 17:52:58 +0000326 Python Debugger,'' for more information.)
Guido van Rossum871cf161997-10-20 22:38:43 +0000327
Fred Drake72182022001-07-18 17:52:58 +0000328 The meaning of the variables is the same as that of the return
329 values from \function{exc_info()} above. (Since there is only one
330 interactive thread, thread-safety is not a concern for these
331 variables, unlike for \code{exc_type} etc.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000332\end{datadesc}
333
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000334\begin{datadesc}{maxint}
Fred Drake72182022001-07-18 17:52:58 +0000335 The largest positive integer supported by Python's regular integer
336 type. This is at least 2**31-1. The largest negative integer is
Fred Drakec05fc7d2001-09-04 18:18:36 +0000337 \code{-maxint-1} --- the asymmetry results from the use of 2's
Fred Drake72182022001-07-18 17:52:58 +0000338 complement binary arithmetic.
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000339\end{datadesc}
340
Fred Drakec05fc7d2001-09-04 18:18:36 +0000341\begin{datadesc}{maxunicode}
342 An integer giving the largest supported code point for a Unicode
343 character. The value of this depends on the configuration option
344 that specifies whether Unicode characters are stored as UCS-2 or
345 UCS-4.
346\end{datadesc}
347
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000348\begin{datadesc}{modules}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000349 This is a dictionary that maps module names to modules which have
350 already been loaded. This can be manipulated to force reloading of
351 modules and other tricks. Note that removing a module from this
352 dictionary is \emph{not} the same as calling
353 \function{reload()}\bifuncindex{reload} on the corresponding module
354 object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000355\end{datadesc}
356
357\begin{datadesc}{path}
Fred Drake2b67bee1998-01-13 18:35:51 +0000358\indexiii{module}{search}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000359 A list of strings that specifies the search path for modules.
Guido van Rossum54ed2d32002-07-15 16:08:10 +0000360 Initialized from the environment variable \envvar{PYTHONPATH}, plus an
Fred Drake72182022001-07-18 17:52:58 +0000361 installation-dependent default.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000362
Guido van Rossum54ed2d32002-07-15 16:08:10 +0000363 As initialized upon program startup,
364 the first item of this list, \code{path[0]}, is the directory
Fred Drake72182022001-07-18 17:52:58 +0000365 containing the script that was used to invoke the Python
366 interpreter. If the script directory is not available (e.g. if the
367 interpreter is invoked interactively or if the script is read from
368 standard input), \code{path[0]} is the empty string, which directs
369 Python to search modules in the current directory first. Notice
370 that the script directory is inserted \emph{before} the entries
371 inserted as a result of \envvar{PYTHONPATH}.
Guido van Rossum54ed2d32002-07-15 16:08:10 +0000372
373 A program is free to modify this list for its own purposes.
Fred Drake38d7c1b2003-07-17 04:22:44 +0000374
Brett Cannon8b6cc2e2004-03-21 14:10:18 +0000375 \versionchanged[Unicode strings are no longer ignored]{2.3}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000376\end{datadesc}
377
Guido van Rossum6b686e91995-07-07 23:00:35 +0000378\begin{datadesc}{platform}
Fred Drake72182022001-07-18 17:52:58 +0000379 This string contains a platform identifier, e.g. \code{'sunos5'} or
380 \code{'linux1'}. This can be used to append platform-specific
381 components to \code{path}, for instance.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000382\end{datadesc}
383
384\begin{datadesc}{prefix}
Fred Drake72182022001-07-18 17:52:58 +0000385 A string giving the site-specific directory prefix where the
386 platform independent Python files are installed; by default, this is
387 the string \code{'/usr/local'}. This can be set at build time with
388 the \longprogramopt{prefix} argument to the \program{configure}
389 script. The main collection of Python library modules is installed
390 in the directory \code{prefix + '/lib/python\var{version}'} while
Martin v. Löwis4f1cd8b2001-07-26 13:41:06 +0000391 the platform independent header files (all except \file{pyconfig.h})
Fred Drake72182022001-07-18 17:52:58 +0000392 are stored in \code{prefix + '/include/python\var{version}'}, where
393 \var{version} is equal to \code{version[:3]}.
Guido van Rossum6b686e91995-07-07 23:00:35 +0000394\end{datadesc}
395
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000396\begin{datadesc}{ps1}
397\dataline{ps2}
Fred Drakee6cedb31998-04-03 07:05:16 +0000398\index{interpreter prompts}
399\index{prompts, interpreter}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000400 Strings specifying the primary and secondary prompt of the
401 interpreter. These are only defined if the interpreter is in
402 interactive mode. Their initial values in this case are
Fred Drake72182022001-07-18 17:52:58 +0000403 \code{'>\code{>}> '} and \code{'... '}. If a non-string object is
404 assigned to either variable, its \function{str()} is re-evaluated
405 each time the interpreter prepares to read a new interactive
406 command; this can be used to implement a dynamic prompt.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000407\end{datadesc}
408
Guido van Rossum9c51e411995-01-10 10:50:58 +0000409\begin{funcdesc}{setcheckinterval}{interval}
Fred Drake72182022001-07-18 17:52:58 +0000410 Set the interpreter's ``check interval''. This integer value
411 determines how often the interpreter checks for periodic things such
Skip Montanaroeec26f92003-07-02 21:38:34 +0000412 as thread switches and signal handlers. The default is \code{100},
413 meaning the check is performed every 100 Python virtual instructions.
Fred Drake72182022001-07-18 17:52:58 +0000414 Setting it to a larger value may increase performance for programs
415 using threads. Setting it to a value \code{<=} 0 checks every
416 virtual instruction, maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000417\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000418
Fred Drake8940faf2000-10-25 21:02:55 +0000419\begin{funcdesc}{setdefaultencoding}{name}
420 Set the current default string encoding used by the Unicode
421 implementation. If \var{name} does not match any available
422 encoding, \exception{LookupError} is raised. This function is only
423 intended to be used by the \refmodule{site} module implementation
424 and, where needed, by \module{sitecustomize}. Once used by the
425 \refmodule{site} module, it is removed from the \module{sys}
426 module's namespace.
427% Note that \refmodule{site} is not imported if
428% the \programopt{-S} option is passed to the interpreter, in which
429% case this function will remain available.
430 \versionadded{2.0}
431\end{funcdesc}
432
Andrew M. Kuchling28bafb82001-07-19 01:17:15 +0000433\begin{funcdesc}{setdlopenflags}{n}
Fred Drake5d808fb2001-07-18 16:35:05 +0000434 Set the flags used by the interpreter for \cfunction{dlopen()}
435 calls, such as when the interpreter loads extension modules. Among
436 other things, this will enable a lazy resolving of symbols when
Andrew M. Kuchling28bafb82001-07-19 01:17:15 +0000437 importing a module, if called as \code{sys.setdlopenflags(0)}. To
438 share symbols across extension modules, call as
Fred Drake72182022001-07-18 17:52:58 +0000439 \code{sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)}. Symbolic
Fred Drake5d808fb2001-07-18 16:35:05 +0000440 names for the flag modules can be either found in the \refmodule{dl}
441 module, or in the \module{DLFCN} module. If \module{DLFCN} is not
Fred Drake72182022001-07-18 17:52:58 +0000442 available, it can be generated from \file{/usr/include/dlfcn.h}
443 using the \program{h2py} script.
Fred Drake5d808fb2001-07-18 16:35:05 +0000444 Availability: \UNIX.
445 \versionadded{2.2}
Martin v. Löwisf0473d52001-07-18 16:17:16 +0000446\end{funcdesc}
447
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000448\begin{funcdesc}{setprofile}{profilefunc}
Fred Drake72182022001-07-18 17:52:58 +0000449 Set the system's profile function,\index{profile function} which
450 allows you to implement a Python source code profiler in
Fred Drake8efc80a2004-11-11 04:39:56 +0000451 Python.\index{profiler} See chapter~\ref{profile} for more
Fred Drake72182022001-07-18 17:52:58 +0000452 information on the Python profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000453 is called similarly to the system's trace function (see
Fred Drake72182022001-07-18 17:52:58 +0000454 \function{settrace()}), but it isn't called for each executed line
Fred Drake64d78632001-10-16 14:54:22 +0000455 of code (only on call and return, but the return event is reported
456 even when an exception has been set). The function is
457 thread-specific, but there is no way for the profiler to know about
458 context switches between threads, so it does not make sense to use
459 this in the presence of multiple threads.
Fred Drake72182022001-07-18 17:52:58 +0000460 Also, its return value is not used, so it can simply return
461 \code{None}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000462\end{funcdesc}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000463
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000464\begin{funcdesc}{setrecursionlimit}{limit}
Fred Drake72182022001-07-18 17:52:58 +0000465 Set the maximum depth of the Python interpreter stack to
466 \var{limit}. This limit prevents infinite recursion from causing an
467 overflow of the C stack and crashing Python.
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000468
Fred Drake72182022001-07-18 17:52:58 +0000469 The highest possible limit is platform-dependent. A user may need
470 to set the limit higher when she has a program that requires deep
471 recursion and a platform that supports a higher limit. This should
472 be done with care, because a too-high limit can lead to a crash.
Fred Drake65faf112000-08-31 19:35:56 +0000473\end{funcdesc}
Jeremy Hyltonee5adfb2000-08-31 19:23:01 +0000474
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000475\begin{funcdesc}{settrace}{tracefunc}
Fred Drake72182022001-07-18 17:52:58 +0000476 Set the system's trace function,\index{trace function} which allows
477 you to implement a Python source code debugger in Python. See
478 section \ref{debugger-hooks}, ``How It Works,'' in the chapter on
Fred Drake64d78632001-10-16 14:54:22 +0000479 the Python debugger.\index{debugger} The function is
480 thread-specific; for a debugger to support multiple threads, it must
481 be registered using \function{settrace()} for each thread being
Phillip J. Eby1884dda2004-08-05 12:13:46 +0000482 debugged. \note{The \function{settrace()} function is intended only
483 for implementing debuggers, profilers, coverage tools and the like.
484 Its behavior is part of the implementation platform, rather than
485 part of the language definition, and thus may not be available in
486 all Python implementations.}
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000487\end{funcdesc}
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000488
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000489\begin{funcdesc}{settscdump}{on_flag}
490 Activate dumping of VM measurements using the Pentium timestamp
491 counter, if \var{on_flag} is true. Deactivate these dumps if
492 \var{on_flag} is off. The function is available only if Python
Fred Drake7f354042004-06-08 14:01:27 +0000493 was compiled with \longprogramopt{with-tsc}. To understand the
494 output of this dump, read \file{Python/ceval.c} in the Python
495 sources.
Martin v. Löwisf30d60e2004-06-08 08:17:44 +0000496 \versionadded{2.4}
497\end{funcdesc}
498
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000499\begin{datadesc}{stdin}
500\dataline{stdout}
501\dataline{stderr}
502 File objects corresponding to the interpreter's standard input,
Fred Drake72182022001-07-18 17:52:58 +0000503 output and error streams. \code{stdin} is used for all interpreter
504 input except for scripts but including calls to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000505 \function{input()}\bifuncindex{input} and
Fred Drake72182022001-07-18 17:52:58 +0000506 \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is
507 used for the output of \keyword{print} and expression statements and
508 for the prompts of \function{input()} and \function{raw_input()}.
509 The interpreter's own prompts and (almost all of) its error messages
510 go to \code{stderr}. \code{stdout} and \code{stderr} needn't be
511 built-in file objects: any object is acceptable as long as it has a
512 \method{write()} method that takes a string argument. (Changing
513 these objects doesn't affect the standard I/O streams of processes
Fred Drake0fd72ee1998-03-08 05:43:51 +0000514 executed by \function{os.popen()}, \function{os.system()} or the
Fred Drake72182022001-07-18 17:52:58 +0000515 \function{exec*()} family of functions in the \refmodule{os}
516 module.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000517\end{datadesc}
518
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000519\begin{datadesc}{__stdin__}
520\dataline{__stdout__}
521\dataline{__stderr__}
Fred Drake72182022001-07-18 17:52:58 +0000522 These objects contain the original values of \code{stdin},
523 \code{stderr} and \code{stdout} at the start of the program. They
524 are used during finalization, and could be useful to restore the
525 actual files to known working file objects in case they have been
526 overwritten with a broken object.
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000527\end{datadesc}
528
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000529\begin{datadesc}{tracebacklimit}
Fred Drake72182022001-07-18 17:52:58 +0000530 When this variable is set to an integer value, it determines the
531 maximum number of levels of traceback information printed when an
532 unhandled exception occurs. The default is \code{1000}. When set
533 to \code{0} or less, all traceback information is suppressed and
534 only the exception type and value are printed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000535\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000536
537\begin{datadesc}{version}
Fred Drake72182022001-07-18 17:52:58 +0000538 A string containing the version number of the Python interpreter
539 plus additional information on the build number and compiler used.
540 It has a value of the form \code{'\var{version}
541 (\#\var{build_number}, \var{build_date}, \var{build_time})
542 [\var{compiler}]'}. The first three characters are used to identify
543 the version in the installation directories (where appropriate on
544 each platform). An example:
Fred Drake38e5d272000-04-03 20:13:55 +0000545
546\begin{verbatim}
547>>> import sys
548>>> sys.version
549'1.5.2 (#0 Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]'
550\end{verbatim}
551\end{datadesc}
552
Skip Montanaro8e790e72002-09-03 13:25:17 +0000553\begin{datadesc}{api_version}
554 The C API version for this interpreter. Programmers may find this useful
555 when debugging version conflicts between Python and extension
556 modules. \versionadded{2.3}
557\end{datadesc}
558
Fred Drake4d65d732000-04-13 16:54:17 +0000559\begin{datadesc}{version_info}
Fred Drake72182022001-07-18 17:52:58 +0000560 A tuple containing the five components of the version number:
561 \var{major}, \var{minor}, \var{micro}, \var{releaselevel}, and
562 \var{serial}. All values except \var{releaselevel} are integers;
563 the release level is \code{'alpha'}, \code{'beta'},
564 \code{'candidate'}, or \code{'final'}. The \code{version_info}
565 value corresponding to the Python version 2.0 is \code{(2, 0, 0,
566 'final', 0)}.
567 \versionadded{2.0}
Fred Drake4d65d732000-04-13 16:54:17 +0000568\end{datadesc}
569
Fred Drakec05fc7d2001-09-04 18:18:36 +0000570\begin{datadesc}{warnoptions}
571 This is an implementation detail of the warnings framework; do not
572 modify this value. Refer to the \refmodule{warnings} module for
573 more information on the warnings framework.
574\end{datadesc}
575
Fred Drake38e5d272000-04-03 20:13:55 +0000576\begin{datadesc}{winver}
Fred Drake72182022001-07-18 17:52:58 +0000577 The version number used to form registry keys on Windows platforms.
578 This is stored as string resource 1000 in the Python DLL. The value
579 is normally the first three characters of \constant{version}. It is
580 provided in the \module{sys} module for informational purposes;
581 modifying this value has no effect on the registry keys used by
582 Python.
583 Availability: Windows.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000584\end{datadesc}
Skip Montanaro8a797272002-03-27 17:29:50 +0000585
586
587\begin{seealso}
588 \seemodule{site}
589 {This describes how to use .pth files to extend \code{sys.path}.}
590\end{seealso}