Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`sys` --- System-specific parameters and functions |
| 2 | ======================================================= |
| 3 | |
| 4 | .. module:: sys |
| 5 | :synopsis: Access system-specific parameters and functions. |
| 6 | |
| 7 | |
| 8 | This module provides access to some variables used or maintained by the |
| 9 | interpreter and to functions that interact strongly with the interpreter. It is |
| 10 | always available. |
| 11 | |
| 12 | |
| 13 | .. data:: argv |
| 14 | |
| 15 | The list of command line arguments passed to a Python script. ``argv[0]`` is the |
| 16 | script name (it is operating system dependent whether this is a full pathname or |
| 17 | not). If the command was executed using the :option:`-c` command line option to |
| 18 | the interpreter, ``argv[0]`` is set to the string ``'-c'``. If no script name |
| 19 | was passed to the Python interpreter, ``argv[0]`` is the empty string. |
| 20 | |
| 21 | To loop over the standard input, or the list of files given on the |
| 22 | command line, see the :mod:`fileinput` module. |
| 23 | |
| 24 | |
| 25 | .. data:: byteorder |
| 26 | |
| 27 | An indicator of the native byte order. This will have the value ``'big'`` on |
| 28 | big-endian (most-significant byte first) platforms, and ``'little'`` on |
| 29 | little-endian (least-significant byte first) platforms. |
| 30 | |
| 31 | .. versionadded:: 2.0 |
| 32 | |
| 33 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 34 | .. data:: builtin_module_names |
| 35 | |
| 36 | A tuple of strings giving the names of all modules that are compiled into this |
| 37 | Python interpreter. (This information is not available in any other way --- |
| 38 | ``modules.keys()`` only lists the imported modules.) |
| 39 | |
| 40 | |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame] | 41 | .. function:: call_tracing(func, args) |
| 42 | |
| 43 | Call ``func(*args)``, while tracing is enabled. The tracing state is saved, |
| 44 | and restored afterwards. This is intended to be called from a debugger from |
| 45 | a checkpoint, to recursively debug some other code. |
| 46 | |
| 47 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 48 | .. data:: copyright |
| 49 | |
| 50 | A string containing the copyright pertaining to the Python interpreter. |
| 51 | |
| 52 | |
Christian Heimes | 422051a | 2008-02-04 18:00:12 +0000 | [diff] [blame] | 53 | .. function:: _clear_type_cache() |
| 54 | |
| 55 | Clear the internal type cache. The type cache is used to speed up attribute |
| 56 | and method lookups. Use the function *only* to drop unnecessary references |
| 57 | during reference leak debugging. |
| 58 | |
| 59 | This function should be used for internal and specialized purposes only. |
Christian Heimes | 908caac | 2008-01-27 23:34:59 +0000 | [diff] [blame] | 60 | |
| 61 | .. versionadded:: 2.6 |
| 62 | |
| 63 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 | .. function:: _current_frames() |
| 65 | |
| 66 | Return a dictionary mapping each thread's identifier to the topmost stack frame |
| 67 | currently active in that thread at the time the function is called. Note that |
| 68 | functions in the :mod:`traceback` module can build the call stack given such a |
| 69 | frame. |
| 70 | |
| 71 | This is most useful for debugging deadlock: this function does not require the |
| 72 | deadlocked threads' cooperation, and such threads' call stacks are frozen for as |
| 73 | long as they remain deadlocked. The frame returned for a non-deadlocked thread |
| 74 | may bear no relationship to that thread's current activity by the time calling |
| 75 | code examines the frame. |
| 76 | |
| 77 | This function should be used for internal and specialized purposes only. |
| 78 | |
| 79 | .. versionadded:: 2.5 |
| 80 | |
| 81 | |
| 82 | .. data:: dllhandle |
| 83 | |
| 84 | Integer specifying the handle of the Python DLL. Availability: Windows. |
| 85 | |
| 86 | |
| 87 | .. function:: displayhook(value) |
| 88 | |
| 89 | If *value* is not ``None``, this function prints it to ``sys.stdout``, and saves |
| 90 | it in ``__builtin__._``. |
| 91 | |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 92 | ``sys.displayhook`` is called on the result of evaluating an :term:`expression` |
| 93 | entered in an interactive Python session. The display of these values can be |
| 94 | customized by assigning another one-argument function to ``sys.displayhook``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 95 | |
| 96 | |
Éric Araujo | 656b04e | 2011-10-05 02:25:58 +0200 | [diff] [blame] | 97 | .. data:: dont_write_bytecode |
| 98 | |
| 99 | If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the |
| 100 | import of source modules. This value is initially set to ``True`` or |
| 101 | ``False`` depending on the :option:`-B` command line option and the |
| 102 | :envvar:`PYTHONDONTWRITEBYTECODE` environment variable, but you can set it |
| 103 | yourself to control bytecode file generation. |
| 104 | |
| 105 | .. versionadded:: 2.6 |
| 106 | |
| 107 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 108 | .. function:: excepthook(type, value, traceback) |
| 109 | |
| 110 | This function prints out a given traceback and exception to ``sys.stderr``. |
| 111 | |
| 112 | When an exception is raised and uncaught, the interpreter calls |
| 113 | ``sys.excepthook`` with three arguments, the exception class, exception |
| 114 | instance, and a traceback object. In an interactive session this happens just |
| 115 | before control is returned to the prompt; in a Python program this happens just |
| 116 | before the program exits. The handling of such top-level exceptions can be |
| 117 | customized by assigning another three-argument function to ``sys.excepthook``. |
| 118 | |
| 119 | |
| 120 | .. data:: __displayhook__ |
| 121 | __excepthook__ |
| 122 | |
| 123 | These objects contain the original values of ``displayhook`` and ``excepthook`` |
| 124 | at the start of the program. They are saved so that ``displayhook`` and |
| 125 | ``excepthook`` can be restored in case they happen to get replaced with broken |
| 126 | objects. |
| 127 | |
| 128 | |
| 129 | .. function:: exc_info() |
| 130 | |
| 131 | This function returns a tuple of three values that give information about the |
| 132 | exception that is currently being handled. The information returned is specific |
| 133 | both to the current thread and to the current stack frame. If the current stack |
| 134 | frame is not handling an exception, the information is taken from the calling |
| 135 | stack frame, or its caller, and so on until a stack frame is found that is |
| 136 | handling an exception. Here, "handling an exception" is defined as "executing |
| 137 | or having executed an except clause." For any stack frame, only information |
| 138 | about the most recently handled exception is accessible. |
| 139 | |
| 140 | .. index:: object: traceback |
| 141 | |
| 142 | If no exception is being handled anywhere on the stack, a tuple containing three |
| 143 | ``None`` values is returned. Otherwise, the values returned are ``(type, value, |
| 144 | traceback)``. Their meaning is: *type* gets the exception type of the exception |
| 145 | being handled (a class object); *value* gets the exception parameter (its |
| 146 | :dfn:`associated value` or the second argument to :keyword:`raise`, which is |
| 147 | always a class instance if the exception type is a class object); *traceback* |
| 148 | gets a traceback object (see the Reference Manual) which encapsulates the call |
| 149 | stack at the point where the exception originally occurred. |
| 150 | |
| 151 | If :func:`exc_clear` is called, this function will return three ``None`` values |
| 152 | until either another exception is raised in the current thread or the execution |
| 153 | stack returns to a frame where another exception is being handled. |
| 154 | |
| 155 | .. warning:: |
| 156 | |
| 157 | Assigning the *traceback* return value to a local variable in a function that is |
| 158 | handling an exception will cause a circular reference. This will prevent |
| 159 | anything referenced by a local variable in the same function or by the traceback |
| 160 | from being garbage collected. Since most functions don't need access to the |
| 161 | traceback, the best solution is to use something like ``exctype, value = |
| 162 | sys.exc_info()[:2]`` to extract only the exception type and value. If you do |
| 163 | need the traceback, make sure to delete it after use (best done with a |
| 164 | :keyword:`try` ... :keyword:`finally` statement) or to call :func:`exc_info` in |
| 165 | a function that does not itself handle an exception. |
| 166 | |
| 167 | .. note:: |
| 168 | |
| 169 | Beginning with Python 2.2, such cycles are automatically reclaimed when garbage |
| 170 | collection is enabled and they become unreachable, but it remains more efficient |
| 171 | to avoid creating cycles. |
| 172 | |
| 173 | |
| 174 | .. function:: exc_clear() |
| 175 | |
| 176 | This function clears all information relating to the current or last exception |
| 177 | that occurred in the current thread. After calling this function, |
| 178 | :func:`exc_info` will return three ``None`` values until another exception is |
| 179 | raised in the current thread or the execution stack returns to a frame where |
| 180 | another exception is being handled. |
| 181 | |
| 182 | This function is only needed in only a few obscure situations. These include |
| 183 | logging and error handling systems that report information on the last or |
| 184 | current exception. This function can also be used to try to free resources and |
| 185 | trigger object finalization, though no guarantee is made as to what objects will |
| 186 | be freed, if any. |
| 187 | |
| 188 | .. versionadded:: 2.3 |
| 189 | |
| 190 | |
| 191 | .. data:: exc_type |
| 192 | exc_value |
| 193 | exc_traceback |
| 194 | |
| 195 | .. deprecated:: 1.5 |
| 196 | Use :func:`exc_info` instead. |
| 197 | |
| 198 | Since they are global variables, they are not specific to the current thread, so |
| 199 | their use is not safe in a multi-threaded program. When no exception is being |
| 200 | handled, ``exc_type`` is set to ``None`` and the other two are undefined. |
| 201 | |
| 202 | |
| 203 | .. data:: exec_prefix |
| 204 | |
| 205 | A string giving the site-specific directory prefix where the platform-dependent |
| 206 | Python files are installed; by default, this is also ``'/usr/local'``. This can |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 207 | be set at build time with the ``--exec-prefix`` argument to the |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 208 | :program:`configure` script. Specifically, all configuration files (e.g. the |
Éric Araujo | 2e4a2b6 | 2011-10-05 02:34:28 +0200 | [diff] [blame] | 209 | :file:`pyconfig.h` header file) are installed in the directory |
Éric Araujo | 060b812 | 2012-02-26 02:00:35 +0100 | [diff] [blame] | 210 | :file:`{exec_prefix}/lib/python{X.Y}/config`, and shared library modules are |
Éric Araujo | 2e4a2b6 | 2011-10-05 02:34:28 +0200 | [diff] [blame] | 211 | installed in :file:`{exec_prefix}/lib/python{X.Y}/lib-dynload`, where *X.Y* |
| 212 | is the version number of Python, for example ``2.7``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 213 | |
| 214 | |
| 215 | .. data:: executable |
| 216 | |
Petri Lehtinen | fe6f9d0 | 2012-02-02 21:11:28 +0200 | [diff] [blame] | 217 | A string giving the absolute path of the executable binary for the Python |
| 218 | interpreter, on systems where this makes sense. If Python is unable to retrieve |
| 219 | the real path to its executable, :data:`sys.executable` will be an empty string |
| 220 | or ``None``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 221 | |
| 222 | |
| 223 | .. function:: exit([arg]) |
| 224 | |
| 225 | Exit from Python. This is implemented by raising the :exc:`SystemExit` |
| 226 | exception, so cleanup actions specified by finally clauses of :keyword:`try` |
Georg Brandl | b8d0e36 | 2010-11-26 07:53:50 +0000 | [diff] [blame] | 227 | statements are honored, and it is possible to intercept the exit attempt at |
| 228 | an outer level. |
| 229 | |
| 230 | The optional argument *arg* can be an integer giving the exit status |
| 231 | (defaulting to zero), or another type of object. If it is an integer, zero |
| 232 | is considered "successful termination" and any nonzero value is considered |
| 233 | "abnormal termination" by shells and the like. Most systems require it to be |
| 234 | in the range 0-127, and produce undefined results otherwise. Some systems |
| 235 | have a convention for assigning specific meanings to specific exit codes, but |
| 236 | these are generally underdeveloped; Unix programs generally use 2 for command |
| 237 | line syntax errors and 1 for all other kind of errors. If another type of |
| 238 | object is passed, ``None`` is equivalent to passing zero, and any other |
| 239 | object is printed to :data:`stderr` and results in an exit code of 1. In |
| 240 | particular, ``sys.exit("some error message")`` is a quick way to exit a |
| 241 | program when an error occurs. |
| 242 | |
| 243 | Since :func:`exit` ultimately "only" raises an exception, it will only exit |
| 244 | the process when called from the main thread, and the exception is not |
| 245 | intercepted. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 246 | |
| 247 | |
| 248 | .. data:: exitfunc |
| 249 | |
| 250 | This value is not actually defined by the module, but can be set by the user (or |
| 251 | by a program) to specify a clean-up action at program exit. When set, it should |
| 252 | be a parameterless function. This function will be called when the interpreter |
| 253 | exits. Only one function may be installed in this way; to allow multiple |
| 254 | functions which will be called at termination, use the :mod:`atexit` module. |
| 255 | |
| 256 | .. note:: |
| 257 | |
| 258 | The exit function is not called when the program is killed by a signal, when a |
| 259 | Python fatal internal error is detected, or when ``os._exit()`` is called. |
| 260 | |
| 261 | .. deprecated:: 2.4 |
| 262 | Use :mod:`atexit` instead. |
| 263 | |
| 264 | |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 265 | .. data:: flags |
| 266 | |
| 267 | The struct sequence *flags* exposes the status of command line flags. The |
| 268 | attributes are read only. |
| 269 | |
Éric Araujo | 254d4b8 | 2011-03-26 02:09:14 +0100 | [diff] [blame] | 270 | ============================= =================================== |
| 271 | attribute flag |
| 272 | ============================= =================================== |
| 273 | :const:`debug` :option:`-d` |
| 274 | :const:`py3k_warning` :option:`-3` |
| 275 | :const:`division_warning` :option:`-Q` |
| 276 | :const:`division_new` :option:`-Qnew <-Q>` |
| 277 | :const:`inspect` :option:`-i` |
| 278 | :const:`interactive` :option:`-i` |
| 279 | :const:`optimize` :option:`-O` or :option:`-OO` |
| 280 | :const:`dont_write_bytecode` :option:`-B` |
| 281 | :const:`no_user_site` :option:`-s` |
| 282 | :const:`no_site` :option:`-S` |
| 283 | :const:`ignore_environment` :option:`-E` |
| 284 | :const:`tabcheck` :option:`-t` or :option:`-tt <-t>` |
| 285 | :const:`verbose` :option:`-v` |
| 286 | :const:`unicode` :option:`-U` |
| 287 | :const:`bytes_warning` :option:`-b` |
Benjamin Peterson | aee9dfb | 2012-02-20 21:44:56 -0500 | [diff] [blame] | 288 | :const:`hash_randomization` :option:`-R` |
Éric Araujo | 254d4b8 | 2011-03-26 02:09:14 +0100 | [diff] [blame] | 289 | ============================= =================================== |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 290 | |
| 291 | .. versionadded:: 2.6 |
| 292 | |
Éric Araujo | 6d37c4e | 2012-02-26 02:03:39 +0100 | [diff] [blame] | 293 | .. versionadded:: 2.7.3 |
| 294 | The ``hash_randomization`` attribute. |
Christian Heimes | f31b69f | 2008-01-14 03:42:48 +0000 | [diff] [blame] | 295 | |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 296 | .. data:: float_info |
| 297 | |
Christian Heimes | c94e2b5 | 2008-01-14 04:13:37 +0000 | [diff] [blame] | 298 | A structseq holding information about the float type. It contains low level |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 299 | information about the precision and internal representation. The values |
| 300 | correspond to the various floating-point constants defined in the standard |
| 301 | header file :file:`float.h` for the 'C' programming language; see section |
| 302 | 5.2.4.2.2 of the 1999 ISO/IEC C standard [C99]_, 'Characteristics of |
| 303 | floating types', for details. |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 304 | |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 305 | +---------------------+----------------+--------------------------------------------------+ |
| 306 | | attribute | float.h macro | explanation | |
| 307 | +=====================+================+==================================================+ |
Mark Dickinson | 91a6334 | 2010-07-03 09:15:09 +0000 | [diff] [blame] | 308 | | :const:`epsilon` | DBL_EPSILON | difference between 1 and the least value greater | |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 309 | | | | than 1 that is representable as a float | |
| 310 | +---------------------+----------------+--------------------------------------------------+ |
| 311 | | :const:`dig` | DBL_DIG | maximum number of decimal digits that can be | |
| 312 | | | | faithfully represented in a float; see below | |
| 313 | +---------------------+----------------+--------------------------------------------------+ |
| 314 | | :const:`mant_dig` | DBL_MANT_DIG | float precision: the number of base-``radix`` | |
| 315 | | | | digits in the significand of a float | |
| 316 | +---------------------+----------------+--------------------------------------------------+ |
| 317 | | :const:`max` | DBL_MAX | maximum representable finite float | |
| 318 | +---------------------+----------------+--------------------------------------------------+ |
| 319 | | :const:`max_exp` | DBL_MAX_EXP | maximum integer e such that ``radix**(e-1)`` is | |
| 320 | | | | a representable finite float | |
| 321 | +---------------------+----------------+--------------------------------------------------+ |
| 322 | | :const:`max_10_exp` | DBL_MAX_10_EXP | maximum integer e such that ``10**e`` is in the | |
| 323 | | | | range of representable finite floats | |
| 324 | +---------------------+----------------+--------------------------------------------------+ |
| 325 | | :const:`min` | DBL_MIN | minimum positive normalized float | |
| 326 | +---------------------+----------------+--------------------------------------------------+ |
| 327 | | :const:`min_exp` | DBL_MIN_EXP | minimum integer e such that ``radix**(e-1)`` is | |
| 328 | | | | a normalized float | |
| 329 | +---------------------+----------------+--------------------------------------------------+ |
| 330 | | :const:`min_10_exp` | DBL_MIN_10_EXP | minimum integer e such that ``10**e`` is a | |
| 331 | | | | normalized float | |
| 332 | +---------------------+----------------+--------------------------------------------------+ |
| 333 | | :const:`radix` | FLT_RADIX | radix of exponent representation | |
| 334 | +---------------------+----------------+--------------------------------------------------+ |
Mark Dickinson | b19284f | 2011-11-19 16:26:08 +0000 | [diff] [blame] | 335 | | :const:`rounds` | FLT_ROUNDS | integer constant representing the rounding mode | |
| 336 | | | | used for arithmetic operations. This reflects | |
| 337 | | | | the value of the system FLT_ROUNDS macro at | |
| 338 | | | | interpreter startup time. See section 5.2.4.2.2 | |
| 339 | | | | of the C99 standard for an explanation of the | |
| 340 | | | | possible values and their meanings. | |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 341 | +---------------------+----------------+--------------------------------------------------+ |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 342 | |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 343 | The attribute :attr:`sys.float_info.dig` needs further explanation. If |
| 344 | ``s`` is any string representing a decimal number with at most |
| 345 | :attr:`sys.float_info.dig` significant digits, then converting ``s`` to a |
| 346 | float and back again will recover a string representing the same decimal |
| 347 | value:: |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 348 | |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 349 | >>> import sys |
| 350 | >>> sys.float_info.dig |
| 351 | 15 |
| 352 | >>> s = '3.14159265358979' # decimal string with 15 significant digits |
| 353 | >>> format(float(s), '.15g') # convert to float and back -> same value |
| 354 | '3.14159265358979' |
| 355 | |
| 356 | But for strings with more than :attr:`sys.float_info.dig` significant digits, |
| 357 | this isn't always true:: |
| 358 | |
| 359 | >>> s = '9876543211234567' # 16 significant digits is too many! |
| 360 | >>> format(float(s), '.16g') # conversion changes value |
| 361 | '9876543211234568' |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 362 | |
Christian Heimes | 3e76d93 | 2007-12-01 15:40:22 +0000 | [diff] [blame] | 363 | .. versionadded:: 2.6 |
| 364 | |
Mark Dickinson | da8652d9 | 2009-10-24 14:01:08 +0000 | [diff] [blame] | 365 | .. data:: float_repr_style |
| 366 | |
| 367 | A string indicating how the :func:`repr` function behaves for |
| 368 | floats. If the string has value ``'short'`` then for a finite |
| 369 | float ``x``, ``repr(x)`` aims to produce a short string with the |
| 370 | property that ``float(repr(x)) == x``. This is the usual behaviour |
| 371 | in Python 2.7 and later. Otherwise, ``float_repr_style`` has value |
| 372 | ``'legacy'`` and ``repr(x)`` behaves in the same way as it did in |
| 373 | versions of Python prior to 2.7. |
| 374 | |
| 375 | .. versionadded:: 2.7 |
| 376 | |
Christian Heimes | dfdfaab | 2007-12-01 11:20:10 +0000 | [diff] [blame] | 377 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 378 | .. function:: getcheckinterval() |
| 379 | |
| 380 | Return the interpreter's "check interval"; see :func:`setcheckinterval`. |
| 381 | |
| 382 | .. versionadded:: 2.3 |
| 383 | |
| 384 | |
| 385 | .. function:: getdefaultencoding() |
| 386 | |
| 387 | Return the name of the current default string encoding used by the Unicode |
| 388 | implementation. |
| 389 | |
| 390 | .. versionadded:: 2.0 |
| 391 | |
| 392 | |
| 393 | .. function:: getdlopenflags() |
| 394 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 395 | Return the current value of the flags that are used for :c:func:`dlopen` calls. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 396 | The flag constants are defined in the :mod:`dl` and :mod:`DLFCN` modules. |
| 397 | Availability: Unix. |
| 398 | |
| 399 | .. versionadded:: 2.2 |
| 400 | |
| 401 | |
| 402 | .. function:: getfilesystemencoding() |
| 403 | |
| 404 | Return the name of the encoding used to convert Unicode filenames into system |
| 405 | file names, or ``None`` if the system default encoding is used. The result value |
| 406 | depends on the operating system: |
| 407 | |
Ezio Melotti | ab9149d | 2010-04-29 16:07:20 +0000 | [diff] [blame] | 408 | * On Mac OS X, the encoding is ``'utf-8'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 409 | |
| 410 | * On Unix, the encoding is the user's preference according to the result of |
Ezio Melotti | ab9149d | 2010-04-29 16:07:20 +0000 | [diff] [blame] | 411 | nl_langinfo(CODESET), or ``None`` if the ``nl_langinfo(CODESET)`` |
| 412 | failed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 413 | |
| 414 | * On Windows NT+, file names are Unicode natively, so no conversion is |
Ezio Melotti | ab9149d | 2010-04-29 16:07:20 +0000 | [diff] [blame] | 415 | performed. :func:`getfilesystemencoding` still returns ``'mbcs'``, as |
| 416 | this is the encoding that applications should use when they explicitly |
| 417 | want to convert Unicode strings to byte strings that are equivalent when |
| 418 | used as file names. |
| 419 | |
| 420 | * On Windows 9x, the encoding is ``'mbcs'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 421 | |
| 422 | .. versionadded:: 2.3 |
| 423 | |
| 424 | |
| 425 | .. function:: getrefcount(object) |
| 426 | |
| 427 | Return the reference count of the *object*. The count returned is generally one |
| 428 | higher than you might expect, because it includes the (temporary) reference as |
| 429 | an argument to :func:`getrefcount`. |
| 430 | |
| 431 | |
| 432 | .. function:: getrecursionlimit() |
| 433 | |
| 434 | Return the current value of the recursion limit, the maximum depth of the Python |
| 435 | interpreter stack. This limit prevents infinite recursion from causing an |
| 436 | overflow of the C stack and crashing Python. It can be set by |
| 437 | :func:`setrecursionlimit`. |
| 438 | |
| 439 | |
Robert Schuppenies | 4762902 | 2008-07-10 17:13:55 +0000 | [diff] [blame] | 440 | .. function:: getsizeof(object[, default]) |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 441 | |
| 442 | Return the size of an object in bytes. The object can be any type of |
| 443 | object. All built-in objects will return correct results, but this |
Robert Schuppenies | 4762902 | 2008-07-10 17:13:55 +0000 | [diff] [blame] | 444 | does not have to hold true for third-party extensions as it is implementation |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 445 | specific. |
| 446 | |
Benjamin Peterson | ca66cb5 | 2009-09-22 22:15:28 +0000 | [diff] [blame] | 447 | If given, *default* will be returned if the object does not provide means to |
Georg Brandl | f6d36745 | 2010-03-12 10:02:03 +0000 | [diff] [blame] | 448 | retrieve the size. Otherwise a :exc:`TypeError` will be raised. |
Robert Schuppenies | 4762902 | 2008-07-10 17:13:55 +0000 | [diff] [blame] | 449 | |
Benjamin Peterson | ca66cb5 | 2009-09-22 22:15:28 +0000 | [diff] [blame] | 450 | :func:`getsizeof` calls the object's ``__sizeof__`` method and adds an |
| 451 | additional garbage collector overhead if the object is managed by the garbage |
| 452 | collector. |
Robert Schuppenies | 4762902 | 2008-07-10 17:13:55 +0000 | [diff] [blame] | 453 | |
Robert Schuppenies | 51df064 | 2008-06-01 16:16:17 +0000 | [diff] [blame] | 454 | .. versionadded:: 2.6 |
| 455 | |
| 456 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 457 | .. function:: _getframe([depth]) |
| 458 | |
| 459 | Return a frame object from the call stack. If optional integer *depth* is |
| 460 | given, return the frame object that many calls below the top of the stack. If |
| 461 | that is deeper than the call stack, :exc:`ValueError` is raised. The default |
| 462 | for *depth* is zero, returning the frame at the top of the call stack. |
| 463 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 464 | .. impl-detail:: |
| 465 | |
| 466 | This function should be used for internal and specialized purposes only. |
| 467 | It is not guaranteed to exist in all implementations of Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 468 | |
| 469 | |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame] | 470 | .. function:: getprofile() |
| 471 | |
| 472 | .. index:: |
| 473 | single: profile function |
| 474 | single: profiler |
| 475 | |
| 476 | Get the profiler function as set by :func:`setprofile`. |
| 477 | |
| 478 | .. versionadded:: 2.6 |
| 479 | |
| 480 | |
| 481 | .. function:: gettrace() |
| 482 | |
| 483 | .. index:: |
| 484 | single: trace function |
| 485 | single: debugger |
| 486 | |
| 487 | Get the trace function as set by :func:`settrace`. |
| 488 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 489 | .. impl-detail:: |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame] | 490 | |
| 491 | The :func:`gettrace` function is intended only for implementing debuggers, |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 492 | profilers, coverage tools and the like. Its behavior is part of the |
| 493 | implementation platform, rather than part of the language definition, and |
| 494 | thus may not be available in all Python implementations. |
Georg Brandl | 5611289 | 2008-01-20 13:59:46 +0000 | [diff] [blame] | 495 | |
| 496 | .. versionadded:: 2.6 |
| 497 | |
| 498 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 499 | .. function:: getwindowsversion() |
| 500 | |
Eric Smith | 096d0bf | 2010-01-27 00:55:16 +0000 | [diff] [blame] | 501 | Return a named tuple describing the Windows version |
Eric Smith | ee931b7 | 2010-01-27 00:28:29 +0000 | [diff] [blame] | 502 | currently running. The named elements are *major*, *minor*, |
| 503 | *build*, *platform*, *service_pack*, *service_pack_minor*, |
| 504 | *service_pack_major*, *suite_mask*, and *product_type*. |
| 505 | *service_pack* contains a string while all other values are |
| 506 | integers. The components can also be accessed by name, so |
| 507 | ``sys.getwindowsversion()[0]`` is equivalent to |
| 508 | ``sys.getwindowsversion().major``. For compatibility with prior |
| 509 | versions, only the first 5 elements are retrievable by indexing. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 510 | |
| 511 | *platform* may be one of the following values: |
| 512 | |
Jeroen Ruigrok van der Werven | aa3cadb | 2008-04-21 20:15:39 +0000 | [diff] [blame] | 513 | +-----------------------------------------+-------------------------+ |
| 514 | | Constant | Platform | |
| 515 | +=========================================+=========================+ |
| 516 | | :const:`0 (VER_PLATFORM_WIN32s)` | Win32s on Windows 3.1 | |
| 517 | +-----------------------------------------+-------------------------+ |
| 518 | | :const:`1 (VER_PLATFORM_WIN32_WINDOWS)` | Windows 95/98/ME | |
| 519 | +-----------------------------------------+-------------------------+ |
| 520 | | :const:`2 (VER_PLATFORM_WIN32_NT)` | Windows NT/2000/XP/x64 | |
| 521 | +-----------------------------------------+-------------------------+ |
| 522 | | :const:`3 (VER_PLATFORM_WIN32_CE)` | Windows CE | |
| 523 | +-----------------------------------------+-------------------------+ |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 524 | |
Eric Smith | ee931b7 | 2010-01-27 00:28:29 +0000 | [diff] [blame] | 525 | *product_type* may be one of the following values: |
| 526 | |
| 527 | +---------------------------------------+---------------------------------+ |
| 528 | | Constant | Meaning | |
| 529 | +=======================================+=================================+ |
| 530 | | :const:`1 (VER_NT_WORKSTATION)` | The system is a workstation. | |
| 531 | +---------------------------------------+---------------------------------+ |
| 532 | | :const:`2 (VER_NT_DOMAIN_CONTROLLER)` | The system is a domain | |
| 533 | | | controller. | |
| 534 | +---------------------------------------+---------------------------------+ |
| 535 | | :const:`3 (VER_NT_SERVER)` | The system is a server, but not | |
| 536 | | | a domain controller. | |
| 537 | +---------------------------------------+---------------------------------+ |
| 538 | |
| 539 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 540 | This function wraps the Win32 :c:func:`GetVersionEx` function; see the |
| 541 | Microsoft documentation on :c:func:`OSVERSIONINFOEX` for more information |
Eric Smith | ee931b7 | 2010-01-27 00:28:29 +0000 | [diff] [blame] | 542 | about these fields. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 543 | |
| 544 | Availability: Windows. |
| 545 | |
| 546 | .. versionadded:: 2.3 |
Eric Smith | ee931b7 | 2010-01-27 00:28:29 +0000 | [diff] [blame] | 547 | .. versionchanged:: 2.7 |
| 548 | Changed to a named tuple and added *service_pack_minor*, |
| 549 | *service_pack_major*, *suite_mask*, and *product_type*. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 550 | |
| 551 | |
| 552 | .. data:: hexversion |
| 553 | |
| 554 | The version number encoded as a single integer. This is guaranteed to increase |
| 555 | with each version, including proper support for non-production releases. For |
| 556 | example, to test that the Python interpreter is at least version 1.5.2, use:: |
| 557 | |
| 558 | if sys.hexversion >= 0x010502F0: |
| 559 | # use some advanced feature |
| 560 | ... |
| 561 | else: |
| 562 | # use an alternative implementation or warn the user |
| 563 | ... |
| 564 | |
| 565 | This is called ``hexversion`` since it only really looks meaningful when viewed |
| 566 | as the result of passing it to the built-in :func:`hex` function. The |
| 567 | ``version_info`` value may be used for a more human-friendly encoding of the |
| 568 | same information. |
| 569 | |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 570 | The ``hexversion`` is a 32-bit number with the following layout: |
R David Murray | a0895db | 2011-04-25 16:10:18 -0400 | [diff] [blame] | 571 | |
| 572 | +-------------------------+------------------------------------------------+ |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 573 | | Bits (big endian order) | Meaning | |
R David Murray | a0895db | 2011-04-25 16:10:18 -0400 | [diff] [blame] | 574 | +=========================+================================================+ |
| 575 | | :const:`1-8` | ``PY_MAJOR_VERSION`` (the ``2`` in | |
| 576 | | | ``2.1.0a3``) | |
| 577 | +-------------------------+------------------------------------------------+ |
| 578 | | :const:`9-16` | ``PY_MINOR_VERSION`` (the ``1`` in | |
| 579 | | | ``2.1.0a3``) | |
| 580 | +-------------------------+------------------------------------------------+ |
| 581 | | :const:`17-24` | ``PY_MICRO_VERSION`` (the ``0`` in | |
| 582 | | | ``2.1.0a3``) | |
| 583 | +-------------------------+------------------------------------------------+ |
| 584 | | :const:`25-28` | ``PY_RELEASE_LEVEL`` (``0xA`` for alpha, | |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 585 | | | ``0xB`` for beta, ``0xC`` for release | |
| 586 | | | candidate and ``0xF`` for final) | |
R David Murray | a0895db | 2011-04-25 16:10:18 -0400 | [diff] [blame] | 587 | +-------------------------+------------------------------------------------+ |
| 588 | | :const:`29-32` | ``PY_RELEASE_SERIAL`` (the ``3`` in | |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 589 | | | ``2.1.0a3``, zero for final releases) | |
R David Murray | a0895db | 2011-04-25 16:10:18 -0400 | [diff] [blame] | 590 | +-------------------------+------------------------------------------------+ |
| 591 | |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 592 | Thus ``2.1.0a3`` is hexversion ``0x020100a3``. |
R David Murray | a0895db | 2011-04-25 16:10:18 -0400 | [diff] [blame] | 593 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 594 | .. versionadded:: 1.5.2 |
| 595 | |
| 596 | |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 597 | .. data:: long_info |
| 598 | |
| 599 | A struct sequence that holds information about Python's |
| 600 | internal representation of integers. The attributes are read only. |
| 601 | |
| 602 | +-------------------------+----------------------------------------------+ |
R David Murray | dcaacbf | 2011-04-30 16:34:35 -0400 | [diff] [blame] | 603 | | Attribute | Explanation | |
Mark Dickinson | efc82f7 | 2009-03-20 15:51:55 +0000 | [diff] [blame] | 604 | +=========================+==============================================+ |
| 605 | | :const:`bits_per_digit` | number of bits held in each digit. Python | |
| 606 | | | integers are stored internally in base | |
| 607 | | | ``2**long_info.bits_per_digit`` | |
| 608 | +-------------------------+----------------------------------------------+ |
| 609 | | :const:`sizeof_digit` | size in bytes of the C type used to | |
| 610 | | | represent a digit | |
| 611 | +-------------------------+----------------------------------------------+ |
| 612 | |
| 613 | .. versionadded:: 2.7 |
| 614 | |
| 615 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 616 | .. data:: last_type |
| 617 | last_value |
| 618 | last_traceback |
| 619 | |
| 620 | These three variables are not always defined; they are set when an exception is |
| 621 | not handled and the interpreter prints an error message and a stack traceback. |
| 622 | Their intended use is to allow an interactive user to import a debugger module |
| 623 | and engage in post-mortem debugging without having to re-execute the command |
| 624 | that caused the error. (Typical use is ``import pdb; pdb.pm()`` to enter the |
| 625 | post-mortem debugger; see chapter :ref:`debugger` for |
| 626 | more information.) |
| 627 | |
| 628 | The meaning of the variables is the same as that of the return values from |
| 629 | :func:`exc_info` above. (Since there is only one interactive thread, |
| 630 | thread-safety is not a concern for these variables, unlike for ``exc_type`` |
| 631 | etc.) |
| 632 | |
| 633 | |
| 634 | .. data:: maxint |
| 635 | |
| 636 | The largest positive integer supported by Python's regular integer type. This |
| 637 | is at least 2\*\*31-1. The largest negative integer is ``-maxint-1`` --- the |
| 638 | asymmetry results from the use of 2's complement binary arithmetic. |
| 639 | |
Martin v. Löwis | 4dd019f | 2008-05-20 08:11:19 +0000 | [diff] [blame] | 640 | .. data:: maxsize |
| 641 | |
| 642 | The largest positive integer supported by the platform's Py_ssize_t type, |
| 643 | and thus the maximum size lists, strings, dicts, and many other containers |
| 644 | can have. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 645 | |
| 646 | .. data:: maxunicode |
| 647 | |
| 648 | An integer giving the largest supported code point for a Unicode character. The |
| 649 | value of this depends on the configuration option that specifies whether Unicode |
| 650 | characters are stored as UCS-2 or UCS-4. |
| 651 | |
| 652 | |
Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 653 | .. data:: meta_path |
| 654 | |
| 655 | A list of :term:`finder` objects that have their :meth:`find_module` |
| 656 | methods called to see if one of the objects can find the module to be |
| 657 | imported. The :meth:`find_module` method is called at least with the |
| 658 | absolute name of the module being imported. If the module to be imported is |
| 659 | contained in package then the parent package's :attr:`__path__` attribute |
Sandro Tosi | a76bb03 | 2012-01-14 16:43:14 +0100 | [diff] [blame] | 660 | is passed in as a second argument. The method returns ``None`` if |
Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 661 | the module cannot be found, else returns a :term:`loader`. |
| 662 | |
| 663 | :data:`sys.meta_path` is searched before any implicit default finders or |
| 664 | :data:`sys.path`. |
| 665 | |
| 666 | See :pep:`302` for the original specification. |
| 667 | |
| 668 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 669 | .. data:: modules |
| 670 | |
| 671 | .. index:: builtin: reload |
| 672 | |
| 673 | This is a dictionary that maps module names to modules which have already been |
| 674 | loaded. This can be manipulated to force reloading of modules and other tricks. |
| 675 | Note that removing a module from this dictionary is *not* the same as calling |
| 676 | :func:`reload` on the corresponding module object. |
| 677 | |
| 678 | |
| 679 | .. data:: path |
| 680 | |
| 681 | .. index:: triple: module; search; path |
| 682 | |
| 683 | A list of strings that specifies the search path for modules. Initialized from |
| 684 | the environment variable :envvar:`PYTHONPATH`, plus an installation-dependent |
| 685 | default. |
| 686 | |
| 687 | As initialized upon program startup, the first item of this list, ``path[0]``, |
| 688 | is the directory containing the script that was used to invoke the Python |
| 689 | interpreter. If the script directory is not available (e.g. if the interpreter |
| 690 | is invoked interactively or if the script is read from standard input), |
| 691 | ``path[0]`` is the empty string, which directs Python to search modules in the |
| 692 | current directory first. Notice that the script directory is inserted *before* |
| 693 | the entries inserted as a result of :envvar:`PYTHONPATH`. |
| 694 | |
| 695 | A program is free to modify this list for its own purposes. |
| 696 | |
| 697 | .. versionchanged:: 2.3 |
| 698 | Unicode strings are no longer ignored. |
| 699 | |
Benjamin Peterson | 4db53b2 | 2009-01-10 23:41:59 +0000 | [diff] [blame] | 700 | .. seealso:: |
| 701 | Module :mod:`site` This describes how to use .pth files to extend |
| 702 | :data:`sys.path`. |
| 703 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 704 | |
Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 705 | .. data:: path_hooks |
| 706 | |
| 707 | A list of callables that take a path argument to try to create a |
| 708 | :term:`finder` for the path. If a finder can be created, it is to be |
| 709 | returned by the callable, else raise :exc:`ImportError`. |
| 710 | |
| 711 | Originally specified in :pep:`302`. |
| 712 | |
| 713 | |
| 714 | .. data:: path_importer_cache |
| 715 | |
| 716 | A dictionary acting as a cache for :term:`finder` objects. The keys are |
| 717 | paths that have been passed to :data:`sys.path_hooks` and the values are |
| 718 | the finders that are found. If a path is a valid file system path but no |
Sandro Tosi | a76bb03 | 2012-01-14 16:43:14 +0100 | [diff] [blame] | 719 | explicit finder is found on :data:`sys.path_hooks` then ``None`` is |
Georg Brandl | 624f337 | 2009-03-31 16:11:45 +0000 | [diff] [blame] | 720 | stored to represent the implicit default finder should be used. If the path |
| 721 | is not an existing path then :class:`imp.NullImporter` is set. |
| 722 | |
| 723 | Originally specified in :pep:`302`. |
| 724 | |
| 725 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 726 | .. data:: platform |
| 727 | |
Georg Brandl | 440f2ff | 2008-01-20 12:57:47 +0000 | [diff] [blame] | 728 | This string contains a platform identifier that can be used to append |
| 729 | platform-specific components to :data:`sys.path`, for instance. |
| 730 | |
Victor Stinner | d99ff29 | 2011-09-05 22:33:55 +0200 | [diff] [blame] | 731 | For most Unix systems, this is the lowercased OS name as returned by ``uname |
| 732 | -s`` with the first part of the version as returned by ``uname -r`` appended, |
| 733 | e.g. ``'sunos5'``, *at the time when Python was built*. Unless you want to |
| 734 | test for a specific system version, it is therefore recommended to use the |
| 735 | following idiom:: |
Antoine Pitrou | ea901ad | 2011-07-09 15:48:29 +0200 | [diff] [blame] | 736 | |
Victor Stinner | d99ff29 | 2011-09-05 22:33:55 +0200 | [diff] [blame] | 737 | if sys.platform.startswith('freebsd'): |
| 738 | # FreeBSD-specific code here... |
| 739 | elif sys.platform.startswith('linux'): |
Antoine Pitrou | ea901ad | 2011-07-09 15:48:29 +0200 | [diff] [blame] | 740 | # Linux-specific code here... |
| 741 | |
Victor Stinner | d99ff29 | 2011-09-05 22:33:55 +0200 | [diff] [blame] | 742 | .. versionchanged:: 2.7.3 |
| 743 | Since lots of code check for ``sys.platform == 'linux2'``, and there is |
| 744 | no essential change between Linux 2.x and 3.x, ``sys.platform`` is always |
| 745 | set to ``'linux2'``, even on Linux 3.x. In Python 3.3 and later, the |
| 746 | value will always be set to ``'linux'``, so it is recommended to always |
| 747 | use the ``startswith`` idiom presented above. |
| 748 | |
Georg Brandl | 440f2ff | 2008-01-20 12:57:47 +0000 | [diff] [blame] | 749 | For other systems, the values are: |
| 750 | |
Victor Stinner | d99ff29 | 2011-09-05 22:33:55 +0200 | [diff] [blame] | 751 | ===================== =========================== |
| 752 | System :data:`platform` value |
| 753 | ===================== =========================== |
| 754 | Linux (2.x *and* 3.x) ``'linux2'`` |
| 755 | Windows ``'win32'`` |
| 756 | Windows/Cygwin ``'cygwin'`` |
| 757 | Mac OS X ``'darwin'`` |
| 758 | OS/2 ``'os2'`` |
| 759 | OS/2 EMX ``'os2emx'`` |
| 760 | RiscOS ``'riscos'`` |
| 761 | AtheOS ``'atheos'`` |
| 762 | ===================== =========================== |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 763 | |
Antoine Pitrou | ea901ad | 2011-07-09 15:48:29 +0200 | [diff] [blame] | 764 | .. seealso:: |
| 765 | :attr:`os.name` has a coarser granularity. :func:`os.uname` gives |
| 766 | system-dependent version information. |
| 767 | |
| 768 | The :mod:`platform` module provides detailed checks for the |
| 769 | system's identity. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 770 | |
| 771 | .. data:: prefix |
| 772 | |
| 773 | A string giving the site-specific directory prefix where the platform |
| 774 | independent Python files are installed; by default, this is the string |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 775 | ``'/usr/local'``. This can be set at build time with the ``--prefix`` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 776 | argument to the :program:`configure` script. The main collection of Python |
Éric Araujo | 2e4a2b6 | 2011-10-05 02:34:28 +0200 | [diff] [blame] | 777 | library modules is installed in the directory :file:`{prefix}/lib/python{X.Y}`` |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 778 | while the platform independent header files (all except :file:`pyconfig.h`) are |
Éric Araujo | 060b812 | 2012-02-26 02:00:35 +0100 | [diff] [blame] | 779 | stored in :file:`{prefix}/include/python{X.Y}`, where *X.Y* is the version |
Éric Araujo | 2e4a2b6 | 2011-10-05 02:34:28 +0200 | [diff] [blame] | 780 | number of Python, for example ``2.7``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 781 | |
| 782 | |
| 783 | .. data:: ps1 |
| 784 | ps2 |
| 785 | |
| 786 | .. index:: |
| 787 | single: interpreter prompts |
| 788 | single: prompts, interpreter |
| 789 | |
| 790 | Strings specifying the primary and secondary prompt of the interpreter. These |
| 791 | are only defined if the interpreter is in interactive mode. Their initial |
| 792 | values in this case are ``'>>> '`` and ``'... '``. If a non-string object is |
| 793 | assigned to either variable, its :func:`str` is re-evaluated each time the |
| 794 | interpreter prepares to read a new interactive command; this can be used to |
| 795 | implement a dynamic prompt. |
| 796 | |
| 797 | |
Christian Heimes | d7b3337 | 2007-11-28 08:02:36 +0000 | [diff] [blame] | 798 | .. data:: py3kwarning |
| 799 | |
Ezio Melotti | 510ff54 | 2012-05-03 19:21:40 +0300 | [diff] [blame] | 800 | Bool containing the status of the Python 3 warning flag. It's ``True`` |
Georg Brandl | 13813f7 | 2009-02-26 17:36:26 +0000 | [diff] [blame] | 801 | when Python is started with the -3 option. (This should be considered |
| 802 | read-only; setting it to a different value doesn't have an effect on |
Ezio Melotti | 510ff54 | 2012-05-03 19:21:40 +0300 | [diff] [blame] | 803 | Python 3 warnings.) |
Christian Heimes | d7b3337 | 2007-11-28 08:02:36 +0000 | [diff] [blame] | 804 | |
Georg Brandl | 5f79446 | 2008-03-21 21:05:03 +0000 | [diff] [blame] | 805 | .. versionadded:: 2.6 |
| 806 | |
Christian Heimes | d7b3337 | 2007-11-28 08:02:36 +0000 | [diff] [blame] | 807 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 808 | .. function:: setcheckinterval(interval) |
| 809 | |
| 810 | Set the interpreter's "check interval". This integer value determines how often |
| 811 | the interpreter checks for periodic things such as thread switches and signal |
| 812 | handlers. The default is ``100``, meaning the check is performed every 100 |
| 813 | Python virtual instructions. Setting it to a larger value may increase |
| 814 | performance for programs using threads. Setting it to a value ``<=`` 0 checks |
| 815 | every virtual instruction, maximizing responsiveness as well as overhead. |
| 816 | |
| 817 | |
| 818 | .. function:: setdefaultencoding(name) |
| 819 | |
| 820 | Set the current default string encoding used by the Unicode implementation. If |
| 821 | *name* does not match any available encoding, :exc:`LookupError` is raised. |
| 822 | This function is only intended to be used by the :mod:`site` module |
| 823 | implementation and, where needed, by :mod:`sitecustomize`. Once used by the |
| 824 | :mod:`site` module, it is removed from the :mod:`sys` module's namespace. |
| 825 | |
Georg Brandl | b19be57 | 2007-12-29 10:57:00 +0000 | [diff] [blame] | 826 | .. Note that :mod:`site` is not imported if the :option:`-S` option is passed |
| 827 | to the interpreter, in which case this function will remain available. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 828 | |
| 829 | .. versionadded:: 2.0 |
| 830 | |
| 831 | |
| 832 | .. function:: setdlopenflags(n) |
| 833 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 834 | Set the flags used by the interpreter for :c:func:`dlopen` calls, such as when |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 835 | the interpreter loads extension modules. Among other things, this will enable a |
| 836 | lazy resolving of symbols when importing a module, if called as |
| 837 | ``sys.setdlopenflags(0)``. To share symbols across extension modules, call as |
| 838 | ``sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)``. Symbolic names for the |
| 839 | flag modules can be either found in the :mod:`dl` module, or in the :mod:`DLFCN` |
| 840 | module. If :mod:`DLFCN` is not available, it can be generated from |
| 841 | :file:`/usr/include/dlfcn.h` using the :program:`h2py` script. Availability: |
| 842 | Unix. |
| 843 | |
| 844 | .. versionadded:: 2.2 |
| 845 | |
| 846 | |
| 847 | .. function:: setprofile(profilefunc) |
| 848 | |
| 849 | .. index:: |
| 850 | single: profile function |
| 851 | single: profiler |
| 852 | |
| 853 | Set the system's profile function, which allows you to implement a Python source |
| 854 | code profiler in Python. See chapter :ref:`profile` for more information on the |
| 855 | Python profiler. The system's profile function is called similarly to the |
| 856 | system's trace function (see :func:`settrace`), but it isn't called for each |
| 857 | executed line of code (only on call and return, but the return event is reported |
| 858 | even when an exception has been set). The function is thread-specific, but |
| 859 | there is no way for the profiler to know about context switches between threads, |
| 860 | so it does not make sense to use this in the presence of multiple threads. Also, |
| 861 | its return value is not used, so it can simply return ``None``. |
| 862 | |
| 863 | |
| 864 | .. function:: setrecursionlimit(limit) |
| 865 | |
| 866 | Set the maximum depth of the Python interpreter stack to *limit*. This limit |
| 867 | prevents infinite recursion from causing an overflow of the C stack and crashing |
| 868 | Python. |
| 869 | |
| 870 | The highest possible limit is platform-dependent. A user may need to set the |
| 871 | limit higher when she has a program that requires deep recursion and a platform |
| 872 | that supports a higher limit. This should be done with care, because a too-high |
| 873 | limit can lead to a crash. |
| 874 | |
| 875 | |
| 876 | .. function:: settrace(tracefunc) |
| 877 | |
| 878 | .. index:: |
| 879 | single: trace function |
| 880 | single: debugger |
| 881 | |
| 882 | Set the system's trace function, which allows you to implement a Python |
Benjamin Peterson | 050f4ad | 2008-11-20 21:25:31 +0000 | [diff] [blame] | 883 | source code debugger in Python. The function is thread-specific; for a |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 884 | debugger to support multiple threads, it must be registered using |
| 885 | :func:`settrace` for each thread being debugged. |
| 886 | |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 887 | Trace functions should have three arguments: *frame*, *event*, and |
| 888 | *arg*. *frame* is the current stack frame. *event* is a string: ``'call'``, |
| 889 | ``'line'``, ``'return'``, ``'exception'``, ``'c_call'``, ``'c_return'``, or |
| 890 | ``'c_exception'``. *arg* depends on the event type. |
| 891 | |
| 892 | The trace function is invoked (with *event* set to ``'call'``) whenever a new |
| 893 | local scope is entered; it should return a reference to a local trace |
| 894 | function to be used that scope, or ``None`` if the scope shouldn't be traced. |
| 895 | |
| 896 | The local trace function should return a reference to itself (or to another |
| 897 | function for further tracing in that scope), or ``None`` to turn off tracing |
| 898 | in that scope. |
| 899 | |
| 900 | The events have the following meaning: |
| 901 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 902 | ``'call'`` |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 903 | A function is called (or some other code block entered). The |
| 904 | global trace function is called; *arg* is ``None``; the return value |
| 905 | specifies the local trace function. |
| 906 | |
| 907 | ``'line'`` |
Jeffrey Yasskin | 655d835 | 2009-05-23 23:23:01 +0000 | [diff] [blame] | 908 | The interpreter is about to execute a new line of code or re-execute the |
| 909 | condition of a loop. The local trace function is called; *arg* is |
| 910 | ``None``; the return value specifies the new local trace function. See |
| 911 | :file:`Objects/lnotab_notes.txt` for a detailed explanation of how this |
| 912 | works. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 913 | |
| 914 | ``'return'`` |
| 915 | A function (or other code block) is about to return. The local trace |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 916 | function is called; *arg* is the value that will be returned, or ``None`` |
| 917 | if the event is caused by an exception being raised. The trace function's |
| 918 | return value is ignored. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 919 | |
| 920 | ``'exception'`` |
| 921 | An exception has occurred. The local trace function is called; *arg* is a |
| 922 | tuple ``(exception, value, traceback)``; the return value specifies the |
| 923 | new local trace function. |
| 924 | |
| 925 | ``'c_call'`` |
| 926 | A C function is about to be called. This may be an extension function or |
Georg Brandl | d7d4fd7 | 2009-07-26 14:37:28 +0000 | [diff] [blame] | 927 | a built-in. *arg* is the C function object. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 928 | |
| 929 | ``'c_return'`` |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 930 | A C function has returned. *arg* is the C function object. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 931 | |
| 932 | ``'c_exception'`` |
Georg Brandl | 78f11ed | 2010-11-26 07:34:20 +0000 | [diff] [blame] | 933 | A C function has raised an exception. *arg* is the C function object. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 934 | |
Benjamin Peterson | 050f4ad | 2008-11-20 21:25:31 +0000 | [diff] [blame] | 935 | Note that as an exception is propagated down the chain of callers, an |
| 936 | ``'exception'`` event is generated at each level. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 937 | |
Benjamin Peterson | 050f4ad | 2008-11-20 21:25:31 +0000 | [diff] [blame] | 938 | For more information on code and frame objects, refer to :ref:`types`. |
Benjamin Peterson | 5ab9c3b | 2008-11-20 04:05:12 +0000 | [diff] [blame] | 939 | |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 940 | .. impl-detail:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 941 | |
| 942 | The :func:`settrace` function is intended only for implementing debuggers, |
Georg Brandl | 6c14e58 | 2009-10-22 11:48:10 +0000 | [diff] [blame] | 943 | profilers, coverage tools and the like. Its behavior is part of the |
| 944 | implementation platform, rather than part of the language definition, and |
| 945 | thus may not be available in all Python implementations. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 946 | |
| 947 | |
| 948 | .. function:: settscdump(on_flag) |
| 949 | |
| 950 | Activate dumping of VM measurements using the Pentium timestamp counter, if |
| 951 | *on_flag* is true. Deactivate these dumps if *on_flag* is off. The function is |
Éric Araujo | a8132ec | 2010-12-16 03:53:53 +0000 | [diff] [blame] | 952 | available only if Python was compiled with ``--with-tsc``. To understand |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 953 | the output of this dump, read :file:`Python/ceval.c` in the Python sources. |
| 954 | |
| 955 | .. versionadded:: 2.4 |
| 956 | |
Benjamin Peterson | a7fa032 | 2010-03-06 03:13:33 +0000 | [diff] [blame] | 957 | .. impl-detail:: |
| 958 | |
| 959 | This function is intimately bound to CPython implementation details and |
| 960 | thus not likely to be implemented elsewhere. |
| 961 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 962 | |
| 963 | .. data:: stdin |
| 964 | stdout |
| 965 | stderr |
| 966 | |
| 967 | .. index:: |
| 968 | builtin: input |
| 969 | builtin: raw_input |
| 970 | |
| 971 | File objects corresponding to the interpreter's standard input, output and error |
| 972 | streams. ``stdin`` is used for all interpreter input except for scripts but |
| 973 | including calls to :func:`input` and :func:`raw_input`. ``stdout`` is used for |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 974 | the output of :keyword:`print` and :term:`expression` statements and for the |
| 975 | prompts of :func:`input` and :func:`raw_input`. The interpreter's own prompts |
| 976 | and (almost all of) its error messages go to ``stderr``. ``stdout`` and |
| 977 | ``stderr`` needn't be built-in file objects: any object is acceptable as long |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 978 | as it has a :meth:`write` method that takes a string argument. (Changing these |
Georg Brandl | 584265b | 2007-12-02 14:58:50 +0000 | [diff] [blame] | 979 | objects doesn't affect the standard I/O streams of processes executed by |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 980 | :func:`os.popen`, :func:`os.system` or the :func:`exec\*` family of functions in |
| 981 | the :mod:`os` module.) |
| 982 | |
| 983 | |
| 984 | .. data:: __stdin__ |
| 985 | __stdout__ |
| 986 | __stderr__ |
| 987 | |
| 988 | These objects contain the original values of ``stdin``, ``stderr`` and |
Georg Brandl | b48adec | 2009-03-31 19:10:35 +0000 | [diff] [blame] | 989 | ``stdout`` at the start of the program. They are used during finalization, |
| 990 | and could be useful to print to the actual standard stream no matter if the |
| 991 | ``sys.std*`` object has been redirected. |
| 992 | |
| 993 | It can also be used to restore the actual files to known working file objects |
| 994 | in case they have been overwritten with a broken object. However, the |
| 995 | preferred way to do this is to explicitly save the previous stream before |
| 996 | replacing it, and restore the saved object. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 997 | |
| 998 | |
Antoine Pitrou | 7370590 | 2011-07-09 16:06:19 +0200 | [diff] [blame] | 999 | .. data:: subversion |
| 1000 | |
| 1001 | A triple (repo, branch, version) representing the Subversion information of the |
| 1002 | Python interpreter. *repo* is the name of the repository, ``'CPython'``. |
| 1003 | *branch* is a string of one of the forms ``'trunk'``, ``'branches/name'`` or |
| 1004 | ``'tags/name'``. *version* is the output of ``svnversion``, if the interpreter |
| 1005 | was built from a Subversion checkout; it contains the revision number (range) |
| 1006 | and possibly a trailing 'M' if there were local modifications. If the tree was |
| 1007 | exported (or svnversion was not available), it is the revision of |
| 1008 | ``Include/patchlevel.h`` if the branch is a tag. Otherwise, it is ``None``. |
| 1009 | |
| 1010 | .. versionadded:: 2.5 |
| 1011 | |
| 1012 | .. note:: |
| 1013 | Python is now `developed <http://docs.python.org/devguide/>`_ using |
| 1014 | Mercurial. In recent Python 2.7 bugfix releases, :data:`subversion` |
| 1015 | therefore contains placeholder information. It is removed in Python |
| 1016 | 3.3. |
| 1017 | |
| 1018 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1019 | .. data:: tracebacklimit |
| 1020 | |
| 1021 | When this variable is set to an integer value, it determines the maximum number |
| 1022 | of levels of traceback information printed when an unhandled exception occurs. |
| 1023 | The default is ``1000``. When set to ``0`` or less, all traceback information |
| 1024 | is suppressed and only the exception type and value are printed. |
| 1025 | |
| 1026 | |
| 1027 | .. data:: version |
| 1028 | |
| 1029 | A string containing the version number of the Python interpreter plus additional |
Georg Brandl | e277325 | 2010-08-01 19:14:56 +0000 | [diff] [blame] | 1030 | information on the build number and compiler used. This string is displayed |
| 1031 | when the interactive interpreter is started. Do not extract version information |
| 1032 | out of it, rather, use :data:`version_info` and the functions provided by the |
| 1033 | :mod:`platform` module. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1034 | |
| 1035 | |
| 1036 | .. data:: api_version |
| 1037 | |
| 1038 | The C API version for this interpreter. Programmers may find this useful when |
| 1039 | debugging version conflicts between Python and extension modules. |
| 1040 | |
| 1041 | .. versionadded:: 2.3 |
| 1042 | |
| 1043 | |
| 1044 | .. data:: version_info |
| 1045 | |
| 1046 | A tuple containing the five components of the version number: *major*, *minor*, |
| 1047 | *micro*, *releaselevel*, and *serial*. All values except *releaselevel* are |
| 1048 | integers; the release level is ``'alpha'``, ``'beta'``, ``'candidate'``, or |
| 1049 | ``'final'``. The ``version_info`` value corresponding to the Python version 2.0 |
Eric Smith | 81fe093 | 2009-02-06 00:48:26 +0000 | [diff] [blame] | 1050 | is ``(2, 0, 0, 'final', 0)``. The components can also be accessed by name, |
| 1051 | so ``sys.version_info[0]`` is equivalent to ``sys.version_info.major`` |
| 1052 | and so on. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1053 | |
| 1054 | .. versionadded:: 2.0 |
Eric Smith | 81fe093 | 2009-02-06 00:48:26 +0000 | [diff] [blame] | 1055 | .. versionchanged:: 2.7 |
| 1056 | Added named component attributes |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1057 | |
| 1058 | |
| 1059 | .. data:: warnoptions |
| 1060 | |
| 1061 | This is an implementation detail of the warnings framework; do not modify this |
| 1062 | value. Refer to the :mod:`warnings` module for more information on the warnings |
| 1063 | framework. |
| 1064 | |
| 1065 | |
| 1066 | .. data:: winver |
| 1067 | |
| 1068 | The version number used to form registry keys on Windows platforms. This is |
| 1069 | stored as string resource 1000 in the Python DLL. The value is normally the |
| 1070 | first three characters of :const:`version`. It is provided in the :mod:`sys` |
| 1071 | module for informational purposes; modifying this value has no effect on the |
| 1072 | registry keys used by Python. Availability: Windows. |
Mark Dickinson | 2547ce7 | 2010-07-02 18:06:52 +0000 | [diff] [blame] | 1073 | |
| 1074 | .. rubric:: Citations |
| 1075 | |
| 1076 | .. [C99] ISO/IEC 9899:1999. "Programming languages -- C." A public draft of this standard is available at http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1256.pdf . |
| 1077 | |