Use PyConfig to initialize Python

This commit is contained in:
Richard Berger
2023-12-08 23:59:41 -07:00
committed by Axel Kohlmeyer
parent e69c65431f
commit 016c9ef4b2

View File

@ -73,16 +73,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
@ -106,12 +109,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