From d3729205fe54bd98fc223bd6a83206d580e8ec67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Thu, 12 Nov 2026 22:43:14 +0000 Subject: [PATCH 1/3] Fix reference counting and error handling in InputData Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientUser.cpp | 41 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 9facd95..e150a9e 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -545,16 +545,36 @@ void PythonClientUser::InputData( StrBuf *strbuf, Error *e ) debug->debug ( P4PYDBG_CALLS, "[P4] InputData(). Using supplied input" ); PyObject * inval = input; + PyObject * tail = input; + Py_INCREF(inval); - if( PyTuple_Check(input) ) + if( PyTuple_Check(input) && PyTuple_Size(input) > 0 ) { + PyObject *tmp = inval; inval = PyTuple_GetItem(input, 0); - input = PyTuple_GetSlice(input, 1, PyTuple_Size(input)); + Py_INCREF(inval); + Py_DECREF(tmp); + tail = PyTuple_GetSlice(input, 1, PyTuple_Size(input)); + } + else if ( PyList_Check(input) && PyList_Size(input) > 0 ) + { + PyObject *tmp = inval; + inval = PyList_GetItem(input, 0); + Py_INCREF(inval); + Py_DECREF(tmp); + tail = PyList_GetSlice(input, 1, PyList_Size(input)); + } + if (tail == NULL) + { + // slice failed, nothing we can do about it + Py_CLEAR(inval); + PyErr_WriteUnraisable(Py_None); } - else if ( PyList_Check(input) ) + else if (tail != input) { - inval = PyList_GetItem(input, 0); - input = PyList_GetSlice(input, 1, PyList_Size(input)); + PyObject *tmp = input; + input = tail; + Py_DECREF(tmp); } if( inval == Py_None || inval == NULL ) @@ -562,6 +582,7 @@ void PythonClientUser::InputData( StrBuf *strbuf, Error *e ) PyErr_WarnEx( PyExc_UserWarning, "[P4] Expected user input, found none. " "Missing call to P4.input ?", 1 ); + Py_XDECREF(inval); return; } @@ -571,11 +592,21 @@ void PythonClientUser::InputData( StrBuf *strbuf, Error *e ) specMgr->AddSpecDef( cmd.Text(), specDef->Text() ); specMgr->SpecToString( cmd.Text(), inval, *strbuf, e ); + Py_DECREF(inval); return; } // Convert whatever's left into a string PyObject * str = PyObject_Str(inval); + Py_DECREF(inval); + if (!str) + { + PyErr_WarnEx( PyExc_UserWarning, + "[P4] Failed to convert input to string", 1 ); + PyErr_Clear(); + return; + } + strbuf->Set( GetPythonString(str) ); Py_XDECREF(str); } From 30acbc4030bc6434a53ca41a9d631198b4b3ddbb 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 2/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 | 48 ++++++++++++++++++++++++++++------------ PythonClientUser.h | 2 ++ 4 files changed, 47 insertions(+), 38 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 e150a9e..825210e 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); @@ -177,10 +203,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 ); @@ -672,11 +696,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)) { @@ -755,11 +777,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 75024281a9f53982723eb4a74f626dce12284eeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kristj=C3=A1n=20Valur=20J=C3=B3nsson?= Date: Sat, 14 Nov 2026 20:00:07 +0000 Subject: [PATCH 3/3] Propagate input conversion exceptions Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- PythonClientUser.cpp | 98 +++++++++++++++++++++++++------------------- 1 file changed, 56 insertions(+), 42 deletions(-) diff --git a/PythonClientUser.cpp b/PythonClientUser.cpp index 825210e..9641e03 100644 --- a/PythonClientUser.cpp +++ b/PythonClientUser.cpp @@ -568,70 +568,84 @@ void PythonClientUser::InputData( StrBuf *strbuf, Error *e ) debug->debug ( P4PYDBG_CALLS, "[P4] InputData(). Using supplied input" ); + // first, convert an input tuple to a list + if ( PyTuple_Check(input) ) + { + PyObject *tmp = PySequence_List(input); + if (!tmp) { + SetPendingCurrentException(); + return; + } + Py_DECREF(input); + input = tmp; + } + PyObject * inval = input; PyObject * tail = input; Py_INCREF(inval); - if( PyTuple_Check(input) && PyTuple_Size(input) > 0 ) - { - PyObject *tmp = inval; - inval = PyTuple_GetItem(input, 0); - Py_INCREF(inval); - Py_DECREF(tmp); - tail = PyTuple_GetSlice(input, 1, PyTuple_Size(input)); - } - else if ( PyList_Check(input) && PyList_Size(input) > 0 ) - { - PyObject *tmp = inval; - inval = PyList_GetItem(input, 0); - Py_INCREF(inval); - Py_DECREF(tmp); - tail = PyList_GetSlice(input, 1, PyList_Size(input)); - } - if (tail == NULL) - { - // slice failed, nothing we can do about it - Py_CLEAR(inval); - PyErr_WriteUnraisable(Py_None); - } - else if (tail != input) + if ( PyList_Check(input) ) { - PyObject *tmp = input; - input = tail; - Py_DECREF(tmp); + if ( PyList_Size(input) > 0 ) + { + // grab the first element of the list as the input value, keep the rest + PyObject *tmp = inval; + inval = PyList_GetItem(input, 0); + Py_INCREF(inval); + Py_DECREF(tmp); + tail = PyList_GetSlice(input, 1, PyList_Size(input)); + if (tail) + { + PyObject *tmp = input; + input = tail; + Py_DECREF(tmp); + } + else + { + // slice failed, nothing we can do about it + Py_CLEAR(inval); + SetPendingCurrentException(); + return; + } + } + else + { + Py_CLEAR(inval); + } } - + if( inval == Py_None || inval == NULL ) { - PyErr_WarnEx( PyExc_UserWarning, - "[P4] Expected user input, found none. " - "Missing call to P4.input ?", 1 ); - Py_XDECREF(inval); - return; + Py_XDECREF(inval); + if (PyErr_WarnEx( PyExc_UserWarning, + "[P4] Expected user input, found none. " + "Missing call to P4.input ?", 1 )) + SetPendingCurrentException(); + return; } if ( PyDict_Check( inval ) ) { - StrPtr * specDef = varList->GetVar( "specdef" ); + StrPtr * specDef = varList->GetVar( "specdef" ); - specMgr->AddSpecDef( cmd.Text(), specDef->Text() ); - specMgr->SpecToString( cmd.Text(), inval, *strbuf, e ); - Py_DECREF(inval); - return; + specMgr->AddSpecDef( cmd.Text(), specDef->Text() ); + specMgr->SpecToString( cmd.Text(), inval, *strbuf, e ); + Py_DECREF(inval); + return; } // Convert whatever's left into a string PyObject * str = PyObject_Str(inval); + const char * s = str ? GetPythonString(str) : NULL; Py_DECREF(inval); - if (!str) + if (!str || !s) { - PyErr_WarnEx( PyExc_UserWarning, - "[P4] Failed to convert input to string", 1 ); - PyErr_Clear(); + SetPendingCurrentException(); + Py_XDECREF(str); return; } - strbuf->Set( GetPythonString(str) ); + strbuf->Set( s ); Py_XDECREF(str); }