Py: make FreeCAD to compile with Py3.11

This commit is contained in:
wmayer
2022-07-01 17:54:50 +02:00
parent 19a69df0fc
commit 1ae55905ba
6 changed files with 97 additions and 3 deletions

View File

@@ -266,7 +266,13 @@ std::stringstream &LogLevel::prefix(std::stringstream &str, const char *src, int
PyFrameObject* frame = PyEval_GetFrame();
if (frame) {
line = PyFrame_GetLineNumber(frame);
#if PY_VERSION_HEX < 0x030b0000
src = PyUnicode_AsUTF8(frame->f_code->co_filename);
#else
PyCodeObject* code = PyFrame_GetCode(frame);
src = PyUnicode_AsUTF8(code->co_filename);
Py_DECREF(code);
#endif
}
}
if (print_src && src && src[0]) {

View File

@@ -523,6 +523,7 @@ void InterpreterSingleton::addPythonPath(const char* Path)
list.append(Py::String(Path));
}
#if PY_VERSION_HEX < 0x030b0000
const char* InterpreterSingleton::init(int argc,char *argv[])
{
if (!Py_IsInitialized()) {
@@ -565,6 +566,61 @@ const char* InterpreterSingleton::init(int argc,char *argv[])
PyGILStateLocker lock;
return Py_EncodeLocale(Py_GetPath(),nullptr);
}
#else
namespace {
void initInterpreter(int argc,char *argv[])
{
PyStatus status;
PyConfig config;
PyConfig_InitPythonConfig(&config);
status = PyConfig_SetBytesArgv(&config, argc, argv);
if (PyStatus_Exception(status)) {
throw Base::RuntimeError("Failed to set config");
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
throw Base::RuntimeError("Failed to init from config");
}
PyConfig_Clear(&config);
Py_Initialize();
const char* virtualenv = getenv("VIRTUAL_ENV");
if (virtualenv) {
PyRun_SimpleString(
"# Check for virtualenv, and activate if present.\n"
"# See https://virtualenv.pypa.io/en/latest/userguide/#using-virtualenv-without-bin-python\n"
"import os\n"
"import sys\n"
"base_path = os.getenv(\"VIRTUAL_ENV\")\n"
"if not base_path is None:\n"
" activate_this = os.path.join(base_path, \"bin\", \"activate_this.py\")\n"
" exec(open(activate_this).read(), {'__file__':activate_this})\n"
);
}
}
}
const char* InterpreterSingleton::init(int argc,char *argv[])
{
try {
if (!Py_IsInitialized()) {
initInterpreter(argc, argv);
PythonStdOutput::init_type();
this->_global = PyEval_SaveThread();
}
PyGILStateLocker lock;
return Py_EncodeLocale(Py_GetPath(),nullptr);
}
catch (const Base::Exception& e) {
e.ReportException();
throw;
}
}
#endif
void InterpreterSingleton::replaceStdOutput()
{

View File

@@ -60,7 +60,11 @@ PyObjectBase::PyObjectBase(void* p,PyTypeObject *T)
, baseProxy(nullptr)
, attrDict(nullptr)
{
#if PY_VERSION_HEX < 0x030b0000
Py_TYPE(this) = T;
#else
Py_SET_TYPE(this, T);
#endif
_Py_NewReference(this);
#ifdef FC_LOGPYOBJECTS
Base::Console().Log("PyO+: %s (%p)\n",T->tp_name, this);

View File

@@ -15,8 +15,10 @@ is provided on an as is basis, without warranties of any kind.
#include <string.h>
#include <assert.h>
#include <compile.h>
#include <eval.h>
#include <frameobject.h>
#if PY_VERSION_HEX < 0x030b0000
#include <eval.h>
#endif
/*****************************************************************************
@@ -310,7 +312,13 @@ void PP_Fetch_Error_Text()
if(!frame)
return;
int line = PyFrame_GetLineNumber(frame);
#if PY_VERSION_HEX < 0x030b0000
const char *file = PyUnicode_AsUTF8(frame->f_code->co_filename);
#else
PyCodeObject* code = PyFrame_GetCode(frame);
const char *file = PyUnicode_AsUTF8(code->co_filename);
Py_DECREF(code);
#endif
#ifdef FC_OS_WIN32
const char *_f = strstr(file, "\\src\\");
#else