Skip to content

Commit

Permalink
Fix raising of RequestFailedException in Python (#2589)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Jul 31, 2024
1 parent ef3c828 commit 765d815
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 25 deletions.
82 changes: 57 additions & 25 deletions python/modules/IcePy/Util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,8 +196,7 @@ IcePy::getAttr(PyObject* obj, const string& attrib, bool allowNone)
{
if (!allowNone)
{
Py_DECREF(v);
v = 0;
v = nullptr;
}
}
else if (!v)
Expand Down Expand Up @@ -272,22 +271,24 @@ IcePy::PyObjectHandle::release()

IcePy::PyException::PyException()
{
PyObject* type;
PyObject* e;
PyObject* tb;

PyErr_Fetch(&type, &e, &tb); // PyErr_Fetch clears the exception.
PyErr_NormalizeException(&type, &e, &tb);

_type = type;
ex = e;
_tb = tb;
ex = PyErr_GetRaisedException();
if (ex)
{
PyObject* type = reinterpret_cast<PyObject*>(Py_TYPE(ex.get()));
Py_INCREF(type);
_type = type;
_tb = PyException_GetTraceback(ex.get());
}
}

IcePy::PyException::PyException(PyObject* p)
IcePy::PyException::PyException(PyObject* raisedException)
{
ex = p;
Py_XINCREF(p);
Py_XINCREF(raisedException);
ex = raisedException;
PyObject* type = reinterpret_cast<PyObject*>(Py_TYPE(raisedException));
Py_INCREF(type);
_type = type;
_tb = PyException_GetTraceback(raisedException);
}

void
Expand Down Expand Up @@ -367,17 +368,48 @@ IcePy::PyException::raiseLocalException()
{
string typeName = getTypeName();

if (typeName == "Ice.ObjectNotExistException")
{
throw Ice::ObjectNotExistException(__FILE__, __LINE__);
}
else if (typeName == "Ice.OperationNotExistException")
{
throw Ice::OperationNotExistException(__FILE__, __LINE__);
}
else if (typeName == "Ice.FacetNotExistException")
PyObject* requestFailedExceptionType = lookupType("Ice.RequestFailedException");

if (PyObject_IsInstance(ex.get(), requestFailedExceptionType))
{
throw Ice::FacetNotExistException(__FILE__, __LINE__);
PyObjectHandle idAttr = getAttr(ex.get(), "id", false);
Ice::Identity id;
if (idAttr.get())
{
IcePy::getIdentity(idAttr.get(), id);
}
PyObjectHandle facetAttr = getAttr(ex.get(), "facet", false);
string facet = getString(facetAttr.get());
PyObjectHandle operationAttr = getAttr(ex.get(), "operation", false);
string operation = getString(operationAttr.get());

if (typeName == "Ice.ObjectNotExistException")
{
throw Ice::ObjectNotExistException(
__FILE__,
__LINE__,
std::move(id),
std::move(facet),
std::move(operation));
}
else if (typeName == "Ice.OperationNotExistException")
{
throw Ice::OperationNotExistException(
__FILE__,
__LINE__,
std::move(id),
std::move(facet),
std::move(operation));
}
else if (typeName == "Ice.FacetNotExistException")
{
throw Ice::FacetNotExistException(
__FILE__,
__LINE__,
std::move(id),
std::move(facet),
std::move(operation));
}
}

IcePy::PyObjectHandle exStr = PyObject_Str(ex.get());
Expand Down
40 changes: 40 additions & 0 deletions python/test/Ice/exceptions/AllTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -839,4 +839,44 @@ def allTests(helper, communicator):
pass
print("ok")

sys.stdout.write("testing exceptions in request failed exception... ")
sys.stdout.flush()
try:
thrower.throwRequestFailedException(
"Ice.ObjectNotExistException",
Ice.Identity("name", "category"),
"facet",
"operation")
test(False)
except Ice.ObjectNotExistException as ex:
test(ex.id == Ice.Identity("name", "category"))
test(ex.facet == "facet")
test(ex.operation == "operation")

try:
thrower.throwRequestFailedException(
"Ice.OperationNotExistException",
Ice.Identity("name", "category"),
"facet",
"operation")
test(False)
except Ice.OperationNotExistException as ex:
test(ex.id == Ice.Identity("name", "category"))
test(ex.facet == "facet")
test(ex.operation == "operation")

try:
thrower.throwRequestFailedException(
"Ice.FacetNotExistException",
Ice.Identity("name", "category"),
"facet",
"operation")
test(False)
except Ice.FacetNotExistException as ex:
test(ex.id == Ice.Identity("name", "category"))
test(ex.facet == "facet")
test(ex.operation == "operation")


print("ok")
return thrower
8 changes: 8 additions & 0 deletions python/test/Ice/exceptions/ServerAMD.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,14 @@ def throwMarshalException(self, current):
return Ice.Future.completed((0, ""))
return Ice.Future.completed(None)

def throwRequestFailedException(self, typeName, id, facet, operation, current):
if typeName == "Ice.ObjectNotExistException":
raise Ice.ObjectNotExistException(id, facet, operation)
elif typeName == "Ice.FacetNotExistException":
raise Ice.FacetNotExistException(id, facet, operation)
elif typeName == "Ice.OperationNotExistException":
raise Ice.OperationNotExistException(id, facet, operation)


class ServerAMD(TestHelper):
def run(self, args):
Expand Down
3 changes: 3 additions & 0 deletions python/test/Ice/exceptions/Test.ice
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#pragma once

#include "Ice/BuiltinSequences.ice"
#include "Ice/Identity.ice"

module Test
{
Expand Down Expand Up @@ -73,6 +74,8 @@ interface Thrower
void throwAfterException() throws A;

int throwMarshalException(out int p);

void throwRequestFailedException(string type, Ice::Identity id, string facet, string operation);
}

interface WrongOperation
Expand Down
8 changes: 8 additions & 0 deletions python/test/Ice/exceptions/TestI.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,11 @@ def throwMarshalException(self, current):
return ("", 0)
if "param" in current.ctx:
return (0, "")

def throwRequestFailedException(self, typeName, id, facet, operation, current):
if typeName == "Ice.ObjectNotExistException":
raise Ice.ObjectNotExistException(id, facet, operation)
elif typeName == "Ice.FacetNotExistException":
raise Ice.FacetNotExistException(id, facet, operation)
elif typeName == "Ice.OperationNotExistException":
raise Ice.OperationNotExistException(id, facet, operation)

0 comments on commit 765d815

Please sign in to comment.