diff options
author | Alexis Hildebrandt <afh@surryhill.net> | 2024-02-25 17:49:18 +0100 |
---|---|---|
committer | John Wiegley <johnw@newartisans.com> | 2024-07-08 10:12:07 -0700 |
commit | ecaf66e51120b335f005d5d6de3b6284a376fadd (patch) | |
tree | 539cb7f2b482c31ec55d0bf69f0d8b9d1f53bf94 | |
parent | 815305b94864556c994934c7a7c830da7de1d94e (diff) | |
download | fork-ledger-ecaf66e51120b335f005d5d6de3b6284a376fadd.tar.gz fork-ledger-ecaf66e51120b335f005d5d6de3b6284a376fadd.tar.bz2 fork-ledger-ecaf66e51120b335f005d5d6de3b6284a376fadd.zip |
Fix Python 3.12 deprecation warning
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/
-rw-r--r-- | src/pyinterp.cc | 21 |
1 files 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(); |