Skip to content
Open
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
42 changes: 42 additions & 0 deletions Include/moduleobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,48 @@ struct PyModuleDef {
freefunc m_free;
};

#if defined(_PyHack_check_version_on_modinit) && defined(Py_BUILD_CORE)
/* The mechanism for the check has been implemented on Python 3.15+:
* https://github.com/python/cpython/pull/137212
* In Fedora, we need this in older Pythons too:
* if somebody attempts to import a module compiled for a different Python version,
* instead of segmentation fault a meaningful error is raised.
*/
PyAPI_DATA(const unsigned long) Py_Version;

static inline int
_PyHack_CheckInternalAPIVersion(const char *mod_name)
{
if (PY_VERSION_HEX != Py_Version) {
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",

mod_name,
(const unsigned long)((PY_VERSION_HEX >> 24) & 0xFF),
(const unsigned long)((PY_VERSION_HEX >> 16) & 0xFF),
(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).

);
return -1;
}
return 0;
}

static inline PyObject *
PyModuleDef_Init_with_check(PyModuleDef *def)
{
if (_PyHack_CheckInternalAPIVersion(def->m_name) < 0) {
return NULL;
}
return PyModuleDef_Init(def);
}

#define PyModuleDef_Init PyModuleDef_Init_with_check
#endif

#ifdef __cplusplus
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions Makefile.pre.in
Original file line number Diff line number Diff line change
Expand Up @@ -3415,3 +3415,6 @@ MODULE__MULTIBYTECODEC_DEPS=$(srcdir)/Modules/cjkcodecs/multibytecodec.h
# Local Variables:
# mode: makefile
# 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

4 changes: 4 additions & 0 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -3489,6 +3489,10 @@ static struct PyModuleDef _tkintermodule = {
PyMODINIT_FUNC
PyInit__tkinter(void)
{
if (_PyHack_CheckInternalAPIVersion("_tkinter") < 0) {
return NULL;
}

PyObject *m, *uexe, *cexe;

tcl_lock = PyThread_allocate_lock();
Expand Down
4 changes: 4 additions & 0 deletions Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ static struct PyModuleDef module_def = {
PyMODINIT_FUNC
PyInit__tracemalloc(void)
{
if (_PyHack_CheckInternalAPIVersion("_tracemalloc") < 0) {
return NULL;
}

PyObject *mod = PyModule_Create(&module_def);
if (mod == NULL) {
return NULL;
Expand Down
4 changes: 4 additions & 0 deletions Modules/readline.c
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,10 @@ static struct PyModuleDef readlinemodule = {
PyMODINIT_FUNC
PyInit_readline(void)
{
if (_PyHack_CheckInternalAPIVersion("readline") < 0) {
return NULL;
}

const char *backend = "readline";
PyObject *m;
readlinestate *mod_state;
Expand Down
1 change: 1 addition & 0 deletions Objects/moduleobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ _PyModule_IsExtension(PyObject *obj)
}


#undef PyModuleDef_Init
PyObject*
PyModuleDef_Init(PyModuleDef* def)
{
Expand Down