blob: 7c88251523fe862e47ccc328b8f3b6be0a9098b5 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
32 .. versionadded:: 2.0
33
34
35.. data:: subversion
36
37 A triple (repo, branch, version) representing the Subversion information of the
38 Python interpreter. *repo* is the name of the repository, ``'CPython'``.
39 *branch* is a string of one of the forms ``'trunk'``, ``'branches/name'`` or
40 ``'tags/name'``. *version* is the output of ``svnversion``, if the interpreter
41 was built from a Subversion checkout; it contains the revision number (range)
42 and possibly a trailing 'M' if there were local modifications. If the tree was
43 exported (or svnversion was not available), it is the revision of
44 ``Include/patchlevel.h`` if the branch is a tag. Otherwise, it is ``None``.
45
46 .. versionadded:: 2.5
47
48
49.. data:: builtin_module_names
50
51 A tuple of strings giving the names of all modules that are compiled into this
52 Python interpreter. (This information is not available in any other way ---
53 ``modules.keys()`` only lists the imported modules.)
54
55
56.. data:: copyright
57
58 A string containing the copyright pertaining to the Python interpreter.
59
60
Christian Heimes908caac2008-01-27 23:34:59 +000061.. function:: _cleartypecache()
62
63 Clear the internal type lookup cache.
64
65 .. versionadded:: 2.6
66
67
Georg Brandl8ec7f652007-08-15 14:28:01 +000068.. function:: _current_frames()
69
70 Return a dictionary mapping each thread's identifier to the topmost stack frame
71 currently active in that thread at the time the function is called. Note that
72 functions in the :mod:`traceback` module can build the call stack given such a
73 frame.
74
75 This is most useful for debugging deadlock: this function does not require the
76 deadlocked threads' cooperation, and such threads' call stacks are frozen for as
77 long as they remain deadlocked. The frame returned for a non-deadlocked thread
78 may bear no relationship to that thread's current activity by the time calling
79 code examines the frame.
80
81 This function should be used for internal and specialized purposes only.
82
83 .. versionadded:: 2.5
84
85
86.. data:: dllhandle
87
88 Integer specifying the handle of the Python DLL. Availability: Windows.
89
90
91.. function:: displayhook(value)
92
93 If *value* is not ``None``, this function prints it to ``sys.stdout``, and saves
94 it in ``__builtin__._``.
95
Georg Brandl584265b2007-12-02 14:58:50 +000096 ``sys.displayhook`` is called on the result of evaluating an :term:`expression`
97 entered in an interactive Python session. The display of these values can be
98 customized by assigning another one-argument function to ``sys.displayhook``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
100
101.. function:: excepthook(type, value, traceback)
102
103 This function prints out a given traceback and exception to ``sys.stderr``.
104
105 When an exception is raised and uncaught, the interpreter calls
106 ``sys.excepthook`` with three arguments, the exception class, exception
107 instance, and a traceback object. In an interactive session this happens just
108 before control is returned to the prompt; in a Python program this happens just
109 before the program exits. The handling of such top-level exceptions can be
110 customized by assigning another three-argument function to ``sys.excepthook``.
111
112
113.. data:: __displayhook__
114 __excepthook__
115
116 These objects contain the original values of ``displayhook`` and ``excepthook``
117 at the start of the program. They are saved so that ``displayhook`` and
118 ``excepthook`` can be restored in case they happen to get replaced with broken
119 objects.
120
121
122.. function:: exc_info()
123
124 This function returns a tuple of three values that give information about the
125 exception that is currently being handled. The information returned is specific
126 both to the current thread and to the current stack frame. If the current stack
127 frame is not handling an exception, the information is taken from the calling
128 stack frame, or its caller, and so on until a stack frame is found that is
129 handling an exception. Here, "handling an exception" is defined as "executing
130 or having executed an except clause." For any stack frame, only information
131 about the most recently handled exception is accessible.
132
133 .. index:: object: traceback
134
135 If no exception is being handled anywhere on the stack, a tuple containing three
136 ``None`` values is returned. Otherwise, the values returned are ``(type, value,
137 traceback)``. Their meaning is: *type* gets the exception type of the exception
138 being handled (a class object); *value* gets the exception parameter (its
139 :dfn:`associated value` or the second argument to :keyword:`raise`, which is
140 always a class instance if the exception type is a class object); *traceback*
141 gets a traceback object (see the Reference Manual) which encapsulates the call
142 stack at the point where the exception originally occurred.
143
144 If :func:`exc_clear` is called, this function will return three ``None`` values
145 until either another exception is raised in the current thread or the execution
146 stack returns to a frame where another exception is being handled.
147
148 .. warning::
149
150 Assigning the *traceback* return value to a local variable in a function that is
151 handling an exception will cause a circular reference. This will prevent
152 anything referenced by a local variable in the same function or by the traceback
153 from being garbage collected. Since most functions don't need access to the
154 traceback, the best solution is to use something like ``exctype, value =
155 sys.exc_info()[:2]`` to extract only the exception type and value. If you do
156 need the traceback, make sure to delete it after use (best done with a
157 :keyword:`try` ... :keyword:`finally` statement) or to call :func:`exc_info` in
158 a function that does not itself handle an exception.
159
160 .. note::
161
162 Beginning with Python 2.2, such cycles are automatically reclaimed when garbage
163 collection is enabled and they become unreachable, but it remains more efficient
164 to avoid creating cycles.
165
166
167.. function:: exc_clear()
168
169 This function clears all information relating to the current or last exception
170 that occurred in the current thread. After calling this function,
171 :func:`exc_info` will return three ``None`` values until another exception is
172 raised in the current thread or the execution stack returns to a frame where
173 another exception is being handled.
174
175 This function is only needed in only a few obscure situations. These include
176 logging and error handling systems that report information on the last or
177 current exception. This function can also be used to try to free resources and
178 trigger object finalization, though no guarantee is made as to what objects will
179 be freed, if any.
180
181 .. versionadded:: 2.3
182
183
184.. data:: exc_type
185 exc_value
186 exc_traceback
187
188 .. deprecated:: 1.5
189 Use :func:`exc_info` instead.
190
191 Since they are global variables, they are not specific to the current thread, so
192 their use is not safe in a multi-threaded program. When no exception is being
193 handled, ``exc_type`` is set to ``None`` and the other two are undefined.
194
195
196.. data:: exec_prefix
197
198 A string giving the site-specific directory prefix where the platform-dependent
199 Python files are installed; by default, this is also ``'/usr/local'``. This can
200 be set at build time with the :option:`--exec-prefix` argument to the
201 :program:`configure` script. Specifically, all configuration files (e.g. the
202 :file:`pyconfig.h` header file) are installed in the directory ``exec_prefix +
203 '/lib/pythonversion/config'``, and shared library modules are installed in
204 ``exec_prefix + '/lib/pythonversion/lib-dynload'``, where *version* is equal to
205 ``version[:3]``.
206
207
208.. data:: executable
209
210 A string giving the name of the executable binary for the Python interpreter, on
211 systems where this makes sense.
212
213
214.. function:: exit([arg])
215
216 Exit from Python. This is implemented by raising the :exc:`SystemExit`
217 exception, so cleanup actions specified by finally clauses of :keyword:`try`
218 statements are honored, and it is possible to intercept the exit attempt at an
219 outer level. The optional argument *arg* can be an integer giving the exit
220 status (defaulting to zero), or another type of object. If it is an integer,
221 zero is considered "successful termination" and any nonzero value is considered
222 "abnormal termination" by shells and the like. Most systems require it to be in
223 the range 0-127, and produce undefined results otherwise. Some systems have a
224 convention for assigning specific meanings to specific exit codes, but these are
225 generally underdeveloped; Unix programs generally use 2 for command line syntax
226 errors and 1 for all other kind of errors. If another type of object is passed,
227 ``None`` is equivalent to passing zero, and any other object is printed to
228 ``sys.stderr`` and results in an exit code of 1. In particular,
229 ``sys.exit("some error message")`` is a quick way to exit a program when an
230 error occurs.
231
232
233.. data:: exitfunc
234
235 This value is not actually defined by the module, but can be set by the user (or
236 by a program) to specify a clean-up action at program exit. When set, it should
237 be a parameterless function. This function will be called when the interpreter
238 exits. Only one function may be installed in this way; to allow multiple
239 functions which will be called at termination, use the :mod:`atexit` module.
240
241 .. note::
242
243 The exit function is not called when the program is killed by a signal, when a
244 Python fatal internal error is detected, or when ``os._exit()`` is called.
245
246 .. deprecated:: 2.4
247 Use :mod:`atexit` instead.
248
249
Christian Heimesf31b69f2008-01-14 03:42:48 +0000250.. data:: flags
251
252 The struct sequence *flags* exposes the status of command line flags. The
253 attributes are read only.
254
255 +------------------------------+------------------------------------------+
256 | attribute | flag |
257 +==============================+==========================================+
258 | :const:`debug` | -d |
259 +------------------------------+------------------------------------------+
260 | :const:`py3k_warning` | -3 |
261 +------------------------------+------------------------------------------+
262 | :const:`division_warning` | -Q |
263 +------------------------------+------------------------------------------+
264 | :const:`division_new` | -Qnew |
265 +------------------------------+------------------------------------------+
266 | :const:`inspect` | -i |
267 +------------------------------+------------------------------------------+
268 | :const:`interactive` | -i |
269 +------------------------------+------------------------------------------+
270 | :const:`optimize` | -O or -OO |
271 +------------------------------+------------------------------------------+
272 | :const:`dont_write_bytecode` | -B |
273 +------------------------------+------------------------------------------+
274 | :const:`no_site` | -S |
275 +------------------------------+------------------------------------------+
Andrew M. Kuchling7ce9b182008-01-15 01:29:16 +0000276 | :const:`ignore_environment` | -E |
Christian Heimesf31b69f2008-01-14 03:42:48 +0000277 +------------------------------+------------------------------------------+
278 | :const:`tabcheck` | -t or -tt |
279 +------------------------------+------------------------------------------+
280 | :const:`verbose` | -v |
281 +------------------------------+------------------------------------------+
282 | :const:`unicode` | -U |
283 +------------------------------+------------------------------------------+
284
285 .. versionadded:: 2.6
286
287
Christian Heimesdfdfaab2007-12-01 11:20:10 +0000288.. data:: float_info
289
Christian Heimesc94e2b52008-01-14 04:13:37 +0000290 A structseq holding information about the float type. It contains low level
Christian Heimesdfdfaab2007-12-01 11:20:10 +0000291 information about the precision and internal representation. Please study
292 your system's :file:`float.h` for more information.
293
294 +---------------------+--------------------------------------------------+
Christian Heimesc94e2b52008-01-14 04:13:37 +0000295 | attribute | explanation |
Christian Heimesdfdfaab2007-12-01 11:20:10 +0000296 +=====================+==================================================+
297 | :const:`epsilon` | Difference between 1 and the next representable |
298 | | floating point number |
299 +---------------------+--------------------------------------------------+
300 | :const:`dig` | digits (see :file:`float.h`) |
301 +---------------------+--------------------------------------------------+
302 | :const:`mant_dig` | mantissa digits (see :file:`float.h`) |
303 +---------------------+--------------------------------------------------+
304 | :const:`max` | maximum representable finite float |
305 +---------------------+--------------------------------------------------+
306 | :const:`max_exp` | maximum int e such that radix**(e-1) is in the |
307 | | range of finite representable floats |
308 +---------------------+--------------------------------------------------+
309 | :const:`max_10_exp` | maximum int e such that 10**e is in the |
310 | | range of finite representable floats |
311 +---------------------+--------------------------------------------------+
312 | :const:`min` | Minimum positive normalizer float |
313 +---------------------+--------------------------------------------------+
314 | :const:`min_exp` | minimum int e such that radix**(e-1) is a |
315 | | normalized float |
316 +---------------------+--------------------------------------------------+
317 | :const:`min_10_exp` | minimum int e such that 10**e is a normalized |
318 | | float |
319 +---------------------+--------------------------------------------------+
320 | :const:`radix` | radix of exponent |
321 +---------------------+--------------------------------------------------+
322 | :const:`rounds` | addition rounds (see :file:`float.h`) |
323 +---------------------+--------------------------------------------------+
324
325 .. note::
326
327 The information in the table is simplified.
328
Christian Heimes3e76d932007-12-01 15:40:22 +0000329 .. versionadded:: 2.6
330
Christian Heimesdfdfaab2007-12-01 11:20:10 +0000331
Georg Brandl8ec7f652007-08-15 14:28:01 +0000332.. function:: getcheckinterval()
333
334 Return the interpreter's "check interval"; see :func:`setcheckinterval`.
335
336 .. versionadded:: 2.3
337
338
339.. function:: getdefaultencoding()
340
341 Return the name of the current default string encoding used by the Unicode
342 implementation.
343
344 .. versionadded:: 2.0
345
346
347.. function:: getdlopenflags()
348
349 Return the current value of the flags that are used for :cfunc:`dlopen` calls.
350 The flag constants are defined in the :mod:`dl` and :mod:`DLFCN` modules.
351 Availability: Unix.
352
353 .. versionadded:: 2.2
354
355
356.. function:: getfilesystemencoding()
357
358 Return the name of the encoding used to convert Unicode filenames into system
359 file names, or ``None`` if the system default encoding is used. The result value
360 depends on the operating system:
361
362 * On Windows 9x, the encoding is "mbcs".
363
364 * On Mac OS X, the encoding is "utf-8".
365
366 * On Unix, the encoding is the user's preference according to the result of
367 nl_langinfo(CODESET), or :const:`None` if the ``nl_langinfo(CODESET)`` failed.
368
369 * On Windows NT+, file names are Unicode natively, so no conversion is
370 performed. :func:`getfilesystemencoding` still returns ``'mbcs'``, as this is
371 the encoding that applications should use when they explicitly want to convert
372 Unicode strings to byte strings that are equivalent when used as file names.
373
374 .. versionadded:: 2.3
375
376
377.. function:: getrefcount(object)
378
379 Return the reference count of the *object*. The count returned is generally one
380 higher than you might expect, because it includes the (temporary) reference as
381 an argument to :func:`getrefcount`.
382
383
384.. function:: getrecursionlimit()
385
386 Return the current value of the recursion limit, the maximum depth of the Python
387 interpreter stack. This limit prevents infinite recursion from causing an
388 overflow of the C stack and crashing Python. It can be set by
389 :func:`setrecursionlimit`.
390
391
392.. function:: _getframe([depth])
393
394 Return a frame object from the call stack. If optional integer *depth* is
395 given, return the frame object that many calls below the top of the stack. If
396 that is deeper than the call stack, :exc:`ValueError` is raised. The default
397 for *depth* is zero, returning the frame at the top of the call stack.
398
399 This function should be used for internal and specialized purposes only.
400
401
Georg Brandl56112892008-01-20 13:59:46 +0000402.. function:: getprofile()
403
404 .. index::
405 single: profile function
406 single: profiler
407
408 Get the profiler function as set by :func:`setprofile`.
409
410 .. versionadded:: 2.6
411
412
413.. function:: gettrace()
414
415 .. index::
416 single: trace function
417 single: debugger
418
419 Get the trace function as set by :func:`settrace`.
420
421 .. note::
422
423 The :func:`gettrace` function is intended only for implementing debuggers,
424 profilers, coverage tools and the like. Its behavior is part of the
425 implementation platform, rather than part of the language definition,
426 and thus may not be available in all Python implementations.
427
428 .. versionadded:: 2.6
429
430
Georg Brandl8ec7f652007-08-15 14:28:01 +0000431.. function:: getwindowsversion()
432
433 Return a tuple containing five components, describing the Windows version
434 currently running. The elements are *major*, *minor*, *build*, *platform*, and
435 *text*. *text* contains a string while all other values are integers.
436
437 *platform* may be one of the following values:
438
439 +-----------------------------------------+-----------------------+
440 | Constant | Platform |
441 +=========================================+=======================+
442 | :const:`0 (VER_PLATFORM_WIN32s)` | Win32s on Windows 3.1 |
443 +-----------------------------------------+-----------------------+
444 | :const:`1 (VER_PLATFORM_WIN32_WINDOWS)` | Windows 95/98/ME |
445 +-----------------------------------------+-----------------------+
446 | :const:`2 (VER_PLATFORM_WIN32_NT)` | Windows NT/2000/XP |
447 +-----------------------------------------+-----------------------+
448 | :const:`3 (VER_PLATFORM_WIN32_CE)` | Windows CE |
449 +-----------------------------------------+-----------------------+
450
451 This function wraps the Win32 :cfunc:`GetVersionEx` function; see the Microsoft
452 documentation for more information about these fields.
453
454 Availability: Windows.
455
456 .. versionadded:: 2.3
457
458
459.. data:: hexversion
460
461 The version number encoded as a single integer. This is guaranteed to increase
462 with each version, including proper support for non-production releases. For
463 example, to test that the Python interpreter is at least version 1.5.2, use::
464
465 if sys.hexversion >= 0x010502F0:
466 # use some advanced feature
467 ...
468 else:
469 # use an alternative implementation or warn the user
470 ...
471
472 This is called ``hexversion`` since it only really looks meaningful when viewed
473 as the result of passing it to the built-in :func:`hex` function. The
474 ``version_info`` value may be used for a more human-friendly encoding of the
475 same information.
476
477 .. versionadded:: 1.5.2
478
479
480.. data:: last_type
481 last_value
482 last_traceback
483
484 These three variables are not always defined; they are set when an exception is
485 not handled and the interpreter prints an error message and a stack traceback.
486 Their intended use is to allow an interactive user to import a debugger module
487 and engage in post-mortem debugging without having to re-execute the command
488 that caused the error. (Typical use is ``import pdb; pdb.pm()`` to enter the
489 post-mortem debugger; see chapter :ref:`debugger` for
490 more information.)
491
492 The meaning of the variables is the same as that of the return values from
493 :func:`exc_info` above. (Since there is only one interactive thread,
494 thread-safety is not a concern for these variables, unlike for ``exc_type``
495 etc.)
496
497
498.. data:: maxint
499
500 The largest positive integer supported by Python's regular integer type. This
501 is at least 2\*\*31-1. The largest negative integer is ``-maxint-1`` --- the
502 asymmetry results from the use of 2's complement binary arithmetic.
503
504
505.. data:: maxunicode
506
507 An integer giving the largest supported code point for a Unicode character. The
508 value of this depends on the configuration option that specifies whether Unicode
509 characters are stored as UCS-2 or UCS-4.
510
511
512.. data:: modules
513
514 .. index:: builtin: reload
515
516 This is a dictionary that maps module names to modules which have already been
517 loaded. This can be manipulated to force reloading of modules and other tricks.
518 Note that removing a module from this dictionary is *not* the same as calling
519 :func:`reload` on the corresponding module object.
520
521
522.. data:: path
523
524 .. index:: triple: module; search; path
525
526 A list of strings that specifies the search path for modules. Initialized from
527 the environment variable :envvar:`PYTHONPATH`, plus an installation-dependent
528 default.
529
530 As initialized upon program startup, the first item of this list, ``path[0]``,
531 is the directory containing the script that was used to invoke the Python
532 interpreter. If the script directory is not available (e.g. if the interpreter
533 is invoked interactively or if the script is read from standard input),
534 ``path[0]`` is the empty string, which directs Python to search modules in the
535 current directory first. Notice that the script directory is inserted *before*
536 the entries inserted as a result of :envvar:`PYTHONPATH`.
537
538 A program is free to modify this list for its own purposes.
539
540 .. versionchanged:: 2.3
541 Unicode strings are no longer ignored.
542
543
544.. data:: platform
545
Georg Brandl440f2ff2008-01-20 12:57:47 +0000546 This string contains a platform identifier that can be used to append
547 platform-specific components to :data:`sys.path`, for instance.
548
549 For Unix systems, this is the lowercased OS name as returned by ``uname -s``
550 with the first part of the version as returned by ``uname -r`` appended,
551 e.g. ``'sunos5'`` or ``'linux2'``, *at the time when Python was built*.
552 For other systems, the values are:
553
554 ================ ===========================
555 System :data:`platform` value
556 ================ ===========================
557 Windows ``'win32'``
558 Windows/Cygwin ``'cygwin'``
559 MacOS X ``'darwin'``
560 MacOS 9 ``'mac'``
561 OS/2 ``'os2'``
562 OS/2 EMX ``'os2emx'``
563 RiscOS ``'riscos'``
564 AtheOS ``'atheos'``
565 ================ ===========================
Georg Brandl8ec7f652007-08-15 14:28:01 +0000566
567
568.. data:: prefix
569
570 A string giving the site-specific directory prefix where the platform
571 independent Python files are installed; by default, this is the string
572 ``'/usr/local'``. This can be set at build time with the :option:`--prefix`
573 argument to the :program:`configure` script. The main collection of Python
574 library modules is installed in the directory ``prefix + '/lib/pythonversion'``
575 while the platform independent header files (all except :file:`pyconfig.h`) are
576 stored in ``prefix + '/include/pythonversion'``, where *version* is equal to
577 ``version[:3]``.
578
579
580.. data:: ps1
581 ps2
582
583 .. index::
584 single: interpreter prompts
585 single: prompts, interpreter
586
587 Strings specifying the primary and secondary prompt of the interpreter. These
588 are only defined if the interpreter is in interactive mode. Their initial
589 values in this case are ``'>>> '`` and ``'... '``. If a non-string object is
590 assigned to either variable, its :func:`str` is re-evaluated each time the
591 interpreter prepares to read a new interactive command; this can be used to
592 implement a dynamic prompt.
593
594
Christian Heimesd7b33372007-11-28 08:02:36 +0000595.. data:: py3kwarning
596
597 Bool containing the status of the Python 3.0 warning flag. It's ``True``
598 when Python is started with the -3 option.
599
600
Georg Brandl2da0fce2008-01-07 17:09:35 +0000601.. data:: dont_write_bytecode
602
603 If this is true, Python won't try to write ``.pyc`` or ``.pyo`` files on the
604 import of source modules. This value is initially set to ``True`` or ``False``
605 depending on the ``-B`` command line option and the ``PYTHONDONTWRITEBYTECODE``
606 environment variable, but you can set it yourself to control bytecode file
607 generation.
608
609 .. versionadded:: 2.6
610
611
Georg Brandl8ec7f652007-08-15 14:28:01 +0000612.. function:: setcheckinterval(interval)
613
614 Set the interpreter's "check interval". This integer value determines how often
615 the interpreter checks for periodic things such as thread switches and signal
616 handlers. The default is ``100``, meaning the check is performed every 100
617 Python virtual instructions. Setting it to a larger value may increase
618 performance for programs using threads. Setting it to a value ``<=`` 0 checks
619 every virtual instruction, maximizing responsiveness as well as overhead.
620
621
622.. function:: setdefaultencoding(name)
623
624 Set the current default string encoding used by the Unicode implementation. If
625 *name* does not match any available encoding, :exc:`LookupError` is raised.
626 This function is only intended to be used by the :mod:`site` module
627 implementation and, where needed, by :mod:`sitecustomize`. Once used by the
628 :mod:`site` module, it is removed from the :mod:`sys` module's namespace.
629
Georg Brandlb19be572007-12-29 10:57:00 +0000630 .. Note that :mod:`site` is not imported if the :option:`-S` option is passed
631 to the interpreter, in which case this function will remain available.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000632
633 .. versionadded:: 2.0
634
635
636.. function:: setdlopenflags(n)
637
638 Set the flags used by the interpreter for :cfunc:`dlopen` calls, such as when
639 the interpreter loads extension modules. Among other things, this will enable a
640 lazy resolving of symbols when importing a module, if called as
641 ``sys.setdlopenflags(0)``. To share symbols across extension modules, call as
642 ``sys.setdlopenflags(dl.RTLD_NOW | dl.RTLD_GLOBAL)``. Symbolic names for the
643 flag modules can be either found in the :mod:`dl` module, or in the :mod:`DLFCN`
644 module. If :mod:`DLFCN` is not available, it can be generated from
645 :file:`/usr/include/dlfcn.h` using the :program:`h2py` script. Availability:
646 Unix.
647
648 .. versionadded:: 2.2
649
650
651.. function:: setprofile(profilefunc)
652
653 .. index::
654 single: profile function
655 single: profiler
656
657 Set the system's profile function, which allows you to implement a Python source
658 code profiler in Python. See chapter :ref:`profile` for more information on the
659 Python profiler. The system's profile function is called similarly to the
660 system's trace function (see :func:`settrace`), but it isn't called for each
661 executed line of code (only on call and return, but the return event is reported
662 even when an exception has been set). The function is thread-specific, but
663 there is no way for the profiler to know about context switches between threads,
664 so it does not make sense to use this in the presence of multiple threads. Also,
665 its return value is not used, so it can simply return ``None``.
666
667
668.. function:: setrecursionlimit(limit)
669
670 Set the maximum depth of the Python interpreter stack to *limit*. This limit
671 prevents infinite recursion from causing an overflow of the C stack and crashing
672 Python.
673
674 The highest possible limit is platform-dependent. A user may need to set the
675 limit higher when she has a program that requires deep recursion and a platform
676 that supports a higher limit. This should be done with care, because a too-high
677 limit can lead to a crash.
678
679
680.. function:: settrace(tracefunc)
681
682 .. index::
683 single: trace function
684 single: debugger
685
686 Set the system's trace function, which allows you to implement a Python
687 source code debugger in Python. See section :ref:`debugger-hooks` in the
688 chapter on the Python debugger. The function is thread-specific; for a
689 debugger to support multiple threads, it must be registered using
690 :func:`settrace` for each thread being debugged.
691
692 .. note::
693
694 The :func:`settrace` function is intended only for implementing debuggers,
695 profilers, coverage tools and the like. Its behavior is part of the
696 implementation platform, rather than part of the language definition, and thus
697 may not be available in all Python implementations.
698
699
700.. function:: settscdump(on_flag)
701
702 Activate dumping of VM measurements using the Pentium timestamp counter, if
703 *on_flag* is true. Deactivate these dumps if *on_flag* is off. The function is
704 available only if Python was compiled with :option:`--with-tsc`. To understand
705 the output of this dump, read :file:`Python/ceval.c` in the Python sources.
706
707 .. versionadded:: 2.4
708
709
710.. data:: stdin
711 stdout
712 stderr
713
714 .. index::
715 builtin: input
716 builtin: raw_input
717
718 File objects corresponding to the interpreter's standard input, output and error
719 streams. ``stdin`` is used for all interpreter input except for scripts but
720 including calls to :func:`input` and :func:`raw_input`. ``stdout`` is used for
Georg Brandl584265b2007-12-02 14:58:50 +0000721 the output of :keyword:`print` and :term:`expression` statements and for the
722 prompts of :func:`input` and :func:`raw_input`. The interpreter's own prompts
723 and (almost all of) its error messages go to ``stderr``. ``stdout`` and
724 ``stderr`` needn't be built-in file objects: any object is acceptable as long
725 as it has a :meth:`write` method that takes a string argument. (Changing these
726 objects doesn't affect the standard I/O streams of processes executed by
Georg Brandl8ec7f652007-08-15 14:28:01 +0000727 :func:`os.popen`, :func:`os.system` or the :func:`exec\*` family of functions in
728 the :mod:`os` module.)
729
730
731.. data:: __stdin__
732 __stdout__
733 __stderr__
734
735 These objects contain the original values of ``stdin``, ``stderr`` and
736 ``stdout`` at the start of the program. They are used during finalization, and
737 could be useful to restore the actual files to known working file objects in
738 case they have been overwritten with a broken object.
739
740
741.. data:: tracebacklimit
742
743 When this variable is set to an integer value, it determines the maximum number
744 of levels of traceback information printed when an unhandled exception occurs.
745 The default is ``1000``. When set to ``0`` or less, all traceback information
746 is suppressed and only the exception type and value are printed.
747
748
749.. data:: version
750
751 A string containing the version number of the Python interpreter plus additional
752 information on the build number and compiler used. It has a value of the form
753 ``'version (#build_number, build_date, build_time) [compiler]'``. The first
754 three characters are used to identify the version in the installation
755 directories (where appropriate on each platform). An example::
756
757 >>> import sys
758 >>> sys.version
759 '1.5.2 (#0 Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)]'
760
761
762.. data:: api_version
763
764 The C API version for this interpreter. Programmers may find this useful when
765 debugging version conflicts between Python and extension modules.
766
767 .. versionadded:: 2.3
768
769
770.. data:: version_info
771
772 A tuple containing the five components of the version number: *major*, *minor*,
773 *micro*, *releaselevel*, and *serial*. All values except *releaselevel* are
774 integers; the release level is ``'alpha'``, ``'beta'``, ``'candidate'``, or
775 ``'final'``. The ``version_info`` value corresponding to the Python version 2.0
776 is ``(2, 0, 0, 'final', 0)``.
777
778 .. versionadded:: 2.0
779
780
781.. data:: warnoptions
782
783 This is an implementation detail of the warnings framework; do not modify this
784 value. Refer to the :mod:`warnings` module for more information on the warnings
785 framework.
786
787
788.. data:: winver
789
790 The version number used to form registry keys on Windows platforms. This is
791 stored as string resource 1000 in the Python DLL. The value is normally the
792 first three characters of :const:`version`. It is provided in the :mod:`sys`
793 module for informational purposes; modifying this value has no effect on the
794 registry keys used by Python. Availability: Windows.
795
796
797.. seealso::
798
799 Module :mod:`site`
800 This describes how to use .pth files to extend ``sys.path``.
801