Skip to content

Commit afa38c0

Browse files
committed
PEP 587: add PyConfig.struct_size
Add a new struct_size field to PyPreConfig and PyConfig structures to allow to modify these structures in the future without breaking the backward compatibility. Remove _config_version fields, replaced by struct_size.
1 parent 586717e commit afa38c0

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

pep-0587.rst

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -239,14 +239,23 @@ The ``PyPreConfig`` structure is used to preinitialize Python:
239239
* Configure the LC_CTYPE locale
240240
* Set the UTF-8 mode
241241

242+
The ``struct_size`` field must be explicitly initialized to
243+
``sizeof(PyPreConfig)``.
244+
242245
Example using the preinitialization to enable the UTF-8 Mode::
243246

247+
PyStatus status;
244248
PyPreConfig preconfig;
245-
PyPreConfig_InitPythonConfig(&preconfig);
249+
preconfig.struct_size = sizeof(PyPreConfig);
250+
251+
status = PyPreConfig_InitPythonConfig(&preconfig);
252+
if (PyStatus_Exception(status)) {
253+
Py_ExitStatusException(status);
254+
}
246255

247256
preconfig.utf8_mode = 1;
248257

249-
PyStatus status = Py_PreInitialize(&preconfig);
258+
status = Py_PreInitialize(&preconfig);
250259
if (PyStatus_Exception(status)) {
251260
Py_ExitStatusException(status);
252261
}
@@ -259,8 +268,8 @@ Example using the preinitialization to enable the UTF-8 Mode::
259268

260269
Function to initialize a preconfiguration:
261270

262-
* ``void PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)``
263-
* ``void PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)``
271+
* ``PyStatus PyPreConfig_InitIsolatedConfig(PyPreConfig *preconfig)``
272+
* ``PyStatus PyPreConfig_InitPythonConfig(PyPreConfig *preconfig)``
264273

265274
Functions to preinitialize Python:
266275

@@ -318,6 +327,9 @@ an effect on the pre-configuration like encodings. For example, the
318327
``Py_PreInitializeFromBytesArgs()`` parse their ``argv`` argument the
319328
same way the regular Python parses command line arguments: see
320329
`Command Line Arguments`_.
330+
* ``struct_size`` (``size_t``:
331+
Size of the structure in bytes: must be initialized to
332+
``sizeof(PyPreConfig)``. Field used for API and ABI compatibility.
321333
* ``use_environment`` (``int``):
322334
See ``PyConfig.use_environment``.
323335
* ``utf8_mode`` (``int``):
@@ -328,8 +340,6 @@ The ``legacy_windows_fs_encoding`` field is only available on Windows.
328340

329341
``PyPreConfig`` private fields, for internal use only:
330342

331-
* ``_config_version`` (``int``):
332-
Configuration version, used for ABI compatibility.
333343
* ``_config_init`` (``int``):
334344
Function used to initialize ``PyConfig``, used for preinitialization.
335345

@@ -349,12 +359,16 @@ Initialization with PyConfig
349359

350360
The ``PyConfig`` structure contains most parameters to configure Python.
351361

362+
The ``struct_size`` field must be explicitly initialized to
363+
``sizeof(PyConfig)``.
364+
352365
Example setting the program name::
353366

354367
void init_python(void)
355368
{
356369
PyStatus status;
357370
PyConfig config;
371+
config.struct_size = sizeof(PyConfig);
358372

359373
status = PyConfig_InitPythonConfig(&config);
360374
if (PyStatus_Exception(status)) {
@@ -557,6 +571,9 @@ exceptions (error or exit) using ``PyStatus_Exception()`` and
557571
``stdio_errors`` (``wchar_t*``):
558572
Encoding and encoding errors of ``sys.stdin``, ``sys.stdout``
559573
and ``sys.stderr``.
574+
* ``struct_size`` (``size_t``:
575+
Size of the structure in bytes: must be initialized to
576+
``sizeof(PyConfig)``. Field used for API and ABI compatibility.
560577
* ``tracemalloc`` (``int``):
561578
If non-zero, call ``tracemalloc.start(value)``.
562579
* ``user_site_directory`` (``int``):
@@ -582,8 +599,6 @@ Options`_.
582599

583600
``PyConfig`` private fields, for internal use only:
584601

585-
* ``_config_version`` (``int``):
586-
Configuration version, used for ABI compatibility.
587602
* ``_config_init`` (``int``):
588603
Function used to initialize ``PyConfig``, used for preinitialization.
589604
* ``_install_importlib`` (``int``):
@@ -599,6 +614,7 @@ configuration, and then override some parameters::
599614
{
600615
PyStatus status;
601616
PyConfig config;
617+
config.struct_size = sizeof(PyConfig);
602618

603619
status = PyConfig_InitPythonConfig(&config);
604620
if (PyStatus_Exception(status)) {
@@ -685,8 +701,9 @@ Example of customized Python always running in isolated mode::
685701

686702
int main(int argc, char **argv)
687703
{
688-
PyConfig config;
689704
PyStatus status;
705+
PyConfig config;
706+
config.struct_size = sizeof(PyConfig);
690707

691708
status = PyConfig_InitPythonConfig(&config);
692709
if (PyStatus_Exception(status)) {
@@ -862,6 +879,7 @@ phases::
862879
{
863880
PyStatus status;
864881
PyConfig config;
882+
config.struct_size = sizeof(PyConfig);
865883

866884
status = PyConfig_InitPythonConfig(&config);
867885
if (PyStatus_Exception(status)) {

0 commit comments

Comments
 (0)