From 0351a1ee7d2c7f0a96a2f9d45bf120c0e6222e0c Mon Sep 17 00:00:00 2001 From: abn Date: Wed, 16 Jan 2019 09:16:16 +0100 Subject: [PATCH] Porting C++ API side of the tool --- tools/CurvePlot/src/cpp/CurvePlot.cxx | 27 +++++++++++-------- tools/CurvePlot/src/cpp/CurvePlot.hxx | 2 +- .../CurvePlot/src/cpp/test/test_curveplot.cxx | 15 ++++++++--- 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/tools/CurvePlot/src/cpp/CurvePlot.cxx b/tools/CurvePlot/src/cpp/CurvePlot.cxx index fe32da67a..9b401088b 100644 --- a/tools/CurvePlot/src/cpp/CurvePlot.cxx +++ b/tools/CurvePlot/src/cpp/CurvePlot.cxx @@ -21,7 +21,9 @@ #include -#define PY_ARRAY_UNIQUE_SYMBOL CURVEPLOT_ARRAY_API // see initializeCurvePlot() +// see https://docs.scipy.org/doc/numpy/reference/c-api.array.html?highlight=import_array +// and https://docs.scipy.org/doc/numpy-1.15.0/reference/c-api.deprecations.html +#define PY_ARRAY_UNIQUE_SYMBOL CURVEPLOT_ARRAY_API #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION #include #include @@ -52,12 +54,13 @@ namespace CURVEPLOT /** * To be called before doing anything */ - void InitializeCurvePlot() + void* InitializeCurvePlot() { PyLockWrapper lock; // TODO: discuss where the below should really happen: // doc: http://docs.scipy.org/doc/numpy/reference/c-api.array.html#importing-the-api - import_array(); // a macro really! + import_array(); // a macro really which contains a return + return NULL; } class ColumnVector::Internal @@ -153,9 +156,11 @@ namespace CURVEPLOT PyObject_CallMethod((PyObject *)_impl->_npArray, (char *)"__str__", NULL) ); // Now extract the returned string - if(!PyString_Check(ret_py)) + if(!PyUnicode_Check(ret_py)) throw Exception("CurvePlot::toStdString(): Unexpected returned type!"); - ret_str = std::string(PyString_AsString(ret_py)); + Py_ssize_t size; + char *ptr = PyUnicode_AsUTF8AndSize(ret_py, &size); + ret_str = std::string(ptr); } return ret_str; } @@ -188,13 +193,13 @@ namespace CURVEPLOT CurvePlot::CurvePlot(bool test_mode) { - // TODO: do use an intermediate variable '__cont', but use directly Py***CallMethod() + // TODO: do not use an intermediate variable '__cont', but use directly Py***CallMethod() _impl = new Internal(); { PyLockWrapper lock; std::string code; if (test_mode) - code = std::string("import curveplot; from SalomePyQt_MockUp import SalomePyQt;") + + code = std::string("import curveplot; from curveplot.SalomePyQt_MockUp import SalomePyQt;") + std::string("__cont=curveplot.PlotController.GetInstance(sgPyQt=SalomePyQt())"); else code = std::string("import curveplot;")+ @@ -266,13 +271,13 @@ namespace CURVEPLOT if(!PyTuple_Check(ret)) throw Exception("CurvePlot::AddCurve(): Unexpected returned type!"); PyObject * o1 = PyTuple_GetItem(ret, 0); - if (!PyInt_Check(o1)) + if (!PyLong_Check(o1)) throw Exception("CurvePlot::AddCurve(): Unexpected returned type!"); - PlotID curveId = PyInt_AsLong(o1); + PlotID curveId = PyLong_AsLong(o1); PyObject * o2 = PyTuple_GetItem(ret, 1); - if (!PyInt_Check(o2)) + if (!PyLong_Check(o2)) throw Exception("CurvePlot::AddCurve(): Unexpected returned type!"); - plot_set_id = PyInt_AsLong(o2); + plot_set_id = PyLong_AsLong(o2); return curveId; } diff --git a/tools/CurvePlot/src/cpp/CurvePlot.hxx b/tools/CurvePlot/src/cpp/CurvePlot.hxx index 2b3fbb23a..5f0d4569d 100644 --- a/tools/CurvePlot/src/cpp/CurvePlot.hxx +++ b/tools/CurvePlot/src/cpp/CurvePlot.hxx @@ -32,7 +32,7 @@ namespace CURVEPLOT /** * This function should be called before doing anything in the CURVEPLOT namespace. */ - void InitializeCurvePlot(); + void* InitializeCurvePlot(); class ColumnVector { diff --git a/tools/CurvePlot/src/cpp/test/test_curveplot.cxx b/tools/CurvePlot/src/cpp/test/test_curveplot.cxx index 3954b7d1c..13409c4ce 100644 --- a/tools/CurvePlot/src/cpp/test/test_curveplot.cxx +++ b/tools/CurvePlot/src/cpp/test/test_curveplot.cxx @@ -19,9 +19,10 @@ // Author : Adrien BRUNETON // +#include // GUI - must come first to avoid conflict on the token "slots" defined in both Qt and Python ... + #include "test_curveplot.hxx" -#include // GUI #include #include #include "CurvePlot.hxx" @@ -70,14 +71,20 @@ void TestCurvePlot::onClicked() std::cout << "setting X label " << CurvePlot::SetXLabel("tôtô") << std::endl; } +/*! + * Similar to: SUIT_PYTHON::init_python() from GUI + * or KERNEL_PYTHON::init_python() from KERNEL + */ void initPython() { if (!Py_IsInitialized()){ // Python is not initialized Py_Initialize(); // Initialize the interpreter - PyEval_InitThreads(); // Create (and acquire) the Python global interpreter lock (GIL) - PyEval_ReleaseLock(); + PyRun_SimpleString("import threading\n"); + + PyThreadState *pts = PyGILState_GetThisThreadState(); + PyEval_ReleaseThread(pts); } } @@ -107,7 +114,7 @@ int main(int argc, char ** argv) mw.resize((int)(dw->width()*0.25), (int)(dw->height()*0.7)); mw.show(); - initPython(); + initPython(); // mimic SALOME Python's initialisation. InitializeCurvePlot(); { -- 2.39.2