17db96d56Sopenharmony_ci.. highlight:: c
27db96d56Sopenharmony_ci
37db96d56Sopenharmony_ci.. _os:
47db96d56Sopenharmony_ci
57db96d56Sopenharmony_ciOperating System Utilities
67db96d56Sopenharmony_ci==========================
77db96d56Sopenharmony_ci
87db96d56Sopenharmony_ci.. c:function:: PyObject* PyOS_FSPath(PyObject *path)
97db96d56Sopenharmony_ci
107db96d56Sopenharmony_ci   Return the file system representation for *path*. If the object is a
117db96d56Sopenharmony_ci   :class:`str` or :class:`bytes` object, then its reference count is
127db96d56Sopenharmony_ci   incremented. If the object implements the :class:`os.PathLike` interface,
137db96d56Sopenharmony_ci   then :meth:`~os.PathLike.__fspath__` is returned as long as it is a
147db96d56Sopenharmony_ci   :class:`str` or :class:`bytes` object. Otherwise :exc:`TypeError` is raised
157db96d56Sopenharmony_ci   and ``NULL`` is returned.
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ci   .. versionadded:: 3.6
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename)
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci   Return true (nonzero) if the standard I/O file *fp* with name *filename* is
237db96d56Sopenharmony_ci   deemed interactive.  This is the case for files for which ``isatty(fileno(fp))``
247db96d56Sopenharmony_ci   is true.  If the global flag :c:data:`Py_InteractiveFlag` is true, this function
257db96d56Sopenharmony_ci   also returns true if the *filename* pointer is ``NULL`` or if the name is equal to
267db96d56Sopenharmony_ci   one of the strings ``'<stdin>'`` or ``'???'``.
277db96d56Sopenharmony_ci
287db96d56Sopenharmony_ci
297db96d56Sopenharmony_ci.. c:function:: void PyOS_BeforeFork()
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci   Function to prepare some internal state before a process fork.  This
327db96d56Sopenharmony_ci   should be called before calling :c:func:`fork` or any similar function
337db96d56Sopenharmony_ci   that clones the current process.
347db96d56Sopenharmony_ci   Only available on systems where :c:func:`fork` is defined.
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ci   .. warning::
377db96d56Sopenharmony_ci      The C :c:func:`fork` call should only be made from the
387db96d56Sopenharmony_ci      :ref:`"main" thread <fork-and-threads>` (of the
397db96d56Sopenharmony_ci      :ref:`"main" interpreter <sub-interpreter-support>`).  The same is
407db96d56Sopenharmony_ci      true for ``PyOS_BeforeFork()``.
417db96d56Sopenharmony_ci
427db96d56Sopenharmony_ci   .. versionadded:: 3.7
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci
457db96d56Sopenharmony_ci.. c:function:: void PyOS_AfterFork_Parent()
467db96d56Sopenharmony_ci
477db96d56Sopenharmony_ci   Function to update some internal state after a process fork.  This
487db96d56Sopenharmony_ci   should be called from the parent process after calling :c:func:`fork`
497db96d56Sopenharmony_ci   or any similar function that clones the current process, regardless
507db96d56Sopenharmony_ci   of whether process cloning was successful.
517db96d56Sopenharmony_ci   Only available on systems where :c:func:`fork` is defined.
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci   .. warning::
547db96d56Sopenharmony_ci      The C :c:func:`fork` call should only be made from the
557db96d56Sopenharmony_ci      :ref:`"main" thread <fork-and-threads>` (of the
567db96d56Sopenharmony_ci      :ref:`"main" interpreter <sub-interpreter-support>`).  The same is
577db96d56Sopenharmony_ci      true for ``PyOS_AfterFork_Parent()``.
587db96d56Sopenharmony_ci
597db96d56Sopenharmony_ci   .. versionadded:: 3.7
607db96d56Sopenharmony_ci
617db96d56Sopenharmony_ci
627db96d56Sopenharmony_ci.. c:function:: void PyOS_AfterFork_Child()
637db96d56Sopenharmony_ci
647db96d56Sopenharmony_ci   Function to update internal interpreter state after a process fork.
657db96d56Sopenharmony_ci   This must be called from the child process after calling :c:func:`fork`,
667db96d56Sopenharmony_ci   or any similar function that clones the current process, if there is
677db96d56Sopenharmony_ci   any chance the process will call back into the Python interpreter.
687db96d56Sopenharmony_ci   Only available on systems where :c:func:`fork` is defined.
697db96d56Sopenharmony_ci
707db96d56Sopenharmony_ci   .. warning::
717db96d56Sopenharmony_ci      The C :c:func:`fork` call should only be made from the
727db96d56Sopenharmony_ci      :ref:`"main" thread <fork-and-threads>` (of the
737db96d56Sopenharmony_ci      :ref:`"main" interpreter <sub-interpreter-support>`).  The same is
747db96d56Sopenharmony_ci      true for ``PyOS_AfterFork_Child()``.
757db96d56Sopenharmony_ci
767db96d56Sopenharmony_ci   .. versionadded:: 3.7
777db96d56Sopenharmony_ci
787db96d56Sopenharmony_ci   .. seealso::
797db96d56Sopenharmony_ci      :func:`os.register_at_fork` allows registering custom Python functions
807db96d56Sopenharmony_ci      to be called by :c:func:`PyOS_BeforeFork()`,
817db96d56Sopenharmony_ci      :c:func:`PyOS_AfterFork_Parent` and  :c:func:`PyOS_AfterFork_Child`.
827db96d56Sopenharmony_ci
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci.. c:function:: void PyOS_AfterFork()
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci   Function to update some internal state after a process fork; this should be
877db96d56Sopenharmony_ci   called in the new process if the Python interpreter will continue to be used.
887db96d56Sopenharmony_ci   If a new executable is loaded into the new process, this function does not need
897db96d56Sopenharmony_ci   to be called.
907db96d56Sopenharmony_ci
917db96d56Sopenharmony_ci   .. deprecated:: 3.7
927db96d56Sopenharmony_ci      This function is superseded by :c:func:`PyOS_AfterFork_Child()`.
937db96d56Sopenharmony_ci
947db96d56Sopenharmony_ci
957db96d56Sopenharmony_ci.. c:function:: int PyOS_CheckStack()
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci   Return true when the interpreter runs out of stack space.  This is a reliable
987db96d56Sopenharmony_ci   check, but is only available when :const:`USE_STACKCHECK` is defined (currently
997db96d56Sopenharmony_ci   on certain versions of Windows using the Microsoft Visual C++ compiler).
1007db96d56Sopenharmony_ci   :const:`USE_STACKCHECK` will be defined automatically; you should never
1017db96d56Sopenharmony_ci   change the definition in your own code.
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci
1047db96d56Sopenharmony_ci.. c:function:: PyOS_sighandler_t PyOS_getsig(int i)
1057db96d56Sopenharmony_ci
1067db96d56Sopenharmony_ci   Return the current signal handler for signal *i*.  This is a thin wrapper around
1077db96d56Sopenharmony_ci   either :c:func:`sigaction` or :c:func:`signal`.  Do not call those functions
1087db96d56Sopenharmony_ci   directly! :c:type:`PyOS_sighandler_t` is a typedef alias for :c:expr:`void
1097db96d56Sopenharmony_ci   (\*)(int)`.
1107db96d56Sopenharmony_ci
1117db96d56Sopenharmony_ci
1127db96d56Sopenharmony_ci.. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h)
1137db96d56Sopenharmony_ci
1147db96d56Sopenharmony_ci   Set the signal handler for signal *i* to be *h*; return the old signal handler.
1157db96d56Sopenharmony_ci   This is a thin wrapper around either :c:func:`sigaction` or :c:func:`signal`.  Do
1167db96d56Sopenharmony_ci   not call those functions directly!  :c:type:`PyOS_sighandler_t` is a typedef
1177db96d56Sopenharmony_ci   alias for :c:expr:`void (\*)(int)`.
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci.. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size)
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci   .. warning::
1227db96d56Sopenharmony_ci      This function should not be called directly: use the :c:type:`PyConfig`
1237db96d56Sopenharmony_ci      API with the :c:func:`PyConfig_SetBytesString` function which ensures
1247db96d56Sopenharmony_ci      that :ref:`Python is preinitialized <c-preinit>`.
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci      This function must not be called before :ref:`Python is preinitialized
1277db96d56Sopenharmony_ci      <c-preinit>` and so that the LC_CTYPE locale is properly configured: see
1287db96d56Sopenharmony_ci      the :c:func:`Py_PreInitialize` function.
1297db96d56Sopenharmony_ci
1307db96d56Sopenharmony_ci   Decode a byte string from the :term:`filesystem encoding and error handler`.
1317db96d56Sopenharmony_ci   If the error handler is :ref:`surrogateescape error handler
1327db96d56Sopenharmony_ci   <surrogateescape>`, undecodable bytes are decoded as characters in range
1337db96d56Sopenharmony_ci   U+DC80..U+DCFF; and if a byte sequence can be decoded as a surrogate
1347db96d56Sopenharmony_ci   character, the bytes are escaped using the surrogateescape error handler
1357db96d56Sopenharmony_ci   instead of decoding them.
1367db96d56Sopenharmony_ci
1377db96d56Sopenharmony_ci   Return a pointer to a newly allocated wide character string, use
1387db96d56Sopenharmony_ci   :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write
1397db96d56Sopenharmony_ci   the number of wide characters excluding the null character into ``*size``
1407db96d56Sopenharmony_ci
1417db96d56Sopenharmony_ci   Return ``NULL`` on decoding error or memory allocation error. If *size* is
1427db96d56Sopenharmony_ci   not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to
1437db96d56Sopenharmony_ci   ``(size_t)-2`` on decoding error.
1447db96d56Sopenharmony_ci
1457db96d56Sopenharmony_ci   The :term:`filesystem encoding and error handler` are selected by
1467db96d56Sopenharmony_ci   :c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
1477db96d56Sopenharmony_ci   :c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
1487db96d56Sopenharmony_ci
1497db96d56Sopenharmony_ci   Decoding errors should never happen, unless there is a bug in the C
1507db96d56Sopenharmony_ci   library.
1517db96d56Sopenharmony_ci
1527db96d56Sopenharmony_ci   Use the :c:func:`Py_EncodeLocale` function to encode the character string
1537db96d56Sopenharmony_ci   back to a byte string.
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci   .. seealso::
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci      The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and
1587db96d56Sopenharmony_ci      :c:func:`PyUnicode_DecodeLocaleAndSize` functions.
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci   .. versionadded:: 3.5
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ci   .. versionchanged:: 3.7
1637db96d56Sopenharmony_ci      The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
1647db96d56Sopenharmony_ci      <utf8-mode>`.
1657db96d56Sopenharmony_ci
1667db96d56Sopenharmony_ci   .. versionchanged:: 3.8
1677db96d56Sopenharmony_ci      The function now uses the UTF-8 encoding on Windows if
1687db96d56Sopenharmony_ci      :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero;
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci
1717db96d56Sopenharmony_ci.. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos)
1727db96d56Sopenharmony_ci
1737db96d56Sopenharmony_ci   Encode a wide character string to the :term:`filesystem encoding and error
1747db96d56Sopenharmony_ci   handler`. If the error handler is :ref:`surrogateescape error handler
1757db96d56Sopenharmony_ci   <surrogateescape>`, surrogate characters in the range U+DC80..U+DCFF are
1767db96d56Sopenharmony_ci   converted to bytes 0x80..0xFF.
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci   Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free`
1797db96d56Sopenharmony_ci   to free the memory. Return ``NULL`` on encoding error or memory allocation
1807db96d56Sopenharmony_ci   error.
1817db96d56Sopenharmony_ci
1827db96d56Sopenharmony_ci   If error_pos is not ``NULL``, ``*error_pos`` is set to ``(size_t)-1`` on
1837db96d56Sopenharmony_ci   success,  or set to the index of the invalid character on encoding error.
1847db96d56Sopenharmony_ci
1857db96d56Sopenharmony_ci   The :term:`filesystem encoding and error handler` are selected by
1867db96d56Sopenharmony_ci   :c:func:`PyConfig_Read`: see :c:member:`~PyConfig.filesystem_encoding` and
1877db96d56Sopenharmony_ci   :c:member:`~PyConfig.filesystem_errors` members of :c:type:`PyConfig`.
1887db96d56Sopenharmony_ci
1897db96d56Sopenharmony_ci   Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back
1907db96d56Sopenharmony_ci   to a wide character string.
1917db96d56Sopenharmony_ci
1927db96d56Sopenharmony_ci   .. warning::
1937db96d56Sopenharmony_ci      This function must not be called before :ref:`Python is preinitialized
1947db96d56Sopenharmony_ci      <c-preinit>` and so that the LC_CTYPE locale is properly configured: see
1957db96d56Sopenharmony_ci      the :c:func:`Py_PreInitialize` function.
1967db96d56Sopenharmony_ci
1977db96d56Sopenharmony_ci   .. seealso::
1987db96d56Sopenharmony_ci
1997db96d56Sopenharmony_ci      The :c:func:`PyUnicode_EncodeFSDefault` and
2007db96d56Sopenharmony_ci      :c:func:`PyUnicode_EncodeLocale` functions.
2017db96d56Sopenharmony_ci
2027db96d56Sopenharmony_ci   .. versionadded:: 3.5
2037db96d56Sopenharmony_ci
2047db96d56Sopenharmony_ci   .. versionchanged:: 3.7
2057db96d56Sopenharmony_ci      The function now uses the UTF-8 encoding in the :ref:`Python UTF-8 Mode
2067db96d56Sopenharmony_ci      <utf8-mode>`.
2077db96d56Sopenharmony_ci
2087db96d56Sopenharmony_ci   .. versionchanged:: 3.8
2097db96d56Sopenharmony_ci      The function now uses the UTF-8 encoding on Windows if
2107db96d56Sopenharmony_ci      :c:data:`Py_LegacyWindowsFSEncodingFlag` is zero.
2117db96d56Sopenharmony_ci
2127db96d56Sopenharmony_ci
2137db96d56Sopenharmony_ci.. _systemfunctions:
2147db96d56Sopenharmony_ci
2157db96d56Sopenharmony_ciSystem Functions
2167db96d56Sopenharmony_ci================
2177db96d56Sopenharmony_ci
2187db96d56Sopenharmony_ciThese are utility functions that make functionality from the :mod:`sys` module
2197db96d56Sopenharmony_ciaccessible to C code.  They all work with the current interpreter thread's
2207db96d56Sopenharmony_ci:mod:`sys` module's dict, which is contained in the internal thread state structure.
2217db96d56Sopenharmony_ci
2227db96d56Sopenharmony_ci.. c:function:: PyObject *PySys_GetObject(const char *name)
2237db96d56Sopenharmony_ci
2247db96d56Sopenharmony_ci   Return the object *name* from the :mod:`sys` module or ``NULL`` if it does
2257db96d56Sopenharmony_ci   not exist, without setting an exception.
2267db96d56Sopenharmony_ci
2277db96d56Sopenharmony_ci.. c:function:: int PySys_SetObject(const char *name, PyObject *v)
2287db96d56Sopenharmony_ci
2297db96d56Sopenharmony_ci   Set *name* in the :mod:`sys` module to *v* unless *v* is ``NULL``, in which
2307db96d56Sopenharmony_ci   case *name* is deleted from the sys module. Returns ``0`` on success, ``-1``
2317db96d56Sopenharmony_ci   on error.
2327db96d56Sopenharmony_ci
2337db96d56Sopenharmony_ci.. c:function:: void PySys_ResetWarnOptions()
2347db96d56Sopenharmony_ci
2357db96d56Sopenharmony_ci   Reset :data:`sys.warnoptions` to an empty list. This function may be
2367db96d56Sopenharmony_ci   called prior to :c:func:`Py_Initialize`.
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci.. c:function:: void PySys_AddWarnOption(const wchar_t *s)
2397db96d56Sopenharmony_ci
2407db96d56Sopenharmony_ci   This API is kept for backward compatibility: setting
2417db96d56Sopenharmony_ci   :c:member:`PyConfig.warnoptions` should be used instead, see :ref:`Python
2427db96d56Sopenharmony_ci   Initialization Configuration <init-config>`.
2437db96d56Sopenharmony_ci
2447db96d56Sopenharmony_ci   Append *s* to :data:`sys.warnoptions`. This function must be called prior
2457db96d56Sopenharmony_ci   to :c:func:`Py_Initialize` in order to affect the warnings filter list.
2467db96d56Sopenharmony_ci
2477db96d56Sopenharmony_ci   .. deprecated:: 3.11
2487db96d56Sopenharmony_ci
2497db96d56Sopenharmony_ci.. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode)
2507db96d56Sopenharmony_ci
2517db96d56Sopenharmony_ci   This API is kept for backward compatibility: setting
2527db96d56Sopenharmony_ci   :c:member:`PyConfig.warnoptions` should be used instead, see :ref:`Python
2537db96d56Sopenharmony_ci   Initialization Configuration <init-config>`.
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ci   Append *unicode* to :data:`sys.warnoptions`.
2567db96d56Sopenharmony_ci
2577db96d56Sopenharmony_ci   Note: this function is not currently usable from outside the CPython
2587db96d56Sopenharmony_ci   implementation, as it must be called prior to the implicit import of
2597db96d56Sopenharmony_ci   :mod:`warnings` in :c:func:`Py_Initialize` to be effective, but can't be
2607db96d56Sopenharmony_ci   called until enough of the runtime has been initialized to permit the
2617db96d56Sopenharmony_ci   creation of Unicode objects.
2627db96d56Sopenharmony_ci
2637db96d56Sopenharmony_ci   .. deprecated:: 3.11
2647db96d56Sopenharmony_ci
2657db96d56Sopenharmony_ci.. c:function:: void PySys_SetPath(const wchar_t *path)
2667db96d56Sopenharmony_ci
2677db96d56Sopenharmony_ci   This API is kept for backward compatibility: setting
2687db96d56Sopenharmony_ci   :c:member:`PyConfig.module_search_paths` and
2697db96d56Sopenharmony_ci   :c:member:`PyConfig.module_search_paths_set` should be used instead, see
2707db96d56Sopenharmony_ci   :ref:`Python Initialization Configuration <init-config>`.
2717db96d56Sopenharmony_ci
2727db96d56Sopenharmony_ci   Set :data:`sys.path` to a list object of paths found in *path* which should
2737db96d56Sopenharmony_ci   be a list of paths separated with the platform's search path delimiter
2747db96d56Sopenharmony_ci   (``:`` on Unix, ``;`` on Windows).
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ci   .. deprecated:: 3.11
2777db96d56Sopenharmony_ci
2787db96d56Sopenharmony_ci.. c:function:: void PySys_WriteStdout(const char *format, ...)
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ci   Write the output string described by *format* to :data:`sys.stdout`.  No
2817db96d56Sopenharmony_ci   exceptions are raised, even if truncation occurs (see below).
2827db96d56Sopenharmony_ci
2837db96d56Sopenharmony_ci   *format* should limit the total size of the formatted output string to
2847db96d56Sopenharmony_ci   1000 bytes or less -- after 1000 bytes, the output string is truncated.
2857db96d56Sopenharmony_ci   In particular, this means that no unrestricted "%s" formats should occur;
2867db96d56Sopenharmony_ci   these should be limited using "%.<N>s" where <N> is a decimal number
2877db96d56Sopenharmony_ci   calculated so that <N> plus the maximum size of other formatted text does not
2887db96d56Sopenharmony_ci   exceed 1000 bytes.  Also watch out for "%f", which can print hundreds of
2897db96d56Sopenharmony_ci   digits for very large numbers.
2907db96d56Sopenharmony_ci
2917db96d56Sopenharmony_ci   If a problem occurs, or :data:`sys.stdout` is unset, the formatted message
2927db96d56Sopenharmony_ci   is written to the real (C level) *stdout*.
2937db96d56Sopenharmony_ci
2947db96d56Sopenharmony_ci.. c:function:: void PySys_WriteStderr(const char *format, ...)
2957db96d56Sopenharmony_ci
2967db96d56Sopenharmony_ci   As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr*
2977db96d56Sopenharmony_ci   instead.
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci.. c:function:: void PySys_FormatStdout(const char *format, ...)
3007db96d56Sopenharmony_ci
3017db96d56Sopenharmony_ci   Function similar to PySys_WriteStdout() but format the message using
3027db96d56Sopenharmony_ci   :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an
3037db96d56Sopenharmony_ci   arbitrary length.
3047db96d56Sopenharmony_ci
3057db96d56Sopenharmony_ci   .. versionadded:: 3.2
3067db96d56Sopenharmony_ci
3077db96d56Sopenharmony_ci.. c:function:: void PySys_FormatStderr(const char *format, ...)
3087db96d56Sopenharmony_ci
3097db96d56Sopenharmony_ci   As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr*
3107db96d56Sopenharmony_ci   instead.
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci   .. versionadded:: 3.2
3137db96d56Sopenharmony_ci
3147db96d56Sopenharmony_ci.. c:function:: void PySys_AddXOption(const wchar_t *s)
3157db96d56Sopenharmony_ci
3167db96d56Sopenharmony_ci   This API is kept for backward compatibility: setting
3177db96d56Sopenharmony_ci   :c:member:`PyConfig.xoptions` should be used instead, see :ref:`Python
3187db96d56Sopenharmony_ci   Initialization Configuration <init-config>`.
3197db96d56Sopenharmony_ci
3207db96d56Sopenharmony_ci   Parse *s* as a set of :option:`-X` options and add them to the current
3217db96d56Sopenharmony_ci   options mapping as returned by :c:func:`PySys_GetXOptions`. This function
3227db96d56Sopenharmony_ci   may be called prior to :c:func:`Py_Initialize`.
3237db96d56Sopenharmony_ci
3247db96d56Sopenharmony_ci   .. versionadded:: 3.2
3257db96d56Sopenharmony_ci
3267db96d56Sopenharmony_ci   .. deprecated:: 3.11
3277db96d56Sopenharmony_ci
3287db96d56Sopenharmony_ci.. c:function:: PyObject *PySys_GetXOptions()
3297db96d56Sopenharmony_ci
3307db96d56Sopenharmony_ci   Return the current dictionary of :option:`-X` options, similarly to
3317db96d56Sopenharmony_ci   :data:`sys._xoptions`.  On error, ``NULL`` is returned and an exception is
3327db96d56Sopenharmony_ci   set.
3337db96d56Sopenharmony_ci
3347db96d56Sopenharmony_ci   .. versionadded:: 3.2
3357db96d56Sopenharmony_ci
3367db96d56Sopenharmony_ci
3377db96d56Sopenharmony_ci.. c:function:: int PySys_Audit(const char *event, const char *format, ...)
3387db96d56Sopenharmony_ci
3397db96d56Sopenharmony_ci   Raise an auditing event with any active hooks. Return zero for success
3407db96d56Sopenharmony_ci   and non-zero with an exception set on failure.
3417db96d56Sopenharmony_ci
3427db96d56Sopenharmony_ci   If any hooks have been added, *format* and other arguments will be used
3437db96d56Sopenharmony_ci   to construct a tuple to pass. Apart from ``N``, the same format characters
3447db96d56Sopenharmony_ci   as used in :c:func:`Py_BuildValue` are available. If the built value is not
3457db96d56Sopenharmony_ci   a tuple, it will be added into a single-element tuple. (The ``N`` format
3467db96d56Sopenharmony_ci   option consumes a reference, but since there is no way to know whether
3477db96d56Sopenharmony_ci   arguments to this function will be consumed, using it may cause reference
3487db96d56Sopenharmony_ci   leaks.)
3497db96d56Sopenharmony_ci
3507db96d56Sopenharmony_ci   Note that ``#`` format characters should always be treated as
3517db96d56Sopenharmony_ci   :c:type:`Py_ssize_t`, regardless of whether ``PY_SSIZE_T_CLEAN`` was defined.
3527db96d56Sopenharmony_ci
3537db96d56Sopenharmony_ci   :func:`sys.audit` performs the same function from Python code.
3547db96d56Sopenharmony_ci
3557db96d56Sopenharmony_ci   .. versionadded:: 3.8
3567db96d56Sopenharmony_ci
3577db96d56Sopenharmony_ci   .. versionchanged:: 3.8.2
3587db96d56Sopenharmony_ci
3597db96d56Sopenharmony_ci      Require :c:type:`Py_ssize_t` for ``#`` format characters. Previously, an
3607db96d56Sopenharmony_ci      unavoidable deprecation warning was raised.
3617db96d56Sopenharmony_ci
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ci.. c:function:: int PySys_AddAuditHook(Py_AuditHookFunction hook, void *userData)
3647db96d56Sopenharmony_ci
3657db96d56Sopenharmony_ci   Append the callable *hook* to the list of active auditing hooks.
3667db96d56Sopenharmony_ci   Return zero on success
3677db96d56Sopenharmony_ci   and non-zero on failure. If the runtime has been initialized, also set an
3687db96d56Sopenharmony_ci   error on failure. Hooks added through this API are called for all
3697db96d56Sopenharmony_ci   interpreters created by the runtime.
3707db96d56Sopenharmony_ci
3717db96d56Sopenharmony_ci   The *userData* pointer is passed into the hook function. Since hook
3727db96d56Sopenharmony_ci   functions may be called from different runtimes, this pointer should not
3737db96d56Sopenharmony_ci   refer directly to Python state.
3747db96d56Sopenharmony_ci
3757db96d56Sopenharmony_ci   This function is safe to call before :c:func:`Py_Initialize`. When called
3767db96d56Sopenharmony_ci   after runtime initialization, existing audit hooks are notified and may
3777db96d56Sopenharmony_ci   silently abort the operation by raising an error subclassed from
3787db96d56Sopenharmony_ci   :class:`Exception` (other errors will not be silenced).
3797db96d56Sopenharmony_ci
3807db96d56Sopenharmony_ci   The hook function is of type :c:expr:`int (*)(const char *event, PyObject
3817db96d56Sopenharmony_ci   *args, void *userData)`, where *args* is guaranteed to be a
3827db96d56Sopenharmony_ci   :c:type:`PyTupleObject`. The hook function is always called with the GIL
3837db96d56Sopenharmony_ci   held by the Python interpreter that raised the event.
3847db96d56Sopenharmony_ci
3857db96d56Sopenharmony_ci   See :pep:`578` for a detailed description of auditing.  Functions in the
3867db96d56Sopenharmony_ci   runtime and standard library that raise events are listed in the
3877db96d56Sopenharmony_ci   :ref:`audit events table <audit-events>`.
3887db96d56Sopenharmony_ci   Details are in each function's documentation.
3897db96d56Sopenharmony_ci
3907db96d56Sopenharmony_ci   .. audit-event:: sys.addaudithook "" c.PySys_AddAuditHook
3917db96d56Sopenharmony_ci
3927db96d56Sopenharmony_ci      If the interpreter is initialized, this function raises a auditing event
3937db96d56Sopenharmony_ci      ``sys.addaudithook`` with no arguments. If any existing hooks raise an
3947db96d56Sopenharmony_ci      exception derived from :class:`Exception`, the new hook will not be
3957db96d56Sopenharmony_ci      added and the exception is cleared. As a result, callers cannot assume
3967db96d56Sopenharmony_ci      that their hook has been added unless they control all existing hooks.
3977db96d56Sopenharmony_ci
3987db96d56Sopenharmony_ci   .. versionadded:: 3.8
3997db96d56Sopenharmony_ci
4007db96d56Sopenharmony_ci
4017db96d56Sopenharmony_ci.. _processcontrol:
4027db96d56Sopenharmony_ci
4037db96d56Sopenharmony_ciProcess Control
4047db96d56Sopenharmony_ci===============
4057db96d56Sopenharmony_ci
4067db96d56Sopenharmony_ci
4077db96d56Sopenharmony_ci.. c:function:: void Py_FatalError(const char *message)
4087db96d56Sopenharmony_ci
4097db96d56Sopenharmony_ci   .. index:: single: abort()
4107db96d56Sopenharmony_ci
4117db96d56Sopenharmony_ci   Print a fatal error message and kill the process.  No cleanup is performed.
4127db96d56Sopenharmony_ci   This function should only be invoked when a condition is detected that would
4137db96d56Sopenharmony_ci   make it dangerous to continue using the Python interpreter; e.g., when the
4147db96d56Sopenharmony_ci   object administration appears to be corrupted.  On Unix, the standard C library
4157db96d56Sopenharmony_ci   function :c:func:`abort` is called which will attempt to produce a :file:`core`
4167db96d56Sopenharmony_ci   file.
4177db96d56Sopenharmony_ci
4187db96d56Sopenharmony_ci   The ``Py_FatalError()`` function is replaced with a macro which logs
4197db96d56Sopenharmony_ci   automatically the name of the current function, unless the
4207db96d56Sopenharmony_ci   ``Py_LIMITED_API`` macro is defined.
4217db96d56Sopenharmony_ci
4227db96d56Sopenharmony_ci   .. versionchanged:: 3.9
4237db96d56Sopenharmony_ci      Log the function name automatically.
4247db96d56Sopenharmony_ci
4257db96d56Sopenharmony_ci
4267db96d56Sopenharmony_ci.. c:function:: void Py_Exit(int status)
4277db96d56Sopenharmony_ci
4287db96d56Sopenharmony_ci   .. index::
4297db96d56Sopenharmony_ci      single: Py_FinalizeEx()
4307db96d56Sopenharmony_ci      single: exit()
4317db96d56Sopenharmony_ci
4327db96d56Sopenharmony_ci   Exit the current process.  This calls :c:func:`Py_FinalizeEx` and then calls the
4337db96d56Sopenharmony_ci   standard C library function ``exit(status)``.  If :c:func:`Py_FinalizeEx`
4347db96d56Sopenharmony_ci   indicates an error, the exit status is set to 120.
4357db96d56Sopenharmony_ci
4367db96d56Sopenharmony_ci   .. versionchanged:: 3.6
4377db96d56Sopenharmony_ci      Errors from finalization no longer ignored.
4387db96d56Sopenharmony_ci
4397db96d56Sopenharmony_ci
4407db96d56Sopenharmony_ci.. c:function:: int Py_AtExit(void (*func) ())
4417db96d56Sopenharmony_ci
4427db96d56Sopenharmony_ci   .. index::
4437db96d56Sopenharmony_ci      single: Py_FinalizeEx()
4447db96d56Sopenharmony_ci      single: cleanup functions
4457db96d56Sopenharmony_ci
4467db96d56Sopenharmony_ci   Register a cleanup function to be called by :c:func:`Py_FinalizeEx`.  The cleanup
4477db96d56Sopenharmony_ci   function will be called with no arguments and should return no value.  At most
4487db96d56Sopenharmony_ci   32 cleanup functions can be registered.  When the registration is successful,
4497db96d56Sopenharmony_ci   :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``.  The cleanup
4507db96d56Sopenharmony_ci   function registered last is called first. Each cleanup function will be called
4517db96d56Sopenharmony_ci   at most once.  Since Python's internal finalization will have completed before
4527db96d56Sopenharmony_ci   the cleanup function, no Python APIs should be called by *func*.
453