|
| 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 | +.. c:struct:: PyInitConfig |
| 21 | +
|
| 22 | + Opaque structure to configure the Python initialization. |
| 23 | + |
| 24 | + |
| 25 | +.. c:function:: PyInitConfig* PyInitConfig_Python_New(void) |
| 26 | +
|
| 27 | + Create a new initialization configuration using :ref:`Python Configuration |
| 28 | + <init-python-config>` default values. |
| 29 | +
|
| 30 | + It must be freed by c:func:`PyInitConfig_Free`. |
| 31 | +
|
| 32 | + Return ``NULL`` on memory allocation failure. |
| 33 | +
|
| 34 | +
|
| 35 | +.. c:function:: PyInitConfig* PyInitConfig_Isolated_New(void) |
| 36 | +
|
| 37 | + Similar to :c:func:`PyInitConfig_Python_New`, but use :ref:`Isolated |
| 38 | + Configuration <init-isolated-conf>` default values. |
| 39 | +
|
| 40 | +
|
| 41 | +.. c:function:: void PyInitConfig_Free(PyInitConfig *config) |
| 42 | +
|
| 43 | + Free memory of a initialization configuration. |
| 44 | +
|
| 45 | +
|
| 46 | +.. c:function:: int PyInitConfig_SetInt(PyInitConfig *config, const char *key, int64_t value) |
| 47 | +
|
| 48 | + Set an integer configuration option. |
| 49 | +
|
| 50 | + Return 0 on success, or return -1 on error. |
| 51 | +
|
| 52 | +
|
| 53 | +.. c:function:: int PyInitConfig_SetStr(PyInitConfig *config, const char *key, const char *value) |
| 54 | +
|
| 55 | + Set a string configuration option from a bytes string. |
| 56 | +
|
| 57 | + The bytes string is decoded by Py_DecodeLocale(). Preinitialize Python if |
| 58 | + needed to ensure that encodings are properly configured. |
| 59 | +
|
| 60 | + Return 0 on success, or return -1 on error. |
| 61 | +
|
| 62 | +.. c:function:: int PyInitConfig_SetWStr(PyInitConfig *config, const char *key, const wchar_t *value) |
| 63 | +
|
| 64 | + Set a string configuration option from a wide string. |
| 65 | +
|
| 66 | + Preinitialize Python if needed. |
| 67 | +
|
| 68 | + Return ``0`` on success, or return ``-1`` on error. |
| 69 | +
|
| 70 | +
|
| 71 | +.. c:function:: int PyInitConfig_SetStrList(PyInitConfig *config, const char *key, size_t length, char * const *items) |
| 72 | +
|
| 73 | + Set a string list configuration option from bytes strings. |
| 74 | +
|
| 75 | + The bytes strings are decoded by Py_DecodeLocale(). Preinitialize Python if |
| 76 | + needed to ensure that encodings are properly configured. |
| 77 | +
|
| 78 | + Return ``0`` on success, or return ``-1`` on error. |
| 79 | +
|
| 80 | +
|
| 81 | +.. c:function:: int PyInitConfig_SetWStrList(PyInitConfig *config, const char *key, size_t length, wchar_t * const *items) |
| 82 | +
|
| 83 | + Set a string list configuration option from a wide strings. |
| 84 | + Preinitialize Python if needed. |
| 85 | + Return 0 on success, or return -1 on error. |
| 86 | +
|
| 87 | +
|
| 88 | +.. c:function:: int Py_InitializeFromInitConfig(PyInitConfig *config) |
| 89 | +
|
| 90 | + Initialize Python from the initialization configuration. |
| 91 | +
|
| 92 | + Return ``0`` on success. |
| 93 | +
|
| 94 | + Return ``-1`` if Python wants to exit and on error. Call |
| 95 | + :c:func:`Py_ExitWithInitConfig` in this case. |
| 96 | +
|
| 97 | +
|
| 98 | +.. c:function:: char* PyInitConfig_GetErrorMsg(PyInitConfig* config) |
| 99 | +
|
| 100 | + Get the current error message. |
| 101 | +
|
| 102 | + Return a UTF-8 string allocated by ``malloc()``. It must be released by |
| 103 | + ``free()``. |
| 104 | +
|
| 105 | + Return ``NULL`` on memory allocation failure. |
| 106 | +
|
| 107 | +
|
| 108 | +.. c:function:: void Py_ExitWithInitConfig(PyInitConfig *config) |
| 109 | +
|
| 110 | + Exit Python and free memory of a initialization configuration. |
| 111 | +
|
| 112 | + The function does not return. |
| 113 | +
|
| 114 | +Example |
| 115 | +------- |
| 116 | +
|
| 117 | +Code:: |
| 118 | +
|
| 119 | + void init_python(void) |
| 120 | + { |
| 121 | + PyInitConfig *config = PyInitConfig_Python_New(); |
| 122 | + if (config == NULL) { |
| 123 | + printf("Init allocation error\n"); |
| 124 | + return; |
| 125 | + } |
| 126 | +
|
| 127 | + if (PyInitConfig_SetInt(config, "dev_mode", 1) < 0) { |
| 128 | + goto error; |
| 129 | + } |
| 130 | +
|
| 131 | + // Set a list of wide strings (argv) |
| 132 | + wchar_t *argv[] = {L"my_program"", L"-c", L"pass"}; |
| 133 | + if (PyInitConfig_SetWStrList(config, "argv", |
| 134 | + Py_ARRAY_LENGTH(argv), argv) < 0) { |
| 135 | + goto error; |
| 136 | + } |
| 137 | +
|
| 138 | + // Set a wide string (program_name) |
| 139 | + if (PyInitConfig_SetWStr(config, "program_name", L"my_program") < 0) { |
| 140 | + goto error; |
| 141 | + } |
| 142 | +
|
| 143 | + // Set a list of bytes strings (xoptions) |
| 144 | + char* xoptions[] = {"faulthandler"}; |
| 145 | + if (PyInitConfig_SetStrList(config, "xoptions", |
| 146 | + Py_ARRAY_LENGTH(xoptions), xoptions) < 0) { |
| 147 | + goto error; |
| 148 | + } |
| 149 | +
|
| 150 | + if (Py_InitializeFromInitConfig(config) < 0) { |
| 151 | + Py_ExitWithInitConfig(config); |
| 152 | + } |
| 153 | + PyInitConfig_Free(config); |
| 154 | + } |
| 155 | +
|
| 156 | +
|
| 157 | +Get the current Python configuration |
| 158 | +==================================== |
| 159 | +
|
| 160 | +.. c:function:: int PyConfig_GetInt(const char *key, int64_t *value) |
| 161 | +
|
| 162 | + Get an integer configuration option. |
| 163 | +
|
| 164 | + Return ``0`` and set *\*value* on success. |
| 165 | +
|
| 166 | + Raise an exception return ``-1`` on error. |
| 167 | +
|
| 168 | +
|
| 169 | +.. c:function:: int PyConfig_GetStr(const char *key, PyObject **value) |
| 170 | +
|
| 171 | + Get a string configuration option. |
| 172 | +
|
| 173 | + Return ``0`` and set *\*value* on success. *\*value* can be set to a Python |
| 174 | + :class:`str` object or to ``None``. |
| 175 | +
|
| 176 | + Raise an exception return ``-1`` on error. |
| 177 | +
|
| 178 | +
|
| 179 | +.. c:function:: int PyConfig_GetStrList(const char *key, PyObject **value) |
| 180 | +
|
| 181 | + Get a string configuration option. |
| 182 | +
|
| 183 | + Return ``0`` and set *\*value* to a Python list on success. |
| 184 | +
|
| 185 | + Raise an exception return ``-1`` on error. |
| 186 | +
|
| 187 | +Example |
| 188 | +------- |
| 189 | +
|
| 190 | +Code:: |
| 191 | +
|
| 192 | + static int get_bytes_warning(void) |
| 193 | + { |
| 194 | + int64_t bytes_warning; |
| 195 | + if (PyConfig_GetInt("bytes_warning", &bytes_warning) < 0) { |
| 196 | + // Silently ignore the error |
| 197 | + PyErr_Clear(); |
| 198 | + return -1; |
| 199 | + } |
| 200 | + return (int)bytes_warning; |
| 201 | + } |
0 commit comments