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