Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-38326: Use Python API version as config version #16496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions Doc/c-api/apiabiversion.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
API and ABI Versioning
***********************

``PY_VERSION_HEX`` is the Python version number encoded in a single integer.
.. c:macro:: PY_VERSION_HEX

For example if the ``PY_VERSION_HEX`` is set to ``0x030401a2``, the underlying
``PY_VERSION_HEX`` is the Python version number encoded in a single integer.

For example if ``PY_VERSION_HEX`` is set to ``0x030401a2``, the underlying
version information can be found by treating it as a 32 bit number in
the following manner:

Expand Down
30 changes: 15 additions & 15 deletions Doc/c-api/init_config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ PyPreConfig
* Configure the LC_CTYPE locale
* Set the UTF-8 mode

The :c:member:`struct_size` field must be explicitly initialized to
``sizeof(PyPreConfig)``.
The :c:member:`header_version` field must be explicitly initialized to
:c:macro:`PY_VERSION_HEX`.

Function to initialize a preconfiguration:

Expand Down Expand Up @@ -274,10 +274,10 @@ PyPreConfig
same way the regular Python parses command line arguments: see
:ref:`Command Line Arguments <using-on-cmdline>`.

.. c:member:: size_t struct_size
.. c:member:: size_t header_version

Size of the structure in bytes: must be initialized to
``sizeof(PyPreConfig)``.
The version of the CPython headers used to build the embedding
application. Must be initialized to :c:macro:`PY_VERSION_HEX`.

Field used for API and ABI compatibility.

Expand Down Expand Up @@ -332,7 +332,7 @@ Example using the preinitialization to enable the UTF-8 Mode::

PyStatus status;
PyPreConfig preconfig;
preconfig.struct_size = sizeof(PyPreConfig);
preconfig.header_version = PY_VERSION_HEX;

status = PyPreConfig_InitPythonConfig(&preconfig);
if (PyStatus_Exception(status)) {
Expand Down Expand Up @@ -360,8 +360,8 @@ PyConfig

Structure containing most parameters to configure Python.

The :c:member:`struct_size` field must be explicitly initialized to
``sizeof(PyConfig)``.
The :c:member:`header_version` field must be explicitly initialized to
:c:macro:`PY_VERSION_HEX`.

Structure methods:

Expand Down Expand Up @@ -679,10 +679,10 @@ PyConfig
Encoding and encoding errors of :data:`sys.stdin`, :data:`sys.stdout` and
:data:`sys.stderr`.

.. c:member:: size_t struct_size
.. c:member:: size_t header_version

Size of the structure in bytes: must be initialized to
``sizeof(PyConfig)``.
The version of the CPython headers used to build the embedding
application. Must be initialized to :c:macro:`PY_VERSION_HEX`.

Field used for API and ABI compatibility.

Expand Down Expand Up @@ -754,7 +754,7 @@ Example setting the program name::
{
PyStatus status;
PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

status = PyConfig_InitPythonConfig(&config);
if (PyStatus_Exception(status)) {
Expand Down Expand Up @@ -787,7 +787,7 @@ configuration, and then override some parameters::
{
PyStatus status;
PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

status = PyConfig_InitPythonConfig(&config);
if (PyStatus_Exception(status)) {
Expand Down Expand Up @@ -875,7 +875,7 @@ Example of customized Python always running in isolated mode::
{
PyStatus status;
PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

status = PyConfig_InitPythonConfig(&config);
if (PyStatus_Exception(status)) {
Expand Down Expand Up @@ -1067,7 +1067,7 @@ phases::
{
PyStatus status;
PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

status = PyConfig_InitPythonConfig(&config);
if (PyStatus_Exception(status)) {
Expand Down
14 changes: 8 additions & 6 deletions Include/cpython/initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ PyAPI_FUNC(PyStatus) PyWideStringList_Insert(PyWideStringList *list,
/* --- PyPreConfig ----------------------------------------------- */

typedef struct {
/* Size of the structure in bytes: must be initialized to
sizeof(PyPreConfig). Field used for API and ABI compatibility. */
size_t struct_size;
/* Version of the CPython header files used to compile the embedding
application. Expected to be set to PY_VERSION_HEX.
Field is used to check for API and ABI compatibility. */
uint32_t header_version;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why uint32_t? Your PEP changes say size_t.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo on the PEP PR side (I renamed struct_size without fixing the type).


int _config_init; /* _PyConfigInitEnum value */

Expand Down Expand Up @@ -131,9 +132,10 @@ PyAPI_FUNC(PyStatus) PyPreConfig_InitIsolatedConfig(PyPreConfig *config);
/* --- PyConfig ---------------------------------------------- */

typedef struct {
/* Size of the structure in bytes: must be initialized to
sizeof(PyConfig). Field used for API and ABI compatibility. */
size_t struct_size;
/* Version of the CPython header files used to compile the embedding
application. Expected to be set to PY_VERSION_HEX.
Field is used to check for API and ABI compatibility. */
uint32_t header_version;

int _config_init; /* _PyConfigInitEnum value */

Expand Down
2 changes: 2 additions & 0 deletions Include/internal/pycore_initconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ PyAPI_FUNC(void) _Py_get_env_flag(
int *flag,
const char *name);

PyAPI_FUNC(PyStatus) _Py_CheckVersionCompat(uint32_t header_version);

/* Py_GetArgcArgv() helper */
PyAPI_FUNC(void) _Py_ClearArgcArgv(void);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Replaced the ``struct_size`` field in :c:type:`PyPreConfig` and
:c:type:`PyConfig` with a ``header_version`` field (accepting
``PY_VERSION_HEX``). This allows the config functions to detect all violations
of the full API/ABI compatibility expectations by embedding applications, not
just changes to the size of the configuration structures themselves.
4 changes: 2 additions & 2 deletions Modules/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ pymain_init(const _PyArgv *args)
#endif

PyPreConfig preconfig;
preconfig.struct_size = sizeof(PyPreConfig);
preconfig.header_version = PY_VERSION_HEX;

status = PyPreConfig_InitPythonConfig(&preconfig);
if (_PyStatus_EXCEPTION(status)) {
Expand All @@ -66,7 +66,7 @@ pymain_init(const _PyArgv *args)
}

PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;
status = PyConfig_InitPythonConfig(&config);
if (_PyStatus_EXCEPTION(status)) {
goto done;
Expand Down
4 changes: 2 additions & 2 deletions PC/python_uwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,10 @@ wmain(int argc, wchar_t **argv)
PyStatus status;

PyPreConfig preconfig;
preconfig.struct_size = sizeof(PyPreConfig);
preconfig.header_version = PY_VERSION_HEX;

PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

const wchar_t *moduleName = NULL;
const wchar_t *p = wcsrchr(argv[0], L'\\');
Expand Down
2 changes: 1 addition & 1 deletion Programs/_freeze_importlib.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ main(int argc, char *argv[])

PyStatus status;
PyConfig config;
config.struct_size = sizeof(PyConfig);
config.header_version = PY_VERSION_HEX;

status = PyConfig_InitIsolatedConfig(&config);
if (PyStatus_Exception(status)) {
Expand Down
Loading