From f88ee28d622ca383db5e6f3d856343533eb2c4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorbj=C3=B8rn=20Lindeijer?= Date: Thu, 2 May 2024 17:28:25 +0200 Subject: [PATCH] Python plugin: Raised minimum Python version to 3.8 Fixed deprecation warnings by replacing `Py_NoSiteFlag` and `Py_NoUserSiteDirectory` variables with their `PyConfig` equivalent, supported since Python 3.8. --- NEWS.md | 1 + qbs/imports/PythonProbe.qbs | 7 +++---- src/plugins/python/python.qbs | 17 +++-------------- src/plugins/python/pythonplugin.cpp | 18 ++++++++++++++---- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/NEWS.md b/NEWS.md index 83dddd0e88..0e3a758ba5 100644 --- a/NEWS.md +++ b/NEWS.md @@ -16,6 +16,7 @@ * Godot 4 plugin: Use Godot 4.2 tile transformation flags (by Rick Yorgason, #3895) * Godot 4 plugin: Fixed positioning of tile collision shapes (by Ryan Petrie, #3862) * GameMaker 2 plugin: Fixed positioning of objects on isometric maps +* Python plugin: Raised minimum Python version to 3.8 * tmxrasterizer: Added --hide-object and --show-object arguments (by Lars Luz, #3819) * tmxrasterizer: Added --frames and --frame-duration arguments to export animated maps as multiple images (#3868) * tmxrasterizer: Fixed --hide/show-layer to work on group layers (#3899) diff --git a/qbs/imports/PythonProbe.qbs b/qbs/imports/PythonProbe.qbs index e0a649ec5b..52eae7fc32 100644 --- a/qbs/imports/PythonProbe.qbs +++ b/qbs/imports/PythonProbe.qbs @@ -7,10 +7,9 @@ import qbs.Utilities Probe { id: pythonDllProbe - condition: qbs.targetOS.contains("windows") - property string pythonDir: pythonInstallDir // Input property string buildVariant: qbs.buildVariant // Input + property string minVersion: "3.5" // Input property string fileNamePrefix // Output configure: { @@ -45,9 +44,9 @@ Probe { + output + "'"); return; } - if (Utilities.versionCompare(versionNumberString, "3.5") < 0) { + if (Utilities.versionCompare(versionNumberString, minVersion) < 0) { printWarning("The Python installation at '" + pythonDir - + "' has version " + versionNumberString + ", but 3.5 or higher " + + "' has version " + versionNumberString + ", but " + minVersion + " or higher " + "is required."); return; } diff --git a/src/plugins/python/python.qbs b/src/plugins/python/python.qbs index f8b70c0b9b..99d23411b0 100644 --- a/src/plugins/python/python.qbs +++ b/src/plugins/python/python.qbs @@ -10,7 +10,7 @@ TiledPlugin { if (qbs.targetOS.contains("windows")) return pythonDllProbe.found; - return pkgConfigPython3Embed.found || pkgConfigPython3.found; + return pkgConfigPython3.found; } cpp.cxxFlags: { @@ -22,25 +22,14 @@ TiledPlugin { Probes.PkgConfigProbe { id: pkgConfigPython3 - name: "python3" - } - - Probes.PkgConfigProbe { - id: pkgConfigPython3Embed name: "python3-embed" + minVersion: "3.8" } PythonProbe { id: pythonDllProbe pythonDir: Environment.getEnv("PYTHONHOME") - } - - Properties { - condition: pkgConfigPython3Embed.found - cpp.cxxFlags: outer.concat(pkgConfigPython3Embed.cflags) - cpp.dynamicLibraries: pkgConfigPython3Embed.libraries - cpp.libraryPaths: pkgConfigPython3Embed.libraryPaths - cpp.linkerFlags: pkgConfigPython3Embed.linkerFlags + minVersion: "3.8" } Properties { diff --git a/src/plugins/python/pythonplugin.cpp b/src/plugins/python/pythonplugin.cpp index 2148681638..363fd7ca14 100644 --- a/src/plugins/python/pythonplugin.cpp +++ b/src/plugins/python/pythonplugin.cpp @@ -76,14 +76,24 @@ PythonPlugin::~PythonPlugin() void PythonPlugin::initialize() { if (!Py_IsInitialized()) { - // PEP370 - Py_NoSiteFlag = 1; - Py_NoUserSiteDirectory = 1; + // PEP 587 + PyConfig config; + PyConfig_InitPythonConfig(&config); + + // PEP 370 + config.site_import = 0; + config.user_site_directory = 0; PyImport_AppendInittab("tiled", PyInit_tiled); PyImport_AppendInittab("tiled.qt", PyInit_tiled); PyImport_AppendInittab("tiled.Tiled", PyInit_tiled); - Py_Initialize(); + + PyStatus status = Py_InitializeFromConfig(&config); + if (PyStatus_Exception(status)) { + Tiled::ERROR("Python initialization failed"); + handleError(); + return; + } PyObject *pmod = PyImport_ImportModule("tiled");