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 | |
Brett Cannon | 746102b | 2016-06-09 16:58:38 -0700 | [diff] [blame] | 8 | .. 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 Cannon | c78ca1e | 2016-06-24 12:03:43 -0700 | [diff] [blame] | 13 | 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 Cannon | 746102b | 2016-06-09 16:58:38 -0700 | [diff] [blame] | 16 | |
| 17 | .. versionadded:: 3.6 |
| 18 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 19 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 20 | .. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 21 | |
| 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 Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 24 | 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] | 25 | 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 Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame^] | 29 | .. 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 | |
| 52 | Function to update some internal state after a process fork. This |
| 53 | should be called from the child process after calling :c:func:`fork` |
| 54 | or any similar function that clones the current process. |
| 55 | Only available on systems where :c:func:`fork` is defined. |
| 56 | |
| 57 | .. versionadded:: 3.7 |
| 58 | |
| 59 | .. seealso:: |
| 60 | :func:`os.register_at_fork` allows registering custom Python functions |
| 61 | to be called by :c:func:`PyOS_BeforeFork()`, |
| 62 | :c:func:`PyOS_AfterFork_Parent` and :c:func:`PyOS_AfterFork_Child`. |
| 63 | |
| 64 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 65 | .. c:function:: void PyOS_AfterFork() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 66 | |
| 67 | Function to update some internal state after a process fork; this should be |
| 68 | called in the new process if the Python interpreter will continue to be used. |
| 69 | If a new executable is loaded into the new process, this function does not need |
| 70 | to be called. |
| 71 | |
Antoine Pitrou | 346cbd3 | 2017-05-27 17:50:54 +0200 | [diff] [blame^] | 72 | .. deprecated:: 3.7 |
| 73 | This function is superseded by :c:func:`PyOS_AfterFork_Child()`. |
| 74 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 76 | .. c:function:: int PyOS_CheckStack() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 77 | |
| 78 | Return true when the interpreter runs out of stack space. This is a reliable |
| 79 | check, but is only available when :const:`USE_STACKCHECK` is defined (currently |
| 80 | on Windows using the Microsoft Visual C++ compiler). :const:`USE_STACKCHECK` |
| 81 | will be defined automatically; you should never change the definition in your |
| 82 | own code. |
| 83 | |
| 84 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 85 | .. c:function:: PyOS_sighandler_t PyOS_getsig(int i) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 86 | |
| 87 | 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] | 88 | either :c:func:`sigaction` or :c:func:`signal`. Do not call those functions |
| 89 | 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] | 90 | (\*)(int)`. |
| 91 | |
| 92 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 93 | .. 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] | 94 | |
| 95 | 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] | 96 | This is a thin wrapper around either :c:func:`sigaction` or :c:func:`signal`. Do |
| 97 | not call those functions directly! :c:type:`PyOS_sighandler_t` is a typedef |
| 98 | alias for :c:type:`void (\*)(int)`. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 99 | |
Victor Stinner | f6a271a | 2014-08-01 12:28:48 +0200 | [diff] [blame] | 100 | .. c:function:: wchar_t* Py_DecodeLocale(const char* arg, size_t *size) |
| 101 | |
| 102 | Decode a byte string from the locale encoding with the :ref:`surrogateescape |
| 103 | error handler <surrogateescape>`: undecodable bytes are decoded as |
| 104 | characters in range U+DC80..U+DCFF. If a byte sequence can be decoded as a |
| 105 | surrogate character, escape the bytes using the surrogateescape error |
| 106 | handler instead of decoding them. |
| 107 | |
| 108 | Return a pointer to a newly allocated wide character string, use |
| 109 | :c:func:`PyMem_RawFree` to free the memory. If size is not ``NULL``, write |
| 110 | the number of wide characters excluding the null character into ``*size`` |
| 111 | |
| 112 | Return ``NULL`` on decoding error or memory allocation error. If *size* is |
| 113 | not ``NULL``, ``*size`` is set to ``(size_t)-1`` on memory error or set to |
| 114 | ``(size_t)-2`` on decoding error. |
| 115 | |
| 116 | Decoding errors should never happen, unless there is a bug in the C |
| 117 | library. |
| 118 | |
| 119 | Use the :c:func:`Py_EncodeLocale` function to encode the character string |
| 120 | back to a byte string. |
| 121 | |
| 122 | .. seealso:: |
| 123 | |
| 124 | The :c:func:`PyUnicode_DecodeFSDefaultAndSize` and |
| 125 | :c:func:`PyUnicode_DecodeLocaleAndSize` functions. |
| 126 | |
| 127 | .. versionadded:: 3.5 |
| 128 | |
| 129 | |
| 130 | .. c:function:: char* Py_EncodeLocale(const wchar_t *text, size_t *error_pos) |
| 131 | |
| 132 | Encode a wide character string to the locale encoding with the |
| 133 | :ref:`surrogateescape error handler <surrogateescape>`: surrogate characters |
| 134 | in the range U+DC80..U+DCFF are converted to bytes 0x80..0xFF. |
| 135 | |
| 136 | Return a pointer to a newly allocated byte string, use :c:func:`PyMem_Free` |
| 137 | to free the memory. Return ``NULL`` on encoding error or memory allocation |
| 138 | error |
| 139 | |
| 140 | If error_pos is not ``NULL``, ``*error_pos`` is set to the index of the |
| 141 | invalid character on encoding error, or set to ``(size_t)-1`` otherwise. |
| 142 | |
| 143 | Use the :c:func:`Py_DecodeLocale` function to decode the bytes string back |
| 144 | to a wide character string. |
| 145 | |
| 146 | .. seealso:: |
| 147 | |
| 148 | The :c:func:`PyUnicode_EncodeFSDefault` and |
| 149 | :c:func:`PyUnicode_EncodeLocale` functions. |
| 150 | |
| 151 | .. versionadded:: 3.5 |
| 152 | |
| 153 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 154 | .. _systemfunctions: |
| 155 | |
| 156 | System Functions |
| 157 | ================ |
| 158 | |
| 159 | These are utility functions that make functionality from the :mod:`sys` module |
| 160 | accessible to C code. They all work with the current interpreter thread's |
| 161 | :mod:`sys` module's dict, which is contained in the internal thread state structure. |
| 162 | |
Serhiy Storchaka | 03863d2 | 2015-06-21 17:11:21 +0300 | [diff] [blame] | 163 | .. c:function:: PyObject *PySys_GetObject(const char *name) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 164 | |
| 165 | Return the object *name* from the :mod:`sys` module or *NULL* if it does |
| 166 | not exist, without setting an exception. |
| 167 | |
Serhiy Storchaka | 03863d2 | 2015-06-21 17:11:21 +0300 | [diff] [blame] | 168 | .. c:function:: int PySys_SetObject(const char *name, PyObject *v) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 169 | |
| 170 | Set *name* in the :mod:`sys` module to *v* unless *v* is *NULL*, in which |
| 171 | case *name* is deleted from the sys module. Returns ``0`` on success, ``-1`` |
| 172 | on error. |
| 173 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 174 | .. c:function:: void PySys_ResetWarnOptions() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 175 | |
| 176 | Reset :data:`sys.warnoptions` to an empty list. |
| 177 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 178 | .. c:function:: void PySys_AddWarnOption(const wchar_t *s) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 179 | |
| 180 | Append *s* to :data:`sys.warnoptions`. |
| 181 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 182 | .. c:function:: void PySys_AddWarnOptionUnicode(PyObject *unicode) |
Victor Stinner | 9ca9c25 | 2010-05-19 16:53:30 +0000 | [diff] [blame] | 183 | |
| 184 | Append *unicode* to :data:`sys.warnoptions`. |
| 185 | |
Serhiy Storchaka | 84b8e92 | 2017-03-30 10:01:03 +0300 | [diff] [blame] | 186 | .. c:function:: void PySys_SetPath(const wchar_t *path) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 187 | |
| 188 | Set :data:`sys.path` to a list object of paths found in *path* which should |
| 189 | be a list of paths separated with the platform's search path delimiter |
| 190 | (``:`` on Unix, ``;`` on Windows). |
| 191 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 192 | .. c:function:: void PySys_WriteStdout(const char *format, ...) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 193 | |
| 194 | Write the output string described by *format* to :data:`sys.stdout`. No |
| 195 | exceptions are raised, even if truncation occurs (see below). |
| 196 | |
| 197 | *format* should limit the total size of the formatted output string to |
| 198 | 1000 bytes or less -- after 1000 bytes, the output string is truncated. |
| 199 | In particular, this means that no unrestricted "%s" formats should occur; |
| 200 | these should be limited using "%.<N>s" where <N> is a decimal number |
| 201 | calculated so that <N> plus the maximum size of other formatted text does not |
| 202 | exceed 1000 bytes. Also watch out for "%f", which can print hundreds of |
| 203 | digits for very large numbers. |
| 204 | |
| 205 | If a problem occurs, or :data:`sys.stdout` is unset, the formatted message |
| 206 | is written to the real (C level) *stdout*. |
| 207 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 208 | .. c:function:: void PySys_WriteStderr(const char *format, ...) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 209 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 210 | 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] | 211 | instead. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 212 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 213 | .. c:function:: void PySys_FormatStdout(const char *format, ...) |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 214 | |
| 215 | Function similar to PySys_WriteStdout() but format the message using |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 216 | :c:func:`PyUnicode_FromFormatV` and don't truncate the message to an |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 217 | arbitrary length. |
| 218 | |
Victor Stinner | ad5b1df | 2010-08-16 18:39:49 +0000 | [diff] [blame] | 219 | .. versionadded:: 3.2 |
| 220 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 221 | .. c:function:: void PySys_FormatStderr(const char *format, ...) |
Victor Stinner | 7976663 | 2010-08-16 17:36:42 +0000 | [diff] [blame] | 222 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 223 | 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] | 224 | instead. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 225 | |
Victor Stinner | ad5b1df | 2010-08-16 18:39:49 +0000 | [diff] [blame] | 226 | .. versionadded:: 3.2 |
| 227 | |
Antoine Pitrou | 9583cac | 2010-10-21 13:42:28 +0000 | [diff] [blame] | 228 | .. c:function:: void PySys_AddXOption(const wchar_t *s) |
| 229 | |
| 230 | Parse *s* as a set of :option:`-X` options and add them to the current |
| 231 | options mapping as returned by :c:func:`PySys_GetXOptions`. |
| 232 | |
| 233 | .. versionadded:: 3.2 |
| 234 | |
| 235 | .. c:function:: PyObject *PySys_GetXOptions() |
| 236 | |
| 237 | Return the current dictionary of :option:`-X` options, similarly to |
| 238 | :data:`sys._xoptions`. On error, *NULL* is returned and an exception is |
| 239 | set. |
| 240 | |
| 241 | .. versionadded:: 3.2 |
| 242 | |
Georg Brandl | 67b21b7 | 2010-08-17 15:07:14 +0000 | [diff] [blame] | 243 | |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 244 | .. _processcontrol: |
| 245 | |
| 246 | Process Control |
| 247 | =============== |
| 248 | |
| 249 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 250 | .. c:function:: void Py_FatalError(const char *message) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 251 | |
| 252 | .. index:: single: abort() |
| 253 | |
| 254 | Print a fatal error message and kill the process. No cleanup is performed. |
| 255 | This function should only be invoked when a condition is detected that would |
| 256 | make it dangerous to continue using the Python interpreter; e.g., when the |
| 257 | object administration appears to be corrupted. On Unix, the standard C library |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 258 | 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] | 259 | file. |
| 260 | |
| 261 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 262 | .. c:function:: void Py_Exit(int status) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 263 | |
| 264 | .. index:: |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 265 | single: Py_FinalizeEx() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 266 | single: exit() |
| 267 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 268 | Exit the current process. This calls :c:func:`Py_FinalizeEx` and then calls the |
| 269 | standard C library function ``exit(status)``. If :c:func:`Py_FinalizeEx` |
| 270 | indicates an error, the exit status is set to 120. |
| 271 | |
| 272 | .. versionchanged:: 3.6 |
| 273 | Errors from finalization no longer ignored. |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 274 | |
| 275 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 276 | .. c:function:: int Py_AtExit(void (*func) ()) |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 277 | |
| 278 | .. index:: |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 279 | single: Py_FinalizeEx() |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 280 | single: cleanup functions |
| 281 | |
Martin Panter | b4ce1fc | 2015-11-30 03:18:29 +0000 | [diff] [blame] | 282 | Register a cleanup function to be called by :c:func:`Py_FinalizeEx`. The cleanup |
Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +0000 | [diff] [blame] | 283 | function will be called with no arguments and should return no value. At most |
| 284 | 32 cleanup functions can be registered. When the registration is successful, |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 285 | :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] | 286 | function registered last is called first. Each cleanup function will be called |
| 287 | at most once. Since Python's internal finalization will have completed before |
| 288 | the cleanup function, no Python APIs should be called by *func*. |