From ecaf66e51120b335f005d5d6de3b6284a376fadd Mon Sep 17 00:00:00 2001 From: Alexis Hildebrandt Date: Sun, 25 Feb 2024 17:49:18 +0100 Subject: Fix Python 3.12 deprecation warning MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit as "the global configuration variable Py_UnbufferedStdioFlag was deprecated in Python 3.12 and using PyConfig.buffered_stdio is recommended instead." — https://peps.python.org/pep-0741/ --- src/pyinterp.cc | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/src/pyinterp.cc b/src/pyinterp.cc index 71bbea22..2fecc8c4 100644 --- a/src/pyinterp.cc +++ b/src/pyinterp.cc @@ -149,15 +149,30 @@ void python_interpreter_t::initialize() try { DEBUG("python.interp", "Initializing Python"); + // PyImport_AppendInittab docs: "This should be called before Py_Initialize()". + PyImport_AppendInittab((const char*)"ledger", PyInit_ledger); + // Unbuffer stdio to avoid python output getting stuck in buffer when // stdout is not a TTY. Normally buffers are flushed by Py_Finalize but // Boost has a long-standing issue preventing proper shutdown of the // interpreter with Py_Finalize when embedded. +#if PY_MINOR_VERSION < 12 Py_UnbufferedStdioFlag = 1; - // PyImport_AppendInittab docs: "This should be called before Py_Initialize()". - PyImport_AppendInittab((const char*)"ledger", PyInit_ledger); - Py_Initialize(); +#else + PyStatus status; + PyConfig config; + PyConfig_InitPythonConfig(&config); + config.buffered_stdio = 0; + status = Py_InitializeFromConfig(&config); + if (PyStatus_Exception(status)) { + PyConfig_Clear(&config); + Py_ExitStatusException(status); + return; + } + PyConfig_Clear(&config); +#endif + assert(Py_IsInitialized()); hack_system_paths(); -- cgit v1.2.3