Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 1 | \section{\module{sys} --- |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 2 | System-specific parameters and functions} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 3 | |
Fred Drake | ffbe687 | 1999-04-22 21:23:22 +0000 | [diff] [blame] | 4 | \declaremodule{builtin}{sys} |
Fred Drake | 295da24 | 1998-08-10 19:42:37 +0000 | [diff] [blame] | 5 | \modulesynopsis{Access system-specific parameters and functions.} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 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. |
| 10 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 12 | \begin{datadesc}{argv} |
| 13 | The list of command line arguments passed to a Python script. |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 20 | \end{datadesc} |
| 21 | |
Fred Drake | a2b6ad6 | 2000-08-15 04:24:43 +0000 | [diff] [blame] | 22 | \begin{datadesc}{byteorder} |
Fred Drake | 68e2915 | 2000-08-14 15:47:30 +0000 | [diff] [blame] | 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} |
| 29 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 30 | \begin{datadesc}{builtin_module_names} |
Guido van Rossum | 0d2971b | 1997-01-06 23:01:02 +0000 | [diff] [blame] | 31 | A tuple of strings giving the names of all modules that are compiled |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 32 | into this Python interpreter. (This information is not available in |
Fred Drake | 0fd72ee | 1998-03-08 05:43:51 +0000 | [diff] [blame] | 33 | any other way --- \code{modules.keys()} only lists the imported |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 34 | modules.) |
| 35 | \end{datadesc} |
| 36 | |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 37 | \begin{datadesc}{copyright} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 38 | A string containing the copyright pertaining to the Python |
| 39 | interpreter. |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 40 | \end{datadesc} |
| 41 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 42 | \begin{datadesc}{dllhandle} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 43 | Integer specifying the handle of the Python DLL. |
| 44 | Availability: Windows. |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 45 | \end{datadesc} |
| 46 | |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 47 | \begin{funcdesc}{displayhook}{\var{value}} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 48 | If \var{value} is not \code{None}, this function prints it to |
| 49 | \code{sys.stdout}, and saves it in \code{__builtin__._}. |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 50 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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}. |
Moshe Zadka | f68f2fe | 2001-01-11 05:41:27 +0000 | [diff] [blame] | 55 | \end{funcdesc} |
| 56 | |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 57 | \begin{funcdesc}{excepthook}{\var{type}, \var{value}, \var{traceback}} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 58 | This function prints out a given traceback and exception to |
| 59 | \code{sys.stderr}. |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 60 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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}. |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 68 | \end{funcdesc} |
| 69 | |
| 70 | \begin{datadesc}{__displayhook__} |
| 71 | \dataline{__excepthook__} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Ka-Ping Yee | b5c5132 | 2001-03-23 02:46:52 +0000 | [diff] [blame] | 76 | \end{datadesc} |
| 77 | |
Guido van Rossum | 871cf16 | 1997-10-20 22:38:43 +0000 | [diff] [blame] | 78 | \begin{funcdesc}{exc_info}{} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 871cf16 | 1997-10-20 22:38:43 +0000 | [diff] [blame] | 89 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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} |
Guido van Rossum | 871cf16 | 1997-10-20 22:38:43 +0000 | [diff] [blame] | 101 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 871cf16 | 1997-10-20 22:38:43 +0000 | [diff] [blame] | 113 | \end{funcdesc} |
| 114 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 115 | \begin{datadesc}{exc_type} |
| 116 | \dataline{exc_value} |
| 117 | \dataline{exc_traceback} |
Fred Drake | 0fd72ee | 1998-03-08 05:43:51 +0000 | [diff] [blame] | 118 | \deprecated {1.5} |
| 119 | {Use \function{exc_info()} instead.} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 124 | \end{datadesc} |
| 125 | |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 126 | \begin{datadesc}{exec_prefix} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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 |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 132 | \file{pyconfig.h} header file) are installed in the directory |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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]}. |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 137 | \end{datadesc} |
| 138 | |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 139 | \begin{datadesc}{executable} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 140 | A string giving the name of the executable binary for the Python |
| 141 | interpreter, on systems where this makes sense. |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 142 | \end{datadesc} |
| 143 | |
Guido van Rossum | 04307ce | 1998-11-23 17:49:53 +0000 | [diff] [blame] | 144 | \begin{funcdesc}{exit}{\optional{arg}} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 163 | \end{funcdesc} |
| 164 | |
| 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 |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 175 | \end{datadesc} |
| 176 | |
Fred Drake | 8940faf | 2000-10-25 21:02:55 +0000 | [diff] [blame] | 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} |
| 182 | |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 183 | \begin{funcdesc}{getdlopenflags}{} |
Fred Drake | 5d808fb | 2001-07-18 16:35:05 +0000 | [diff] [blame] | 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} |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 189 | \end{funcdesc} |
| 190 | |
Guido van Rossum | 6e91c6a | 1998-02-07 21:17:05 +0000 | [diff] [blame] | 191 | \begin{funcdesc}{getrefcount}{object} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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()}. |
Guido van Rossum | 6e91c6a | 1998-02-07 21:17:05 +0000 | [diff] [blame] | 196 | \end{funcdesc} |
| 197 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 198 | \begin{funcdesc}{getrecursionlimit}{} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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()}. |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 203 | \end{funcdesc} |
| 204 | |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 205 | \begin{funcdesc}{_getframe}{\optional{depth}} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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. |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 211 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 212 | This function should be used for internal and specialized purposes |
| 213 | only. |
Barry Warsaw | b6a54d2 | 2000-12-06 21:47:46 +0000 | [diff] [blame] | 214 | \end{funcdesc} |
| 215 | |
Fred Drake | 4d65d73 | 2000-04-13 16:54:17 +0000 | [diff] [blame] | 216 | \begin{datadesc}{hexversion} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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: |
Fred Drake | 4d65d73 | 2000-04-13 16:54:17 +0000 | [diff] [blame] | 221 | |
| 222 | \begin{verbatim} |
| 223 | if sys.hexversion >= 0x010502F0: |
| 224 | # use some advanced feature |
| 225 | ... |
| 226 | else: |
| 227 | # use an alternative implementation or warn the user |
| 228 | ... |
| 229 | \end{verbatim} |
| 230 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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} |
Fred Drake | 4d65d73 | 2000-04-13 16:54:17 +0000 | [diff] [blame] | 236 | \end{datadesc} |
| 237 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 238 | \begin{datadesc}{last_type} |
| 239 | \dataline{last_value} |
| 240 | \dataline{last_traceback} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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.) |
Guido van Rossum | 871cf16 | 1997-10-20 22:38:43 +0000 | [diff] [blame] | 249 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 254 | \end{datadesc} |
| 255 | |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 256 | \begin{datadesc}{maxint} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 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 |
Fred Drake | c05fc7d | 2001-09-04 18:18:36 +0000 | [diff] [blame] | 259 | \code{-maxint-1} --- the asymmetry results from the use of 2's |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 260 | complement binary arithmetic. |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 261 | \end{datadesc} |
| 262 | |
Fred Drake | c05fc7d | 2001-09-04 18:18:36 +0000 | [diff] [blame] | 263 | \begin{datadesc}{maxunicode} |
| 264 | An integer giving the largest supported code point for a Unicode |
| 265 | character. The value of this depends on the configuration option |
| 266 | that specifies whether Unicode characters are stored as UCS-2 or |
| 267 | UCS-4. |
| 268 | \end{datadesc} |
| 269 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 270 | \begin{datadesc}{modules} |
Fred Drake | 0fd72ee | 1998-03-08 05:43:51 +0000 | [diff] [blame] | 271 | This is a dictionary that maps module names to modules which have |
| 272 | already been loaded. This can be manipulated to force reloading of |
| 273 | modules and other tricks. Note that removing a module from this |
| 274 | dictionary is \emph{not} the same as calling |
| 275 | \function{reload()}\bifuncindex{reload} on the corresponding module |
| 276 | object. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 277 | \end{datadesc} |
| 278 | |
| 279 | \begin{datadesc}{path} |
Fred Drake | 2b67bee | 1998-01-13 18:35:51 +0000 | [diff] [blame] | 280 | \indexiii{module}{search}{path} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 281 | A list of strings that specifies the search path for modules. |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 282 | Initialized from the environment variable \envvar{PYTHONPATH}, or an |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 283 | installation-dependent default. |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 284 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 285 | The first item of this list, \code{path[0]}, is the directory |
| 286 | containing the script that was used to invoke the Python |
| 287 | interpreter. If the script directory is not available (e.g. if the |
| 288 | interpreter is invoked interactively or if the script is read from |
| 289 | standard input), \code{path[0]} is the empty string, which directs |
| 290 | Python to search modules in the current directory first. Notice |
| 291 | that the script directory is inserted \emph{before} the entries |
| 292 | inserted as a result of \envvar{PYTHONPATH}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 293 | \end{datadesc} |
| 294 | |
Guido van Rossum | 6b686e9 | 1995-07-07 23:00:35 +0000 | [diff] [blame] | 295 | \begin{datadesc}{platform} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 296 | This string contains a platform identifier, e.g. \code{'sunos5'} or |
| 297 | \code{'linux1'}. This can be used to append platform-specific |
| 298 | components to \code{path}, for instance. |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 299 | \end{datadesc} |
| 300 | |
| 301 | \begin{datadesc}{prefix} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 302 | A string giving the site-specific directory prefix where the |
| 303 | platform independent Python files are installed; by default, this is |
| 304 | the string \code{'/usr/local'}. This can be set at build time with |
| 305 | the \longprogramopt{prefix} argument to the \program{configure} |
| 306 | script. The main collection of Python library modules is installed |
| 307 | in the directory \code{prefix + '/lib/python\var{version}'} while |
Martin v. Löwis | 4f1cd8b | 2001-07-26 13:41:06 +0000 | [diff] [blame] | 308 | the platform independent header files (all except \file{pyconfig.h}) |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 309 | are stored in \code{prefix + '/include/python\var{version}'}, where |
| 310 | \var{version} is equal to \code{version[:3]}. |
Guido van Rossum | 6b686e9 | 1995-07-07 23:00:35 +0000 | [diff] [blame] | 311 | \end{datadesc} |
| 312 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 313 | \begin{datadesc}{ps1} |
| 314 | \dataline{ps2} |
Fred Drake | e6cedb3 | 1998-04-03 07:05:16 +0000 | [diff] [blame] | 315 | \index{interpreter prompts} |
| 316 | \index{prompts, interpreter} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 317 | Strings specifying the primary and secondary prompt of the |
| 318 | interpreter. These are only defined if the interpreter is in |
| 319 | interactive mode. Their initial values in this case are |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 320 | \code{'>\code{>}> '} and \code{'... '}. If a non-string object is |
| 321 | assigned to either variable, its \function{str()} is re-evaluated |
| 322 | each time the interpreter prepares to read a new interactive |
| 323 | command; this can be used to implement a dynamic prompt. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 324 | \end{datadesc} |
| 325 | |
Guido van Rossum | 9c51e41 | 1995-01-10 10:50:58 +0000 | [diff] [blame] | 326 | \begin{funcdesc}{setcheckinterval}{interval} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 327 | Set the interpreter's ``check interval''. This integer value |
| 328 | determines how often the interpreter checks for periodic things such |
| 329 | as thread switches and signal handlers. The default is \code{10}, |
| 330 | meaning the check is performed every 10 Python virtual instructions. |
| 331 | Setting it to a larger value may increase performance for programs |
| 332 | using threads. Setting it to a value \code{<=} 0 checks every |
| 333 | virtual instruction, maximizing responsiveness as well as overhead. |
Guido van Rossum | 7f49b7a | 1995-01-12 12:38:46 +0000 | [diff] [blame] | 334 | \end{funcdesc} |
Guido van Rossum | 9c51e41 | 1995-01-10 10:50:58 +0000 | [diff] [blame] | 335 | |
Fred Drake | 8940faf | 2000-10-25 21:02:55 +0000 | [diff] [blame] | 336 | \begin{funcdesc}{setdefaultencoding}{name} |
| 337 | Set the current default string encoding used by the Unicode |
| 338 | implementation. If \var{name} does not match any available |
| 339 | encoding, \exception{LookupError} is raised. This function is only |
| 340 | intended to be used by the \refmodule{site} module implementation |
| 341 | and, where needed, by \module{sitecustomize}. Once used by the |
| 342 | \refmodule{site} module, it is removed from the \module{sys} |
| 343 | module's namespace. |
| 344 | % Note that \refmodule{site} is not imported if |
| 345 | % the \programopt{-S} option is passed to the interpreter, in which |
| 346 | % case this function will remain available. |
| 347 | \versionadded{2.0} |
| 348 | \end{funcdesc} |
| 349 | |
Andrew M. Kuchling | 28bafb8 | 2001-07-19 01:17:15 +0000 | [diff] [blame] | 350 | \begin{funcdesc}{setdlopenflags}{n} |
Fred Drake | 5d808fb | 2001-07-18 16:35:05 +0000 | [diff] [blame] | 351 | Set the flags used by the interpreter for \cfunction{dlopen()} |
| 352 | calls, such as when the interpreter loads extension modules. Among |
| 353 | other things, this will enable a lazy resolving of symbols when |
Andrew M. Kuchling | 28bafb8 | 2001-07-19 01:17:15 +0000 | [diff] [blame] | 354 | importing a module, if called as \code{sys.setdlopenflags(0)}. To |
| 355 | share symbols across extension modules, call as |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 356 | \code{sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)}. Symbolic |
Fred Drake | 5d808fb | 2001-07-18 16:35:05 +0000 | [diff] [blame] | 357 | names for the flag modules can be either found in the \refmodule{dl} |
| 358 | module, or in the \module{DLFCN} module. If \module{DLFCN} is not |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 359 | available, it can be generated from \file{/usr/include/dlfcn.h} |
| 360 | using the \program{h2py} script. |
Fred Drake | 5d808fb | 2001-07-18 16:35:05 +0000 | [diff] [blame] | 361 | Availability: \UNIX. |
| 362 | \versionadded{2.2} |
Martin v. Löwis | f0473d5 | 2001-07-18 16:17:16 +0000 | [diff] [blame] | 363 | \end{funcdesc} |
| 364 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 365 | \begin{funcdesc}{setprofile}{profilefunc} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 366 | Set the system's profile function,\index{profile function} which |
| 367 | allows you to implement a Python source code profiler in |
| 368 | Python.\index{profiler} See chapter \ref{profile} for more |
| 369 | information on the Python profiler. The system's profile function |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 370 | is called similarly to the system's trace function (see |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 371 | \function{settrace()}), but it isn't called for each executed line |
Fred Drake | 64d7863 | 2001-10-16 14:54:22 +0000 | [diff] [blame] | 372 | of code (only on call and return, but the return event is reported |
| 373 | even when an exception has been set). The function is |
| 374 | thread-specific, but there is no way for the profiler to know about |
| 375 | context switches between threads, so it does not make sense to use |
| 376 | this in the presence of multiple threads. |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 377 | Also, its return value is not used, so it can simply return |
| 378 | \code{None}. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 379 | \end{funcdesc} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 380 | |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 381 | \begin{funcdesc}{setrecursionlimit}{limit} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 382 | Set the maximum depth of the Python interpreter stack to |
| 383 | \var{limit}. This limit prevents infinite recursion from causing an |
| 384 | overflow of the C stack and crashing Python. |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 385 | |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 386 | The highest possible limit is platform-dependent. A user may need |
| 387 | to set the limit higher when she has a program that requires deep |
| 388 | recursion and a platform that supports a higher limit. This should |
| 389 | be done with care, because a too-high limit can lead to a crash. |
Fred Drake | 65faf11 | 2000-08-31 19:35:56 +0000 | [diff] [blame] | 390 | \end{funcdesc} |
Jeremy Hylton | ee5adfb | 2000-08-31 19:23:01 +0000 | [diff] [blame] | 391 | |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 392 | \begin{funcdesc}{settrace}{tracefunc} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 393 | Set the system's trace function,\index{trace function} which allows |
| 394 | you to implement a Python source code debugger in Python. See |
| 395 | section \ref{debugger-hooks}, ``How It Works,'' in the chapter on |
Fred Drake | 64d7863 | 2001-10-16 14:54:22 +0000 | [diff] [blame] | 396 | the Python debugger.\index{debugger} The function is |
| 397 | thread-specific; for a debugger to support multiple threads, it must |
| 398 | be registered using \function{settrace()} for each thread being |
| 399 | debugged. |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 400 | \end{funcdesc} |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 401 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 402 | \begin{datadesc}{stdin} |
| 403 | \dataline{stdout} |
| 404 | \dataline{stderr} |
| 405 | File objects corresponding to the interpreter's standard input, |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 406 | output and error streams. \code{stdin} is used for all interpreter |
| 407 | input except for scripts but including calls to |
Fred Drake | 0fd72ee | 1998-03-08 05:43:51 +0000 | [diff] [blame] | 408 | \function{input()}\bifuncindex{input} and |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 409 | \function{raw_input()}\bifuncindex{raw_input}. \code{stdout} is |
| 410 | used for the output of \keyword{print} and expression statements and |
| 411 | for the prompts of \function{input()} and \function{raw_input()}. |
| 412 | The interpreter's own prompts and (almost all of) its error messages |
| 413 | go to \code{stderr}. \code{stdout} and \code{stderr} needn't be |
| 414 | built-in file objects: any object is acceptable as long as it has a |
| 415 | \method{write()} method that takes a string argument. (Changing |
| 416 | these objects doesn't affect the standard I/O streams of processes |
Fred Drake | 0fd72ee | 1998-03-08 05:43:51 +0000 | [diff] [blame] | 417 | executed by \function{os.popen()}, \function{os.system()} or the |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 418 | \function{exec*()} family of functions in the \refmodule{os} |
| 419 | module.) |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 420 | \end{datadesc} |
| 421 | |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 422 | \begin{datadesc}{__stdin__} |
| 423 | \dataline{__stdout__} |
| 424 | \dataline{__stderr__} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 425 | These objects contain the original values of \code{stdin}, |
| 426 | \code{stderr} and \code{stdout} at the start of the program. They |
| 427 | are used during finalization, and could be useful to restore the |
| 428 | actual files to known working file objects in case they have been |
| 429 | overwritten with a broken object. |
Guido van Rossum | 3e5fe42 | 1998-06-10 17:57:44 +0000 | [diff] [blame] | 430 | \end{datadesc} |
| 431 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 432 | \begin{datadesc}{tracebacklimit} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 433 | When this variable is set to an integer value, it determines the |
| 434 | maximum number of levels of traceback information printed when an |
| 435 | unhandled exception occurs. The default is \code{1000}. When set |
| 436 | to \code{0} or less, all traceback information is suppressed and |
| 437 | only the exception type and value are printed. |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 438 | \end{datadesc} |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 439 | |
| 440 | \begin{datadesc}{version} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 441 | A string containing the version number of the Python interpreter |
| 442 | plus additional information on the build number and compiler used. |
| 443 | It has a value of the form \code{'\var{version} |
| 444 | (\#\var{build_number}, \var{build_date}, \var{build_time}) |
| 445 | [\var{compiler}]'}. The first three characters are used to identify |
| 446 | the version in the installation directories (where appropriate on |
| 447 | each platform). An example: |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 448 | |
| 449 | \begin{verbatim} |
| 450 | >>> import sys |
| 451 | >>> sys.version |
| 452 | '1.5.2 (#0 Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]' |
| 453 | \end{verbatim} |
| 454 | \end{datadesc} |
| 455 | |
Fred Drake | 4d65d73 | 2000-04-13 16:54:17 +0000 | [diff] [blame] | 456 | \begin{datadesc}{version_info} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 457 | A tuple containing the five components of the version number: |
| 458 | \var{major}, \var{minor}, \var{micro}, \var{releaselevel}, and |
| 459 | \var{serial}. All values except \var{releaselevel} are integers; |
| 460 | the release level is \code{'alpha'}, \code{'beta'}, |
| 461 | \code{'candidate'}, or \code{'final'}. The \code{version_info} |
| 462 | value corresponding to the Python version 2.0 is \code{(2, 0, 0, |
| 463 | 'final', 0)}. |
| 464 | \versionadded{2.0} |
Fred Drake | 4d65d73 | 2000-04-13 16:54:17 +0000 | [diff] [blame] | 465 | \end{datadesc} |
| 466 | |
Fred Drake | c05fc7d | 2001-09-04 18:18:36 +0000 | [diff] [blame] | 467 | \begin{datadesc}{warnoptions} |
| 468 | This is an implementation detail of the warnings framework; do not |
| 469 | modify this value. Refer to the \refmodule{warnings} module for |
| 470 | more information on the warnings framework. |
| 471 | \end{datadesc} |
| 472 | |
Fred Drake | 38e5d27 | 2000-04-03 20:13:55 +0000 | [diff] [blame] | 473 | \begin{datadesc}{winver} |
Fred Drake | 7218202 | 2001-07-18 17:52:58 +0000 | [diff] [blame] | 474 | The version number used to form registry keys on Windows platforms. |
| 475 | This is stored as string resource 1000 in the Python DLL. The value |
| 476 | is normally the first three characters of \constant{version}. It is |
| 477 | provided in the \module{sys} module for informational purposes; |
| 478 | modifying this value has no effect on the registry keys used by |
| 479 | Python. |
| 480 | Availability: Windows. |
Guido van Rossum | 0a3c753 | 1997-06-02 17:32:41 +0000 | [diff] [blame] | 481 | \end{datadesc} |