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 9facd95..ca0022b 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 ); @@ -641,11 +665,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)) { @@ -679,6 +701,12 @@ int PythonClientUser::Resolve( ClientMerge *m, Error *e ) /* * Resolver method for action resolve */ + +#define BAIL_OUT_EXC() do {\ + SetPendingCurrentException(); \ + return CMS_QUIT; \ +} while (0) + int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) { debug->debug ( P4PYDBG_CALLS, "[P4] Resolve(Action)" ); @@ -694,8 +722,9 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) return m->Resolve( 0, e ); /* no preview, actually do it */ } else { - PyErr_WarnEx( PyExc_UserWarning, - "[P4::Resolve] Resolve called with no resolver and no input -> skipping resolve", 1); + if ( PyErr_WarnEx( PyExc_UserWarning, + "[P4::Resolve] Resolve called with no resolver and no input -> skipping resolve", 1) != 0 ) + BAIL_OUT_EXC(); return CMS_QUIT; } } @@ -721,19 +750,23 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) } PyObject * mergeData = MkActionMergeInfo( m, t ); + if ( mergeData == NULL ) + BAIL_OUT_EXC(); 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; + Py_DECREF( mergeData ); + BAIL_OUT_EXC(); } + Py_DECREF( mergeData ); - StrBuf reply = GetPythonString( result ); + const char *tmp = GetPythonString( result ); + if ( tmp == NULL ) { + Py_DECREF( result ); + BAIL_OUT_EXC(); + } + StrBuf reply = tmp; Py_DECREF( result ); - Py_DECREF(mergeData); if( reply == "ay" ) return CMS_YOURS; else if( reply == "at" ) return CMS_THEIRS; @@ -745,10 +778,12 @@ int PythonClientUser::Resolve( ClientResolveA *m, int preview, Error *e ) warning << reply; warning << "', skipping resolve"; - PyErr_WarnEx( PyExc_UserWarning, warning.Text(), 1); + if ( PyErr_WarnEx( PyExc_UserWarning, warning.Text(), 1) != 0 ) + BAIL_OUT_EXC(); return CMS_QUIT; - } + } } +#undef BAIL_OUT_EXC /* * Accept input from Python. We just save what we're given here because we may not @@ -867,8 +902,17 @@ PyObject * PythonClientUser::MkActionMergeInfo( ClientResolveA *m, StrPtr &hint // retrieve the last entry in the result array PyObject * output = results.GetOutputInternal(); + if (output == NULL) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_RuntimeError, "Missing internal output"); + return NULL; + } Py_ssize_t len = PyList_Size(output); + if (len < 0) + return NULL; PyObject * info = PyList_GetItem(output, len - 1); + if (info == NULL) + return NULL; P4ActionMergeData *mergeObj = PyObject_New(P4ActionMergeData, &P4ActionMergeDataType); if (mergeObj != NULL) { 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/SpecMgr.cpp b/SpecMgr.cpp index 19dcf7f..bd07514 100644 --- a/SpecMgr.cpp +++ b/SpecMgr.cpp @@ -769,7 +769,7 @@ const char * GetPythonString(PyObject *obj) { return PyUnicode_AsUTF8(obj); } else - return PyBytes_AS_STRING(obj); + return PyBytes_AsString(obj); } #endif diff --git a/p4test.py b/p4test.py index 16acaf1..556db98 100644 --- a/p4test.py +++ b/p4test.py @@ -850,6 +850,29 @@ def resolve(self, mergeData): self.assertEqual(len(result), 2, "No two resolves scheduled") + class InvalidActionResolver(P4.Resolver): + def __init__(self, value): + self.value = value + self.called = False + + def actionResolve(self, mergeData): + self.called = True + return self.value + + for value, error in ((object(), TypeError), ('\ud800', UnicodeEncodeError)): + resolver = InvalidActionResolver(value) + with self.assertRaises(error): + self.p4.run_resolve(resolver=resolver) + self.assertTrue(resolver.called) + + import warnings + resolver = InvalidActionResolver('invalid') + with warnings.catch_warnings(): + warnings.simplefilter('error', UserWarning) + with self.assertRaisesRegex(UserWarning, 'Illegal response'): + self.p4.run_resolve(resolver=resolver) + self.assertTrue(resolver.called) + class ActionResolver(P4.Resolver): def __init__(self, testObject): self.t = testObject