Skip to content

Commit

Permalink
Some minor cleaning up.
Browse files Browse the repository at this point in the history
  • Loading branch information
agarny committed Oct 24, 2019
1 parent a86786e commit 95f6279
Show file tree
Hide file tree
Showing 10 changed files with 157 additions and 201 deletions.
54 changes: 28 additions & 26 deletions src/plugins/dataStore/DataStore/src/datastorepythonwrapper.cpp
Expand Up @@ -23,10 +23,6 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

#include <Qt>

//==============================================================================

#include "pythonbegin.h"

//==============================================================================
// Note: yes, these two header files must be included in this order...

Expand Down Expand Up @@ -81,15 +77,17 @@ static PyObject * DataStoreValuesDict_subscript(PyObject *pValuesDict,
return PyFloat_FromDouble(dataStoreValue->value());
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================

typedef struct {
PyDictObject mDict;
SimulationSupport::SimulationDataUpdatedFunction *mSimulationDataUpdatedFunction;
} DataStoreValuesDictObject;
using DataStoreValuesDictObject = struct {
PyDictObject mDict;
SimulationSupport::SimulationDataUpdatedFunction *mSimulationDataUpdatedFunction;
};

//==============================================================================

Expand All @@ -107,7 +105,9 @@ static int DataStoreValuesDict_ass_subscript(PyObject *pValuesDict,
auto dataStoreValue = getDataStoreValue(pValuesDict, pKey);

if (dataStoreValue != nullptr) {
auto newValue = PyFloat_AS_DOUBLE(PyNumber_Float(pValue));
#include "pythonbegin.h"
auto newValue = PyFloat_AS_DOUBLE(PyNumber_Float(pValue)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (!qFuzzyCompare(dataStoreValue->value(), newValue)) {
dataStoreValue->setValue(newValue);
Expand Down Expand Up @@ -140,13 +140,14 @@ static PyMappingMethods DataStoreValuesDict_as_mapping = {

//==============================================================================

#include "pythonbegin.h"
static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDict)
{
// A string representation of a values dictionary
// Note: this is a modified version of dict_repr() from dictobject.c in the
// Python source code...

PyDictObject *mp = reinterpret_cast<PyDictObject *>(pValuesDict);
auto mp = reinterpret_cast<PyDictObject *>(pValuesDict);
Py_ssize_t i = Py_ReprEnter(reinterpret_cast<PyObject *>(mp));
PyObject *key = nullptr;
PyObject *value = nullptr;
Expand All @@ -172,7 +173,7 @@ static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDic
writer.min_length = 1+4+(2+4)*(mp->ma_used-1)+1;

if (_PyUnicodeWriter_WriteChar(&writer, '{') < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

while (PyDict_Next(reinterpret_cast<PyObject *>(mp), &i, &key, &value)) {
Expand All @@ -184,7 +185,7 @@ static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDic

if (!first) {
if (_PyUnicodeWriter_WriteASCIIString(&writer, ", ", 2) < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}
}

Expand All @@ -193,19 +194,19 @@ static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDic
s = PyObject_Repr(key);

if (s == nullptr) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

res = _PyUnicodeWriter_WriteStr(&writer, s);

Py_DECREF(s);

if (res < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

if (_PyUnicodeWriter_WriteASCIIString(&writer, ": ", 2) < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

auto wrappedValue = PythonQtSupport::getInstanceWrapper(value);
Expand All @@ -221,15 +222,15 @@ static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDic
s = PyObject_Repr(value);

if (s == nullptr) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

res = _PyUnicodeWriter_WriteStr(&writer, s);

Py_DECREF(s);

if (res < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

Py_CLEAR(key);
Expand All @@ -239,22 +240,23 @@ static PyObject * DataStoreValuesDict_repr(DataStoreValuesDictObject *pValuesDic
writer.overallocate = 0;

if (_PyUnicodeWriter_WriteChar(&writer, '}') < 0) {
goto error;
goto error; // NOLINT(cppcoreguidelines-avoid-goto)
}

Py_ReprLeave((PyObject *)mp);
Py_ReprLeave((PyObject *)mp); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)

return _PyUnicodeWriter_Finish(&writer);

error:
Py_ReprLeave((PyObject *)mp);
Py_ReprLeave((PyObject *)mp); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
_PyUnicodeWriter_Dealloc(&writer);

Py_XDECREF(key);
Py_XDECREF(value);

return nullptr;
}
#include "pythonend.h"

//==============================================================================
// Note: a DataStoreValuesDict is a dictionary sub-class for mapping between the
Expand Down Expand Up @@ -374,7 +376,9 @@ PyObject * DataStorePythonWrapper::values(DataStoreVariable *pDataStoreVariable,
return numPyArray->numPyArray();
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================
Expand Down Expand Up @@ -451,10 +455,12 @@ NumPyPythonWrapper::NumPyPythonWrapper(DataStoreArray *pDataStoreArray,

npy_intp dims[1] = { npy_intp((pSize > 0)?pSize:pDataStoreArray->size()) };

mNumPyArray = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, static_cast<void *>(mArray->data()));
#include "pythonbegin.h"
mNumPyArray = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, static_cast<void *>(mArray->data())); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)

PyArray_SetBaseObject(reinterpret_cast<PyArrayObject *>(mNumPyArray),
PyArray_SetBaseObject(reinterpret_cast<PyArrayObject *>(mNumPyArray), // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
PythonQtSupport::wrapQObject(this));
#include "pythonend.h"
}

//==============================================================================
Expand All @@ -480,10 +486,6 @@ PyObject * NumPyPythonWrapper::numPyArray() const
} // namespace DataStore
} // namespace OpenCOR

//==============================================================================

#include "pythonend.h"

//==============================================================================
// End of file
//==============================================================================
7 changes: 6 additions & 1 deletion src/plugins/pythoninterface.h
Expand Up @@ -26,7 +26,12 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "plugin.h"
#include "pythoninclude.h"

//==============================================================================

#include "pythonbegin.h"
#include "PythonQt/PythonQtPythonInclude.h"
#include "pythonend.h"

//==============================================================================

Expand Down
Expand Up @@ -24,6 +24,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include "centralwidget.h"
#include "coreguiutils.h"
#include "file.h"
#include "pythonqtsupport.h"
#include "simulation.h"
#include "simulationexperimentviewplugin.h"
#include "simulationexperimentviewpythonwrapper.h"
Expand All @@ -32,15 +33,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.

//==============================================================================

#include "pythonbegin.h"

//==============================================================================

#include "pythonqtsupport.h"

//==============================================================================

#include <stdexcept>
#include <array>

//==============================================================================

Expand All @@ -50,7 +43,7 @@ namespace OpenCOR {

namespace SimulationSupport {
class Simulation;
} // namespace Simulationsupport
} // namespace SimulationSupport

//==============================================================================

Expand All @@ -63,8 +56,8 @@ static PyObject *getSimulation(const QString &pFileName,
{
SimulationSupport::Simulation *simulation = pSimulationExperimentViewWidget->simulation(pFileName);

if (simulation) {
if (!simulation->runtime()) {
if (simulation != nullptr) {
if (simulation->runtime() != nullptr) {
// The simulation is missing a runtime so raise a Python exception

PyErr_SetString(PyExc_ValueError, qPrintable(QObject::tr("unable to get simulations's runtime")));
Expand All @@ -85,7 +78,9 @@ static PyObject *getSimulation(const QString &pFileName,
return PythonQt::priv()->wrapQObject(simulation);
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================
Expand All @@ -94,13 +89,15 @@ static PyObject *initializeSimulation(const QString &pFileName)
{
SimulationExperimentViewWidget *simulationExperimentViewWidget = SimulationExperimentViewPlugin::instance()->viewWidget();

if (simulationExperimentViewWidget) {
if (simulationExperimentViewWidget != nullptr) {
simulationExperimentViewWidget->initialize(pFileName);

return getSimulation(pFileName, simulationExperimentViewWidget);
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================
Expand All @@ -112,12 +109,16 @@ static PyObject *openSimulation(PyObject *self, PyObject *args)
PyObject *bytes;
char *name;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes)) {
if (PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
PyBytes_AsStringAndSize(bytes, &name, &len);
QString fileName = QString::fromUtf8(name, len);
QString fileName = QString::fromUtf8(name, int(len));
#include "pythonbegin.h"
Py_DECREF(bytes);
#include "pythonend.h"

QString ioError = Core::centralWidget()->openFile(fileName,
Core::File::Type::Local,
Expand All @@ -141,12 +142,16 @@ static PyObject *openRemoteSimulation(PyObject *self, PyObject *args)
PyObject *bytes;
char *name;
Py_ssize_t len;
if (!PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes)) {
if (PyArg_ParseTuple(args, "O&", PyUnicode_FSConverter, &bytes) == 0) { // NOLINT(cppcoreguidelines-pro-type-vararg)
#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}
PyBytes_AsStringAndSize(bytes, &name, &len);
QString url = QString::fromUtf8(name, len);
QString url = QString::fromUtf8(name, int(len));
#include "pythonbegin.h"
Py_DECREF(bytes);
#include "pythonend.h"

QString ioError = Core::centralWidget()->openRemoteFile(url, false);

Expand All @@ -166,10 +171,12 @@ static PyObject *closeSimulation(PyObject *self, PyObject *args)
Q_UNUSED(self)

if (PyTuple_Size(args) > 0) {
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(args, 0));
#include "pythonbegin.h"
PythonQtInstanceWrapper *wrappedSimulation = PythonQtSupport::getInstanceWrapper(PyTuple_GET_ITEM(args, 0)); // NOLINT(cppcoreguidelines-pro-type-cstyle-cast)
#include "pythonend.h"

if (wrappedSimulation) {
SimulationSupport::Simulation *simulation = (SimulationSupport::Simulation *)wrappedSimulation->_objPointerCopy;
if (wrappedSimulation != nullptr) {
auto simulation = static_cast<SimulationSupport::Simulation *>(wrappedSimulation->_objPointerCopy);

// Close the simulation by closing its file, raising an exception if we
// are unable to do so
Expand All @@ -182,7 +189,9 @@ static PyObject *closeSimulation(PyObject *self, PyObject *args)
}
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================
Expand All @@ -193,40 +202,36 @@ static PyObject *OpenCOR_simulation(PyObject *self, PyObject *args)
Q_UNUSED(args)

SimulationExperimentViewWidget *simulationExperimentViewWidget = SimulationExperimentViewPlugin::instance()->viewWidget();
if (simulationExperimentViewWidget) {

if (simulationExperimentViewWidget != nullptr) {
return getSimulation(Core::centralWidget()->currentFileName(), simulationExperimentViewWidget);
}

#include "pythonbegin.h"
Py_RETURN_NONE;
#include "pythonend.h"
}

//==============================================================================

static PyMethodDef pythonSimulationExperimentViewMethods[] = {
{"simulation", OpenCOR_simulation, METH_VARARGS, "Current simulation."},
{"openSimulation", openSimulation, METH_VARARGS, "Open a simulation."},
{"openRemoteSimulation", openRemoteSimulation, METH_VARARGS, "Open a remote simulation."},
{"closeSimulation", closeSimulation, METH_VARARGS, "Close a simulation."},
{nullptr, nullptr, 0, nullptr}
};

//==============================================================================

SimulationExperimentViewPythonWrapper::SimulationExperimentViewPythonWrapper(PyObject *pModule, QObject *pParent) : QObject(pParent)
{
PyModule_AddFunctions(pModule, pythonSimulationExperimentViewMethods);
static std::array<PyMethodDef, 5> PythonSimulationExperimentViewMethods = {{
{ "simulation", OpenCOR_simulation, METH_VARARGS, "Current simulation." },
{ "openSimulation", openSimulation, METH_VARARGS, "Open a simulation." },
{ "openRemoteSimulation", openRemoteSimulation, METH_VARARGS, "Open a remote simulation." },
{ "closeSimulation", closeSimulation, METH_VARARGS, "Close a simulation." },
{ nullptr, nullptr, 0, nullptr }
}};

PyModule_AddFunctions(pModule, PythonSimulationExperimentViewMethods.data());
}

//==============================================================================

} // namespace SimulationExperimentView
} // namespace OpenCOR

//==============================================================================

#include "pythonend.h"

//==============================================================================
// End of file
//==============================================================================
Expand Up @@ -30,7 +30,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
//==============================================================================

#include "pythonbegin.h"
#include "PythonQt/PythonQtPythonInclude.h"
#include "PythonQt/PythonQtPythonInclude.h"
#include "pythonend.h"

//==============================================================================
Expand Down

0 comments on commit 95f6279

Please sign in to comment.