qpid-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From g...@apache.org
Subject svn commit: r1163534 - /qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i
Date Wed, 31 Aug 2011 08:57:40 GMT
Author: gsim
Date: Wed Aug 31 08:57:40 2011
New Revision: 1163534

URL: http://svn.apache.org/viewvc?rev=1163534&view=rev
Log:
QPID-3333: Patch from Anthony Foglia - Convert UUIDs to python equivalents (0011)

Modified:
    qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i

Modified: qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i
URL: http://svn.apache.org/viewvc/qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i?rev=1163534&r1=1163533&r2=1163534&view=diff
==============================================================================
--- qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i (original)
+++ qpid/trunk/qpid/cpp/bindings/swig_python_typemaps.i Wed Aug 31 08:57:40 2011
@@ -17,6 +17,25 @@
  * under the License.
  */
 
+/* For UUID objects, to convert them to Python uuid.UUID objects,
+ * we'll need a reference to the uuid module.
+ */
+%{
+static PyObject* pUuidModule;
+%}
+
+%init %{
+  pUuidModule = PyImport_ImportModule("uuid");
+
+  /* Although it is not required, we'll publish the uuid module in our
+   * module, as if this module was a python module and we called
+   * "import uuid"
+   */
+  Py_INCREF(pUuidModule);
+  PyModule_AddObject(m, "uuid", pUuidModule);
+%}
+
+
 %wrapper %{
 
 #if PY_VERSION_HEX < 0x02050000 && !defined(PY_SSIZE_T_MIN)
@@ -28,6 +47,7 @@ typedef int Py_ssize_t;
 
     PyObject* MapToPy(const qpid::types::Variant::Map*);
     PyObject* ListToPy(const qpid::types::Variant::List*);
+    PyObject* UuidToPy(const qpid::types::Uuid*);
     void PyToMap(PyObject*, qpid::types::Variant::Map*);
     void PyToList(PyObject*, qpid::types::Variant::List*);
 
@@ -104,6 +124,9 @@ typedef int Py_ssize_t;
                 break;
             }
             case qpid::types::VAR_UUID : {
+                qpid::types::Uuid uuid = v->asUuid();
+                result = UuidToPy(&uuid);
+                break;
             }
             }
         } catch (qpid::types::Exception& ex) {
@@ -143,6 +166,30 @@ typedef int Py_ssize_t;
         return result;
     }
 
+    PyObject* UuidToPy(const qpid::types::Uuid * uuid) {
+        PyObject* pUuidClass = PyObject_GetAttrString(pUuidModule, "UUID");
+        if (!pUuidClass) {
+          // Failed to get UUID class
+          return 0;
+        }
+
+        PyObject* pArgs = PyTuple_New(0);
+        PyObject* pKw = PyDict_New();
+        PyObject* pData = PyString_FromStringAndSize(
+          (const char*)(uuid->data()), 16);
+        PyDict_SetItemString(pKw, "bytes", pData);
+
+        PyObject* result = PyObject_Call(pUuidClass, pArgs, pKw);
+
+        Py_DECREF(pData);
+        Py_DECREF(pKw);
+        Py_DECREF(pArgs);
+        Py_DECREF(pUuidClass);
+
+        return result;
+    }
+
+
     void PyToMap(PyObject* obj, qpid::types::Variant::Map* map) {
         map->clear();
         Py_ssize_t iter(0);
@@ -304,6 +351,15 @@ typedef int Py_ssize_t;
         Py_INCREF($result);
 }
 
+/*
+ * UUID type: C++ --> Python
+ */
+%typemap(out) qpid::types::UUID & {
+    $result = UuidToPy($1);
+    if ($result)
+        Py_INCREF($result);
+}
+
 
 /*
  * Variant types: Ruby --> C++



---------------------------------------------------------------------
Apache Qpid - AMQP Messaging Implementation
Project:      http://qpid.apache.org
Use/Interact: mailto:commits-subscribe@qpid.apache.org


Mime
View raw message