summaryrefslogtreecommitdiff
path: root/py/modules/IcePy/Util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'py/modules/IcePy/Util.cpp')
-rw-r--r--py/modules/IcePy/Util.cpp128
1 files changed, 127 insertions, 1 deletions
diff --git a/py/modules/IcePy/Util.cpp b/py/modules/IcePy/Util.cpp
index a25677805e9..d01d56b3b76 100644
--- a/py/modules/IcePy/Util.cpp
+++ b/py/modules/IcePy/Util.cpp
@@ -8,7 +8,7 @@
// **********************************************************************
#include <Util.h>
-#include <Identity.h>
+#include <Ice/IdentityUtil.h>
#include <Ice/LocalException.h>
#include <Slice/PythonUtil.h>
@@ -535,3 +535,129 @@ IcePy::handleSystemExit()
ex = 0;
Py_Exit(status);
}
+
+PyObject*
+IcePy::createIdentity(const Ice::Identity& identity)
+{
+ PyObject* identityType = lookupType("Ice.Identity");
+
+ PyObjectHandle obj = PyObject_CallObject(identityType, NULL);
+ if(obj.get() == NULL)
+ {
+ return NULL;
+ }
+
+ if(!setIdentity(obj.get(), identity))
+ {
+ return NULL;
+ }
+
+ return obj.release();
+}
+
+bool
+IcePy::checkIdentity(PyObject* p)
+{
+ PyObject* identityType = lookupType("Ice.Identity");
+ return PyObject_IsInstance(p, identityType) == 1;
+}
+
+bool
+IcePy::setIdentity(PyObject* p, const Ice::Identity& identity)
+{
+ assert(checkIdentity(p));
+ PyObjectHandle name = PyString_FromString(const_cast<char*>(identity.name.c_str()));
+ PyObjectHandle category = PyString_FromString(const_cast<char*>(identity.category.c_str()));
+ if(name.get() == NULL || category.get() == NULL)
+ {
+ return false;
+ }
+ if(PyObject_SetAttrString(p, "name", name.get()) < 0 || PyObject_SetAttrString(p, "category", category.get()) < 0)
+ {
+ return false;
+ }
+ return true;
+}
+
+bool
+IcePy::getIdentity(PyObject* p, Ice::Identity& identity)
+{
+ assert(checkIdentity(p));
+ PyObjectHandle name = PyObject_GetAttrString(p, "name");
+ PyObjectHandle category = PyObject_GetAttrString(p, "category");
+ if(name.get() != NULL)
+ {
+ char* s = PyString_AsString(name.get());
+ if(s == NULL)
+ {
+ PyErr_Format(PyExc_ValueError, "identity name must be a string");
+ return false;
+ }
+ identity.name = s;
+ }
+ if(category.get() != NULL)
+ {
+ char* s = PyString_AsString(category.get());
+ if(s == NULL)
+ {
+ PyErr_Format(PyExc_ValueError, "identity category must be a string");
+ return false;
+ }
+ identity.category = s;
+ }
+ return true;
+}
+
+extern "C"
+PyObject*
+Ice_identityToString(PyObject* /*self*/, PyObject* args)
+{
+ PyObject* identityType = IcePy::lookupType("Ice.Identity");
+ PyObject* p;
+ if(!PyArg_ParseTuple(args, "O!", identityType, &p))
+ {
+ return NULL;
+ }
+
+ Ice::Identity id;
+ if(!IcePy::getIdentity(p, id))
+ {
+ return NULL;
+ }
+
+ string s;
+ try
+ {
+ s = Ice::identityToString(id);
+ }
+ catch(const Ice::Exception& ex)
+ {
+ IcePy::setPythonException(ex);
+ return NULL;
+ }
+ return PyString_FromString(const_cast<char*>(s.c_str()));
+}
+
+extern "C"
+PyObject*
+Ice_stringToIdentity(PyObject* /*self*/, PyObject* args)
+{
+ char* str;
+ if(!PyArg_ParseTuple(args, "s", &str))
+ {
+ return NULL;
+ }
+
+ Ice::Identity id;
+ try
+ {
+ id = Ice::stringToIdentity(str);
+ }
+ catch(const Ice::Exception& ex)
+ {
+ IcePy::setPythonException(ex);
+ return NULL;
+ }
+
+ return IcePy::createIdentity(id);
+}