diff --git a/P4API.cpp b/P4API.cpp index 657fd09..db42f5f 100644 --- a/P4API.cpp +++ b/P4API.cpp @@ -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; } diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 1e4e517..4bef180 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -69,6 +69,7 @@ PythonClientAPI::PythonClientAPI() : ui(&debug, &specMgr), specMgr(&debug) { + breakCallback = NULL; server2 = 0; depth = 0; exceptionLevel = 2; @@ -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; } @@ -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 ); } @@ -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(); @@ -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); @@ -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; } } diff --git a/PythonClientAPI.h b/PythonClientAPI.h index 191dbc9..64589db 100644 --- a/PythonClientAPI.h +++ b/PythonClientAPI.h @@ -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 @@ -321,6 +321,7 @@ class PythonClientAPI private: ClientApi client; // Perforce API Class PythonClientUser ui; + PythonKeepAlive * breakCallback; Enviro * enviro; PythonDebug debug; p4py::SpecMgr specMgr; diff --git a/PythonClientProgress.cpp b/PythonClientProgress.cpp index 5b32a35..bf3cfeb 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -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(); } } @@ -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(); } } @@ -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(); } } @@ -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; @@ -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(); } } diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..9f50514 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -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" @@ -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; } @@ -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); @@ -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 ); @@ -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)) { @@ -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 ); diff --git a/PythonClientUser.h b/PythonClientUser.h index aae8a5e..9c8a0a9 100644 --- a/PythonClientUser.h +++ b/PythonClientUser.h @@ -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() diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h new file mode 100644 index 0000000..c9bb9e0 --- /dev/null +++ b/PythonClientUserHelper.h @@ -0,0 +1,11 @@ +#ifndef PYTHON_CLIENT_USER_HELPER_H +#define PYTHON_CLIENT_USER_HELPER_H + +#include + +class PythonClientUser; + +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui); +int PythonClientUserIsAlive(PythonClientUser *ui); + +#endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index c3993d8..6f1a20f 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -40,9 +40,11 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #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); } @@ -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 diff --git a/PythonKeepAlive.h b/PythonKeepAlive.h index 564e879..bbcd070 100644 --- a/PythonKeepAlive.h +++ b/PythonKeepAlive.h @@ -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; }; diff --git a/p4test.py b/p4test.py index 16acaf1..2187a8d 100644 --- a/p4test.py +++ b/p4test.py @@ -1807,6 +1807,17 @@ def isAlive(self): if self.counter > self.total_count: return 0 return 1 + + class RaisingKeepAlive: + def __init__(self): + self.called = False + + def isAlive(self): + self.called = True + raise KeyboardInterrupt("KeepAlive callback interrupted command") + + def __call__(self): + return self.isAlive() #create a multiple changelist revision for i in range(100): @@ -1913,6 +1924,22 @@ def _run(): _p4d_proc2.kill() _p4d_proc2.wait() + # --- Case 3: callback exceptions propagate out of the command --- + _p4d_proc3, _free_port3 = _start_tcp_p4d() + _p4_tcp3 = _connect_tcp(_free_port3) + raising_ka = RaisingKeepAlive() + _p4_tcp3.setbreak(raising_ka) + try: + with self.assertRaises(KeyboardInterrupt): + _p4_tcp3.run("changes") + self.assertTrue(raising_ka.called, "isAlive callback was not invoked") + finally: + try: + _p4_tcp3.disconnect() + finally: + _p4d_proc3.kill() + _p4d_proc3.wait() + # Replace self.p4 with fresh unconnected object so tearDown skips # disconnect on the stale RSH connection. self.p4 = P4.P4() @@ -1933,6 +1960,14 @@ def _run(): total_files = len(self.p4.run("changes")) self.assertEqual(100, total_files, "Setbreak is not working") self.p4.disconnect() + + self.p4.connect() + raising_ka = RaisingKeepAlive() + self.p4.setbreak(raising_ka) + with self.assertRaises(KeyboardInterrupt): + self.p4.run("changes") + self.assertTrue(raising_ka.called, "isAlive callback was not invoked") + self.p4.disconnect() return def testMergeToolReturnCode(self):