Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions P4API.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -617,9 +617,7 @@ static PyObject* P4Adapter_setBreak(P4Adapter* self, PyObject* args) {
return NULL;
}

// Create an object pointer of PythonKeepAlive and pass py_callable
PythonKeepAlive* cb = new PythonKeepAlive(py_callable);
self->clientAPI->SetBreak(cb);
self->clientAPI->SetBreak(py_callable);

Py_RETURN_NONE;
}
Expand Down
46 changes: 29 additions & 17 deletions PythonClientAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ PythonClientAPI::PythonClientAPI()
: ui(&debug, &specMgr),
specMgr(&debug)
{
breakCallback = NULL;
server2 = 0;
depth = 0;
exceptionLevel = 2;
Expand Down Expand Up @@ -123,11 +124,13 @@ PythonClientAPI::~PythonClientAPI()
// can't use logger here, probably already destructed at this time
debug.printDebug(P4PYDBG_GC, "Destructor PythonClientAPI::~PythonClientAPI called");

client.SetBreak(NULL);
if( IsConnected() ) {
Error e;
client.Final( &e );
// Ignore errors
}
delete breakCallback;
delete enviro;
}

Expand Down Expand Up @@ -721,13 +724,7 @@ PyObject * PythonClientAPI::ConnectOrReconnect()
if ( e.Test() )
Py_RETURN_FALSE;

// If an iterator is defined, reset the break functionality
// for the KeepAlive function

if( ui.GetHandler() != Py_None )
{
client.SetBreak( &ui );
}
SetBreakHandler();

SetConnected();
Py_RETURN_NONE;
Expand Down Expand Up @@ -826,11 +823,8 @@ PyObject * PythonClientAPI::Run( const char *cmd, int argc, char * const *argv )
}

// Check for pending exception from callbacks
PyObject *excType, *excValue, *excTb;
if( ui.GetPendingException(&excType, &excValue, &excTb) ) {
PyErr_Restore(excType, excValue, excTb);
return NULL;
}
if (ui.RaisePendingException())
return NULL;

p4py::P4Result &results = ui.GetResults();

Expand Down Expand Up @@ -905,10 +899,7 @@ int PythonClientAPI::SetHandler( PyObject * iterator )
return -1;
}

if( iterator == Py_None)
client.SetBreak(NULL);
else
client.SetBreak(&ui);
SetBreakHandler();

return 0;
}
Expand All @@ -927,9 +918,27 @@ int PythonClientAPI::SetProgress( PyObject * progress )
return -1;
}

SetBreakHandler();

return 0;
}

void PythonClientAPI::SetBreakHandler()
{
if( breakCallback ) {
client.SetBreak( breakCallback );
return;
}

PyObject *handler = ui.GetHandler();
PyObject *progress = ui.GetProgress();
bool hasCallback = handler != Py_None || progress != Py_None;
Py_DECREF(handler);
Py_DECREF(progress);

client.SetBreak( hasCallback ? &ui : NULL );
}

PyObject * PythonClientAPI::GetProgress()
{
return ui.GetProgress();
Expand Down Expand Up @@ -1303,11 +1312,14 @@ void PythonClientAPI::RunCmd(const char *cmd, ClientUser *ui, int argc, char * c

}

void PythonClientAPI::SetBreak(PythonKeepAlive* cb)
void PythonClientAPI::SetBreak(PyObject* callable)
{
if( IsConnected() ) {
debug.debug(P4PYDBG_COMMANDS, "[P4] Establish the callback" );
PythonKeepAlive *cb = new PythonKeepAlive(&ui, callable);
client.SetBreak(cb);
delete breakCallback;
breakCallback = cb;
}

}
4 changes: 3 additions & 1 deletion PythonClientAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ class PythonClientAPI
void Except( const char *func, const char *msg, const char *cmd );

// SetBreak
void SetBreak( PythonKeepAlive* cb );
void SetBreak( PyObject* callable );

public:
// setter/getter methods and attributes
Expand Down Expand Up @@ -285,6 +285,7 @@ class PythonClientAPI

void InitFlags() { flags = S_INITIAL_STATE; }
void ResetFlags() { flags &= ~S_RESET_MASK; }
void SetBreakHandler();

void SetTag() { flags |= S_TAGGED; }
void ClearTag() { flags &= ~S_TAGGED; }
Expand Down Expand Up @@ -321,6 +322,7 @@ class PythonClientAPI
private:
ClientApi client; // Perforce API Class
PythonClientUser ui;
PythonKeepAlive * breakCallback;
Enviro * enviro;
PythonDebug debug;
p4py::SpecMgr specMgr;
Expand Down
55 changes: 34 additions & 21 deletions PythonClientProgress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
using namespace std;

PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObject * prog, int type)
: ui(clientUser), progress(prog)
: ui(clientUser), progress(prog), got_exception(false)
{
EnsurePythonLock guard;

Expand All @@ -70,9 +70,9 @@ PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObjec
Py_DECREF( res );
}
else {
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
got_exception = true;
ui->SetPendingCurrentException();
ui->SetAlive(false);
}
}

Expand All @@ -86,29 +86,37 @@ void PythonClientProgress::Description( const StrPtr *desc, int units )
{
EnsurePythonLock guard;

if ( got_exception ) {
return;
}

PyObject * res = PyObject_CallMethod( this->progress , (char*) "setDescription", (char*)"si", desc->Text(), units );
if( res ) {
Py_DECREF( res );
Py_DECREF( res );
}
else {
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
got_exception = true;
ui->SetPendingCurrentException();
ui->SetAlive(false);
}
}

