| 1 |
.. highlightlang:: c |
| 2 |
|
| 3 |
.. _os: |
| 4 |
|
| 5 |
Operating System Utilities |
| 6 |
========================== |
| 7 |
|
| 8 |
|
| 9 |
.. c:function:: int Py_FdIsInteractive(FILE *fp, const char *filename) |
| 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))`` |
| 13 |
is true. If the global flag :c:data:`Py_InteractiveFlag` is true, this function |
| 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 |
|
| 18 |
.. c:function:: void PyOS_AfterFork() |
| 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 |
|
| 26 |
.. c:function:: int PyOS_CheckStack() |
| 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 |
|
| 35 |
.. c:function:: PyOS_sighandler_t PyOS_getsig(int i) |
| 36 |
|
| 37 |
Return the current signal handler for signal *i*. This is a thin wrapper around |
| 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 |
| 40 |
(\*)(int)`. |
| 41 |
|
| 42 |
|
| 43 |
.. c:function:: PyOS_sighandler_t PyOS_setsig(int i, PyOS_sighandler_t h) |
| 44 |
|
| 45 |
Set the signal handler for signal *i* to be *h*; return the old signal handler. |
| 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)`. |
| 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 |
|
| 59 |
.. c:function:: PyObject *PySys_GetObject(char *name) |
| 60 |
|
| 61 |
Return the object *name* from the :mod:`sys` module or *NULL* if it does |
| 62 |
not exist, without setting an exception. |
| 63 |
|
| 64 |
.. c:function:: FILE *PySys_GetFile(char *name, FILE *def) |
| 65 |
|
| 66 |
Return the :c:type:`FILE*` associated with the object *name* in the |
| 67 |
:mod:`sys` module, or *def* if *name* is not in the module or is not associated |
| 68 |
with a :c:type:`FILE*`. |
| 69 |
|
| 70 |
.. c:function:: int PySys_SetObject(char *name, PyObject *v) |
| 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 |
|
| 76 |
.. c:function:: void PySys_ResetWarnOptions() |
| 77 |
|
| 78 |
Reset :data:`sys.warnoptions` to an empty list. |
| 79 |
|
| 80 |
.. c:function:: void PySys_AddWarnOption(char *s) |
| 81 |
|
| 82 |
Append *s* to :data:`sys.warnoptions`. |
| 83 |
|
| 84 |
.. c:function:: void PySys_SetPath(char *path) |
| 85 |
|
| 86 |
Set :data:`sys.path` to a list object of paths found in *path* which should |
| 87 |
be a list of paths separated with the platform's search path delimiter |
| 88 |
(``:`` on Unix, ``;`` on Windows). |
| 89 |
|
| 90 |
.. c:function:: void PySys_WriteStdout(const char *format, ...) |
| 91 |
|
| 92 |
Write the output string described by *format* to :data:`sys.stdout`. No |
| 93 |
exceptions are raised, even if truncation occurs (see below). |
| 94 |
|
| 95 |
*format* should limit the total size of the formatted output string to |
| 96 |
1000 bytes or less -- after 1000 bytes, the output string is truncated. |
| 97 |
In particular, this means that no unrestricted "%s" formats should occur; |
| 98 |
these should be limited using "%.<N>s" where <N> is a decimal number |
| 99 |
calculated so that <N> plus the maximum size of other formatted text does not |
| 100 |
exceed 1000 bytes. Also watch out for "%f", which can print hundreds of |
| 101 |
digits for very large numbers. |
| 102 |
|
| 103 |
If a problem occurs, or :data:`sys.stdout` is unset, the formatted message |
| 104 |
is written to the real (C level) *stdout*. |
| 105 |
|
| 106 |
.. c:function:: void PySys_WriteStderr(const char *format, ...) |
| 107 |
|
| 108 |
As above, but write to :data:`sys.stderr` or *stderr* instead. |
| 109 |
|
| 110 |
|
| 111 |
.. _processcontrol: |
| 112 |
|
| 113 |
Process Control |
| 114 |
=============== |
| 115 |
|
| 116 |
|
| 117 |
.. c:function:: void Py_FatalError(const char *message) |
| 118 |
|
| 119 |
.. index:: single: abort() |
| 120 |
|
| 121 |
Print a fatal error message and kill the process. No cleanup is performed. |
| 122 |
This function should only be invoked when a condition is detected that would |
| 123 |
make it dangerous to continue using the Python interpreter; e.g., when the |
| 124 |
object administration appears to be corrupted. On Unix, the standard C library |
| 125 |
function :c:func:`abort` is called which will attempt to produce a :file:`core` |
| 126 |
file. |
| 127 |
|
| 128 |
|
| 129 |
.. c:function:: void Py_Exit(int status) |
| 130 |
|
| 131 |
.. index:: |
| 132 |
single: Py_Finalize() |
| 133 |
single: exit() |
| 134 |
|
| 135 |
Exit the current process. This calls :c:func:`Py_Finalize` and then calls the |
| 136 |
standard C library function ``exit(status)``. |
| 137 |
|
| 138 |
|
| 139 |
.. c:function:: int Py_AtExit(void (*func) ()) |
| 140 |
|
| 141 |
.. index:: |
| 142 |
single: Py_Finalize() |
| 143 |
single: cleanup functions |
| 144 |
|
| 145 |
Register a cleanup function to be called by :c:func:`Py_Finalize`. The cleanup |
| 146 |
function will be called with no arguments and should return no value. At most |
| 147 |
32 cleanup functions can be registered. When the registration is successful, |
| 148 |
:c:func:`Py_AtExit` returns ``0``; on failure, it returns ``-1``. The cleanup |
| 149 |
function registered last is called first. Each cleanup function will be called |
| 150 |
at most once. Since Python's internal finalization will have completed before |
| 151 |
the cleanup function, no Python APIs should be called by *func*. |