From 19e0fb562859799f70dc54f36983c7e5f4b90fe3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 12 Nov 2026 21:21:32 +0000 Subject: [PATCH 1/3] Handle Python exception converting return value in CallOutputMethod --- PythonClientUser.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..6eb5b72 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -174,19 +174,22 @@ static const int CANCEL = 2; bool PythonClientUser::CallOutputMethod( const char * method, PyObject * data) { long answer = REPORT; + PyObject *excType, *excValue, *excTb; 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; } - else { + else + { long a = PyInt_AsLong( result ); Py_DECREF( result ); - if( a == -1 ) { - alive = 0; // exception thrown or silly return value + if( a == -1 && PyErr_Occurred()) { + PyErr_Fetch(&excType, &excValue, &excTb); + SetPendingException(excType, excValue, excTb); + alive = 0; } else { From 7fbfc5d7d4604a548deb0d70a82b28523423f1cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Fri, 13 Nov 2026 17:21:23 +0000 Subject: [PATCH 2/3] Test invalid OutputHandler return conversion --- p4test.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/p4test.py b/p4test.py index 16acaf1..5a2b7a8 100644 --- a/p4test.py +++ b/p4test.py @@ -2094,6 +2094,21 @@ def outputStat(self, stat): self.assertEqual(str(cm.exception), 'boom from outputStat') self.p4.handler = None + class InvalidReturnOutputHandler(P4.OutputHandler): + def __init__(self): + self.called = False + + def outputStat(self, stat): + self.called = True + return "not an integer" + + handler = InvalidReturnOutputHandler() + self.p4.handler = handler + with self.assertRaises(TypeError): + self.p4.run('info') + self.assertTrue(handler.called, "outputStat handler was not invoked") + self.p4.handler = None + # Verify connection is still usable result = self.p4.run('info') self.assertTrue(len(result) > 0, "Connection should remain usable after callback exception") From 8f5029c2295aa5614ffd3056f2e530196d8d5ae2 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 3/3] Add helpers for pending Python exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientAPI.cpp | 7 ++---- PythonClientProgress.cpp | 28 +++++++--------------- PythonClientUser.cpp | 52 +++++++++++++++++++++++++++------------- PythonClientUser.h | 2 ++ 4 files changed, 48 insertions(+), 41 deletions(-) diff --git a/PythonClientAPI.cpp b/PythonClientAPI.cpp index 1e4e517..f6d1482 100644 --- a/PythonClientAPI.cpp +++ b/PythonClientAPI.cpp @@ -826,11 +826,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 6eb5b72..30b1025 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -125,6 +125,9 @@ void PythonClientUser::SetPendingException(PyObject *type, PyObject *value, PyOb bool PythonClientUser::GetPendingException(PyObject **type, PyObject **value, PyObject **tb) { if (pendingExcType == NULL) { + *type = NULL; + *value = NULL; + *tb = NULL; return false; } @@ -140,6 +143,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); @@ -174,21 +200,17 @@ static const int CANCEL = 2; bool PythonClientUser::CallOutputMethod( const char * method, PyObject * data) { long answer = REPORT; - PyObject *excType, *excValue, *excTb; - PyObject * result = PyObject_CallMethod( this->handler , (char*) method, (char*)"O", data ); if( result == NULL ) { // exception thrown - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); - alive = 0; + SetPendingCurrentException(); + alive = 0; } else { long a = PyInt_AsLong( result ); Py_DECREF( result ); if( a == -1 && PyErr_Occurred()) { - PyErr_Fetch(&excType, &excValue, &excTb); - SetPendingException(excType, excValue, excTb); + SetPendingCurrentException(); alive = 0; } else @@ -644,11 +666,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)) { @@ -727,11 +747,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()