1
0
mirror of https://github.com/thp/pyotherside.git synced 2025-01-28 23:52:55 +08:00

Added null-checks for callbacks.

This commit is contained in:
Dennis Tomas 2014-09-04 21:31:14 +02:00
parent 3e4ea6ee5f
commit 52e75e918f

View File

@ -19,6 +19,8 @@
#include "qpython_priv.h"
#include "pyglrenderer.h"
#include <QDebug>
PyGLRenderer::PyGLRenderer()
: m_paintGLCallable(0)
@ -72,11 +74,18 @@ void PyGLRenderer::setRect(QRect rect) {
}
void PyGLRenderer::init() {
if (!m_initialized) {
if (m_initialized)
return;
if (!m_initGL.isEmpty()) {
QPythonPriv *priv = QPythonPriv::instance();
priv->enter();
PyObject *initGLCallable = priv->eval(m_initGL);
if (!initGLCallable) {
qWarning() << "Init callback " << m_initGL << " not defined.";
priv->leave();
return;
}
PyObject *args = PyTuple_New(0);
PyObject *o = PyObject_Call(initGLCallable, args, NULL);
if (o) Py_DECREF(o); else PyErr_PrintEx(0);
@ -85,7 +94,6 @@ void PyGLRenderer::init() {
priv->leave();
}
m_initialized = true;
}
}
@ -96,8 +104,15 @@ void PyGLRenderer::paint()
QPythonPriv *priv = QPythonPriv::instance();
priv->enter();
if (!m_paintGLCallable)
if (!m_paintGLCallable) {
m_paintGLCallable = priv->eval(m_paintGL);
if (!m_paintGLCallable) {
qWarning() << "Paint callback " << m_paintGL << " not defined.";
priv->leave();
return;
}
}
// Call the paintGL callback with arguments x, y, width, height.
// These are the boundaries in which the callback should render,
@ -122,10 +137,17 @@ void PyGLRenderer::paint()
void PyGLRenderer::cleanup()
{
if (!m_cleanupGL.isEmpty()) {
if (m_cleanupGL.isEmpty())
return;
QPythonPriv *priv = QPythonPriv::instance();
priv->enter();
PyObject *cleanupGLCallable = priv->eval(m_cleanupGL);
if (!cleanupGLCallable) {
qWarning() << "Cleanup callback " << m_cleanupGL << " not defined.";
priv->leave();
return;
}
PyObject *args = PyTuple_New(0);
PyObject *o = PyObject_Call(cleanupGLCallable, args, NULL);
if (o) Py_DECREF(o); else PyErr_PrintEx(0);
@ -133,5 +155,4 @@ void PyGLRenderer::cleanup()
Py_DECREF(args);
Py_DECREF(cleanupGLCallable);
priv->leave();
}
}