|
| 1 | +.. highlight:: c |
| 2 | + |
| 3 | +.. _config-c-api: |
| 4 | + |
| 5 | +******************** |
| 6 | +Python Configuration |
| 7 | +******************** |
| 8 | + |
| 9 | +.. versionadded:: 3.13 |
| 10 | + |
| 11 | +API part of the limited C API version 3.13 to configure the Python |
| 12 | +initialization and get the Python runtime configuration. |
| 13 | + |
| 14 | +See also :ref:`Python Initialization Configuration <init-config>`. |
| 15 | + |
| 16 | + |
| 17 | +Initialize Python |
| 18 | +================= |
| 19 | + |
| 20 | +Configuration to customize Python initialization. Configuration option names |
| 21 | +are names of a :c:type:`PyConfig` members. |
| 22 | + |
| 23 | +.. c:struct:: PyInitConfig |
| 24 | +
|
| 25 | + Opaque structure to configure the Python initialization. |
| 26 | + |
| 27 | + |
| 28 | +.. c:function:: PyInitConfig* PyInitConfig_Python_New(void) |
| 29 | +
|
| 30 | + Create a new initialization configuration using :ref:`Python Configuration |
| 31 | + <init-python-config>` default values. |
| 32 | +
|
| 33 | + It must be freed by :c:func:`PyInitConfig_Free`. |
| 34 | +
|
| 35 | + Return ``NULL`` on memory allocation failure. |
| 36 | +
|
| 37 | +
|
| 38 | +.. c:function:: PyInitConfig* PyInitConfig_Isolated_New(void) |
| 39 | +
|
| 40 | + Similar to :c:func:`PyInitConfig_Python_New`, but use :ref:`Isolated |
| 41 | + Configuration <init-isolated-conf>` default values. |
| 42 | +
|
| 43 | +
|
| 44 | +.. c:function:: void PyInitConfig_Free(PyInitConfig *config) |
| 45 | +
|
| 46 | + Free memory of an initialization configuration. |
| 47 | +
|
| 48 | +
|
| 49 | +.. c:function:: int PyInitConfig_SetInt(PyInitConfig *config, const char *name, int64_t value) |
| 50 | +
|
| 51 | + Set an integer configuration option. |
| 52 | +
|
| 53 | + * Return ``0`` on success. |
| 54 | + * Set an error in *config* and return ``-1`` on error. |
| 55 | +
|
| 56 | +
|
| 57 | +.. c:function:: int PyInitConfig_SetStr(PyInitConfig *config, const char *name, const char *value) |
| 58 | +
|
| 59 | + Set a string configuration option from a null-terminated bytes string. |
| 60 | +
|
| 61 | + The bytes string is decoded by :c:func:`Py_DecodeLocale`. If Python is not |
| 62 | + yet preinitialized, this function preinitializes it to ensure that encodings |
| 63 | + are properly configured. |
| 64 | +
|
| 65 | + * Return ``0`` on success. |
| 66 | + * Set an error in *config* and return ``-1`` on error. |
| 67 | +
|
| 68 | +
|
| 69 | +.. c:function:: int PyInitConfig_SetWStr(PyInitConfig *config, const char *name, const wchar_t *value) |
| 70 | +
|
| 71 | + Set a string configuration option from a null-terminated wide string. |
| 72 | +
|
| 73 | + If Python is not yet preinitialized, this function preinitializes it. |
| 74 | +
|
| 75 | + * Return ``0`` on success. |
| 76 | + * Set an error in *config* and return ``-1`` on error. |
| 77 | +
|
| 78 | +
|
| 79 | +.. c:function:: int PyInitConfig_SetStrList(PyInitConfig *config, const char *name, size_t length, char * const *items) |
| 80 | +
|
| 81 | + Set a string list configuration option from an array of null-terminated |
| 82 | + bytes strings. |
| 83 | +
|
| 84 | + The bytes string is decoded by :c:func:`Py_DecodeLocale`. If Python is not |
| 85 | + yet preinitialized, this function preinitializes it to ensure that encodings |
| 86 | + are properly configured. |
| 87 | +
|
| 88 | + * Return ``0`` on success. |
| 89 | + * Set an error in *config* and return ``-1`` on error. |
| 90 | +
|
| 91 | +
|
| 92 | +.. c:function:: int PyInitConfig_SetWStrList(PyInitConfig *config, const char *name, size_t length, wchar_t * const *items) |
| 93 | +
|
| 94 | + Set a string list configuration option from a null-terminated wide strings. |
| 95 | +
|
| 96 | + If Python is not yet preinitialized, this function preinitializes it. |
| 97 | +
|
| 98 | + * Return ``0`` on success. |
| 99 | + * Set an error in *config* and return ``-1`` on error. |
| 100 | +
|
| 101 | +
|
| 102 | +.. c:function:: int Py_InitializeFromInitConfig(PyInitConfig *config) |
| 103 | +
|
| 104 | + Initialize Python from the initialization configuration. |
| 105 | +
|
| 106 | + * Return ``0`` on success. |
| 107 | + * Return ``-1`` if an error was set or if an exit code was set. |
| 108 | +
|
| 109 | +
|
| 110 | +Error handling |
| 111 | +============== |
| 112 | +
|
| 113 | +.. c:function:: int PyInitConfig_Exception(PyInitConfig* config) |
| 114 | +
|
| 115 | + Check if an exception is set in *config*: |
| 116 | +
|
| 117 | + * Return non-zero if an error was set or if an exit code was set. |
| 118 | + * Return zero otherwise. |
| 119 | +
|
| 120 | +
|
| 121 | +.. c:function:: int PyInitConfig_GetError(PyInitConfig* config, const char **err_msg) |
| 122 | +
|
| 123 | + Get the *config* error message. |
| 124 | +
|
| 125 | + * Set *\*err_msg* and return ``1`` if an error is set. |
| 126 | + * Set *\*err_msg* to ``NULL`` and return ``0`` otherwise. |
| 127 | +
|
| 128 | +
|
| 129 | +.. c:function:: int PyInitConfig_GetExitCode(PyInitConfig* config, int *exitcode) |
| 130 | +
|
| 131 | + Get the *config* exit code. |
| 132 | +
|
| 133 | + * Set *\*exitcode* and return ``1`` if an exit code is set. |
| 134 | + * Return ``0`` otherwise. |
| 135 | +
|
| 136 | +
|
| 137 | +.. c:function:: void Py_ExitWithInitConfig(PyInitConfig *config) |
| 138 | +
|
| 139 | + Exit Python and free memory of a initialization configuration. |
| 140 | +
|
| 141 | + If an error message is set, display the error message. |
| 142 | +
|
| 143 | + If an exit code is set, use it to exit the process. |
| 144 | +
|
| 145 | + The function does not return. |
| 146 | +
|
| 147 | +
|
| 148 | +Example |
| 149 | +------- |
| 150 | +
|
| 151 | +Code:: |
| 152 | +
|
| 153 | + void init_python(void) |
| 154 | + { |
| 155 | + PyInitConfig *config = PyInitConfig_Python_New(); |
| 156 | + if (config == NULL) { |
| 157 | + printf("Init allocation error\n"); |
| 158 | + return; |
| 159 | + } |
| 160 | +
|
| 161 | + if (PyInitConfig_SetInt(config, "dev_mode", 1) < 0) { |
| 162 | + goto error; |
| 163 | + } |
| 164 | +
|
| 165 | + // Set a list of wide strings (argv) |
| 166 | + wchar_t *argv[] = {L"my_program"", L"-c", L"pass"}; |
| 167 | + if (PyInitConfig_SetWStrList(config, "argv", |
| 168 | + Py_ARRAY_LENGTH(argv), argv) < 0) { |
| 169 | + goto error; |
| 170 | + } |
| 171 | +
|
| 172 | + // Set a wide string (program_name) |
| 173 | + if (PyInitConfig_SetWStr(config, "program_name", L"my_program") < 0) { |
| 174 | + goto error; |
| 175 | + } |
| 176 | +
|
| 177 | + // Set a list of bytes strings (xoptions) |
| 178 | + char* xoptions[] = {"faulthandler"}; |
| 179 | + if (PyInitConfig_SetStrList(config, "xoptions", |
| 180 | + Py_ARRAY_LENGTH(xoptions), xoptions) < 0) { |
| 181 | + goto error; |
| 182 | + } |
| 183 | +
|
| 184 | + if (Py_InitializeFromInitConfig(config) < 0) { |
| 185 | + Py_ExitWithInitConfig(config); |
| 186 | + } |
| 187 | + PyInitConfig_Free(config); |
| 188 | + } |
| 189 | +
|
| 190 | +
|
| 191 | +Get the current Python configuration |
| 192 | +==================================== |
| 193 | +
|
| 194 | +Get a configuration option where *name* is the name of a :c:type:`PyConfig` |
| 195 | +member. |
| 196 | +
|
| 197 | +.. c:function:: int PyConfig_GetInt(const char *name, int64_t *value) |
| 198 | +
|
| 199 | + Get an integer configuration option. |
| 200 | +
|
| 201 | + Return ``0`` and set *\*value* on success. |
| 202 | +
|
| 203 | + Raise an exception and return ``-1`` on error. |
| 204 | +
|
| 205 | +
|
| 206 | +.. c:function:: int PyConfig_GetStr(const char *name, PyObject **value) |
| 207 | +
|
| 208 | + Get a string configuration option. |
| 209 | +
|
| 210 | + Return ``0`` and set *\*value* on success. *\*value* can be set to a Python |
| 211 | + :class:`str` object or to ``None``. |
| 212 | +
|
| 213 | + Raise an exception and return ``-1`` on error. |
| 214 | +
|
| 215 | +
|
| 216 | +.. c:function:: int PyConfig_GetStrList(const char *name, PyObject **value) |
| 217 | +
|
| 218 | + Get a string configuration option. |
| 219 | +
|
| 220 | + Return ``0`` and set *\*value* to a Python list of strings on success. |
| 221 | +
|
| 222 | + Raise an exception and return ``-1`` on error. |
| 223 | +
|
| 224 | +
|
| 225 | +Example |
| 226 | +------- |
| 227 | +
|
| 228 | +Code:: |
| 229 | +
|
| 230 | + static int get_bytes_warning(void) |
| 231 | + { |
| 232 | + int64_t bytes_warning; |
| 233 | + if (PyConfig_GetInt("bytes_warning", &bytes_warning) < 0) { |
| 234 | + // Silently ignore the error |
| 235 | + PyErr_Clear(); |
| 236 | + return -1; |
| 237 | + } |
| 238 | + return (int)bytes_warning; |
| 239 | + } |
0 commit comments