blob: 97717f5fc192303fee1c3a74af9d771dc88d0459 [file] [log] [blame]
Stéphane Wirtelcbb64842019-05-17 11:55:34 +02001.. highlight:: c
Georg Brandl54a3faa2008-01-20 09:30:57 +00002
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
Serhiy Storchaka25fc0882019-10-30 12:03:20 +020025 also returns true if the *filename* pointer is ``NULL`` or if the name is equal to
Georg Brandl54a3faa2008-01-20 09:30:57 +000026 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
Eric Snow73cdb0c2019-11-15 13:28:54 -080036 .. warning::
37 The C :c:func:`fork` call should only be made from the
38 :ref:`"main" thread <fork-and-threads>` (of the
39 :ref:`"main" interpreter <sub-interpreter-support>`). The same is
40 true for ``PyOS_BeforeFork()``.
41
Antoine Pitrou346cbd32017-05-27 17:50:54 +020042 .. versionadded:: 3.7
43
44
45.. c:function:: void PyOS_AfterFork_Parent()
46
47 Function to update some internal state after a process fork. This
48 should be called from the parent process after calling :c:func:`fork`
49 or any similar function that clones the current process, regardless
50 of whether process cloning was successful.
51 Only available on systems where :c:func:`fork` is defined.
52
Eric Snow73cdb0c2019-11-15 13:28:54 -080053 .. warning::
54 The C :c:func:`fork` call should only be made from the
55 :ref:`"main" thread <fork-and-threads>` (of the
56 :ref:`"main" interpreter <sub-interpreter-support>`). The same is
57 true for ``PyOS_AfterFork_Parent()``.
58
Antoine Pitrou346cbd32017-05-27 17:50:54 +020059 .. versionadded:: 3.7
60
61
62.. c:function:: void PyOS_AfterFork_Child()
63
Gregory P. Smith163468a2017-05-29 10:03:41 -070064 Function to update internal interpreter state after a process fork.
65 This must be called from the child process after calling :c:func:`fork`,
66 or any similar function that clones the current process, if there is
67 any chance the process will call back into the Python interpreter.
Antoine Pitrou346cbd32017-05-27 17:50:54 +020068 Only available on systems where :c:func:`fork` is defined.
69
Eric Snow73cdb0c2019-11-15 13:28:54 -080070 .. warning::
71 The C :c:func:`fork` call should only be made from the
72 :ref:`"main" thread <fork-and-threads>` (of the
73 :ref:`"main" interpreter <sub-interpreter-support>`). The same is
74 true for ``PyOS_AfterFork_Child()``.
75
Antoine Pitrou346cbd32017-05-27 17:50:54 +020076 .. versionadded:: 3.7
77
78 .. seealso::
79 :func:`os.register_at_fork` allows registering custom Python functions
80 to be called by :c:func:`PyOS_BeforeFork()`,
81 :c:func:`PyOS_AfterFork_Parent` and :c:func:`PyOS_AfterFork_Child`.
82
83
Georg Brandl60203b42010-10-06 10:11:56 +000084.. c:function:: void PyOS_AfterFork()
Georg Brandl54a3faa2008-01-20 09:30:57 +000085
86 Function to update some internal state after a process fork; this should be
87 called in the new process if the Python interpreter will continue to be used.
88 If a new executable is loaded into the new process, this function does not need
89 to be called.
90
Antoine Pitrou346cbd32017-05-27 17:50:54 +020091 .. deprecated:: 3.7
92 This function is superseded by :c:func:`PyOS_AfterFork_Child()`.
93
Georg Brandl54a3faa2008-01-20 09:30:57 +000094
Georg Brandl60203b42010-10-06 10:11:56 +000095.. c:function:: int PyOS_CheckStack()
Georg Brandl54a3faa2008-01-20 09:30:57 +000096
97 Return true when the interpreter runs out of stack space. This is a reliable
98 check, but is only available when :const:`USE_STACKCHECK` is defined (currently
99 on Windows using the Microsoft Visual C++ compiler). :const:`USE_STACKCHECK`
100 will be defined automatically; you should never change the definition in your
101 own code.
102
103
Georg Brandl60203b42010-10-06 10:11:56 +0000104.. c:function:: PyOS_sighandler_t PyOS_getsig(int i)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000105
106 Return the current signal handler for signal *i*. This is a thin wrapper around
Georg Brandl60203b42010-10-06 10:11:56 +0000107 either :c:func:`sigaction` or :c:func:`signal`. Do not call those functions
108 directly! :c:type:`PyOS_sighandler_t` is a typedef alias for :c:type:`void
Georg Brandl54a3faa2008-01-20 09:30:57 +0000109 (\*)(int)`.
110
111
Georg Brandl60203b42010-10-06 10:11:56 +0000112.. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000113
114 Set the signal handler for signal *i* to be *h*; return the old signal handler.
Georg Brandl60203b42010-10-06 10:11:56 +0000115 This is a thin wrapper around either :c:func:`sigaction` or :c:func:`signal`. Do
116 not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef
117 alias for :c:type:`void (\*)(int)`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000118
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200119.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
120
Victor Stinner4b9aad42020-11-02 16:49:54 +0100121 .. warning::
122 This function should not be called directly: use the :c:type:`PyConfig`
123 API with the :c:func:`PyConfig_SetBytesString` function which ensures
124 that :ref:`Python is preinitialized <c-preinit>`.
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200125
Victor Stinner4b9aad42020-11-02 16:49:54 +0100126 This function must not be called before :ref:`Python is preinitialized
127 <c-preinit>` and so that the LC_CTYPE locale is properly configured: see
128 the :c:func:`Py_PreInitialize` function.
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100129
Victor Stinner4b9aad42020-11-02 16:49:54 +0100130 Decode a byte string from the :term:`filesystem encoding and error handler`.
131 If the error handler is :ref:`surrogateescape error handler
132 <surrogateescape>`, undecodable bytes are decoded as characters in range
133 U+DC80..U+DCFF; and if a byte sequence can be decoded as a surrogate
134 character, the bytes are escaped using the surrogateescape error handler
135 instead of decoding them.
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100136
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200137 Return a pointer to a newly allocated wide character string, use
138 :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
139 the number of wide characters excluding the null character into ``*size``
140
141 Return ``NULL`` on decoding error or memory allocation error. If *size* is
142 not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
143 ``(size_t)-2`` on decoding error.
144
Victor Stinner4b9aad42020-11-02 16:49:54 +0100145 The :term:`filesystem encoding and error handler` are selected by
146 :c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
147 :c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
148
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200149 Decoding errors should never happen, unless there is a bug in the C
150 library.
151
152 Use the :c:func:`Py_EncodeLocale` function to encode the character string
153 back to a byte string.
154
155 .. seealso::
156
157 The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and
158 :c:func:`PyUnicode_DecodeLocaleAndSize` functions.
159
160 .. versionadded:: 3.5
161
Victor Stinner91106cd2017-12-13 12:29:09 +0100162 .. versionchanged:: 3.7
Victor Stinner4b9aad42020-11-02 16:49:54 +0100163 The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
164 <utf8-mode>`.
Victor Stinner91106cd2017-12-13 12:29:09 +0100165
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200166 .. versionchanged:: 3.8
167 The function now uses the UTF-8 encoding on Windows if
168 :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
169
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200170
171.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
172
Victor Stinner4b9aad42020-11-02 16:49:54 +0100173 Encode a wide character string to the :term:`filesystem encoding and error
174 handler`. If the error handler is :ref:`surrogateescape error handler
175 <surrogateescape>`, surrogate characters in the range U+DC80..U+DCFF are
176 converted to bytes 0x80..0xFF.
Victor Stinner7ed7aea2018-01-15 10:45:49 +0100177
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200178 Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
179 to free the memory. Return ``NULL`` on encoding error or memory allocation
180 error
181
Victor Stinner91106cd2017-12-13 12:29:09 +0100182 If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
183 success, or set to the index of the invalid character on encoding error.
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200184
Victor Stinner4b9aad42020-11-02 16:49:54 +0100185 The :term:`filesystem encoding and error handler` are selected by
186 :c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
187 :c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
188
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200189 Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
190 to a wide character string.
191
Victor Stinner4b9aad42020-11-02 16:49:54 +0100192 .. warning::
193 This function must not be called before :ref:`Python is preinitialized
194 <c-preinit>` and so that the LC_CTYPE locale is properly configured: see
195 the :c:func:`Py_PreInitialize` function.
196
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200197 .. seealso::
198
199 The :c:func:`PyUnicode_EncodeFSDefault` and
200 :c:func:`PyUnicode_EncodeLocale` functions.
201
202 .. versionadded:: 3.5
203
Victor Stinner91106cd2017-12-13 12:29:09 +0100204 .. versionchanged:: 3.7
Victor Stinner4b9aad42020-11-02 16:49:54 +0100205 The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
206 <utf8-mode>`.
Victor Stinnerc5989cd2018-08-29 19:32:47 +0200207
208 .. versionchanged:: 3.8
209 The function now uses the UTF-8 encoding on Windows if
210 :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
Victor Stinner91106cd2017-12-13 12:29:09 +0100211
Victor Stinnerf6a271a2014-08-01 12:28:48 +0200212
Georg Brandl54a3faa2008-01-20 09:30:57 +0000213.. _systemfunctions:
214
215System Functions
216================
217
218These are utility functions that make functionality from the :mod:`sys` module
219accessible to C code. They all work with the current interpreter thread's
220:mod:`sys` module's dict, which is contained in the internal thread state structure.
221
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300222.. c:function:: PyObject *PySys_GetObject(const char *name)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000223
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200224 Return the object *name* from the :mod:`sys` module or ``NULL`` if it does
Georg Brandl54a3faa2008-01-20 09:30:57 +0000225 not exist, without setting an exception.
226
Serhiy Storchaka03863d22015-06-21 17:11:21 +0300227.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000228
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200229 Set *name* in the :mod:`sys` module to *v* unless *v* is ``NULL``, in which
Georg Brandl54a3faa2008-01-20 09:30:57 +0000230 case *name* is deleted from the sys module. Returns ``0`` on success, ``-1``
231 on error.
232
Georg Brandl60203b42010-10-06 10:11:56 +0000233.. c:function:: void PySys_ResetWarnOptions()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000234
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000235 Reset :data:`sys.warnoptions` to an empty list. This function may be
236 called prior to :c:func:`Py_Initialize`.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000237
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300238.. c:function:: void PySys_AddWarnOption(const wchar_t *s)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000239
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000240 Append *s* to :data:`sys.warnoptions`. This function must be called prior
241 to :c:func:`Py_Initialize` in order to affect the warnings filter list.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000242
Georg Brandl60203b42010-10-06 10:11:56 +0000243.. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
Victor Stinner9ca9c252010-05-19 16:53:30 +0000244
245 Append *unicode* to :data:`sys.warnoptions`.
246
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000247 Note: this function is not currently usable from outside the CPython
248 implementation, as it must be called prior to the implicit import of
249 :mod:`warnings` in :c:func:`Py_Initialize` to be effective, but can't be
250 called until enough of the runtime has been initialized to permit the
251 creation of Unicode objects.
252
Serhiy Storchaka84b8e922017-03-30 10:01:03 +0300253.. c:function:: void PySys_SetPath(const wchar_t *path)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000254
255 Set :data:`sys.path` to a list object of paths found in *path* which should
256 be a list of paths separated with the platform's search path delimiter
257 (``:`` on Unix, ``;`` on Windows).
258
Georg Brandl60203b42010-10-06 10:11:56 +0000259.. c:function:: void PySys_WriteStdout(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000260
261 Write the output string described by *format* to :data:`sys.stdout`. No
262 exceptions are raised, even if truncation occurs (see below).
263
264 *format* should limit the total size of the formatted output string to
265 1000 bytes or less -- after 1000 bytes, the output string is truncated.
266 In particular, this means that no unrestricted "%s" formats should occur;
267 these should be limited using "%.<N>s" where <N> is a decimal number
268 calculated so that <N> plus the maximum size of other formatted text does not
269 exceed 1000 bytes. Also watch out for "%f", which can print hundreds of
270 digits for very large numbers.
271
272 If a problem occurs, or :data:`sys.stdout` is unset, the formatted message
273 is written to the real (C level) *stdout*.
274
Georg Brandl60203b42010-10-06 10:11:56 +0000275.. c:function:: void PySys_WriteStderr(const char *format, ...)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000276
Georg Brandl60203b42010-10-06 10:11:56 +0000277 As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000278 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000279
Georg Brandl60203b42010-10-06 10:11:56 +0000280.. c:function:: void PySys_FormatStdout(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000281
282 Function similar to PySys_WriteStdout() but format the message using
Georg Brandl60203b42010-10-06 10:11:56 +0000283 :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an
Victor Stinner79766632010-08-16 17:36:42 +0000284 arbitrary length.
285
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000286 .. versionadded:: 3.2
287
Georg Brandl60203b42010-10-06 10:11:56 +0000288.. c:function:: void PySys_FormatStderr(const char *format, ...)
Victor Stinner79766632010-08-16 17:36:42 +0000289
Georg Brandl60203b42010-10-06 10:11:56 +0000290 As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr*
Victor Stinner79766632010-08-16 17:36:42 +0000291 instead.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000292
Victor Stinnerad5b1df2010-08-16 18:39:49 +0000293 .. versionadded:: 3.2
294
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000295.. c:function:: void PySys_AddXOption(const wchar_t *s)
296
297 Parse *s* as a set of :option:`-X` options and add them to the current
Nick Coghlanbc77eff2018-03-25 20:44:30 +1000298 options mapping as returned by :c:func:`PySys_GetXOptions`. This function
299 may be called prior to :c:func:`Py_Initialize`.
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000300
301 .. versionadded:: 3.2
302
303.. c:function:: PyObject *PySys_GetXOptions()
304
305 Return the current dictionary of :option:`-X` options, similarly to
Serhiy Storchaka25fc0882019-10-30 12:03:20 +0200306 :data:`sys._xoptions`. On error, ``NULL`` is returned and an exception is
Antoine Pitrou9583cac2010-10-21 13:42:28 +0000307 set.
308
309 .. versionadded:: 3.2
310
Georg Brandl67b21b72010-08-17 15:07:14 +0000311
Steve Dowerb82e17e2019-05-23 08:45:22 -0700312.. c:function:: int PySys_Audit(const char *event, const char *format, ...)
313
Terry Jan Reedye563a152019-11-26 12:07:48 -0500314 Raise an auditing event with any active hooks. Return zero for success
Steve Dowerb82e17e2019-05-23 08:45:22 -0700315 and non-zero with an exception set on failure.
316
317 If any hooks have been added, *format* and other arguments will be used
318 to construct a tuple to pass. Apart from ``N``, the same format characters
319 as used in :c:func:`Py_BuildValue` are available. If the built value is not
320 a tuple, it will be added into a single-element tuple. (The ``N`` format
321 option consumes a reference, but since there is no way to know whether
322 arguments to this function will be consumed, using it may cause reference
323 leaks.)
324
Steve Dowerb8cbe742019-12-09 11:05:39 -0800325 Note that ``#`` format characters should always be treated as
326 ``Py_ssize_t``, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.
327
Steve Dowerb82e17e2019-05-23 08:45:22 -0700328 :func:`sys.audit` performs the same function from Python code.
329
330 .. versionadded:: 3.8
331
Steve Dowerb8cbe742019-12-09 11:05:39 -0800332 .. versionchanged:: 3.8.2
333
334 Require ``Py_ssize_t`` for ``#`` format characters. Previously, an
335 unavoidable deprecation warning was raised.
336
Steve Dowerb82e17e2019-05-23 08:45:22 -0700337
338.. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
339
Terry Jan Reedye563a152019-11-26 12:07:48 -0500340 Append the callable *hook* to the list of active auditing hooks.
341 Return zero for success
342 and non-zero on failure. If the runtime has been initialized, also set an
Steve Dowerb82e17e2019-05-23 08:45:22 -0700343 error on failure. Hooks added through this API are called for all
344 interpreters created by the runtime.
345
Terry Jan Reedye563a152019-11-26 12:07:48 -0500346 The *userData* pointer is passed into the hook function. Since hook
347 functions may be called from different runtimes, this pointer should not
348 refer directly to Python state.
349
Steve Dowerb82e17e2019-05-23 08:45:22 -0700350 This function is safe to call before :c:func:`Py_Initialize`. When called
351 after runtime initialization, existing audit hooks are notified and may
352 silently abort the operation by raising an error subclassed from
353 :class:`Exception` (other errors will not be silenced).
354
355 The hook function is of type :c:type:`int (*)(const char *event, PyObject
356 *args, void *userData)`, where *args* is guaranteed to be a
357 :c:type:`PyTupleObject`. The hook function is always called with the GIL
358 held by the Python interpreter that raised the event.
359
Terry Jan Reedye563a152019-11-26 12:07:48 -0500360 See :pep:`578` for a detailed description of auditing. Functions in the
361 runtime and standard library that raise events are listed in the
362 :ref:`audit events table <audit-events>`.
363 Details are in each function's documentation.
Steve Dower894e30c2019-10-26 13:02:30 -0700364
365 .. audit-event:: sys.addaudithook "" c.PySys_AddAuditHook
366
367 If the interpreter is initialized, this function raises a auditing event
368 ``sys.addaudithook`` with no arguments. If any existing hooks raise an
369 exception derived from :class:`Exception`, the new hook will not be
370 added and the exception is cleared. As a result, callers cannot assume
371 that their hook has been added unless they control all existing hooks.
Steve Dowerb82e17e2019-05-23 08:45:22 -0700372
373 .. versionadded:: 3.8
374
375
Georg Brandl54a3faa2008-01-20 09:30:57 +0000376.. _processcontrol:
377
378Process Control
379===============
380
381
Georg Brandl60203b42010-10-06 10:11:56 +0000382.. c:function:: void Py_FatalError(const char *message)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000383
384 .. index:: single: abort()
385
386 Print a fatal error message and kill the process. No cleanup is performed.
387 This function should only be invoked when a condition is detected that would
388 make it dangerous to continue using the Python interpreter; e.g., when the
389 object administration appears to be corrupted. On Unix, the standard C library
Georg Brandl60203b42010-10-06 10:11:56 +0000390 function :c:func:`abort` is called which will attempt to produce a :file:`core`
Georg Brandl54a3faa2008-01-20 09:30:57 +0000391 file.
392
Victor Stinner9e5d30c2020-03-07 00:54:20 +0100393 The ``Py_FatalError()`` function is replaced with a macro which logs
394 automatically the name of the current function, unless the
395 ``Py_LIMITED_API`` macro is defined.
396
397 .. versionchanged:: 3.9
398 Log the function name automatically.
399
Georg Brandl54a3faa2008-01-20 09:30:57 +0000400
Georg Brandl60203b42010-10-06 10:11:56 +0000401.. c:function:: void Py_Exit(int status)
Georg Brandl54a3faa2008-01-20 09:30:57 +0000402
403 .. index::
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000404 single: Py_FinalizeEx()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000405 single: exit()
406
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000407 Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the
408 standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx`
409 indicates an error, the exit status is set to 120.
410
411 .. versionchanged:: 3.6
412 Errors from finalization no longer ignored.
Georg Brandl54a3faa2008-01-20 09:30:57 +0000413
414
Georg Brandl60203b42010-10-06 10:11:56 +0000415.. c:function:: int Py_AtExit(void (*func) ())
Georg Brandl54a3faa2008-01-20 09:30:57 +0000416
417 .. index::
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000418 single: Py_FinalizeEx()
Georg Brandl54a3faa2008-01-20 09:30:57 +0000419 single: cleanup functions
420
Martin Panterb4ce1fc2015-11-30 03:18:29 +0000421 Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000422 function will be called with no arguments and should return no value. At most
423 32 cleanup functions can be registered. When the registration is successful,
Georg Brandl60203b42010-10-06 10:11:56 +0000424 :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup
Georg Brandl54a3faa2008-01-20 09:30:57 +0000425 function registered last is called first. Each cleanup function will be called
426 at most once. Since Python's internal finalization will have completed before
427 the cleanup function, no Python APIs should be called by *func*.