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..83cbb38 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; } @@ -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; @@ -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(); @@ -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; } @@ -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(); @@ -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; } } diff --git a/PythonClientAPI.h b/PythonClientAPI.h index 191dbc9..9285646 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 @@ -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; } @@ -321,6 +322,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..223a820 100644 --- a/PythonClientProgress.cpp +++ b/PythonClientProgress.cpp @@ -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; @@ -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); } } @@ -86,14 +86,18 @@ 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); } } @@ -101,14 +105,18 @@ 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); } } @@ -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; @@ -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; +} diff --git a/PythonClientProgress.h b/PythonClientProgress.h index 6574c06..d45d916 100644 --- a/PythonClientProgress.h +++ b/PythonClientProgress.h @@ -49,6 +49,7 @@ class PythonClientProgress : public ClientProgress private: PythonClientUser * ui; PyObject * progress; + bool got_exception; }; diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..79fd137 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" @@ -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) @@ -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; } @@ -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); @@ -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 ); @@ -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)) { @@ -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 ); diff --git a/PythonClientUser.h b/PythonClientUser.h index aae8a5e..6e105cc 100644 --- a/PythonClientUser.h +++ b/PythonClientUser.h @@ -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); 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..23d2b64 100644 --- a/p4test.py +++ b/p4test.py @@ -1168,6 +1168,105 @@ def done(self, fail): "Total does not match position %d <> %d" % (callback.totalFileCount, callback.position)) self.assertEqual(total, callback.position, "Total does not match position %d <> %d" % (total, callback.position)) + + cancel_dir = "progress_cancel" + cancel_absolute_dir = os.path.join(self.client_root, cancel_dir) + os.mkdir(cancel_absolute_dir) + cancel_files = [os.path.join(cancel_absolute_dir, "000_large_file")] + cancel_files.extend( + os.path.join(cancel_absolute_dir, "zzz_file%02d" % index) + for index in range(10) + ) + with open(cancel_files[0], 'w') as f: + f.write('A' * (8 * 1024 * 1024)) + for fname in cancel_files[1:]: + with open(fname, 'w') as f: + f.write('small file\n') + for fname in cancel_files: + self.p4.run_add(fname) + self.p4.run_submit('-dProgress cancellation files') + for fname in cancel_files: + os.remove(fname) + + class CancelOnUpdate(P4.Progress, P4.OutputHandler): + def __init__(self): + P4.Progress.__init__(self) + P4.OutputHandler.__init__(self) + self.updateCalls = 0 + self.exceptionRaised = False + self.callsAfterException = 0 + self.doneCalls = 0 + self.doneAfterException = 0 + + def recordCall(self): + if self.exceptionRaised: + self.callsAfterException += 1 + + def init(self, progress_type): + self.recordCall() + + def setDescription(self, description, unit): + self.recordCall() + + def setTotal(self, total): + self.recordCall() + + def update(self, position): + self.recordCall() + self.updateCalls += 1 + self.exceptionRaised = True + raise RuntimeError("Progress update interrupted sync") + + def done(self, fail): + self.doneCalls += 1 + if self.exceptionRaised: + self.doneAfterException += 1 + self.exceptionRaised = False + + cancel_callback = CancelOnUpdate() + with self.assertRaises(RuntimeError) as cm: + self.p4.run_sync('-f', '//depot/%s/...' % cancel_dir, + progress=cancel_callback, + handler=cancel_callback) + self.assertEqual(str(cm.exception), "Progress update interrupted sync") + self.assertGreater(cancel_callback.updateCalls, 0, + "Progress update callback was not invoked") + self.assertEqual(cancel_callback.callsAfterException, 0, + "Progress callback ran again before Done after an exception") + self.assertGreater(cancel_callback.doneAfterException, 0, + "Progress done callback was skipped after an exception") + self.assertTrue(os.path.exists(cancel_files[-1]), + "Returning 1 alone unexpectedly aborted sync") + + class AlwaysAlive: + def __init__(self): + self.calls = 0 + + def __call__(self): + self.calls += 1 + return True + + keepalive = AlwaysAlive() + self.p4.setbreak(keepalive) + + class RaisingProgress(P4.Progress): + def __init__(self): + P4.Progress.__init__(self) + self.keepaliveCallsAtException = None + + def update(self, position): + self.keepaliveCallsAtException = keepalive.calls + raise RuntimeError("Progress interrupted with custom keepalive") + + raising_progress = RaisingProgress() + with self.assertRaises(RuntimeError) as cm: + self.p4.run_sync('-f', '-q', '//depot/%s/000_large_file' % cancel_dir, + progress=raising_progress) + self.assertEqual(str(cm.exception), "Progress interrupted with custom keepalive") + self.assertGreater(raising_progress.keepaliveCallsAtException, 0, + "Custom KeepAlive was not called before the Progress error") + self.assertEqual(keepalive.calls, raising_progress.keepaliveCallsAtException, + "Internal Progress abort did not suppress the Python KeepAlive callback") else: print("Test case testProgress needs a 2012.2+ Perforce Server to run") @@ -1807,6 +1906,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 +2023,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 +2059,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):