From 1a3d36fdbfbfc274c1d26b0e8e85c5e08fcb3cb6 Mon Sep 17 00:00:00 2001 From: Richard Berger Date: Fri, 8 Dec 2023 23:59:41 -0700 Subject: [PATCH] Use PyConfig to initialize Python --- src/PYTHON/python_impl.cpp | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/PYTHON/python_impl.cpp b/src/PYTHON/python_impl.cpp index 57f8ea1cf0..6dd5175621 100644 --- a/src/PYTHON/python_impl.cpp +++ b/src/PYTHON/python_impl.cpp @@ -61,16 +61,19 @@ PythonImpl::PythonImpl(LAMMPS *lmp) : Pointers(lmp) nfunc = 0; pfuncs = nullptr; -#if PY_MAJOR_VERSION >= 3 -#ifndef Py_LIMITED_API +#if PY_MAJOR_VERSION >= 3 && !defined(Py_LIMITED_API) // check for PYTHONUNBUFFERED environment variable const char *PYTHONUNBUFFERED = getenv("PYTHONUNBUFFERED"); + // Force the stdout and stderr streams to be unbuffered. + bool unbuffered = PYTHONUNBUFFERED != nullptr && strcmp(PYTHONUNBUFFERED, "1") == 0; - if (PYTHONUNBUFFERED != nullptr && strcmp(PYTHONUNBUFFERED, "1") == 0) { - // Python Global configuration variable - // Force the stdout and stderr streams to be unbuffered. - Py_UnbufferedStdioFlag = 1; - } +#if PY_VERSION_HEX >= 0x030800f0 + PyConfig config; + PyConfig_InitPythonConfig(&config); + config.buffered_stdio = !unbuffered; +#else + // Python Global configuration variable + Py_UnbufferedStdioFlag = unbuffered; #endif #endif @@ -94,12 +97,17 @@ PythonImpl::PythonImpl(LAMMPS *lmp) : Pointers(lmp) #endif #endif +#if PY_VERSION_HEX >= 0x030800f0 && !defined(Py_LIMITED_API) + Py_InitializeFromConfig(&config); + PyConfig_Clear(&config); +#else Py_Initialize(); +#endif // only needed for Python 2.x and Python 3 < 3.7 // With Python 3.7 this function is now called by Py_Initialize() // Deprecated since version 3.9, will be removed in version 3.11 -#if PY_MAJOR_VERSION < 3 || PY_MINOR_VERSION < 7 +#if PY_VERSION_HEX < 0x030700f0 if (!PyEval_ThreadsInitialized()) { PyEval_InitThreads(); } #endif