Skip to content

Raise an error when importing modules compiled for an older Python#139

Open
befeleme wants to merge 1 commit intofedora-python:fedora-3.14from
befeleme:disallow-running-update
Open

Raise an error when importing modules compiled for an older Python#139
befeleme wants to merge 1 commit intofedora-python:fedora-3.14from
befeleme:disallow-running-update

Conversation

@befeleme
Copy link

@befeleme befeleme commented Feb 6, 2026

This is a downstream workaround for
python#128341 -
it is resolved for Python 3.15+, but the issue happened again in the update from 3.14.2 to 3.14.3 (segfault when importing _pickle).

@befeleme befeleme force-pushed the disallow-running-update branch from 28a5464 to 9dc0ede Compare February 6, 2026 09:57
@hroncok
Copy link
Member

hroncok commented Feb 6, 2026

[root@216b304ef039 /]# for module in /usr/lib64/python3.14/lib-dynload/*.so; do strings $module | grep -qF 'internal Python C API version mismatch' || echo $module BAD; done
/usr/lib64/python3.14/lib-dynload/_posixshmem.cpython-314-x86_64-linux-gnu.so BAD
/usr/lib64/python3.14/lib-dynload/_statistics.cpython-314-x86_64-linux-gnu.so BAD
/usr/lib64/python3.14/lib-dynload/_uuid.cpython-314-x86_64-linux-gnu.so BAD
/usr/lib64/python3.14/lib-dynload/fcntl.cpython-314-x86_64-linux-gnu.so BAD
/usr/lib64/python3.14/lib-dynload/termios.cpython-314-x86_64-linux-gnu.so BAD

This is a downstream workaround "implementing"
python#137212 -
the mechanism for the check exists in Python 3.15+, where it needs to be
added to the standard library modules.
In Fedora, we need it also in previous Python versions, as we experience
segmentation fault when importing stdlib modules after update while
Python is running.

_tkinter, _tracemalloc and readline are not calling PyModuleDef_Init,
which is modified with this patch, hence they need a
direct call to the check function.
@befeleme befeleme force-pushed the disallow-running-update branch from 9dc0ede to 0505ac8 Compare February 6, 2026 15:16
@befeleme
Copy link
Author

befeleme commented Feb 6, 2026

TODO: patch for 3.13

Comment on lines 120 to 122
/* Workaround for https://github.com/python/cpython/issues/128341
* This occurred again with the update from 3.14.2 to 3.14.3.
* Instead of a segmentation fault, meaningful error is raised.
Copy link

Choose a reason for hiding this comment

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

Suggested change
/* Workaround for https://github.com/python/cpython/issues/128341
* This occurred again with the update from 3.14.2 to 3.14.3.
* Instead of a segmentation fault, meaningful error is raised.
/* Workaround for https://github.com/python/cpython/issues/128341.
* This occurred again with the update from 3.14.2 to 3.14.3.
* Instead of a segmentation fault, meaningful error is raised.

# End:

# Fedora-specific, downstream only
CFLAGS += -D_PyHack_check_version_on_modinit=1
Copy link

Choose a reason for hiding this comment

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

You should use PY_STDMODULE_CFLAGS to not leak the flag to flags used to build 3rd party extension (setuptools & cie).

Suggested change
CFLAGS += -D_PyHack_check_version_on_modinit=1
PY_STDMODULE_CFLAGS += -D_PyHack_check_version_on_modinit=1

PyErr_Format(
PyExc_ImportError,
"internal Python C API version mismatch: "
"module %s compiled with %lu.%lu.%lu; runtime version is %lu.%lu.%lu",
Copy link

Choose a reason for hiding this comment

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

Suggested change
"module %s compiled with %lu.%lu.%lu; runtime version is %lu.%lu.%lu",
"module %s compiled with %lu.%lu.%lu; "
"runtime version is %lu.%lu.%lu",

(const unsigned long)((PY_VERSION_HEX >> 8) & 0xFF),
(const unsigned long)((Py_Version >> 24) & 0xFF),
(const unsigned long)((Py_Version >> 16) & 0xFF),
(const unsigned long)((Py_Version >> 8) & 0xFF)
Copy link

Choose a reason for hiding this comment

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

The format major.minor.micro lacks PY_RELEASE_LEVEL and PY_RELEASE_SERIAL which is need to distinguish two alpha releases. But since this change is only for Python 3.14, it's fine to fine the release level/serial (we don't ship 3.14 alpha releases anymore).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants