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