blob: e16fbd568689f6d46fa8517fd4197e1b592cd8fe [file] [log] [blame]
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00001\section{Built-in Module \sectcode{sys}}
2
3\bimodindex{sys}
4This module provides access to some variables used or maintained by the
5interpreter and to functions that interact strongly with the interpreter.
6It is always available.
7
8\renewcommand{\indexsubitem}{(in module sys)}
9\begin{datadesc}{argv}
10 The list of command line arguments passed to a Python script.
11 \code{sys.argv[0]} is the script name.
12 If no script name was passed to the Python interpreter,
13 \code{sys.argv} is empty.
14\end{datadesc}
15
16\begin{datadesc}{builtin_module_names}
17 A list of strings giving the names of all modules that are compiled
18 into this Python interpreter. (This information is not available in
19 any other way --- \code{sys.modules.keys()} only lists the imported
20 modules.)
21\end{datadesc}
22
Guido van Rossum16cd7f91994-10-06 10:29:26 +000023\begin{datadesc}{check_interval}
24When this variable is set to an integer value, it determines how often
25the interpreter checks for periodic things such as thread switches and
26signal handlers. The default is 10, meaning the check is performed
27every 10 Python virtual instructions. Setting this to a large value
28may increase performance for programs using threads. Setting it to a
29value <= 0 checks every virtual instruction, maximizing responsiveness
30as well as overhead.
31
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000032\begin{datadesc}{exc_type}
33\dataline{exc_value}
34\dataline{exc_traceback}
35 These three variables are not always defined; they are set when an
36 exception handler (an \code{except} clause of a \code{try} statement) is
37 invoked. Their meaning is: \code{exc_type} gets the exception type of
38 the exception being handled; \code{exc_value} gets the exception
39 parameter (its \dfn{associated value} or the second argument to
40 \code{raise}); \code{exc_traceback} gets a traceback object which
41 encapsulates the call stack at the point where the exception
42 originally occurred.
43\end{datadesc}
44
45\begin{funcdesc}{exit}{n}
46 Exit from Python with numeric exit status \var{n}. This is
47 implemented by raising the \code{SystemExit} exception, so cleanup
48 actions specified by \code{finally} clauses of \code{try} statements
49 are honored, and it is possible to catch the exit attempt at an outer
50 level.
51\end{funcdesc}
52
53\begin{datadesc}{exitfunc}
54 This value is not actually defined by the module, but can be set by
55 the user (or by a program) to specify a clean-up action at program
56 exit. When set, it should be a parameterless function. This function
57 will be called when the interpreter exits in any way (but not when a
58 fatal error occurs: in that case the interpreter's internal state
59 cannot be trusted).
60\end{datadesc}
61
62\begin{datadesc}{last_type}
63\dataline{last_value}
64\dataline{last_traceback}
65 These three variables are not always defined; they are set when an
66 exception is not handled and the interpreter prints an error message
67 and a stack traceback. Their intended use is to allow an interactive
68 user to import a debugger module and engage in post-mortem debugging
Guido van Rossum16d6e711994-08-08 12:30:22 +000069 without having to re-execute the command that caused the error (which
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000070 may be hard to reproduce). The meaning of the variables is the same
71 as that of \code{exc_type}, \code{exc_value} and \code{exc_tracaback},
72 respectively.
73\end{datadesc}
74
75\begin{datadesc}{modules}
76 Gives the list of modules that have already been loaded.
77 This can be manipulated to force reloading of modules and other tricks.
78\end{datadesc}
79
80\begin{datadesc}{path}
81 A list of strings that specifies the search path for modules.
82 Initialized from the environment variable \code{PYTHONPATH}, or an
83 installation-dependent default.
84\end{datadesc}
85
86\begin{datadesc}{ps1}
87\dataline{ps2}
88 Strings specifying the primary and secondary prompt of the
89 interpreter. These are only defined if the interpreter is in
90 interactive mode. Their initial values in this case are
91 \code{'>>> '} and \code{'... '}.
92\end{datadesc}
93
94\begin{funcdesc}{settrace}{tracefunc}
95 Set the system's trace function, which allows you to implement a
96 Python source code debugger in Python. The standard modules
97 \code{pdb} and \code{wdb} are such debuggers; the difference is that
98 \code{wdb} uses windows and needs STDWIN, while \code{pdb} has a
99 line-oriented interface not unlike dbx. See the file \file{pdb.doc}
100 in the Python library source directory for more documentation (both
101 about \code{pdb} and \code{sys.trace}).
102\end{funcdesc}
103\ttindex{pdb}
104\ttindex{wdb}
105\index{trace function}
106
107\begin{funcdesc}{setprofile}{profilefunc}
108 Set the system's profile function, which allows you to implement a
109 Python source code profiler in Python. The system's profile function
110 is called similarly to the system's trace function (see
111 \code{sys.settrace}), but it isn't called for each executed line of
112 code (only on call and return and when an exception occurs). Also,
113 its return value is not used, so it can just return \code{None}.
114\end{funcdesc}
115\index{profile function}
116
117\begin{datadesc}{stdin}
118\dataline{stdout}
119\dataline{stderr}
120 File objects corresponding to the interpreter's standard input,
121 output and error streams. \code{sys.stdin} is used for all
122 interpreter input except for scripts but including calls to
123 \code{input()} and \code{raw_input()}. \code{sys.stdout} is used
124 for the output of \code{print} and expression statements and for the
125 prompts of \code{input()} and \code{raw_input()}. The interpreter's
126 own prompts and (almost all of) its error messages go to
127 \code{sys.stderr}. \code{sys.stdout} and \code{sys.stderr} needn't
128 be built-in file objects: any object is acceptable as long as it has
129 a \code{write} method that takes a string argument.
130\end{datadesc}
131
132\begin{datadesc}{tracebacklimit}
133When this variable is set to an integer value, it determines the
134maximum number of levels of traceback information printed when an
135unhandled exception occurs. The default is 1000. When set to 0 or
136less, all traceback information is suppressed and only the exception
137type and value are printed.
138\end{datadesc}