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