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) ...@@ -26,6 +26,7 @@ IF(QT_TESTING_WITH_PYTHON)
) )
SET(PYTHON_MOCS SET(PYTHON_MOCS
pqPythonEventObserver.h pqPythonEventObserver.h
pqPythonEventSource.h
) )
SET(PYTHON_SRCS SET(PYTHON_SRCS
pqPythonEventObserver.cxx pqPythonEventObserver.cxx
......
...@@ -145,8 +145,6 @@ void pqEventDispatcher::playNextEvent() ...@@ -145,8 +145,6 @@ void pqEventDispatcher::playNextEvent()
} }
bool error = false; 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); this->Implementation->Player->playEvent(object, command, arguments, error);
if(error) if(error)
{ {
......
...@@ -56,15 +56,6 @@ static QString PropertyObject; ...@@ -56,15 +56,6 @@ static QString PropertyObject;
static QString PropertyResult; static QString PropertyResult;
static QWaitCondition WaitResults; static QWaitCondition WaitResults;
namespace
{
class pqGetPropertyEvent : public QEvent
{
public:
pqGetPropertyEvent() : QEvent(QEvent::User) {}
};
}
static PyObject* static PyObject*
QtTesting_playCommand(PyObject* /*self*/, PyObject* args) QtTesting_playCommand(PyObject* /*self*/, PyObject* args)
...@@ -115,12 +106,14 @@ QtTesting_getProperty(PyObject* /*self*/, PyObject* args) ...@@ -115,12 +106,14 @@ QtTesting_getProperty(PyObject* /*self*/, PyObject* args)
return NULL; return NULL;
} }
PropertyResult = property;
PropertyObject = object; PropertyObject = object;
PropertyResult = property;
QCoreApplication::postEvent(Instance, new pqGetPropertyEvent());
QMutex mut; QMutex mut;
mut.lock(); mut.lock();
QMetaObject::invokeMethod(Instance, "getProperty", Qt::QueuedConnection);
WaitResults.wait(&mut); WaitResults.wait(&mut);
if(PropertyObject == QString::null) if(PropertyObject == QString::null)
...@@ -187,42 +180,35 @@ void pqPythonEventSource::setContent(const QString& path) ...@@ -187,42 +180,35 @@ void pqPythonEventSource::setContent(const QString& path)
this->start(); 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); PropertyResult = qobject->property(
if(!qobject) PropertyResult.toAscii().data()).toString();
{
PropertyObject = QString::null;
}
else
{
PropertyResult = qobject->property(
PropertyResult.toAscii().data()).toString();
}
WaitResults.wakeAll();
return true;
} }
return pqThreadedEventSource::event(e); WaitResults.wakeAll();
} }
void pqPythonEventSource::run() 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 // initialize threading
PyEval_InitThreads(); PyEval_InitThreads();
this->Internal->MainThreadState = PyThreadState_Get(); this->Internal->MainThreadState = PyThreadState_Get();
PyEval_ReleaseLock(); 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(); PyEval_AcquireLock();
PyInterpreterState* mainInterpreterState; PyInterpreterState* mainInterpreterState;
...@@ -233,11 +219,8 @@ void pqPythonEventSource::run() ...@@ -233,11 +219,8 @@ void pqPythonEventSource::run()
Instance = this; Instance = this;
// finally run the script // finally run the script
int result = PyRun_SimpleFile(pythonScript, filename.toAscii().data()); QByteArray wholeFile = file.readAll();
if( result == 2 ) int result = PyRun_SimpleString(wholeFile.data()) == 0 ? 0 : 1;
{
printf("invalid python file\n");
}
PyThreadState_Swap(NULL); PyThreadState_Swap(NULL);
...@@ -246,8 +229,6 @@ void pqPythonEventSource::run() ...@@ -246,8 +229,6 @@ void pqPythonEventSource::run()
PyEval_ReleaseLock(); PyEval_ReleaseLock();
fclose(pythonScript);
this->done(result); this->done(result);
} }
...@@ -42,6 +42,7 @@ by pqPythonEventObserver */ ...@@ -42,6 +42,7 @@ by pqPythonEventObserver */
class QTTESTING_EXPORT pqPythonEventSource : class QTTESTING_EXPORT pqPythonEventSource :
public pqThreadedEventSource public pqThreadedEventSource
{ {
Q_OBJECT
public: public:
pqPythonEventSource(QObject* p = 0); pqPythonEventSource(QObject* p = 0);
~pqPythonEventSource(); ~pqPythonEventSource();
...@@ -50,8 +51,11 @@ public: ...@@ -50,8 +51,11 @@ public:
protected: protected:
virtual void run(); virtual void run();
virtual bool event(QEvent*);
protected slots:
void getProperty();
private:
class pqInternal; class pqInternal;
pqInternal* Internal; pqInternal* Internal;
......
...@@ -129,20 +129,16 @@ int pqThreadedEventSource::getNextEvent( ...@@ -129,20 +129,16 @@ int pqThreadedEventSource::getNextEvent(
return SUCCESS; return SUCCESS;
} }
bool pqThreadedEventSource::event(QEvent* e) void pqThreadedEventSource::relayEvent(QString object, QString command, QString arguments)
{ {
pqPlayCommandEvent* pe = dynamic_cast<pqPlayCommandEvent*>(e); this->Internal->CurrentObject = object;
if(pe) this->Internal->CurrentCommand = command;
{ this->Internal->CurrentArgument = arguments;
this->Internal->CurrentObject = pe->Object; this->Internal->GotEvent = true;
this->Internal->CurrentCommand = pe->Command; if(this->Internal->Loop.isRunning())
this->Internal->CurrentArgument = pe->Arguments; {
this->Internal->GotEvent = true;
this->Internal->Loop.quit(); this->Internal->Loop.quit();
return true; }
}
return pqEventSource::event(e);
} }
...@@ -150,10 +146,14 @@ void pqThreadedEventSource::postNextEvent(const QString& object, ...@@ -150,10 +146,14 @@ void pqThreadedEventSource::postNextEvent(const QString& object,
const QString& command, const QString& command,
const QString& argument) 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; QMutex mut;
mut.lock(); 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); this->Internal->WaitCondition.wait(&mut);
} }
......
...@@ -64,10 +64,11 @@ public: ...@@ -64,10 +64,11 @@ public:
const QString& command, const QString& command,
const QString& argument); const QString& argument);
protected: private slots:
bool event(QEvent* e); void relayEvent(QString object, QString command, QString arguments);
protected:
// start the thread // start the thread
void start(); 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