Commit e14ea47c authored by Clinton Stimpson's avatar Clinton Stimpson

ENH: Improve python testing robustness.

parent a1476eda
......@@ -26,6 +26,7 @@ IF(QT_TESTING_WITH_PYTHON)
)
SET(PYTHON_MOCS
pqPythonEventObserver.h
pqPythonEventSource.h
)
SET(PYTHON_SRCS
pqPythonEventObserver.cxx
......
......@@ -145,8 +145,6 @@ void pqEventDispatcher::playNextEvent()
}
bool error = false;
// block signals as some event sources may interact with the event loop, as
// well as some players interact with the event loop
this->Implementation->Player->playEvent(object, command, arguments, error);
if(error)
{
......
......@@ -56,15 +56,6 @@ static QString PropertyObject;
static QString PropertyResult;
static QWaitCondition WaitResults;
namespace
{
class pqGetPropertyEvent : public QEvent
{
public:
pqGetPropertyEvent() : QEvent(QEvent::User) {}
};
}
static PyObject*
QtTesting_playCommand(PyObject* /*self*/, PyObject* args)
......@@ -115,12 +106,14 @@ QtTesting_getProperty(PyObject* /*self*/, PyObject* args)
return NULL;
}
PropertyResult = property;
PropertyObject = object;
PropertyResult = property;
QCoreApplication::postEvent(Instance, new pqGetPropertyEvent());
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(Instance, "getProperty", Qt::QueuedConnection);
WaitResults.wait(&mut);
if(PropertyObject == QString::null)
......@@ -187,42 +180,35 @@ void pqPythonEventSource::setContent(const QString& path)
this->start();
}
bool pqPythonEventSource::event(QEvent* e)
void pqPythonEventSource::getProperty()
{
if(dynamic_cast<pqGetPropertyEvent*>(e))
QObject* qobject = pqObjectNaming::GetObject(PropertyObject);
if(!qobject)
{
PropertyObject = QString::null;
}
else
{
QObject* qobject = pqObjectNaming::GetObject(PropertyObject);
if(!qobject)
{
PropertyObject = QString::null;
}
else
{
PropertyResult = qobject->property(
PropertyResult.toAscii().data()).toString();
}
WaitResults.wakeAll();
return true;
PropertyResult = qobject->property(
PropertyResult.toAscii().data()).toString();
}
return pqThreadedEventSource::event(e);
WaitResults.wakeAll();
}
void pqPythonEventSource::run()
{
QFile file(this->Internal->FileName);
if(!file.open(QFile::ReadOnly | QFile::Text))
{
printf("Unable to open python script\n");
return;
}
// initialize threading
PyEval_InitThreads();
this->Internal->MainThreadState = PyThreadState_Get();
PyEval_ReleaseLock();
QString filename = this->Internal->FileName;
FILE* pythonScript = fopen(filename.toAscii().data(), "r");
if(!pythonScript)
{
printf("Unable to open python script\n");
return;
}
PyEval_AcquireLock();
PyInterpreterState* mainInterpreterState;
......@@ -233,11 +219,8 @@ void pqPythonEventSource::run()
Instance = this;
// finally run the script
int result = PyRun_SimpleFile(pythonScript, filename.toAscii().data());
if( result == 2 )
{
printf("invalid python file\n");
}
QByteArray wholeFile = file.readAll();
int result = PyRun_SimpleString(wholeFile.data()) == 0 ? 0 : 1;
PyThreadState_Swap(NULL);
......@@ -246,8 +229,6 @@ void pqPythonEventSource::run()
PyEval_ReleaseLock();
fclose(pythonScript);
this->done(result);
}
......@@ -42,6 +42,7 @@ by pqPythonEventObserver */
class QTTESTING_EXPORT pqPythonEventSource :
public pqThreadedEventSource
{
Q_OBJECT
public:
pqPythonEventSource(QObject* p = 0);
~pqPythonEventSource();
......@@ -50,8 +51,11 @@ public:
protected:
virtual void run();
virtual bool event(QEvent*);
protected slots:
void getProperty();
private:
class pqInternal;
pqInternal* Internal;
......
......@@ -129,20 +129,16 @@ int pqThreadedEventSource::getNextEvent(
return SUCCESS;
}
bool pqThreadedEventSource::event(QEvent* e)
void pqThreadedEventSource::relayEvent(QString object, QString command, QString arguments)
{
pqPlayCommandEvent* pe = dynamic_cast<pqPlayCommandEvent*>(e);
if(pe)
{
this->Internal->CurrentObject = pe->Object;
this->Internal->CurrentCommand = pe->Command;
this->Internal->CurrentArgument = pe->Arguments;
this->Internal->GotEvent = true;
this->Internal->CurrentObject = object;
this->Internal->CurrentCommand = command;
this->Internal->CurrentArgument = arguments;
this->Internal->GotEvent = true;
if(this->Internal->Loop.isRunning())
{
this->Internal->Loop.quit();
return true;
}
return pqEventSource::event(e);
}
}
......@@ -150,10 +146,14 @@ void pqThreadedEventSource::postNextEvent(const QString& object,
const QString& command,
const QString& argument)
{
QApplication::postEvent(this, new pqPlayCommandEvent(object, command, argument));
// wait for the GUI thread to take the event and wake us up
QMutex mut;
mut.lock();
QMetaObject::invokeMethod(this, "relayEvent", Qt::QueuedConnection,
Q_ARG(QString, object),
Q_ARG(QString, command),
Q_ARG(QString, argument));
// wait for the GUI thread to take the event and wake us up
this->Internal->WaitCondition.wait(&mut);
}
......
......@@ -64,10 +64,11 @@ public:
const QString& command,
const QString& argument);
protected:
private slots:
bool event(QEvent* e);
void relayEvent(QString object, QString command, QString arguments);
protected:
// start the thread
void start();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment