blob: eadd9785d6fb46f48319b90be58a90ba290016b6 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{sys} ---
2 System-specific parameters and functions.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{sys}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00004
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 Drake0fd72ee1998-03-08 05:43:51 +000014 \code{argv[0]} is the script name (it is operating system
Guido van Rossum470be141995-03-17 16:07:09 +000015 dependent whether this is a full pathname or not).
16 If the command was executed using the \samp{-c} command line option
Fred Drake0fd72ee1998-03-08 05:43:51 +000017 to the interpreter, \code{argv[0]} is set to the string
Fred Drakeb91e9341998-07-23 17:59:49 +000018 \code{'-c'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000019 If no script name was passed to the Python interpreter,
Fred Drake0fd72ee1998-03-08 05:43:51 +000020 \code{argv} has zero length.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000021\end{datadesc}
22
23\begin{datadesc}{builtin_module_names}
Guido van Rossum0d2971b1997-01-06 23:01:02 +000024 A tuple of strings giving the names of all modules that are compiled
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000025 into this Python interpreter. (This information is not available in
Fred Drake0fd72ee1998-03-08 05:43:51 +000026 any other way --- \code{modules.keys()} only lists the imported
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000027 modules.)
28\end{datadesc}
29
Guido van Rossum3e5fe421998-06-10 17:57:44 +000030\begin{datadesc}{copyright}
31A string containing the copyright pertaining to the Python interpreter.
32\end{datadesc}
33
Guido van Rossum871cf161997-10-20 22:38:43 +000034\begin{funcdesc}{exc_info}{}
35This function returns a tuple of three values that give information
36about the exception that is currently being handled. The information
37returned is specific both to the current thread and to the current
38stack frame. If the current stack frame is not handling an exception,
39the information is taken from the calling stack frame, or its caller,
40and so on until a stack frame is found that is handling an exception.
41Here, ``handling an exception'' is defined as ``executing or having
Fred Drake0fd72ee1998-03-08 05:43:51 +000042executed an except clause.'' For any stack frame, only
Guido van Rossum871cf161997-10-20 22:38:43 +000043information about the most recently handled exception is accessible.
44
45If no exception is being handled anywhere on the stack, a tuple
46containing three \code{None} values is returned. Otherwise, the
47values returned are
48\code{(\var{type}, \var{value}, \var{traceback})}.
49Their meaning is: \var{type} gets the exception type of the exception
50being handled (a string or class object); \var{value} gets the
51exception parameter (its \dfn{associated value} or the second argument
Fred Drake0fd72ee1998-03-08 05:43:51 +000052to \keyword{raise}, which is always a class instance if the exception
Guido van Rossum871cf161997-10-20 22:38:43 +000053type is a class object); \var{traceback} gets a traceback object (see
54the Reference Manual) which encapsulates the call stack at the point
55where the exception originally occurred.
56\obindex{traceback}
57
58\strong{Warning:} assigning the \var{traceback} return value to a
59local variable in a function that is handling an exception will cause
60a circular reference. This will prevent anything referenced by a local
61variable in the same function or by the traceback from being garbage
62collected. Since most functions don't need access to the traceback,
63the best solution is to use something like
64\code{type, value = sys.exc_info()[:2]}
65to extract only the exception type and value. If you do need the
66traceback, make sure to delete it after use (best done with a
Fred Drake0fd72ee1998-03-08 05:43:51 +000067\keyword{try} ... \keyword{finally} statement) or to call
68\function{exc_info()} in a function that does not itself handle an
69exception.
Guido van Rossum871cf161997-10-20 22:38:43 +000070\end{funcdesc}
71
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000072\begin{datadesc}{exc_type}
73\dataline{exc_value}
74\dataline{exc_traceback}
Fred Drake0fd72ee1998-03-08 05:43:51 +000075\deprecated {1.5}
76 {Use \function{exc_info()} instead.}
77Since they are global variables, they are not specific to the current
Guido van Rossum871cf161997-10-20 22:38:43 +000078thread, so their use is not safe in a multi-threaded program. When no
Fred Drake0fd72ee1998-03-08 05:43:51 +000079exception is being handled, \code{exc_type} is set to \code{None} and
80the other two are undefined.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000081\end{datadesc}
82
Guido van Rossum0a3c7531997-06-02 17:32:41 +000083\begin{datadesc}{exec_prefix}
84A string giving the site-specific
85directory prefix where the platform-dependent Python files are
Fred Drakeb91e9341998-07-23 17:59:49 +000086installed; by default, this is also \code{'/usr/local'}. This can be
Fred Drakef76abb51998-03-27 00:37:40 +000087set at build time with the \code{-}\code{-exec-prefix} argument to the
Fred Drake0fd72ee1998-03-08 05:43:51 +000088\program{configure} script. Specifically, all configuration files
89(e.g. the \file{config.h} header file) are installed in the directory
Fred Drakeb91e9341998-07-23 17:59:49 +000090\code{exec_prefix + '/lib/python\var{version}/config'}, and shared library
Guido van Rossum0a3c7531997-06-02 17:32:41 +000091modules are installed in
Fred Drakeb91e9341998-07-23 17:59:49 +000092\code{exec_prefix + '/lib/python\var{version}/lib-dynload'},
Fred Drake0fd72ee1998-03-08 05:43:51 +000093where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +000094\end{datadesc}
95
Guido van Rossum3e5fe421998-06-10 17:57:44 +000096\begin{datadesc}{executable}
97A string giving the name of the executable binary for the Python
98interpreter, on systems where this makes sense.
99\end{datadesc}
100
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000101\begin{funcdesc}{exit}{n}
102 Exit from Python with numeric exit status \var{n}. This is
Fred Drake0fd72ee1998-03-08 05:43:51 +0000103 implemented by raising the \exception{SystemExit} exception, so cleanup
104 actions specified by finally clauses of \keyword{try} statements
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000105 are honored, and it is possible to catch the exit attempt at an outer
106 level.
107\end{funcdesc}
108
109\begin{datadesc}{exitfunc}
110 This value is not actually defined by the module, but can be set by
111 the user (or by a program) to specify a clean-up action at program
112 exit. When set, it should be a parameterless function. This function
Guido van Rossum6b686e91995-07-07 23:00:35 +0000113 will be called when the interpreter exits in any way (except when a
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000114 fatal error occurs: in that case the interpreter's internal state
115 cannot be trusted).
116\end{datadesc}
117
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000118\begin{funcdesc}{getrefcount}{object}
119Return the reference count of the \var{object}. The count returned is
120generally one higher than you might expect, because it includes the
Fred Drakeb91e9341998-07-23 17:59:49 +0000121(temporary) reference as an argument to \function{getrefcount()}.
Guido van Rossum6e91c6a1998-02-07 21:17:05 +0000122\end{funcdesc}
123
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000124\begin{datadesc}{last_type}
125\dataline{last_value}
126\dataline{last_traceback}
Guido van Rossum871cf161997-10-20 22:38:43 +0000127These three variables are not always defined; they are set when an
128exception is not handled and the interpreter prints an error message
129and a stack traceback. Their intended use is to allow an interactive
130user to import a debugger module and engage in post-mortem debugging
131without having to re-execute the command that caused the error.
Fred Drake0fd72ee1998-03-08 05:43:51 +0000132(Typical use is \samp{import pdb; pdb.pm()} to enter the post-mortem
Guido van Rossum871cf161997-10-20 22:38:43 +0000133debugger; see the chapter ``The Python Debugger'' for more
134information.)
Fred Drake54820dc1997-12-15 21:56:05 +0000135\refstmodindex{pdb}
Guido van Rossum871cf161997-10-20 22:38:43 +0000136
137The meaning of the variables is the same
Fred Drake0fd72ee1998-03-08 05:43:51 +0000138as that of the return values from \function{exc_info()} above.
Guido van Rossum871cf161997-10-20 22:38:43 +0000139(Since there is only one interactive thread, thread-safety is not a
Fred Drake0fd72ee1998-03-08 05:43:51 +0000140concern for these variables, unlike for \code{exc_type} etc.)
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000141\end{datadesc}
142
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000143\begin{datadesc}{maxint}
144The largest positive integer supported by Python's regular integer
145type. This is at least 2**31-1. The largest negative integer is
146\code{-maxint-1} -- the asymmetry results from the use of 2's
147complement binary arithmetic.
148\end{datadesc}
149
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000150\begin{datadesc}{modules}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000151 This is a dictionary that maps module names to modules which have
152 already been loaded. This can be manipulated to force reloading of
153 modules and other tricks. Note that removing a module from this
154 dictionary is \emph{not} the same as calling
155 \function{reload()}\bifuncindex{reload} on the corresponding module
156 object.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000157\end{datadesc}
158
159\begin{datadesc}{path}
Fred Drake2b67bee1998-01-13 18:35:51 +0000160\indexiii{module}{search}{path}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000161 A list of strings that specifies the search path for modules.
Fred Drakeb91e9341998-07-23 17:59:49 +0000162 Initialized from the environment variable \envvar{PYTHONPATH}, or an
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000163 installation-dependent default.
164
Fred Drake0fd72ee1998-03-08 05:43:51 +0000165The first item of this list, \code{path[0]}, is the
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000166directory containing the script that was used to invoke the Python
167interpreter. If the script directory is not available (e.g. if the
168interpreter is invoked interactively or if the script is read from
Fred Drake0fd72ee1998-03-08 05:43:51 +0000169standard input), \code{path[0]} is the empty string, which directs
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000170Python to search modules in the current directory first. Notice that
Fred Drake54820dc1997-12-15 21:56:05 +0000171the script directory is inserted \emph{before} the entries inserted as
Fred Drakeb91e9341998-07-23 17:59:49 +0000172a result of \envvar{PYTHONPATH}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000173\end{datadesc}
174
Guido van Rossum6b686e91995-07-07 23:00:35 +0000175\begin{datadesc}{platform}
Fred Drake0fd72ee1998-03-08 05:43:51 +0000176This string contains a platform identifier, e.g. \code{'sunos5'} or
177\code{'linux1'}. This can be used to append platform-specific
178components to \code{path}, for instance.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000179\end{datadesc}
180
181\begin{datadesc}{prefix}
182A string giving the site-specific directory prefix where the platform
183independent Python files are installed; by default, this is the string
Fred Drakeb91e9341998-07-23 17:59:49 +0000184\code{'/usr/local'}. This can be set at build time with the
Fred Drakef76abb51998-03-27 00:37:40 +0000185\code{-}\code{-prefix} argument to the \program{configure} script. The main
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000186collection of Python library modules is installed in the directory
Fred Drakeb91e9341998-07-23 17:59:49 +0000187\code{prefix + '/lib/python\var{version}'} while the platform
Fred Drake0fd72ee1998-03-08 05:43:51 +0000188independent header files (all except \file{config.h}) are stored in
Fred Drakeb91e9341998-07-23 17:59:49 +0000189\code{prefix + '/include/python\var{version}'},
Fred Drake0fd72ee1998-03-08 05:43:51 +0000190where \var{version} is equal to \code{version[:3]}.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000191
Guido van Rossum6b686e91995-07-07 23:00:35 +0000192\end{datadesc}
193
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000194\begin{datadesc}{ps1}
195\dataline{ps2}
Fred Drakee6cedb31998-04-03 07:05:16 +0000196\index{interpreter prompts}
197\index{prompts, interpreter}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000198 Strings specifying the primary and secondary prompt of the
199 interpreter. These are only defined if the interpreter is in
200 interactive mode. Their initial values in this case are
Guido van Rossumee9f8201997-11-25 21:12:27 +0000201 \code{'>>> '} and \code{'... '}. If a non-string object is assigned
Fred Drake0fd72ee1998-03-08 05:43:51 +0000202 to either variable, its \function{str()} is re-evaluated each time
203 the interpreter prepares to read a new interactive command; this can
204 be used to implement a dynamic prompt.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000205\end{datadesc}
206
Guido van Rossum9c51e411995-01-10 10:50:58 +0000207\begin{funcdesc}{setcheckinterval}{interval}
208Set the interpreter's ``check interval''. This integer value
209determines how often the interpreter checks for periodic things such
Fred Drake0fd72ee1998-03-08 05:43:51 +0000210as thread switches and signal handlers. The default is \code{10}, meaning
Guido van Rossum9c51e411995-01-10 10:50:58 +0000211the check is performed every 10 Python virtual instructions. Setting
212it to a larger value may increase performance for programs using
Guido van Rossumf259efe1997-11-25 01:00:40 +0000213threads. Setting it to a value \code{<=} 0 checks every virtual instruction,
Guido van Rossum9c51e411995-01-10 10:50:58 +0000214maximizing responsiveness as well as overhead.
Guido van Rossum7f49b7a1995-01-12 12:38:46 +0000215\end{funcdesc}
Guido van Rossum9c51e411995-01-10 10:50:58 +0000216
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000217\begin{funcdesc}{setprofile}{profilefunc}
218 Set the system's profile function, which allows you to implement a
Guido van Rossum470be141995-03-17 16:07:09 +0000219 Python source code profiler in Python. See the chapter on the
220 Python Profiler. The system's profile function
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000221 is called similarly to the system's trace function (see
Fred Drake0fd72ee1998-03-08 05:43:51 +0000222 \function{settrace()}), but it isn't called for each executed line of
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000223 code (only on call and return and when an exception occurs). Also,
224 its return value is not used, so it can just return \code{None}.
225\end{funcdesc}
226\index{profile function}
Guido van Rossum470be141995-03-17 16:07:09 +0000227\index{profiler}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000228
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000229\begin{funcdesc}{settrace}{tracefunc}
230 Set the system's trace function, which allows you to implement a
231 Python source code debugger in Python. See section ``How It Works''
232 in the chapter on the Python Debugger.
233\end{funcdesc}
234\index{trace function}
235\index{debugger}
236
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000237\begin{datadesc}{stdin}
238\dataline{stdout}
239\dataline{stderr}
240 File objects corresponding to the interpreter's standard input,
Fred Drake0fd72ee1998-03-08 05:43:51 +0000241 output and error streams. \code{stdin} is used for all
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000242 interpreter input except for scripts but including calls to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000243 \function{input()}\bifuncindex{input} and
244 \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is used
245 for the output of \keyword{print} and expression statements and for the
246 prompts of \function{input()} and \function{raw_input()}. The interpreter's
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000247 own prompts and (almost all of) its error messages go to
Fred Drake0fd72ee1998-03-08 05:43:51 +0000248 \code{stderr}. \code{stdout} and \code{stderr} needn't
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000249 be built-in file objects: any object is acceptable as long as it has
Fred Drake0fd72ee1998-03-08 05:43:51 +0000250 a \method{write()} method that takes a string argument. (Changing these
Guido van Rossum470be141995-03-17 16:07:09 +0000251 objects doesn't affect the standard I/O streams of processes
Fred Drake0fd72ee1998-03-08 05:43:51 +0000252 executed by \function{os.popen()}, \function{os.system()} or the
253 \function{exec*()} family of functions in the \module{os} module.)
Fred Drake54820dc1997-12-15 21:56:05 +0000254\refstmodindex{os}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000255\end{datadesc}
256
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000257\begin{datadesc}{__stdin__}
258\dataline{__stdout__}
259\dataline{__stderr__}
260These objects contain the original values of \code{stdin},
261\code{stderr} and \code{stdout} at the start of the program. They are
262used during finalization, and could be useful to restore the actual
263files to known working file objects in case they have been overwritten
264with a broken object.
265\end{datadesc}
266
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000267\begin{datadesc}{tracebacklimit}
268When this variable is set to an integer value, it determines the
269maximum number of levels of traceback information printed when an
Fred Drake0fd72ee1998-03-08 05:43:51 +0000270unhandled exception occurs. The default is \code{1000}. When set to
2710 or less, all traceback information is suppressed and only the
272exception type and value are printed.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000273\end{datadesc}
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000274
275\begin{datadesc}{version}
Guido van Rossum3e5fe421998-06-10 17:57:44 +0000276A string containing the version number of the Python interpreter.
Guido van Rossum0a3c7531997-06-02 17:32:41 +0000277\end{datadesc}