| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 1 | .. highlightlang:: c | 
 | 2 |  | 
 | 3 | .. _os: | 
 | 4 |  | 
 | 5 | Operating System Utilities | 
 | 6 | ========================== | 
 | 7 |  | 
 | 8 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 9 | .. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 10 |  | 
 | 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 13 |    is true.  If the global flag :c:data:`Py_InteractiveFlag` is true, this function | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 14 |    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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 18 | .. c:function:: void PyOS_AfterFork() | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 |  | 
 | 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 26 | .. c:function:: int PyOS_CheckStack() | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 27 |  | 
 | 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 35 | .. c:function:: PyOS_sighandler_t PyOS_getsig(int i) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 36 |  | 
 | 37 |    Return the current signal handler for signal *i*.  This is a thin wrapper around | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 38 |    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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 40 |    (\*)(int)`. | 
 | 41 |  | 
 | 42 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 43 | .. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 44 |  | 
 | 45 |    Set the signal handler for signal *i* to be *h*; return the old signal handler. | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 46 |    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 Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 49 |  | 
 | 50 | .. _systemfunctions: | 
 | 51 |  | 
 | 52 | System Functions | 
 | 53 | ================ | 
 | 54 |  | 
 | 55 | These are utility functions that make functionality from the :mod:`sys` module | 
 | 56 | accessible 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 59 | .. c:function:: PyObject *PySys_GetObject(char *name) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 60 |  | 
 | 61 |    Return the object *name* from the :mod:`sys` module or *NULL* if it does | 
 | 62 |    not exist, without setting an exception. | 
 | 63 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 64 | .. c:function:: FILE *PySys_GetFile(char *name, FILE *def) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 65 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 66 |    Return the :c:type:`FILE*` associated with the object *name* in the | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 67 |    :mod:`sys` module, or *def* if *name* is not in the module or is not associated | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 68 |    with a :c:type:`FILE*`. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 69 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 70 | .. c:function:: int PySys_SetObject(char *name, PyObject *v) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 71 |  | 
 | 72 |    Set *name* in the :mod:`sys` module to *v* unless *v* is *NULL*, in which | 
 | 73 |    case *name* is deleted from the sys module. Returns ``0`` on success, ``-1`` | 
 | 74 |    on error. | 
 | 75 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: void PySys_ResetWarnOptions() | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 |  | 
 | 78 |    Reset :data:`sys.warnoptions` to an empty list. | 
 | 79 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 80 | .. c:function:: void PySys_AddWarnOption(wchar_t *s) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 81 |  | 
 | 82 |    Append *s* to :data:`sys.warnoptions`. | 
 | 83 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 84 | .. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode) | 
| Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 85 |  | 
 | 86 |    Append *unicode* to :data:`sys.warnoptions`. | 
 | 87 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 88 | .. c:function:: void PySys_SetPath(wchar_t *path) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 89 |  | 
 | 90 |    Set :data:`sys.path` to a list object of paths found in *path* which should | 
 | 91 |    be a list of paths separated with the platform's search path delimiter | 
 | 92 |    (``:`` on Unix, ``;`` on Windows). | 
 | 93 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 94 | .. c:function:: void PySys_WriteStdout(const char *format, ...) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 95 |  | 
 | 96 |    Write the output string described by *format* to :data:`sys.stdout`.  No | 
 | 97 |    exceptions are raised, even if truncation occurs (see below). | 
 | 98 |  | 
 | 99 |    *format* should limit the total size of the formatted output string to | 
 | 100 |    1000 bytes or less -- after 1000 bytes, the output string is truncated. | 
 | 101 |    In particular, this means that no unrestricted "%s" formats should occur; | 
 | 102 |    these should be limited using "%.<N>s" where <N> is a decimal number | 
 | 103 |    calculated so that <N> plus the maximum size of other formatted text does not | 
 | 104 |    exceed 1000 bytes.  Also watch out for "%f", which can print hundreds of | 
 | 105 |    digits for very large numbers. | 
 | 106 |  | 
 | 107 |    If a problem occurs, or :data:`sys.stdout` is unset, the formatted message | 
 | 108 |    is written to the real (C level) *stdout*. | 
 | 109 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 110 | .. c:function:: void PySys_WriteStderr(const char *format, ...) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 111 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 112 |    As :c:func:`PySys_WriteStdout`, but write to :data:`sys.stderr` or *stderr* | 
| Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 113 |    instead. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 114 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 115 | .. c:function:: void PySys_FormatStdout(const char *format, ...) | 
| Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 116 |  | 
 | 117 |    Function similar to PySys_WriteStdout() but format the message using | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 118 |    :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an | 
| Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 119 |    arbitrary length. | 
 | 120 |  | 
| Victor Stinner | ad5b1df | 2010-08-16 18:39:49 +0000 | [diff] [blame] | 121 |    .. versionadded:: 3.2 | 
 | 122 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 123 | .. c:function:: void PySys_FormatStderr(const char *format, ...) | 
| Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 124 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 125 |    As :c:func:`PySys_FormatStdout`, but write to :data:`sys.stderr` or *stderr* | 
| Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 126 |    instead. | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 127 |  | 
| Victor Stinner | ad5b1df | 2010-08-16 18:39:49 +0000 | [diff] [blame] | 128 |    .. versionadded:: 3.2 | 
 | 129 |  | 
| Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 130 | .. c:function:: void PySys_AddXOption(const wchar_t *s) | 
 | 131 |  | 
 | 132 |    Parse *s* as a set of :option:`-X` options and add them to the current | 
 | 133 |    options mapping as returned by :c:func:`PySys_GetXOptions`. | 
 | 134 |  | 
 | 135 |    .. versionadded:: 3.2 | 
 | 136 |  | 
 | 137 | .. c:function:: PyObject *PySys_GetXOptions() | 
 | 138 |  | 
 | 139 |    Return the current dictionary of :option:`-X` options, similarly to | 
 | 140 |    :data:`sys._xoptions`.  On error, *NULL* is returned and an exception is | 
 | 141 |    set. | 
 | 142 |  | 
 | 143 |    .. versionadded:: 3.2 | 
 | 144 |  | 
| Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 145 |  | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 146 | .. _processcontrol: | 
 | 147 |  | 
 | 148 | Process Control | 
 | 149 | =============== | 
 | 150 |  | 
 | 151 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 152 | .. c:function:: void Py_FatalError(const char *message) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 153 |  | 
 | 154 |    .. index:: single: abort() | 
 | 155 |  | 
 | 156 |    Print a fatal error message and kill the process.  No cleanup is performed. | 
 | 157 |    This function should only be invoked when a condition is detected that would | 
 | 158 |    make it dangerous to continue using the Python interpreter; e.g., when the | 
 | 159 |    object administration appears to be corrupted.  On Unix, the standard C library | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 160 |    function :c:func:`abort` is called which will attempt to produce a :file:`core` | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 161 |    file. | 
 | 162 |  | 
 | 163 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 164 | .. c:function:: void Py_Exit(int status) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 165 |  | 
 | 166 |    .. index:: | 
 | 167 |       single: Py_Finalize() | 
 | 168 |       single: exit() | 
 | 169 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 170 |    Exit the current process.  This calls :c:func:`Py_Finalize` and then calls the | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 171 |    standard C library function ``exit(status)``. | 
 | 172 |  | 
 | 173 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 174 | .. c:function:: int Py_AtExit(void (*func) ()) | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 175 |  | 
 | 176 |    .. index:: | 
 | 177 |       single: Py_Finalize() | 
 | 178 |       single: cleanup functions | 
 | 179 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 180 |    Register a cleanup function to be called by :c:func:`Py_Finalize`.  The cleanup | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 181 |    function will be called with no arguments and should return no value.  At most | 
 | 182 |    32 cleanup functions can be registered.  When the registration is successful, | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 183 |    :c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``.  The cleanup | 
| Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 184 |    function registered last is called first. Each cleanup function will be called | 
 | 185 |    at most once.  Since Python's internal finalization will have completed before | 
 | 186 |    the cleanup function, no Python APIs should be called by *func*. |