From 2dd7fead391572a2d623b7a78bef74d4ef262977 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 09:22:43 +0000 Subject: [PATCH 1/6] Wire PythonKeepAlive to PythonClientUser --- P4API.cpp | 4 +--- PythonClientAPI.cpp | 18 +++++++++++++++--- PythonClientAPI.h | 3 ++- PythonKeepAlive.cpp | 5 +++-- PythonKeepAlive.h | 5 ++++- 5 files changed, 25 insertions(+), 10 deletions(-) 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..15084a7 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 ); } @@ -905,7 +912,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 +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..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/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index c3993d8..bd29232 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -42,7 +42,8 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "PythonThreadGuard.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); } @@ -62,7 +63,7 @@ int PythonKeepAlive::IsAlive() { Py_DECREF(result); return( 0 ); // To terminate the connection } - + return( 1 ); } 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; }; From c509fcbffe773af09864807a7c0ccf0b057b96af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 10:01:36 +0000 Subject: [PATCH 2/6] Propagate KeepAlive callback exceptions --- PythonClientUser.cpp | 7 +++++++ PythonClientUserHelper.h | 11 +++++++++++ PythonKeepAlive.cpp | 20 ++++++++++++++++---- 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 PythonClientUserHelper.h diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..8275b2a 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,6 +123,12 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb pendingExcTb = tb; } +void PythonClientUserSetPendingException(PythonClientUser *ui, + PyObject *type, PyObject *value, PyObject *tb) +{ + ui->SetPendingException(type, value, tb); +} + bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h new file mode 100644 index 0000000..e274a34 --- /dev/null +++ b/PythonClientUserHelper.h @@ -0,0 +1,11 @@ +#ifndef PYTHON_CLIENT_USER_HELPER_H +#define PYTHON_CLIENT_USER_HELPER_H + +#include + +class PythonClientUser; + +void PythonClientUserSetPendingException(PythonClientUser *ui, + PyObject *type, PyObject *value, PyObject *tb); + +#endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index bd29232..8dc62a4 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -40,6 +40,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "PythonThreadGuard.h" +#include "PythonClientUserHelper.h" #include "PythonKeepAlive.h" PythonKeepAlive::PythonKeepAlive(PythonClientUser* ui, PyObject* callable) @@ -59,12 +60,23 @@ int PythonKeepAlive::IsAlive() { 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) { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + return( 0 ); } - return( 1 ); + int alive = PyObject_IsTrue(result); + Py_DECREF(result); + if (alive < 0) { + PyObject *excType, *excValue, *excTb; + PyErr_Fetch(&excType, &excValue, &excTb); + PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + return( 0 ); + } + + return alive; } return( 1 ); // Default to true if callable is not set or failed From 2663870ec71d838a1efaa5fac23b4108f346a042 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 10:30:19 +0000 Subject: [PATCH 3/6] Test KeepAlive KeyboardInterrupt propagation --- p4test.py | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) 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): From 37210bac531436a416e1338a09d5e820371a58a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 11:07:25 +0000 Subject: [PATCH 4/6] Consult PythonClientUser's break flag before calling the python callback --- PythonClientUser.cpp | 5 +++++ PythonClientUserHelper.h | 1 + PythonKeepAlive.cpp | 4 ++++ 3 files changed, 10 insertions(+) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 8275b2a..0bd96dd 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -129,6 +129,11 @@ void PythonClientUserSetPendingException(PythonClientUser *ui, ui->SetPendingException(type, value, tb); } +int PythonClientUserIsAlive(PythonClientUser *ui) +{ + return ui->IsAlive(); +} + bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h index e274a34..449437d 100644 --- a/PythonClientUserHelper.h +++ b/PythonClientUserHelper.h @@ -7,5 +7,6 @@ class PythonClientUser; void PythonClientUserSetPendingException(PythonClientUser *ui, PyObject *type, PyObject *value, PyObject *tb); +int PythonClientUserIsAlive(PythonClientUser *ui); #endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index 8dc62a4..59644ec 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -58,6 +58,10 @@ 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) { From d7095ff1a92376de0fc47cb33e0cf11191cd0831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 19:59:24 +0000 Subject: [PATCH 5/6] Add helpers for pending Python exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientAPI.cpp | 7 ++---- PythonClientProgress.cpp | 28 ++++++++--------------- PythonClientUser.cpp | 48 ++++++++++++++++++++++++++++------------ PythonClientUser.h | 2 ++ 4 files changed, 47 insertions(+), 38 deletions(-) diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 15084a7..4bef180 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -833,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(); 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 0bd96dd..5dd4f85 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -137,6 +137,9 @@ int PythonClientUserIsAlive(PythonClientUser *ui) bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { + *type = NULL; + *value = NULL; + *tb = NULL; return false; } @@ -152,6 +155,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); @@ -189,10 +215,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 ); @@ -653,11 +677,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)) { @@ -736,11 +758,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() From 8e8abd9a383c8c7b3e8ce6307418cff4536e4c3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 20:05:06 +0000 Subject: [PATCH 6/6] Use pending exception helper in keepalive callbacks Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientUser.cpp | 5 ++--- PythonClientUserHelper.h | 3 +-- PythonKeepAlive.cpp | 8 ++------ 3 files changed, 5 insertions(+), 11 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 5dd4f85..9f50514 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -123,10 +123,9 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb pendingExcTb = tb; } -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb) +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui) { - ui->SetPendingException(type, value, tb); + return ui->SetPendingCurrentException(); } int PythonClientUserIsAlive(PythonClientUser *ui) diff --git a/PythonClientUserHelper.h b/PythonClientUserHelper.h index 449437d..c9bb9e0 100644 --- a/PythonClientUserHelper.h +++ b/PythonClientUserHelper.h @@ -5,8 +5,7 @@ class PythonClientUser; -void PythonClientUserSetPendingException(PythonClientUser *ui, - PyObject *type, PyObject *value, PyObject *tb); +bool PythonClientUserSetPendingCurrentException(PythonClientUser *ui); int PythonClientUserIsAlive(PythonClientUser *ui); #endif diff --git a/PythonKeepAlive.cpp b/PythonKeepAlive.cpp index 59644ec..6f1a20f 100644 --- a/PythonKeepAlive.cpp +++ b/PythonKeepAlive.cpp @@ -65,18 +65,14 @@ int PythonKeepAlive::IsAlive() { if (py_callable && PyCallable_Check(py_callable)) { PyObject* result = PyObject_CallObject(py_callable, NULL); if (result == NULL) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); } int alive = PyObject_IsTrue(result); Py_DECREF(result); if (alive < 0) { - PyObject *excType, *excValue, *excTb; - PyErr_Fetch(&excType, &excValue, &excTb); - PythonClientUserSetPendingException(py_ui, excType, excValue, excTb); + PythonClientUserSetPendingCurrentException(py_ui); return( 0 ); }