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
25 changes: 17 additions & 8 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 @@ -724,7 +727,11 @@ PyObject * PythonClientAPI::ConnectOrReconnect()
// If an iterator is defined, reset the break functionality
// for the KeepAlive function

if( ui.GetHandler() != Py_None )
if( breakCallback )
{
client.SetBreak( breakCallback );
}
else if( ui.GetHandler() != Py_None )
{
client.SetBreak( &ui );
}
Expand Down Expand Up @@ -826,11 +833,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,7 +909,9 @@ int PythonClientAPI::SetHandler( PyObject * iterator )
return -1;
}

if( iterator == Py_None)
if( breakCallback )
client.SetBreak( breakCallback );
else if( iterator == Py_None)
client.SetBreak(NULL);
else
client.SetBreak(&ui);
Expand Down Expand Up @@ -1303,11 +1309,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;
}

}
3 changes: 2 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 @@ -321,6 +321,7 @@ class PythonClientAPI
private:
ClientApi client; // Perforce API Class
PythonClientUser ui;
PythonKeepAlive * breakCallback;
Enviro * enviro;
PythonDebug debug;
p4py::SpecMgr specMgr;
Expand Down
28 changes: 9 additions & 19 deletions PythonClientProgress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ PythonClientProgress::PythonClientProgress(PythonClientUser *clientUser, PyObjec
Py_DECREF( res );
}
else {
PyObject *excType, *excValue, *excTb;
PyErr_Fetch(&excType, &excValue, &excTb);
ui->SetPendingException(excType, excValue, excTb);
ui->SetPendingCurrentException();
}
}

Expand All @@ -88,12 +86,10 @@ void PythonClientProgress::Description( const StrPtr *desc, int units )

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);
ui->SetPendingCurrentException();
}
}

Expand All @@ -103,12 +99,10 @@ void PythonClientProgress::Total( long total )

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

}
Expand All @@ -119,13 +113,11 @@ int PythonClientProgress::Update( long pos )

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);
return 1;
ui->SetPendingCurrentException();
return 1;
}
else {
Py_DECREF( result );
Py_DECREF( result );
}

return 0;
Expand All @@ -140,9 +132,7 @@ 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();
}
}

59 changes: 45 additions & 14 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 @@ -122,9 +123,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 +154,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 +214,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 +676,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 +757,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
2 changes: 2 additions & 0 deletions PythonClientUser.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ 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()
Expand Down
11 changes: 11 additions & 0 deletions PythonClientUserHelper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef PYTHON_CLIENT_USER_HELPER_H
#define PYTHON_CLIENT_USER_HELPER_H

#include <Python.h>

class PythonClientUser;

bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui);
int PythonClientUserIsAlive(PythonClientUser *ui);

#endif
25 changes: 19 additions & 6 deletions PythonKeepAlive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include <Python.h>
#include <keepalive.h>
#include "PythonThreadGuard.h"
#include "PythonClientUserHelper.h"
#include "PythonKeepAlive.h"

PythonKeepAlive::PythonKeepAlive(PyObject* callable) : py_callable(callable) {
PythonKeepAlive::PythonKeepAlive(PythonClientUser* ui, PyObject* callable)
: py_ui(ui), py_callable(callable) {
// Increment the reference count for the callable
Py_XINCREF(py_callable);
}
Expand All @@ -56,14 +58,25 @@ int PythonKeepAlive::IsAlive() {

EnsurePythonLock guard;

if (!PythonClientUserIsAlive(py_ui)) {
return( 0 );
}

if (py_callable && PyCallable_Check(py_callable)) {
PyObject* result = PyObject_CallObject(py_callable, NULL);
if (result != NULL && PyLong_Check(result) && PyLong_AsLong(result) == 0) {
Py_DECREF(result);
return( 0 ); // To terminate the connection
if (result == NULL) {
PythonClientUserSetPendingCurrentException(py_ui);
return( 0 );
}

return( 1 );

int alive = PyObject_IsTrue(result);
Py_DECREF(result);
if (alive < 0) {
PythonClientUserSetPendingCurrentException(py_ui);
return( 0 );
}

return alive;
}

return( 1 ); // Default to true if callable is not set or failed
Expand Down
5 changes: 4 additions & 1 deletion PythonKeepAlive.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,18 @@
#ifndef PYTHON_KEEP_ALIVE_H
#define PYTHON_KEEP_ALIVE_H

class PythonClientUser;

class PythonKeepAlive : public KeepAlive {
public:
PythonKeepAlive(PyObject* callable);
PythonKeepAlive(PythonClientUser* ui, PyObject* callable);
~PythonKeepAlive();

virtual int IsAlive() override;
virtual int PollMs() override { return 0; }

private:
PythonClientUser* py_ui;
PyObject* py_callable;
};

Expand Down
Loading