blob: 9760dca2df1e6149910f1a7f878ff34b8495cf05 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _os:
4
5Operating System Utilities
6==========================
7
8
Georg Brandl60203b42010-10-06 10:11:56 +00009.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename)
Georg Brandl54a3faa2008-01-20 09:30:57 +000010
11 Return true (nonzero) if the standard I/O file *fp* with name *filename* is
12 deemed interactive. This is the case for files for which ``isatty(fileno(fp))``
Georg Brandl60203b42010-10-06 10:11:56 +000013 is true. If the global flag :c:data:`Py_InteractiveFlag` is true, this function
Georg Brandl54a3faa2008-01-20 09:30:57 +000014 also returns true if the *filename* pointer is *NULL* or if the name is equal to
15 one of the strings ``'<stdin>'`` or ``'???'``.
16
17
Georg Brandl60203b42010-10-06 10:11:56 +000018.. c:function:: void PyOS_AfterFork()
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
20 Function to update some internal state after a process fork; this should be
21 called in the new process if the Python interpreter will continue to be used.
22 If a new executable is loaded into the new process, this function does not need
23 to be called.
24
25
Georg Brandl60203b42010-10-06 10:11:56 +000026.. c:function:: int PyOS_CheckStack()
Georg Brandl54a3faa2008-01-20 09:30:57 +000027
28 Return true when the interpreter runs out of stack space. This is a reliable
29 check, but is only available when :const:`USE_STACKCHECK` is defined (currently
30 on Windows using the Microsoft Visual C++ compiler). :const:`USE_STACKCHECK`
31 will be defined automatically; you should never change the definition in your
32 own code.
33
34
Georg Brandl60203b42010-10-06 10:11:56 +000035.. c:function:: PyOS_sighandler_t PyOS_getsig(int i)
Georg Brandl54a3faa2008-01-20 09:30:57 +000036
37 Return the current signal handler for signal *i*. This is a thin wrapper around
Georg Brandl60203b42010-10-06 10:11:56 +000038 either :c:func:`sigaction` or :c:func:`signal`. Do not call those functions
39 directly! :c:type:`PyOS_sighandler_t` is a typedef alias for :c:type:`void
Georg Brandl54a3faa2008-01-20 09:30:57 +000040 (\*)(int)`.
41
42
Georg Brandl60203b42010-10-06 10:11:56 +000043.. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
Georg Brandl54a3faa2008-01-20 09:30:57 +000044
45 Set the signal handler for signal *i* to be *h*; return the old signal handler.
Georg Brandl60203b42010-10-06 10:11:56 +000046 This is a thin wrapper around either :c:func:`sigaction` or :c:func:`signal`. Do
47 not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef
48 alias for :c:type:`void (\*)(int)`.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50.. _systemfunctions:
51
52System Functions
53================
54
55These are utility functions that make functionality from the :mod:`sys` module
56accessible to C code. They all work with the current interpreter thread's
57:mod:`sys` module's dict, which is contained in the internal thread state structure.
58
Georg Brandl60203b42010-10-06 10:11:56 +000059.. c:function:: PyObject *PySys_GetObject(char *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +000060
61 Return the object *name* from the :mod:`sys` module or *NULL* if it does
62 not exist, without setting an exception.
63
Georg Brandl60203b42010-10-06 10:11:56 +000064.. c:function:: int PySys_SetObject(char *name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +000065
66 Set *name* in the :mod:`sys` module to *v* unless *v* is *NULL*, in which
67 case *name* is deleted from the sys module. Returns ``0`` on success, ``-1``
68 on error.
69
Georg Brandl60203b42010-10-06 10:11:56 +000070.. c:function:: void PySys_ResetWarnOptions()
Georg Brandl54a3faa2008-01-20 09:30:57 +000071
72 Reset :data:`sys.warnoptions` to an empty list.
73
Georg Brandl60203b42010-10-06 10:11:56 +000074.. c:function:: void PySys_AddWarnOption(wchar_t *s)
Georg Brandl54a3faa2008-01-20 09:30:57 +000075
76 Append *s* to :data:`sys.warnoptions`.
77
Georg Brandl60203b42010-10-06 10:11:56 +000078.. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
Victor Stinner9ca9c252010-05-19 16:53:30 +000079
80 Append *unicode* to :data:`sys.warnoptions`.
81
Georg Brandl60203b42010-10-06 10:11:56 +000082.. c:function:: void PySys_SetPath(wchar_t *path)
Georg Brandl54a3faa2008-01-20 09:30:57 +000083
84 Set :data:`sys.path` to a list object of paths found in *path* which should
85 be a list of paths separated with the platform's search path delimiter
86 (``:`` on Unix, ``;`` on Windows).
87
Georg Brandl60203b42010-10-06 10:11:56 +000088.. c:function:: void PySys_WriteStdout(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +000089
90 Write the output string described by *format* to :data:`sys.stdout`. No
91 exceptions are raised, even if truncation occurs (see below).
92
93 *format* should limit the total size of the formatted output string to
94 1000 bytes or less -- after 1000 bytes, the output string is truncated.
95 In particular, this means that no unrestricted "%s" formats should occur;
96 these should be limited using "%.<N>s" where <N> is a decimal number
97 calculated so that <N> plus the maximum size of other formatted text does not
98 exceed 1000 bytes. Also watch out for "%f", which can print hundreds of
99 digits for very large numbers.
100
101 If a problem occurs, or :data:`sys.stdout` is unset, the formatted message
102 is written to the real (C level) *stdout*.
103
Georg Brandl60203b42010-10-06 10:11:56 +0000104.. c:function:: void PySys_WriteStderr(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000105
Georg Brandl60203b42010-10-06 10:11:56 +0000106 As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000107 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000108
Georg Brandl60203b42010-10-06 10:11:56 +0000109.. c:function:: void PySys_FormatStdout(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000110
111 Function similar to PySys_WriteStdout() but format the message using
Georg Brandl60203b42010-10-06 10:11:56 +0000112 :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an
Victor Stinner79766632010-08-16 17:36:42 +0000113 arbitrary length.
114
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000115 .. versionadded:: 3.2
116
Georg Brandl60203b42010-10-06 10:11:56 +0000117.. c:function:: void PySys_FormatStderr(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000118
Georg Brandl60203b42010-10-06 10:11:56 +0000119 As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000120 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000121
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000122 .. versionadded:: 3.2
123
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000124.. c:function:: void PySys_AddXOption(const wchar_t *s)
125
126 Parse *s* as a set of :option:`-X` options and add them to the current
127 options mapping as returned by :c:func:`PySys_GetXOptions`.
128
129 .. versionadded:: 3.2
130
131.. c:function:: PyObject *PySys_GetXOptions()
132
133 Return the current dictionary of :option:`-X` options, similarly to
134 :data:`sys._xoptions`. On error, *NULL* is returned and an exception is
135 set.
136
137 .. versionadded:: 3.2
138
Georg Brandl67b21b72010-08-17 15:07:14 +0000139
Georg Brandl54a3faa2008-01-20 09:30:57 +0000140.. _processcontrol:
141
142Process Control
143===============
144
145
Georg Brandl60203b42010-10-06 10:11:56 +0000146.. c:function:: void Py_FatalError(const char *message)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000147
148 .. index:: single: abort()
149
150 Print a fatal error message and kill the process. No cleanup is performed.
151 This function should only be invoked when a condition is detected that would
152 make it dangerous to continue using the Python interpreter; e.g., when the
153 object administration appears to be corrupted. On Unix, the standard C library
Georg Brandl60203b42010-10-06 10:11:56 +0000154 function :c:func:`abort` is called which will attempt to produce a :file:`core`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000155 file.
156
157
Georg Brandl60203b42010-10-06 10:11:56 +0000158.. c:function:: void Py_Exit(int status)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000159
160 .. index::
161 single: Py_Finalize()
162 single: exit()
163
Georg Brandl60203b42010-10-06 10:11:56 +0000164 Exit the current process. This calls :c:func:`Py_Finalize` and then calls the
Georg Brandl54a3faa2008-01-20 09:30:57 +0000165 standard C library function ``exit(status)``.
166
167
Georg Brandl60203b42010-10-06 10:11:56 +0000168.. c:function:: int Py_AtExit(void (*func) ())
Georg Brandl54a3faa2008-01-20 09:30:57 +0000169
170 .. index::
171 single: Py_Finalize()
172 single: cleanup functions
173
Georg Brandl60203b42010-10-06 10:11:56 +0000174 Register a cleanup function to be called by :c:func:`Py_Finalize`. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000175 function will be called with no arguments and should return no value. At most
176 32 cleanup functions can be registered. When the registration is successful,
Georg Brandl60203b42010-10-06 10:11:56 +0000177 :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000178 function registered last is called first. Each cleanup function will be called
179 at most once. Since Python's internal finalization will have completed before
180 the cleanup function, no Python APIs should be called by *func*.