@@ -8,55 +8,68 @@ Python Initialization Configuration
8
8
9
9
.. versionadded :: 3.8
10
10
11
- Structures:
12
-
13
- * :c:type: `PyConfig `
14
- * :c:type: `PyPreConfig `
15
- * :c:type: `PyStatus `
16
- * :c:type: `PyWideStringList `
17
-
18
- Functions:
19
-
20
- * :c:func: `PyConfig_Clear `
21
- * :c:func: `PyConfig_InitIsolatedConfig `
22
- * :c:func: `PyConfig_InitPythonConfig `
23
- * :c:func: `PyConfig_Read `
24
- * :c:func: `PyConfig_SetArgv `
25
- * :c:func: `PyConfig_SetBytesArgv `
26
- * :c:func: `PyConfig_SetBytesString `
27
- * :c:func: `PyConfig_SetString `
28
- * :c:func: `PyConfig_SetWideStringList `
29
- * :c:func: `PyPreConfig_InitIsolatedConfig `
30
- * :c:func: `PyPreConfig_InitPythonConfig `
31
- * :c:func: `PyStatus_Error `
32
- * :c:func: `PyStatus_Exception `
33
- * :c:func: `PyStatus_Exit `
34
- * :c:func: `PyStatus_IsError `
35
- * :c:func: `PyStatus_IsExit `
36
- * :c:func: `PyStatus_NoMemory `
37
- * :c:func: `PyStatus_Ok `
38
- * :c:func: `PyWideStringList_Append `
39
- * :c:func: `PyWideStringList_Insert `
40
- * :c:func: `Py_ExitStatusException `
41
- * :c:func: `Py_InitializeFromConfig `
42
- * :c:func: `Py_PreInitialize `
43
- * :c:func: `Py_PreInitializeFromArgs `
44
- * :c:func: `Py_PreInitializeFromBytesArgs `
45
- * :c:func: `Py_RunMain `
46
- * :c:func: `Py_GetArgcArgv `
47
-
48
- The preconfiguration (``PyPreConfig `` type) is stored in
49
- ``_PyRuntime.preconfig `` and the configuration (``PyConfig `` type) is stored in
50
- ``PyInterpreterState.config ``.
11
+ Python can be initialized with :c:func: `Py_InitializeFromConfig ` and the
12
+ :c:type: `PyConfig ` structure. It can be preinitialized with
13
+ :c:func: `Py_PreInitialize ` and the :c:type: `PyPreConfig ` structure.
14
+
15
+ There are two kinds of configuration:
16
+
17
+ * The :ref: `Python Configuration <init-python-config >` can be used to build a
18
+ customized Python which behaves as the regular Python. For example,
19
+ environments variables and command line arguments are used to configure
20
+ Python.
21
+
22
+ * The :ref: `Isolated Configuration <init-isolated-conf >` can be used to embed
23
+ Python into an application. It isolates Python from the system. For example,
24
+ environments variables are ignored, the LC_CTYPE locale is left unchanged and
25
+ no signal handler is registred.
51
26
52
27
See also :ref: `Initialization, Finalization, and Threads <initialization >`.
53
28
54
29
.. seealso ::
55
30
:pep: `587 ` "Python Initialization Configuration".
56
31
32
+ Example
33
+ =======
34
+
35
+ Example of customized Python always running in isolated mode::
36
+
37
+ int main(int argc, char **argv)
38
+ {
39
+ PyStatus status;
40
+
41
+ PyConfig config;
42
+ PyConfig_InitPythonConfig(&config);
43
+ config.isolated = 1;
44
+
45
+ /* Decode command line arguments.
46
+ Implicitly preinitialize Python (in isolated mode). */
47
+ status = PyConfig_SetBytesArgv(&config, argc, argv);
48
+ if (PyStatus_Exception(status)) {
49
+ goto exception;
50
+ }
51
+
52
+ status = Py_InitializeFromConfig(&config);
53
+ if (PyStatus_Exception(status)) {
54
+ goto exception;
55
+ }
56
+ PyConfig_Clear(&config);
57
+
58
+ return Py_RunMain();
59
+
60
+ exception:
61
+ PyConfig_Clear(&config);
62
+ if (PyStatus_IsExit(status)) {
63
+ return status.exitcode;
64
+ }
65
+ /* Display the error message and exit the process with
66
+ non-zero exit code */
67
+ Py_ExitStatusException(status);
68
+ }
69
+
57
70
58
71
PyWideStringList
59
- ----------------
72
+ ================
60
73
61
74
.. c :type :: PyWideStringList
62
75
@@ -95,7 +108,7 @@ PyWideStringList
95
108
List items.
96
109
97
110
PyStatus
98
- --------
111
+ ========
99
112
100
113
.. c :type :: PyStatus
101
114
@@ -187,7 +200,7 @@ Example::
187
200
188
201
189
202
PyPreConfig
190
- -----------
203
+ ===========
191
204
192
205
.. c :type :: PyPreConfig
193
206
@@ -317,7 +330,7 @@ PyPreConfig
317
330
.. _c-preinit :
318
331
319
332
Preinitialize Python with PyPreConfig
320
- -------------------------------------
333
+ =====================================
321
334
322
335
The preinitialization of Python:
323
336
@@ -326,26 +339,35 @@ The preinitialization of Python:
326
339
* Set the :ref:`Python UTF-8 Mode <utf8-mode>`
327
340
(:c:member: `PyPreConfig.utf8_mode `)
328
341
342
+ The current preconfiguration (``PyPreConfig `` type) is stored in
343
+ ``_PyRuntime.preconfig``.
344
+
329
345
Functions to preinitialize Python:
330
346
331
347
.. c:function:: PyStatus Py_PreInitialize(const PyPreConfig *preconfig)
332
348
333
349
Preinitialize Python from *preconfig * preconfiguration.
334
350
351
+ *preconfig * must not be ``NULL ``.
352
+
335
353
.. c :function :: PyStatus Py_PreInitializeFromBytesArgs (const PyPreConfig *preconfig, int argc, char * const *argv)
336
354
337
355
Preinitialize Python from *preconfig * preconfiguration.
338
356
339
357
Parse *argv * command line arguments (bytes strings) if
340
358
:c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
341
359
360
+ *preconfig* must not be ``NULL``.
361
+
342
362
.. c:function:: PyStatus Py_PreInitializeFromArgs(const PyPreConfig *preconfig, int argc, wchar_t * const * argv)
343
363
344
364
Preinitialize Python from *preconfig * preconfiguration.
345
365
346
366
Parse *argv * command line arguments (wide strings) if
347
367
:c:member:`~PyPreConfig.parse_argv` of *preconfig* is non-zero.
348
368
369
+ *preconfig* must not be ``NULL``.
370
+
349
371
The caller is responsible to handle exceptions (error or exit) using
350
372
:c:func:`PyStatus_Exception` and :c:func:`Py_ExitStatusException`.
351
373
@@ -388,7 +410,7 @@ the :ref:`Python UTF-8 Mode <utf8-mode>`::
388
410
389
411
390
412
PyConfig
391
- --------
413
+ ========
392
414
393
415
.. c :type :: PyConfig
394
416
@@ -449,8 +471,20 @@ PyConfig
449
471
450
472
Fields which are already initialized are left unchanged.
451
473
474
+ The :c:func: `PyConfig_Read ` function only parses
475
+ :c:member: `PyConfig.argv ` arguments once: :c:member: `PyConfig.parse_argv `
476
+ is set to ``2 `` after arguments are parsed. Since Python arguments are
477
+ strippped from :c:member: `PyConfig.argv `, parsing arguments twice would
478
+ parse the application options as Python options.
479
+
452
480
:ref: `Preinitialize Python <c-preinit >` if needed.
453
481
482
+ .. versionchanged :: 3.10
483
+ The :c:member: `PyConfig.argv ` arguments are now only parsed once,
484
+ :c:member: `PyConfig.parse_argv ` is set to ``2 `` after arguments are
485
+ parsed, and arguments are only parsed if
486
+ :c:member: `PyConfig.parse_argv ` equals ``1 ``.
487
+
454
488
.. c :function :: void PyConfig_Clear (PyConfig *config)
455
489
456
490
Release configuration memory.
@@ -833,7 +867,7 @@ PyConfig
833
867
834
868
If :c:member:`~PyConfig.orig_argv` list is empty and
835
869
:c:member:`~PyConfig.argv` is not a list only containing an empty
836
- string, :c:func:`PyConfig_Read() ` copies :c:member:`~PyConfig.argv` into
870
+ string, :c:func:`PyConfig_Read` copies :c:member:`~PyConfig.argv` into
837
871
:c:member:`~PyConfig.orig_argv` before modifying
838
872
:c:member:`~PyConfig.argv` (if :c:member: `~PyConfig.parse_argv ` is
839
873
non-zero).
@@ -849,12 +883,22 @@ PyConfig
849
883
850
884
Parse command line arguments?
851
885
852
- If non-zero , parse :c:member:`~PyConfig.argv` the same way the regular
886
+ If equals to ``1`` , parse :c:member:`~PyConfig.argv` the same way the regular
853
887
Python parses :ref:`command line arguments <using-on-cmdline>`, and strip
854
888
Python arguments from :c:member:`~PyConfig.argv`.
855
889
890
+ The :c:func:`PyConfig_Read` function only parses
891
+ :c:member:`PyConfig.argv` arguments once: :c:member:`PyConfig.parse_argv`
892
+ is set to ``2`` after arguments are parsed. Since Python arguments are
893
+ strippped from :c:member:`PyConfig.argv`, parsing arguments twice would
894
+ parse the application options as Python options.
895
+
856
896
Default: ``1`` in Python mode, ``0`` in isolated mode.
857
897
898
+ .. versionchanged:: 3.10
899
+ The :c:member:`PyConfig.argv` arguments are now only parsed if
900
+ :c:member:`PyConfig.parse_argv` equals to ``1``.
901
+
858
902
.. c:member:: int parser_debug
859
903
860
904
Parser debug mode. If greater than 0, turn on parser debugging output (for expert only, depending
@@ -1108,7 +1152,7 @@ the :option:`-X` command line option.
1108
1152
1109
1153
1110
1154
Initialization with PyConfig
1111
- ----------------------------
1155
+ ============================
1112
1156
1113
1157
Function to initialize Python:
1114
1158
@@ -1123,6 +1167,9 @@ If :c:func:`PyImport_FrozenModules`, :c:func:`PyImport_AppendInittab` or
1123
1167
:c:func:`PyImport_ExtendInittab` are used, they must be set or called after
1124
1168
Python preinitialization and before the Python initialization.
1125
1169
1170
+ The current configuration (``PyConfig `` type) is stored in
1171
+ ``PyInterpreterState.config``.
1172
+
1126
1173
Example setting the program name::
1127
1174
1128
1175
void init_python(void)
@@ -1136,17 +1183,17 @@ Example setting the program name::
1136
1183
status = PyConfig_SetString(&config, &config.program_name,
1137
1184
L"/path/to/my_program");
1138
1185
if (PyStatus_Exception(status)) {
1139
- goto fail ;
1186
+ goto exception ;
1140
1187
}
1141
1188
1142
1189
status = Py_InitializeFromConfig (&config);
1143
1190
if (PyStatus_Exception (status)) {
1144
- goto fail ;
1191
+ goto exception ;
1145
1192
}
1146
1193
PyConfig_Clear (&config);
1147
1194
return;
1148
1195
1149
- fail :
1196
+ exception :
1150
1197
PyConfig_Clear (&config);
1151
1198
Py_ExitStatusException (status);
1152
1199
}
@@ -1202,7 +1249,7 @@ configuration, and then override some parameters::
1202
1249
.. _init-isolated-conf :
1203
1250
1204
1251
Isolated Configuration
1205
- ----------------------
1252
+ ======================
1206
1253
1207
1254
:c:func: `PyPreConfig_InitIsolatedConfig ` and
1208
1255
:c:func: `PyConfig_InitIsolatedConfig ` functions create a configuration to
@@ -1223,7 +1270,7 @@ configuration.
1223
1270
.. _init-python-config:
1224
1271
1225
1272
Python Configuration
1226
- --------------------
1273
+ ====================
1227
1274
1228
1275
:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1229
1276
functions create a configuration to build a customized Python which behaves as
@@ -1237,46 +1284,11 @@ and :ref:`Python UTF-8 Mode <utf8-mode>`
1237
1284
(:pep: `540 `) depending on the LC_CTYPE locale, :envvar:`PYTHONUTF8` and
1238
1285
:envvar:`PYTHONCOERCECLOCALE` environment variables.
1239
1286
1240
- Example of customized Python always running in isolated mode::
1241
-
1242
- int main(int argc, char **argv)
1243
- {
1244
- PyStatus status;
1245
-
1246
- PyConfig config;
1247
- PyConfig_InitPythonConfig(&config);
1248
- config.isolated = 1 ;
1249
-
1250
- /* Decode command line arguments.
1251
- Implicitly preinitialize Python (in isolated mode). */
1252
- status = PyConfig_SetBytesArgv(&config, argc, argv);
1253
- if (PyStatus_Exception(status)) {
1254
- goto fail;
1255
- }
1256
-
1257
- status = Py_InitializeFromConfig(&config);
1258
- if (PyStatus_Exception(status)) {
1259
- goto fail;
1260
- }
1261
- PyConfig_Clear (&config);
1262
-
1263
- return Py_RunMain();
1264
-
1265
- fail:
1266
- PyConfig_Clear(&config);
1267
- if (PyStatus_IsExit(status)) {
1268
- return status.exitcode ;
1269
- }
1270
- /* Display the error message and exit the process with
1271
- non-zero exit code */
1272
- Py_ExitStatusException (status);
1273
- }
1274
-
1275
1287
1276
1288
.. _init-path-config:
1277
1289
1278
1290
Path Configuration
1279
- ------------------
1291
+ ==================
1280
1292
1281
1293
:c:type:`PyConfig` contains multiple fields for the path configuration:
1282
1294
@@ -1356,7 +1368,7 @@ The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1356
1368
1357
1369
1358
1370
Py_RunMain()
1359
- ------------
1371
+ ============
1360
1372
1361
1373
.. c:function:: int Py_RunMain(void)
1362
1374
@@ -1376,7 +1388,7 @@ customized Python always running in isolated mode using
1376
1388
1377
1389
1378
1390
Py_GetArgcArgv()
1379
- ----------------
1391
+ ================
1380
1392
1381
1393
.. c:function:: void Py_GetArgcArgv(int *argc, wchar_t ***argv)
1382
1394
@@ -1386,7 +1398,7 @@ Py_GetArgcArgv()
1386
1398
1387
1399
1388
1400
Multi-Phase Initialization Private Provisional API
1389
- --------------------------------------------------
1401
+ ==================================================
1390
1402
1391
1403
This section is a private provisional API introducing multi-phase
1392
1404
initialization, the core feature of the :pep: `432 `:
0 commit comments