Skip to content

Commit 040e394

Browse files
committed
[SofaPython3] Use the pybindapi instead of cpython's one for getCallpoint
1 parent c498333 commit 040e394

1 file changed

Lines changed: 9 additions & 14 deletions

File tree

Plugin/src/SofaPython3/PythonEnvironment.cpp

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ class PythonEnvironmentData
103103

104104
std::set<std::string> addedPath;
105105

106-
py::module m_sofamodule ;
106+
py::module m_sofaModule ;
107+
py::module m_sofaRuntimeModule ;
107108
private:
108109
std::vector<wchar_t*> m_argv;
109110
};
@@ -237,7 +238,8 @@ void PythonEnvironment::Init()
237238
// Lastly, we (try to) add modules from the root of SOFA
238239
addPythonModulePathsFromDirectory( Utils::getSofaPathPrefix() );
239240

240-
getStaticData()->m_sofamodule = py::module::import("Sofa");
241+
getStaticData()->m_sofaModule = py::module::import("Sofa");
242+
getStaticData()->m_sofaRuntimeModule = py::module::import("SofaRuntime");
241243
PyRun_SimpleString("import SofaRuntime");
242244

243245
// python livecoding related
@@ -508,22 +510,15 @@ std::string PythonEnvironment::getStackAsString()
508510

509511
std::string PythonEnvironment::getPythonCallingPointString()
510512
{
511-
return py::cast<std::string>(getStaticData()->m_sofamodule.attr("getPythonCallingPointAsString")());
513+
gil lock;
514+
return py::cast<std::string>(getStaticData()->m_sofaModule.attr("getPythonCallingPointAsString")());
512515
}
513516

514517
sofa::helper::logging::FileInfo::SPtr PythonEnvironment::getPythonCallingPointAsFileInfo()
515518
{
516-
PyObject* pDict = PyModule_GetDict(PyImport_AddModule("SofaRuntime"));
517-
PyObject* res = PyDict_GetItemString(pDict, "getPythonCallingPoint");
518-
if(res && PySequence_Check(res) ){
519-
PyObject* filename = PySequence_GetItem(res, 0) ;
520-
PyObject* number = PySequence_GetItem(res, 1) ;
521-
std::string tmp=PyBytes_AsString(filename);
522-
auto lineno = PyLong_AsLong(number);
523-
Py_DECREF(res) ;
524-
return SOFA_FILE_INFO_COPIED_FROM(tmp, lineno);
525-
}
526-
return SOFA_FILE_INFO_COPIED_FROM("undefined", -1);
519+
gil lock;
520+
py::tuple cp = getStaticData()->m_sofaRuntimeModule.attr("getPythonCallingPoint")();
521+
return SOFA_FILE_INFO_COPIED_FROM(py::cast<std::string>(cp[0]), py::cast<int>(cp[1]));
527522
}
528523

529524
void PythonEnvironment::setArguments(const std::string& filename, const std::vector<std::string>& arguments)

0 commit comments

Comments
 (0)