6
6
Python Initialization Configuration
7
7
***********************************
8
8
9
+ PyConfig C API
10
+ ==============
11
+
9
12
.. versionadded :: 3.8
10
13
11
14
Python can be initialized with :c:func: `Py_InitializeFromConfig ` and the
@@ -34,7 +37,7 @@ See also :ref:`Initialization, Finalization, and Threads <initialization>`.
34
37
35
38
36
39
Example
37
- =======
40
+ -------
38
41
39
42
Example of customized Python always running in isolated mode::
40
43
@@ -73,7 +76,7 @@ Example of customized Python always running in isolated mode::
73
76
74
77
75
78
PyWideStringList
76
- ================
79
+ ----------------
77
80
78
81
.. c :type :: PyWideStringList
79
82
@@ -116,7 +119,7 @@ PyWideStringList
116
119
List items.
117
120
118
121
PyStatus
119
- ========
122
+ --------
120
123
121
124
.. c :type :: PyStatus
122
125
@@ -210,7 +213,7 @@ Example::
210
213
211
214
212
215
PyPreConfig
213
- ===========
216
+ -----------
214
217
215
218
.. c :type :: PyPreConfig
216
219
@@ -360,7 +363,7 @@ PyPreConfig
360
363
.. _c-preinit :
361
364
362
365
Preinitialize Python with PyPreConfig
363
- =====================================
366
+ -------------------------------------
364
367
365
368
The preinitialization of Python:
366
369
@@ -440,7 +443,7 @@ the :ref:`Python UTF-8 Mode <utf8-mode>`::
440
443
441
444
442
445
PyConfig
443
- ========
446
+ --------
444
447
445
448
.. c :type :: PyConfig
446
449
@@ -1349,7 +1352,7 @@ the :option:`-X` command line option.
1349
1352
1350
1353
1351
1354
Initialization with PyConfig
1352
- ============================
1355
+ ----------------------------
1353
1356
1354
1357
Function to initialize Python:
1355
1358
@@ -1461,7 +1464,7 @@ initialization::
1461
1464
.. _init-isolated-conf :
1462
1465
1463
1466
Isolated Configuration
1464
- ======================
1467
+ ----------------------
1465
1468
1466
1469
:c:func: `PyPreConfig_InitIsolatedConfig ` and
1467
1470
:c:func: `PyConfig_InitIsolatedConfig ` functions create a configuration to
@@ -1481,7 +1484,7 @@ to avoid computing the default path configuration.
1481
1484
.. _init-python-config:
1482
1485
1483
1486
Python Configuration
1484
- ====================
1487
+ --------------------
1485
1488
1486
1489
:c:func:`PyPreConfig_InitPythonConfig` and :c:func:`PyConfig_InitPythonConfig`
1487
1490
functions create a configuration to build a customized Python which behaves as
@@ -1499,7 +1502,7 @@ and :ref:`Python UTF-8 Mode <utf8-mode>`
1499
1502
.. _init-path-config:
1500
1503
1501
1504
Python Path Configuration
1502
- =========================
1505
+ -------------------------
1503
1506
1504
1507
:c:type:`PyConfig` contains multiple fields for the path configuration:
1505
1508
@@ -1585,6 +1588,228 @@ The ``__PYVENV_LAUNCHER__`` environment variable is used to set
1585
1588
:c:member:`PyConfig.base_executable`.
1586
1589
1587
1590
1591
+ PyInitConfig C API
1592
+ ==================
1593
+
1594
+ C API to configure the Python initialization (:pep: `741 `).
1595
+
1596
+ .. versionadded:: 3.14
1597
+
1598
+ Create Config
1599
+ -------------
1600
+
1601
+ .. c:struct:: PyInitConfig
1602
+
1603
+ Opaque structure to configure the Python initialization.
1604
+
1605
+
1606
+ .. c:function:: PyInitConfig* PyInitConfig_Create(void)
1607
+
1608
+ Create a new initialization configuration using :ref:`Isolated Configuration
1609
+ <init-isolated-conf>` default values.
1610
+
1611
+ It must be freed by :c:func:`PyInitConfig_Free`.
1612
+
1613
+ Return ``NULL`` on memory allocation failure.
1614
+
1615
+
1616
+ .. c:function:: void PyInitConfig_Free(PyInitConfig *config)
1617
+
1618
+ Free memory of the initialization configuration *config *.
1619
+
1620
+
1621
+ Error Handling
1622
+ --------------
1623
+
1624
+ .. c :function :: int PyInitConfig_GetError (PyInitConfig* config, const char **err_msg)
1625
+
1626
+ Get the *config * error message.
1627
+
1628
+ * Set *\* err_msg * and return ``1 `` if an error is set.
1629
+ * Set *\* err_msg * to ``NULL `` and return ``0 `` otherwise.
1630
+
1631
+ An error message is an UTF-8 encoded string.
1632
+
1633
+ If *config * has an exit code, format the exit code as an error
1634
+ message.
1635
+
1636
+ The error message remains valid until another ``PyInitConfig ``
1637
+ function is called with *config *. The caller doesn't have to free the
1638
+ error message.
1639
+
1640
+
1641
+ .. c :function :: int PyInitConfig_GetExitCode (PyInitConfig* config, int *exitcode)
1642
+
1643
+ Get the *config * exit code.
1644
+
1645
+ * Set *\* exitcode * and return ``1 `` if *config * has an exit code set.
1646
+ * Return ``0 `` if *config * has no exit code set.
1647
+
1648
+ Only the ``Py_InitializeFromInitConfig() `` function can set an exit
1649
+ code if the ``parse_argv `` option is non-zero.
1650
+
1651
+ An exit code can be set when parsing the command line failed (exit
1652
+ code ``2 ``) or when a command line option asks to display the command
1653
+ line help (exit code ``0 ``).
1654
+
1655
+
1656
+ Get Options
1657
+ -----------
1658
+
1659
+ The configuration option *name* parameter must be a non-NULL
1660
+ null-terminated UTF-8 encoded string.
1661
+
1662
+ .. c:function:: int PyInitConfig_HasOption(PyInitConfig *config, const char *name)
1663
+
1664
+ Test if the configuration has an option called *name *.
1665
+
1666
+ Return ``1 `` if the option exists, or return ``0 `` otherwise.
1667
+
1668
+
1669
+ .. c :function :: int PyInitConfig_GetInt (PyInitConfig *config, const char *name, int64_t *value)
1670
+
1671
+ Get an integer configuration option.
1672
+
1673
+ * Set *\* value *, and return ``0 `` on success.
1674
+ * Set an error in *config * and return ``-1 `` on error.
1675
+
1676
+
1677
+ .. c :function :: int PyInitConfig_GetStr (PyInitConfig *config, const char *name, char **value)
1678
+
1679
+ Get a string configuration option as a null-terminated UTF-8
1680
+ encoded string.
1681
+
1682
+ * Set *\* value *, and return ``0 `` on success.
1683
+ * Set an error in *config * and return ``-1 `` on error.
1684
+
1685
+ *\* value * can be set to ``NULL `` if the option is an optional string and the
1686
+ option is unset.
1687
+
1688
+ On success, the string must be released with ``free(value) `` if it's not
1689
+ ``NULL ``.
1690
+
1691
+
1692
+ .. c :function :: int PyInitConfig_GetStrList (PyInitConfig *config, const char *name, size_t *length, char ***items)
1693
+
1694
+ Get a string list configuration option as an array of
1695
+ null-terminated UTF-8 encoded strings.
1696
+
1697
+ * Set *\* length * and *\* value *, and return ``0 `` on success.
1698
+ * Set an error in *config * and return ``-1 `` on error.
1699
+
1700
+ On success, the string list must be released with
1701
+ ``PyInitConfig_FreeStrList(length, items) ``.
1702
+
1703
+
1704
+ .. c :function :: void PyInitConfig_FreeStrList (size_t length, char **items)
1705
+
1706
+ Free memory of a string list created by
1707
+ ``PyInitConfig_GetStrList() ``.
1708
+
1709
+
1710
+ Set Options
1711
+ -----------
1712
+
1713
+ The configuration option *name * parameter must be a non-NULL null-terminated
1714
+ UTF-8 encoded string.
1715
+
1716
+ Some configuration options have side effects on other options. This logic is
1717
+ only implemented when ``Py_InitializeFromInitConfig() `` is called, not by the
1718
+ "Set" functions below. For example, setting ``dev_mode `` to ``1 `` does not set
1719
+ ``faulthandler `` to ``1 ``.
1720
+
1721
+ .. c :function :: int PyInitConfig_SetInt (PyInitConfig *config, const char *name, int64_t value)
1722
+
1723
+ Set an integer configuration option.
1724
+
1725
+ * Return ``0 `` on success.
1726
+ * Set an error in *config * and return ``-1 `` on error.
1727
+
1728
+
1729
+ .. c :function :: int PyInitConfig_SetStr (PyInitConfig *config, const char *name, const char *value)
1730
+
1731
+ Set a string configuration option from a null-terminated UTF-8
1732
+ encoded string. The string is copied.
1733
+
1734
+ * Return ``0 `` on success.
1735
+ * Set an error in *config * and return ``-1 `` on error.
1736
+
1737
+
1738
+ .. c :function :: int PyInitConfig_SetStrList (PyInitConfig *config, const char *name, size_t length, char * const *items)
1739
+
1740
+ Set a string list configuration option from an array of
1741
+ null-terminated UTF-8 encoded strings. The string list is copied.
1742
+
1743
+ * Return ``0 `` on success.
1744
+ * Set an error in *config * and return ``-1 `` on error.
1745
+
1746
+
1747
+ Initialize Python
1748
+ -----------------
1749
+
1750
+ .. c :function :: int Py_InitializeFromInitConfig (PyInitConfig *config)
1751
+
1752
+ Initialize Python from the initialization configuration.
1753
+
1754
+ * Return ``0 `` on success.
1755
+ * Set an error in *config * and return ``-1 `` on error.
1756
+ * Set an exit code in *config * and return ``-1 `` if Python wants to
1757
+ exit.
1758
+
1759
+ See ``PyInitConfig_GetExitcode() `` for the exit code case.
1760
+
1761
+
1762
+ Example
1763
+ -------
1764
+
1765
+ Example initializing Python, set configuration options of various types,
1766
+ return ``-1 `` on error:
1767
+
1768
+ .. code-block :: c
1769
+
1770
+ int init_python(void)
1771
+ {
1772
+ PyInitConfig *config = PyInitConfig_Create();
1773
+ if (config == NULL) {
1774
+ printf("PYTHON INIT ERROR: memory allocation failed\n");
1775
+ return -1;
1776
+ }
1777
+
1778
+ // Set an integer (dev mode)
1779
+ if (PyInitConfig_SetInt(config, "dev_mode", 1) < 0) {
1780
+ goto error;
1781
+ }
1782
+
1783
+ // Set a list of UTF-8 strings (argv)
1784
+ char *argv[] = {"my_program", "-c", "pass"};
1785
+ if (PyInitConfig_SetStrList(config, "argv",
1786
+ Py_ARRAY_LENGTH(argv), argv) < 0) {
1787
+ goto error;
1788
+ }
1789
+
1790
+ // Set a UTF-8 string (program name)
1791
+ if (PyInitConfig_SetStr(config, "program_name", L"my_program") < 0) {
1792
+ goto error;
1793
+ }
1794
+
1795
+ // Initialize Python with the configuration
1796
+ if (Py_InitializeFromInitConfig(config) < 0) {
1797
+ goto error;
1798
+ }
1799
+ PyInitConfig_Free(config);
1800
+ return 0;
1801
+
1802
+ // Display the error message
1803
+ const char *err_msg;
1804
+ error:
1805
+ (void)PyInitConfig_GetError(config, &err_msg);
1806
+ printf("PYTHON INIT ERROR: %s\n", err_msg);
1807
+ PyInitConfig_Free(config);
1808
+
1809
+ return -1;
1810
+ }
1811
+
1812
+
1588
1813
Py_RunMain()
1589
1814
============
1590
1815
0 commit comments