blob: 5fdff4429c3bd4d2936bb5ad716f16df5d1dc07c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`sys` --- System-specific parameters and functions
3=======================================================
4
5.. module:: sys
6 :synopsis: Access system-specific parameters and functions.
7
8
9This module provides access to some variables used or maintained by the
10interpreter and to functions that interact strongly with the interpreter. It is
11always available.
12
13
14.. data:: argv
15
16 The list of command line arguments passed to a Python script. ``argv[0]`` is the
17 script name (it is operating system dependent whether this is a full pathname or
18 not). If the command was executed using the :option:`-c` command line option to
19 the interpreter, ``argv[0]`` is set to the string ``'-c'``. If no script name
20 was passed to the Python interpreter, ``argv[0]`` is the empty string.
21
22 To loop over the standard input, or the list of files given on the
23 command line, see the :mod:`fileinput` module.
24
25
26.. data:: byteorder
27
28 An indicator of the native byte order. This will have the value ``'big'`` on
29 big-endian (most-significant byte first) platforms, and ``'little'`` on
30 little-endian (least-significant byte first) platforms.
31
Georg Brandl116aa622007-08-15 14:28:22 +000032
33.. data:: subversion
34
35 A triple (repo, branch, version) representing the Subversion information of the
36 Python interpreter. *repo* is the name of the repository, ``'CPython'``.
37 *branch* is a string of one of the forms ``'trunk'``, ``'branches/name'`` or
38 ``'tags/name'``. *version* is the output of ``svnversion``, if the interpreter
39 was built from a Subversion checkout; it contains the revision number (range)
40 and possibly a trailing 'M' if there were local modifications. If the tree was
41 exported (or svnversion was not available), it is the revision of
42 ``Include/patchlevel.h`` if the branch is a tag. Otherwise, it is ``None``.
43
Georg Brandl116aa622007-08-15 14:28:22 +000044
45.. data:: builtin_module_names
46
47 A tuple of strings giving the names of all modules that are compiled into this
48 Python interpreter. (This information is not available in any other way ---
49 ``modules.keys()`` only lists the imported modules.)
50
51
52.. data:: copyright
53
54 A string containing the copyright pertaining to the Python interpreter.
55
56
Christian Heimes26855632008-01-27 23:50:43 +000057.. function:: _cleartypecache()
58
59 Clear the internal type lookup cache.
60
61 .. versionadded:: 2.6
62
63
Georg Brandl116aa622007-08-15 14:28:22 +000064.. 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
Georg Brandl116aa622007-08-15 14:28:22 +000079
80.. data:: dllhandle
81
82 Integer specifying the handle of the Python DLL. Availability: Windows.
83
84
85.. function:: displayhook(value)
86
87 If *value* is not ``None``, this function prints it to ``sys.stdout``, and saves
Georg Brandl1a3284e2007-12-02 09:40:06 +000088 it in ``builtins._``.
Georg Brandl116aa622007-08-15 14:28:22 +000089
Christian Heimesd8654cf2007-12-02 15:22:16 +000090 ``sys.displayhook`` is called on the result of evaluating an :term:`expression`
91 entered in an interactive Python session. The display of these values can be
92 customized by assigning another one-argument function to ``sys.displayhook``.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
95.. function:: excepthook(type, value, traceback)
96
97 This function prints out a given traceback and exception to ``sys.stderr``.
98
99 When an exception is raised and uncaught, the interpreter calls
100 ``sys.excepthook`` with three arguments, the exception class, exception
101 instance, and a traceback object. In an interactive session this happens just
102 before control is returned to the prompt; in a Python program this happens just
103 before the program exits. The handling of such top-level exceptions can be
104 customized by assigning another three-argument function to ``sys.excepthook``.
105
106
107.. data:: __displayhook__
108 __excepthook__
109
110 These objects contain the original values of ``displayhook`` and ``excepthook``
111 at the start of the program. They are saved so that ``displayhook`` and
112 ``excepthook`` can be restored in case they happen to get replaced with broken
113 objects.
114
115
116.. function:: exc_info()
117
118 This function returns a tuple of three values that give information about the
119 exception that is currently being handled. The information returned is specific
120 both to the current thread and to the current stack frame. If the current stack
121 frame is not handling an exception, the information is taken from the calling
122 stack frame, or its caller, and so on until a stack frame is found that is
123 handling an exception. Here, "handling an exception" is defined as "executing
124 or having executed an except clause." For any stack frame, only information
125 about the most recently handled exception is accessible.
126
127 .. index:: object: traceback
128
129 If no exception is being handled anywhere on the stack, a tuple containing three
130 ``None`` values is returned. Otherwise, the values returned are ``(type, value,
131 traceback)``. Their meaning is: *type* gets the exception type of the exception
132 being handled (a class object); *value* gets the exception parameter (its
133 :dfn:`associated value` or the second argument to :keyword:`raise`, which is
134 always a class instance if the exception type is a class object); *traceback*
135 gets a traceback object (see the Reference Manual) which encapsulates the call
136 stack at the point where the exception originally occurred.
137
138 .. warning::
139
140 Assigning the *traceback* return value to a local variable in a function that is
141 handling an exception will cause a circular reference. This will prevent
142 anything referenced by a local variable in the same function or by the traceback
143 from being garbage collected. Since most functions don't need access to the
144 traceback, the best solution is to use something like ``exctype, value =
145 sys.exc_info()[:2]`` to extract only the exception type and value. If you do
146 need the traceback, make sure to delete it after use (best done with a
147 :keyword:`try` ... :keyword:`finally` statement) or to call :func:`exc_info` in
148 a function that does not itself handle an exception.
149
150 .. note::
151
152 Beginning with Python 2.2, such cycles are automatically reclaimed when garbage
153 collection is enabled and they become unreachable, but it remains more efficient
154 to avoid creating cycles.
155
156
157.. data:: exec_prefix
158
159 A string giving the site-specific directory prefix where the platform-dependent
160 Python files are installed; by default, this is also ``'/usr/local'``. This can
161 be set at build time with the :option:`--exec-prefix` argument to the
162 :program:`configure` script. Specifically, all configuration files (e.g. the
163 :file:`pyconfig.h` header file) are installed in the directory ``exec_prefix +
164 '/lib/pythonversion/config'``, and shared library modules are installed in
165 ``exec_prefix + '/lib/pythonversion/lib-dynload'``, where *version* is equal to
166 ``version[:3]``.
167
168
169.. data:: executable
170
171 A string giving the name of the executable binary for the Python interpreter, on
172 systems where this makes sense.
173
174
175.. function:: exit([arg])
176
177 Exit from Python. This is implemented by raising the :exc:`SystemExit`
178 exception, so cleanup actions specified by finally clauses of :keyword:`try`
179 statements are honored, and it is possible to intercept the exit attempt at an
180 outer level. The optional argument *arg* can be an integer giving the exit
181 status (defaulting to zero), or another type of object. If it is an integer,
182 zero is considered "successful termination" and any nonzero value is considered
183 "abnormal termination" by shells and the like. Most systems require it to be in
184 the range 0-127, and produce undefined results otherwise. Some systems have a
185 convention for assigning specific meanings to specific exit codes, but these are
186 generally underdeveloped; Unix programs generally use 2 for command line syntax
187 errors and 1 for all other kind of errors. If another type of object is passed,
188 ``None`` is equivalent to passing zero, and any other object is printed to
189 ``sys.stderr`` and results in an exit code of 1. In particular,
190 ``sys.exit("some error message")`` is a quick way to exit a program when an
191 error occurs.
192
193
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000194.. data:: flags
195
196 The struct sequence *flags* exposes the status of command line flags. The
197 attributes are read only.
198
199 +------------------------------+------------------------------------------+
200 | attribute | flag |
201 +==============================+==========================================+
202 | :const:`debug` | -d |
203 +------------------------------+------------------------------------------+
204 | :const:`py3k_warning` | -3 |
205 +------------------------------+------------------------------------------+
206 | :const:`division_warning` | -Q |
207 +------------------------------+------------------------------------------+
208 | :const:`division_new` | -Qnew |
209 +------------------------------+------------------------------------------+
210 | :const:`inspect` | -i |
211 +------------------------------+------------------------------------------+
212 | :const:`interactive` | -i |
213 +------------------------------+------------------------------------------+
214 | :const:`optimize` | -O or -OO |
215 +------------------------------+------------------------------------------+
216 | :const:`dont_write_bytecode` | -B |
217 +------------------------------+------------------------------------------+
218 | :const:`no_site` | -S |
219 +------------------------------+------------------------------------------+
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000220 | :const:`ignore_environment` | -E |
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000221 +------------------------------+------------------------------------------+
222 | :const:`tabcheck` | -t or -tt |
223 +------------------------------+------------------------------------------+
224 | :const:`verbose` | -v |
225 +------------------------------+------------------------------------------+
226 | :const:`unicode` | -U |
227 +------------------------------+------------------------------------------+
228
229 .. versionadded:: 2.6
230
231
Christian Heimes93852662007-12-01 12:22:32 +0000232.. data:: float_info
233
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000234 A structseq holding information about the float type. It contains low level
Christian Heimes93852662007-12-01 12:22:32 +0000235 information about the precision and internal representation. Please study
236 your system's :file:`float.h` for more information.
237
238 +---------------------+--------------------------------------------------+
Christian Heimesd32ed6f2008-01-14 18:49:24 +0000239 | attribute | explanation |
Christian Heimes93852662007-12-01 12:22:32 +0000240 +=====================+==================================================+
241 | :const:`epsilon` | Difference between 1 and the next representable |
242 | | floating point number |
243 +---------------------+--------------------------------------------------+
244 | :const:`dig` | digits (see :file:`float.h`) |
245 +---------------------+--------------------------------------------------+
246 | :const:`mant_dig` | mantissa digits (see :file:`float.h`) |
247 +---------------------+--------------------------------------------------+
248 | :const:`max` | maximum representable finite float |
249 +---------------------+--------------------------------------------------+
250 | :const:`max_exp` | maximum int e such that radix**(e-1) is in the |
251 | | range of finite representable floats |
252 +---------------------+--------------------------------------------------+
253 | :const:`max_10_exp` | maximum int e such that 10**e is in the |
254 | | range of finite representable floats |
255 +---------------------+--------------------------------------------------+
256 | :const:`min` | Minimum positive normalizer float |
257 +---------------------+--------------------------------------------------+
258 | :const:`min_exp` | minimum int e such that radix**(e-1) is a |
259 | | normalized float |
260 +---------------------+--------------------------------------------------+
261 | :const:`min_10_exp` | minimum int e such that 10**e is a normalized |
262 | | float |
263 +---------------------+--------------------------------------------------+
264 | :const:`radix` | radix of exponent |
265 +---------------------+--------------------------------------------------+
266 | :const:`rounds` | addition rounds (see :file:`float.h`) |
267 +---------------------+--------------------------------------------------+
268
269 .. note::
270
271 The information in the table is simplified.
272
273
Georg Brandl116aa622007-08-15 14:28:22 +0000274.. function:: getcheckinterval()
275
276 Return the interpreter's "check interval"; see :func:`setcheckinterval`.
277
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279.. function:: getdefaultencoding()
280
281 Return the name of the current default string encoding used by the Unicode
282 implementation.
283
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285.. function:: getdlopenflags()
286
287 Return the current value of the flags that are used for :cfunc:`dlopen` calls.
288 The flag constants are defined in the :mod:`dl` and :mod:`DLFCN` modules.
289 Availability: Unix.
290
Georg Brandl116aa622007-08-15 14:28:22 +0000291
292.. function:: getfilesystemencoding()
293
294 Return the name of the encoding used to convert Unicode filenames into system
295 file names, or ``None`` if the system default encoding is used. The result value
296 depends on the operating system:
297
298 * On Windows 9x, the encoding is "mbcs".
299
300 * On Mac OS X, the encoding is "utf-8".
301
302 * On Unix, the encoding is the user's preference according to the result of
303 nl_langinfo(CODESET), or :const:`None` if the ``nl_langinfo(CODESET)`` failed.
304
305 * On Windows NT+, file names are Unicode natively, so no conversion is
306 performed. :func:`getfilesystemencoding` still returns ``'mbcs'``, as this is
307 the encoding that applications should use when they explicitly want to convert
308 Unicode strings to byte strings that are equivalent when used as file names.
309
Georg Brandl116aa622007-08-15 14:28:22 +0000310
311.. function:: getrefcount(object)
312
313 Return the reference count of the *object*. The count returned is generally one
314 higher than you might expect, because it includes the (temporary) reference as
315 an argument to :func:`getrefcount`.
316
317
318.. function:: getrecursionlimit()
319
320 Return the current value of the recursion limit, the maximum depth of the Python
321 interpreter stack. This limit prevents infinite recursion from causing an
322 overflow of the C stack and crashing Python. It can be set by
323 :func:`setrecursionlimit`.
324
325
326.. function:: _getframe([depth])
327
328 Return a frame object from the call stack. If optional integer *depth* is
329 given, return the frame object that many calls below the top of the stack. If
330 that is deeper than the call stack, :exc:`ValueError` is raised. The default
331 for *depth* is zero, returning the frame at the top of the call stack.
332
333 This function should be used for internal and specialized purposes only.
334
335
Christian Heimes9bd667a2008-01-20 15:14:11 +0000336.. function:: getprofile()
337
338 .. index::
339 single: profile function
340 single: profiler
341
342 Get the profiler function as set by :func:`setprofile`.
343
344 .. versionadded:: 2.6
345
346
347.. function:: gettrace()
348
349 .. index::
350 single: trace function
351 single: debugger
352
353 Get the trace function as set by :func:`settrace`.
354
355 .. note::
356
357 The :func:`gettrace` function is intended only for implementing debuggers,
358 profilers, coverage tools and the like. Its behavior is part of the
359 implementation platform, rather than part of the language definition,
360 and thus may not be available in all Python implementations.
361
362 .. versionadded:: 2.6
363
364
Georg Brandl116aa622007-08-15 14:28:22 +0000365.. function:: getwindowsversion()
366
367 Return a tuple containing five components, describing the Windows version
368 currently running. The elements are *major*, *minor*, *build*, *platform*, and
369 *text*. *text* contains a string while all other values are integers.
370
371 *platform* may be one of the following values:
372
373 +-----------------------------------------+-----------------------+
374 | Constant | Platform |
375 +=========================================+=======================+
376 | :const:`0 (VER_PLATFORM_WIN32s)` | Win32s on Windows 3.1 |
377 +-----------------------------------------+-----------------------+
378 | :const:`1 (VER_PLATFORM_WIN32_WINDOWS)` | Windows 95/98/ME |
379 +-----------------------------------------+-----------------------+
380 | :const:`2 (VER_PLATFORM_WIN32_NT)` | Windows NT/2000/XP |
381 +-----------------------------------------+-----------------------+
382 | :const:`3 (VER_PLATFORM_WIN32_CE)` | Windows CE |
383 +-----------------------------------------+-----------------------+
384
385 This function wraps the Win32 :cfunc:`GetVersionEx` function; see the Microsoft
386 documentation for more information about these fields.
387
388 Availability: Windows.
389
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391.. data:: hexversion
392
393 The version number encoded as a single integer. This is guaranteed to increase
394 with each version, including proper support for non-production releases. For
395 example, to test that the Python interpreter is at least version 1.5.2, use::
396
397 if sys.hexversion >= 0x010502F0:
398 # use some advanced feature
399 ...
400 else:
401 # use an alternative implementation or warn the user
402 ...
403
404 This is called ``hexversion`` since it only really looks meaningful when viewed
405 as the result of passing it to the built-in :func:`hex` function. The
406 ``version_info`` value may be used for a more human-friendly encoding of the
407 same information.
408
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410.. function:: intern(string)
411
412 Enter *string* in the table of "interned" strings and return the interned string
413 -- which is *string* itself or a copy. Interning strings is useful to gain a
414 little performance on dictionary lookup -- if the keys in a dictionary are
415 interned, and the lookup key is interned, the key comparisons (after hashing)
416 can be done by a pointer compare instead of a string compare. Normally, the
417 names used in Python programs are automatically interned, and the dictionaries
418 used to hold module, class or instance attributes have interned keys.
419
Georg Brandl55ac8f02007-09-01 13:51:09 +0000420 Interned strings are not immortal; you must keep a reference to the return
421 value of :func:`intern` around to benefit from it.
Georg Brandl116aa622007-08-15 14:28:22 +0000422
423
424.. data:: last_type
425 last_value
426 last_traceback
427
428 These three variables are not always defined; they are set when an exception is
429 not handled and the interpreter prints an error message and a stack traceback.
430 Their intended use is to allow an interactive user to import a debugger module
431 and engage in post-mortem debugging without having to re-execute the command
432 that caused the error. (Typical use is ``import pdb; pdb.pm()`` to enter the
433 post-mortem debugger; see chapter :ref:`debugger` for
434 more information.)
435
436 The meaning of the variables is the same as that of the return values from
437 :func:`exc_info` above. (Since there is only one interactive thread,
438 thread-safety is not a concern for these variables, unlike for ``exc_type``
439 etc.)
440
441
Christian Heimesa37d4c62007-12-04 23:02:19 +0000442.. data:: maxsize
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Georg Brandl33770552007-12-15 09:55:35 +0000444 An integer giving the maximum value a variable of type :ctype:`Py_ssize_t` can
445 take. It's usually ``2**31 - 1`` on a 32-bit platform and ``2**63 - 1`` on a
446 64-bit platform.
Christian Heimesa37d4c62007-12-04 23:02:19 +0000447
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449.. data:: maxunicode
450
451 An integer giving the largest supported code point for a Unicode character. The
452 value of this depends on the configuration option that specifies whether Unicode
453 characters are stored as UCS-2 or UCS-4.
454
455
456.. data:: modules
457
458 This is a dictionary that maps module names to modules which have already been
459 loaded. This can be manipulated to force reloading of modules and other tricks.
460
461
462.. data:: path
463
464 .. index:: triple: module; search; path
465
466 A list of strings that specifies the search path for modules. Initialized from
467 the environment variable :envvar:`PYTHONPATH`, plus an installation-dependent
468 default.
469
470 As initialized upon program startup, the first item of this list, ``path[0]``,
471 is the directory containing the script that was used to invoke the Python
472 interpreter. If the script directory is not available (e.g. if the interpreter
473 is invoked interactively or if the script is read from standard input),
474 ``path[0]`` is the empty string, which directs Python to search modules in the
475 current directory first. Notice that the script directory is inserted *before*
476 the entries inserted as a result of :envvar:`PYTHONPATH`.
477
478 A program is free to modify this list for its own purposes.
479
Georg Brandl116aa622007-08-15 14:28:22 +0000480
481.. data:: platform
482
Christian Heimes9bd667a2008-01-20 15:14:11 +0000483 This string contains a platform identifier that can be used to append
484 platform-specific components to :data:`sys.path`, for instance.
485
486 For Unix systems, this is the lowercased OS name as returned by ``uname -s``
487 with the first part of the version as returned by ``uname -r`` appended,
488 e.g. ``'sunos5'`` or ``'linux2'``, *at the time when Python was built*.
489 For other systems, the values are:
490
491 ================ ===========================
492 System :data:`platform` value
493 ================ ===========================
494 Windows ``'win32'``
495 Windows/Cygwin ``'cygwin'``
496 MacOS X ``'darwin'``
497 MacOS 9 ``'mac'``
498 OS/2 ``'os2'``
499 OS/2 EMX ``'os2emx'``
500 RiscOS ``'riscos'``
501 AtheOS ``'atheos'``
502 ================ ===========================
Georg Brandl116aa622007-08-15 14:28:22 +0000503
504
505.. data:: prefix
506
507 A string giving the site-specific directory prefix where the platform
508 independent Python files are installed; by default, this is the string
509 ``'/usr/local'``. This can be set at build time with the :option:`--prefix`
510 argument to the :program:`configure` script. The main collection of Python
511 library modules is installed in the directory ``prefix + '/lib/pythonversion'``
512 while the platform independent header files (all except :file:`pyconfig.h`) are
513 stored in ``prefix + '/include/pythonversion'``, where *version* is equal to
514 ``version[:3]``.
515
516
517.. data:: ps1
518 ps2
519
520 .. index::
521 single: interpreter prompts
522 single: prompts, interpreter
523
524 Strings specifying the primary and secondary prompt of the interpreter. These
525 are only defined if the interpreter is in interactive mode. Their initial
526 values in this case are ``'>>> '`` and ``'... '``. If a non-string object is
527 assigned to either variable, its :func:`str` is re-evaluated each time the
528 interpreter prepares to read a new interactive command; this can be used to
529 implement a dynamic prompt.
530
531
Christian Heimes790c8232008-01-07 21:14:23 +0000532.. data:: dont_write_bytecode
533
534 If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the
535 import of source modules. This value is initially set to ``True`` or ``False``
536 depending on the ``-B`` command line option and the ``PYTHONDONTWRITEBYTECODE``
537 environment variable, but you can set it yourself to control bytecode file
538 generation.
539
540 .. versionadded:: 2.6
541
542
Georg Brandl116aa622007-08-15 14:28:22 +0000543.. function:: setcheckinterval(interval)
544
545 Set the interpreter's "check interval". This integer value determines how often
546 the interpreter checks for periodic things such as thread switches and signal
547 handlers. The default is ``100``, meaning the check is performed every 100
548 Python virtual instructions. Setting it to a larger value may increase
549 performance for programs using threads. Setting it to a value ``<=`` 0 checks
550 every virtual instruction, maximizing responsiveness as well as overhead.
551
552
553.. function:: setdefaultencoding(name)
554
555 Set the current default string encoding used by the Unicode implementation. If
556 *name* does not match any available encoding, :exc:`LookupError` is raised.
557 This function is only intended to be used by the :mod:`site` module
558 implementation and, where needed, by :mod:`sitecustomize`. Once used by the
559 :mod:`site` module, it is removed from the :mod:`sys` module's namespace.
560
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000561 .. Note that :mod:`site` is not imported if the :option:`-S` option is passed
562 to the interpreter, in which case this function will remain available.
Georg Brandl116aa622007-08-15 14:28:22 +0000563
Georg Brandl116aa622007-08-15 14:28:22 +0000564
565.. function:: setdlopenflags(n)
566
567 Set the flags used by the interpreter for :cfunc:`dlopen` calls, such as when
568 the interpreter loads extension modules. Among other things, this will enable a
569 lazy resolving of symbols when importing a module, if called as
570 ``sys.setdlopenflags(0)``. To share symbols across extension modules, call as
571 ``sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)``. Symbolic names for the
572 flag modules can be either found in the :mod:`dl` module, or in the :mod:`DLFCN`
573 module. If :mod:`DLFCN` is not available, it can be generated from
574 :file:`/usr/include/dlfcn.h` using the :program:`h2py` script. Availability:
575 Unix.
576
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578.. function:: setprofile(profilefunc)
579
580 .. index::
581 single: profile function
582 single: profiler
583
584 Set the system's profile function, which allows you to implement a Python source
585 code profiler in Python. See chapter :ref:`profile` for more information on the
586 Python profiler. The system's profile function is called similarly to the
587 system's trace function (see :func:`settrace`), but it isn't called for each
588 executed line of code (only on call and return, but the return event is reported
589 even when an exception has been set). The function is thread-specific, but
590 there is no way for the profiler to know about context switches between threads,
591 so it does not make sense to use this in the presence of multiple threads. Also,
592 its return value is not used, so it can simply return ``None``.
593
594
595.. function:: setrecursionlimit(limit)
596
597 Set the maximum depth of the Python interpreter stack to *limit*. This limit
598 prevents infinite recursion from causing an overflow of the C stack and crashing
599 Python.
600
601 The highest possible limit is platform-dependent. A user may need to set the
602 limit higher when she has a program that requires deep recursion and a platform
603 that supports a higher limit. This should be done with care, because a too-high
604 limit can lead to a crash.
605
606
607.. function:: settrace(tracefunc)
608
609 .. index::
610 single: trace function
611 single: debugger
612
613 Set the system's trace function, which allows you to implement a Python
614 source code debugger in Python. See section :ref:`debugger-hooks` in the
615 chapter on the Python debugger. The function is thread-specific; for a
616 debugger to support multiple threads, it must be registered using
617 :func:`settrace` for each thread being debugged.
618
619 .. note::
620
621 The :func:`settrace` function is intended only for implementing debuggers,
622 profilers, coverage tools and the like. Its behavior is part of the
623 implementation platform, rather than part of the language definition, and thus
624 may not be available in all Python implementations.
625
626
627.. function:: settscdump(on_flag)
628
629 Activate dumping of VM measurements using the Pentium timestamp counter, if
630 *on_flag* is true. Deactivate these dumps if *on_flag* is off. The function is
631 available only if Python was compiled with :option:`--with-tsc`. To understand
632 the output of this dump, read :file:`Python/ceval.c` in the Python sources.
633
Georg Brandl116aa622007-08-15 14:28:22 +0000634
635.. data:: stdin
636 stdout
637 stderr
638
639 File objects corresponding to the interpreter's standard input, output and error
Christian Heimesd8654cf2007-12-02 15:22:16 +0000640 streams. ``stdin`` is used for all interpreter input except for scripts but
641 including calls to :func:`input`. ``stdout`` is used for
642 the output of :func:`print` and :term:`expression` statements and for the
643 prompts of :func:`input`. The interpreter's own prompts
644 and (almost all of) its error messages go to ``stderr``. ``stdout`` and
645 ``stderr`` needn't be built-in file objects: any object is acceptable as long
646 as it has a :meth:`write` method that takes a string argument. (Changing these
647 objects doesn't affect the standard I/O streams of processes executed by
648 :func:`os.popen`, :func:`os.system` or the :func:`exec\*` family of functions in
649 the :mod:`os` module.)
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651
652.. data:: __stdin__
653 __stdout__
654 __stderr__
655
656 These objects contain the original values of ``stdin``, ``stderr`` and
657 ``stdout`` at the start of the program. They are used during finalization, and
658 could be useful to restore the actual files to known working file objects in
659 case they have been overwritten with a broken object.
660
Christian Heimes58cb1b82007-11-13 02:19:40 +0000661 .. note::
662
663 Under some conditions ``stdin``, ``stdout`` and ``stderr`` as well as the
664 original values ``__stdin__``, ``__stdout__`` and ``__stderr__`` can be
665 None. It is usually the case for Windows GUI apps that aren't connected to
666 a console and Python apps started with :program:`pythonw`.
667
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669.. data:: tracebacklimit
670
671 When this variable is set to an integer value, it determines the maximum number
672 of levels of traceback information printed when an unhandled exception occurs.
673 The default is ``1000``. When set to ``0`` or less, all traceback information
674 is suppressed and only the exception type and value are printed.
675
676
677.. data:: version
678
679 A string containing the version number of the Python interpreter plus additional
680 information on the build number and compiler used. It has a value of the form
681 ``'version (#build_number, build_date, build_time) [compiler]'``. The first
682 three characters are used to identify the version in the installation
683 directories (where appropriate on each platform). An example::
684
685 >>> import sys
686 >>> sys.version
687 '1.5.2 (#0 Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]'
688
689
690.. data:: api_version
691
692 The C API version for this interpreter. Programmers may find this useful when
693 debugging version conflicts between Python and extension modules.
694
Georg Brandl116aa622007-08-15 14:28:22 +0000695
696.. data:: version_info
697
698 A tuple containing the five components of the version number: *major*, *minor*,
699 *micro*, *releaselevel*, and *serial*. All values except *releaselevel* are
700 integers; the release level is ``'alpha'``, ``'beta'``, ``'candidate'``, or
701 ``'final'``. The ``version_info`` value corresponding to the Python version 2.0
702 is ``(2, 0, 0, 'final', 0)``.
703
Georg Brandl116aa622007-08-15 14:28:22 +0000704
705.. data:: warnoptions
706
707 This is an implementation detail of the warnings framework; do not modify this
708 value. Refer to the :mod:`warnings` module for more information on the warnings
709 framework.
710
711
712.. data:: winver
713
714 The version number used to form registry keys on Windows platforms. This is
715 stored as string resource 1000 in the Python DLL. The value is normally the
716 first three characters of :const:`version`. It is provided in the :mod:`sys`
717 module for informational purposes; modifying this value has no effect on the
718 registry keys used by Python. Availability: Windows.
719
720
721.. seealso::
722
723 Module :mod:`site`
724 This describes how to use .pth files to extend ``sys.path``.
725
Christian Heimes58cb1b82007-11-13 02:19:40 +0000726