void PythonClientProgress::Total( long total )
{
EnsurePythonLock guard;

PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"i", total );
if ( got_exception ) {
return;
}

PyObject * res = PyObject_CallMethod( this->progress , (char*) "setTotal", (char*)"l", total );
if( res ) {
Py_DECREF( res );
Py_DECREF( res );
}
else {
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
got_exception = true;
ui->SetPendingCurrentException();
ui->SetAlive(false);
}

}
Expand All @@ -117,15 +125,20 @@ int PythonClientProgress::Update( long pos )
{
EnsurePythonLock guard;

if ( got_exception ) {
return 1;
}

PyObject * result = PyObject_CallMethod( this->progress , (char*) "update", (char*)"l", pos );
if( result == NULL ) { // exception thrown
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
got_exception = true;
ui->SetPendingCurrentException();
ui->SetAlive(false);
// Returning 1 here does not stop the operation by itself.
return 1;
}
else {
Py_DECREF( result );
Py_DECREF( result );
}

return 0;
Expand All @@ -140,9 +153,9 @@ void PythonClientProgress::Done( int fail )
Py_DECREF( res );
}
else {
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
ui->SetPendingCurrentException();
ui->SetAlive(false);
}
}

got_exception = false;
}
1 change: 1 addition & 0 deletions PythonClientProgress.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class PythonClientProgress : public ClientProgress
private:
PythonClientUser * ui;
PyObject * progress;
bool got_exception;
};


Expand Down
60 changes: 45 additions & 15 deletions PythonClientUser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "SpecMgr.h"
#include "P4Result.h"
#include "PythonClientUser.h"
#include "PythonClientUserHelper.h"
#include "PythonClientAPI.h"
#include "P4PythonDebug.h"
#include "PythonThreadGuard.h"
Expand Down Expand Up @@ -106,7 +107,6 @@ void PythonClientUser::Reset()
// input data is untouched

alive = 1; // yes, we want data from the server
ClearPendingException();
}

void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyObject *tb)
Expand All @@ -122,9 +122,22 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb
pendingExcTb = tb;
}

bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui)
{
return ui->SetPendingCurrentException();
}

int PythonClientUserIsAlive(PythonClientUser *ui)
{
return ui->IsAlive();
}

bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb)
{
if (pendingExcType == NULL) {
*type = NULL;
*value = NULL;
*tb = NULL;
return false;
}

Expand All @@ -140,6 +153,29 @@ bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, Py
return true;
}

bool PythonClientUser::SetPendingCurrentException()
{
if (PyErr_Occurred())
{
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
SetPendingException(excType, excValue, excTb);
return true;
}
return false;
}

bool PythonClientUser::RaisePendingException()
{
PyObject *excType, *excValue, *excTb;
if (GetPendingException(&excType, &excValue, &excTb))
{
PyErr_Restore(excType, excValue, excTb);
return true;
}
return false;
}

void PythonClientUser::ClearPendingException()
{
Py_XDECREF(pendingExcType);
Expand Down Expand Up @@ -177,10 +213,8 @@ bool PythonClientUser::CallOutputMethod( const char * method, PyObject * data)

PyObject * result = PyObject_CallMethod( this->handler , (char*) method, (char*)"O", data );
if( result == NULL ) { // exception thrown
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
SetPendingException(excType, excValue, excTb);
alive = 0;
SetPendingCurrentException();
alive = 0;
}
else {
long a = PyInt_AsLong( result );
Expand Down Expand Up @@ -641,11 +675,9 @@ int PythonClientUser::Resolve( ClientMerge *m, Error *e )

PyObject * result = PyObject_CallMethod( this->resolver , (char*)"resolve", (char*)"(O)", mergeData );
if( result == NULL ) { // exception thrown, bug out of here
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
SetPendingException(excType, excValue, excTb);
Py_DECREF(mergeData);
return CMS_QUIT;
SetPendingCurrentException();
Py_DECREF(mergeData);
return CMS_QUIT;
}

if (IsString(result)) {
Expand Down Expand Up @@ -724,11 +756,9 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e )

PyObject * result = PyObject_CallMethod( this->resolver , (char*)"actionResolve", (char*)"(O)", mergeData );
if( result == NULL ) { // exception thrown, bug out of here
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
SetPendingException(excType, excValue, excTb);
Py_DECREF(mergeData);
return CMS_QUIT;
SetPendingCurrentException();
Py_DECREF(mergeData);
return CMS_QUIT;
}

StrBuf reply = GetPythonString( result );
Expand Down
6 changes: 6 additions & 0 deletions PythonClientUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,18 @@ class PythonClientUser: public ClientUser, public KeepAlive
void SetPendingException(PyObject *type, PyObject *value, PyObject *tb);
bool GetPendingException(PyObject **type, PyObject **value, PyObject **tb);
void ClearPendingException();
bool SetPendingCurrentException(); // returns true if an exception was set
bool RaisePendingException(); // returns true if an exception was raised

// override from KeepAlive
virtual int IsAlive()
{
return alive;
}
void SetAlive(bool isAlive)
{
alive = isAlive;
}

private:
PyObject * MkMergeInfo(ClientMerge *m, StrPtr &hint);
Expand Down
Loading