blob: c8ea78363dfa51524f542d8f5a0f49ff5c1670b1 [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _os:
4
5Operating System Utilities
6==========================
7
Brett Cannon746102b2016-06-09 16:58:38 -07008.. c:function:: PyObject* PyOS_FSPath(PyObject *path)
9
10 Return the file system representation for *path*. If the object is a
11 :class:`str` or :class:`bytes` object, then its reference count is
12 incremented. If the object implements the :class:`os.PathLike` interface,
Brett Cannonc78ca1e2016-06-24 12:03:43 -070013 then :meth:`~os.PathLike.__fspath__` is returned as long as it is a
14 :class:`str` or :class:`bytes` object. Otherwise :exc:`TypeError` is raised
15 and ``NULL`` is returned.
Brett Cannon746102b2016-06-09 16:58:38 -070016
17 .. versionadded:: 3.6
18
Georg Brandl54a3faa2008-01-20 09:30:57 +000019
Georg Brandl60203b42010-10-06 10:11:56 +000020.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename)
Georg Brandl54a3faa2008-01-20 09:30:57 +000021
22 Return true (nonzero) if the standard I/O file *fp* with name *filename* is
23 deemed interactive. This is the case for files for which ``isatty(fileno(fp))``
Georg Brandl60203b42010-10-06 10:11:56 +000024 is true. If the global flag :c:data:`Py_InteractiveFlag` is true, this function
Georg Brandl54a3faa2008-01-20 09:30:57 +000025 also returns true if the *filename* pointer is *NULL* or if the name is equal to
26 one of the strings ``'<stdin>'`` or ``'???'``.
27
28
Antoine Pitrou346cbd32017-05-27 17:50:54 +020029.. c:function:: void PyOS_BeforeFork()
30
31 Function to prepare some internal state before a process fork. This
32 should be called before calling :c:func:`fork` or any similar function
33 that clones the current process.
34 Only available on systems where :c:func:`fork` is defined.
35
36 .. versionadded:: 3.7
37
38
39.. c:function:: void PyOS_AfterFork_Parent()
40
41 Function to update some internal state after a process fork. This
42 should be called from the parent process after calling :c:func:`fork`
43 or any similar function that clones the current process, regardless
44 of whether process cloning was successful.
45 Only available on systems where :c:func:`fork` is defined.
46
47 .. versionadded:: 3.7
48
49
50.. c:function:: void PyOS_AfterFork_Child()
51
Gregory P. Smith163468a2017-05-29 10:03:41 -070052 Function to update internal interpreter state after a process fork.
53 This must be called from the child process after calling :c:func:`fork`,
54 or any similar function that clones the current process, if there is
55 any chance the process will call back into the Python interpreter.
Antoine Pitrou346cbd32017-05-27 17:50:54 +020056 Only available on systems where :c:func:`fork` is defined.
57
58 .. versionadded:: 3.7
59
60 .. seealso::
61 :func:`os.register_at_fork` allows registering custom Python functions
62 to be called by :c:func:`PyOS_BeforeFork()`,
63 :c:func:`PyOS_AfterFork_Parent` and :c:func:`PyOS_AfterFork_Child`.
64
65
Georg Brandl60203b42010-10-06 10:11:56 +000066.. c:function:: void PyOS_AfterFork()
Georg Brandl54a3faa2008-01-20 09:30:57 +000067
68 Function to update some internal state after a process fork; this should be
69 called in the new process if the Python interpreter will continue to be used.
70 If a new executable is loaded into the new process, this function does not need
71 to be called.
72
Antoine Pitrou346cbd32017-05-27 17:50:54 +020073 .. deprecated:: 3.7
74 This function is superseded by :c:func:`PyOS_AfterFork_Child()`.
75
Georg Brandl54a3faa2008-01-20 09:30:57 +000076
Georg Brandl60203b42010-10-06 10:11:56 +000077.. c:function:: int PyOS_CheckStack()
Georg Brandl54a3faa2008-01-20 09:30:57 +000078
79 Return true when the interpreter runs out of stack space. This is a reliable
80 check, but is only available when :const:`USE_STACKCHECK` is defined (currently
81 on Windows using the Microsoft Visual C++ compiler). :const:`USE_STACKCHECK`
82 will be defined automatically; you should never change the definition in your
83 own code.
84
85
Georg Brandl60203b42010-10-06 10:11:56 +000086.. c:function:: PyOS_sighandler_t PyOS_getsig(int i)
Georg Brandl54a3faa2008-01-20 09:30:57 +000087
88 Return the current signal handler for signal *i*. This is a thin wrapper around
Georg Brandl60203b42010-10-06 10:11:56 +000089 either :c:func:`sigaction` or :c:func:`signal`. Do not call those functions
90 directly! :c:type:`PyOS_sighandler_t` is a typedef alias for :c:type:`void
Georg Brandl54a3faa2008-01-20 09:30:57 +000091 (\*)(int)`.
92
93
Georg Brandl60203b42010-10-06 10:11:56 +000094.. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
Georg Brandl54a3faa2008-01-20 09:30:57 +000095
96 Set the signal handler for signal *i* to be *h*; return the old signal handler.
Georg Brandl60203b42010-10-06 10:11:56 +000097 This is a thin wrapper around either :c:func:`sigaction` or :c:func:`signal`. Do
98 not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef
99 alias for :c:type:`void (\*)(int)`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000100
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200101.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
102
103 Decode a byte string from the locale encoding with the :ref:`surrogateescape
104 error handler <surrogateescape>`: undecodable bytes are decoded as
105 characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a
106 surrogate character, escape the bytes using the surrogateescape error
107 handler instead of decoding them.
108
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100109 Encoding, highest priority to lowest priority:
110
pxinwrf4b0a1c2019-03-04 17:02:06 +0800111 * ``UTF-8`` on macOS, Android, and VxWorks;
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200112 * ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100113 * ``UTF-8`` if the Python UTF-8 mode is enabled;
114 * ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
115 ``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
116 and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
117 ``ISO-8859-1`` encoding.
118 * the current locale encoding.
119
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200120 Return a pointer to a newly allocated wide character string, use
121 :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
122 the number of wide characters excluding the null character into ``*size``
123
124 Return ``NULL`` on decoding error or memory allocation error. If *size* is
125 not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
126 ``(size_t)-2`` on decoding error.
127
128 Decoding errors should never happen, unless there is a bug in the C
129 library.
130
131 Use the :c:func:`Py_EncodeLocale` function to encode the character string
132 back to a byte string.
133
134 .. seealso::
135
136 The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and
137 :c:func:`PyUnicode_DecodeLocaleAndSize` functions.
138
139 .. versionadded:: 3.5
140
Victor Stinner91106cd2017-12-13 12:29:09 +0100141 .. versionchanged:: 3.7
142 The function now uses the UTF-8 encoding in the UTF-8 mode.
143
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200144 .. versionchanged:: 3.8
145 The function now uses the UTF-8 encoding on Windows if
146 :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
147
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200148
149.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
150
151 Encode a wide character string to the locale encoding with the
152 :ref:`surrogateescape error handler <surrogateescape>`: surrogate characters
153 in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF.
154
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100155 Encoding, highest priority to lowest priority:
156
pxinwrf4b0a1c2019-03-04 17:02:06 +0800157 * ``UTF-8`` on macOS, Android, and VxWorks;
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200158 * ``UTF-8`` on Windows if :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100159 * ``UTF-8`` if the Python UTF-8 mode is enabled;
160 * ``ASCII`` if the ``LC_CTYPE`` locale is ``"C"``,
161 ``nl_langinfo(CODESET)`` returns the ``ASCII`` encoding (or an alias),
162 and :c:func:`mbstowcs` and :c:func:`wcstombs` functions uses the
163 ``ISO-8859-1`` encoding.
164 * the current locale encoding.
165
166 The function uses the UTF-8 encoding in the Python UTF-8 mode.
167
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200168 Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
169 to free the memory. Return ``NULL`` on encoding error or memory allocation
170 error
171
Victor Stinner91106cd2017-12-13 12:29:09 +0100172 If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
173 success, or set to the index of the invalid character on encoding error.
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200174
175 Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
176 to a wide character string.
177
178 .. seealso::
179
180 The :c:func:`PyUnicode_EncodeFSDefault` and
181 :c:func:`PyUnicode_EncodeLocale` functions.
182
183 .. versionadded:: 3.5
184
Victor Stinner91106cd2017-12-13 12:29:09 +0100185 .. versionchanged:: 3.7
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200186 The function now uses the UTF-8 encoding in the UTF-8 mode.
187
188 .. versionchanged:: 3.8
189 The function now uses the UTF-8 encoding on Windows if
190 :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
Victor Stinner91106cd2017-12-13 12:29:09 +0100191
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200192
Georg Brandl54a3faa2008-01-20 09:30:57 +0000193.. _systemfunctions:
194
195System Functions
196================
197
198These are utility functions that make functionality from the :mod:`sys` module
199accessible to C code. They all work with the current interpreter thread's
200:mod:`sys` module's dict, which is contained in the internal thread state structure.
201
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300202.. c:function:: PyObject *PySys_GetObject(const char *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000203
204 Return the object *name* from the :mod:`sys` module or *NULL* if it does
205 not exist, without setting an exception.
206
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300207.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000208
209 Set *name* in the :mod:`sys` module to *v* unless *v* is *NULL*, in which
210 case *name* is deleted from the sys module. Returns ``0`` on success, ``-1``
211 on error.
212
Georg Brandl60203b42010-10-06 10:11:56 +0000213.. c:function:: void PySys_ResetWarnOptions()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000214
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000215 Reset :data:`sys.warnoptions` to an empty list. This function may be
216 called prior to :c:func:`Py_Initialize`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000217
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300218.. c:function:: void PySys_AddWarnOption(const wchar_t *s)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000219
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000220 Append *s* to :data:`sys.warnoptions`. This function must be called prior
221 to :c:func:`Py_Initialize` in order to affect the warnings filter list.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000222
Georg Brandl60203b42010-10-06 10:11:56 +0000223.. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
Victor Stinner9ca9c252010-05-19 16:53:30 +0000224
225 Append *unicode* to :data:`sys.warnoptions`.
226
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000227 Note: this function is not currently usable from outside the CPython
228 implementation, as it must be called prior to the implicit import of
229 :mod:`warnings` in :c:func:`Py_Initialize` to be effective, but can't be
230 called until enough of the runtime has been initialized to permit the
231 creation of Unicode objects.
232
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300233.. c:function:: void PySys_SetPath(const wchar_t *path)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000234
235 Set :data:`sys.path` to a list object of paths found in *path* which should
236 be a list of paths separated with the platform's search path delimiter
237 (``:`` on Unix, ``;`` on Windows).
238
Georg Brandl60203b42010-10-06 10:11:56 +0000239.. c:function:: void PySys_WriteStdout(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000240
241 Write the output string described by *format* to :data:`sys.stdout`. No
242 exceptions are raised, even if truncation occurs (see below).
243
244 *format* should limit the total size of the formatted output string to
245 1000 bytes or less -- after 1000 bytes, the output string is truncated.
246 In particular, this means that no unrestricted "%s" formats should occur;
247 these should be limited using "%.<N>s" where <N> is a decimal number
248 calculated so that <N> plus the maximum size of other formatted text does not
249 exceed 1000 bytes. Also watch out for "%f", which can print hundreds of
250 digits for very large numbers.
251
252 If a problem occurs, or :data:`sys.stdout` is unset, the formatted message
253 is written to the real (C level) *stdout*.
254
Georg Brandl60203b42010-10-06 10:11:56 +0000255.. c:function:: void PySys_WriteStderr(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000256
Georg Brandl60203b42010-10-06 10:11:56 +0000257 As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000258 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000259
Georg Brandl60203b42010-10-06 10:11:56 +0000260.. c:function:: void PySys_FormatStdout(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000261
262 Function similar to PySys_WriteStdout() but format the message using
Georg Brandl60203b42010-10-06 10:11:56 +0000263 :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an
Victor Stinner79766632010-08-16 17:36:42 +0000264 arbitrary length.
265
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000266 .. versionadded:: 3.2
267
Georg Brandl60203b42010-10-06 10:11:56 +0000268.. c:function:: void PySys_FormatStderr(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000269
Georg Brandl60203b42010-10-06 10:11:56 +0000270 As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000271 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000272
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000273 .. versionadded:: 3.2
274
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000275.. c:function:: void PySys_AddXOption(const wchar_t *s)
276
277 Parse *s* as a set of :option:`-X` options and add them to the current
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000278 options mapping as returned by :c:func:`PySys_GetXOptions`. This function
279 may be called prior to :c:func:`Py_Initialize`.
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000280
281 .. versionadded:: 3.2
282
283.. c:function:: PyObject *PySys_GetXOptions()
284
285 Return the current dictionary of :option:`-X` options, similarly to
286 :data:`sys._xoptions`. On error, *NULL* is returned and an exception is
287 set.
288
289 .. versionadded:: 3.2
290
Georg Brandl67b21b72010-08-17 15:07:14 +0000291
Georg Brandl54a3faa2008-01-20 09:30:57 +0000292.. _processcontrol:
293
294Process Control
295===============
296
297
Georg Brandl60203b42010-10-06 10:11:56 +0000298.. c:function:: void Py_FatalError(const char *message)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000299
300 .. index:: single: abort()
301
302 Print a fatal error message and kill the process. No cleanup is performed.
303 This function should only be invoked when a condition is detected that would
304 make it dangerous to continue using the Python interpreter; e.g., when the
305 object administration appears to be corrupted. On Unix, the standard C library
Georg Brandl60203b42010-10-06 10:11:56 +0000306 function :c:func:`abort` is called which will attempt to produce a :file:`core`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000307 file.
308
309
Georg Brandl60203b42010-10-06 10:11:56 +0000310.. c:function:: void Py_Exit(int status)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000311
312 .. index::
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000313 single: Py_FinalizeEx()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000314 single: exit()
315
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000316 Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the
317 standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx`
318 indicates an error, the exit status is set to 120.
319
320 .. versionchanged:: 3.6
321 Errors from finalization no longer ignored.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000322
323
Georg Brandl60203b42010-10-06 10:11:56 +0000324.. c:function:: int Py_AtExit(void (*func) ())
Georg Brandl54a3faa2008-01-20 09:30:57 +0000325
326 .. index::
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000327 single: Py_FinalizeEx()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000328 single: cleanup functions
329
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000330 Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000331 function will be called with no arguments and should return no value. At most
332 32 cleanup functions can be registered. When the registration is successful,
Georg Brandl60203b42010-10-06 10:11:56 +0000333 :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000334 function registered last is called first. Each cleanup function will be called
335 at most once. Since Python's internal finalization will have completed before
336 the cleanup function, no Python APIs should be called by *func*.