From 21436e4fe4c54e709c0c7f8ec39c9a63e85b5f0d Mon Sep 17 00:00:00 2001 From: Charles PIGNEROL <> Date: Wed, 20 Nov 2024 10:31:00 +0100 Subject: [PATCH] Version 6.4.4. QtPython::preInitialize throws an exception if called for the first time after Py_Initialize to avoid a fatal error in Python 3. --- cmake/version.cmake | 4 ++-- src/QtPython3/QtPython.cpp | 20 +++++++++++++++++--- versions.txt | 6 ++++++ 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cmake/version.cmake b/cmake/version.cmake index 7b1e23f..af2624c 100644 --- a/cmake/version.cmake +++ b/cmake/version.cmake @@ -5,12 +5,12 @@ # Pour la bibliothèque QtPython : set (QT_PYTHON_MAJOR_VERSION "6") set (QT_PYTHON_MINOR_VERSION "4") -set (QT_PYTHON_RELEASE_VERSION "3") +set (QT_PYTHON_RELEASE_VERSION "4") set (QT_PYTHON_VERSION ${QT_PYTHON_MAJOR_VERSION}.${QT_PYTHON_MINOR_VERSION}.${QT_PYTHON_RELEASE_VERSION}) # Pour la bibliothèque QtPython3 : set (QT_PYTHON_3_MAJOR_VERSION "6") set (QT_PYTHON_3_MINOR_VERSION "4") -set (QT_PYTHON_3_RELEASE_VERSION "3") +set (QT_PYTHON_3_RELEASE_VERSION "4") set (QT_PYTHON_3_VERSION ${QT_PYTHON_3_MAJOR_VERSION}.${QT_PYTHON_3_MINOR_VERSION}.${QT_PYTHON_3_RELEASE_VERSION}) diff --git a/src/QtPython3/QtPython.cpp b/src/QtPython3/QtPython.cpp index 48cc816..4a47a32 100644 --- a/src/QtPython3/QtPython.cpp +++ b/src/QtPython3/QtPython.cpp @@ -190,9 +190,23 @@ QtPython::~QtPython ( ) void QtPython::preInitialize ( ) // CP v 5.1.0 { - // A faire avant Py_Initialize : - if (-1 == PyImport_AppendInittab ("redirector", PyInit_redirector)) - throw Exception ("Echec de l'importation du module redirector par la console Python."); + static bool preInitialized = false; // v 6.4.4 + + if (false == preInitialized) + { + // A faire avant Py_Initialize : + if (false == Py_IsInitialized ( )) + { + if (-1 == PyImport_AppendInittab ("redirector", PyInit_redirector)) + throw Exception ("Echec de l'importation du module redirector par la console Python."); + } + else + // v 6.4.4. Eviter (python 3.12.5) : Fatal Python error: PyImport_AppendInittab: PyImport_AppendInittab() may not be called after Py_Initialize() + // Python runtime state: initialized + throw Exception ("QtPython::preInitialize doit être appelé avant Py_Initialize. PyImport_AppendInittab non appelé car l'application planterait. La console python ne devrait pas fonctionner correctement."); + + preInitialized = true; + } // if (false == preInitialized) } // QtPython::preInitialize diff --git a/versions.txt b/versions.txt index 5d6678d..bc851b0 100644 --- a/versions.txt +++ b/versions.txt @@ -1,3 +1,9 @@ +Version 6.4.4 : 20/11/24 +=============== + +QtPython::preInitialize lève une exception si appelée pour la première fois après Py_Initialize en vue d'éviter un fatal error de python 3. + + Version 6.4.3 : 04/10/24 ===============