diff options
author | Gordon Sim <gsim@apache.org> | 2011-08-31 08:57:40 +0000 |
---|---|---|
committer | Gordon Sim <gsim@apache.org> | 2011-08-31 08:57:40 +0000 |
commit | b132e640c77bac30982000602b36cfed7e02ff3d (patch) | |
tree | a9a197acbaa3519644c07ebc13551d76882aa84f | |
parent | 4d21cc3d80bb96e2bb43e99dc4707abd054e4532 (diff) | |
download | qpid-python-b132e640c77bac30982000602b36cfed7e02ff3d.tar.gz |
QPID-3333: Patch from Anthony Foglia - Convert UUIDs to python equivalents (0011)
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1163534 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | qpid/cpp/bindings/swig_python_typemaps.i | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/qpid/cpp/bindings/swig_python_typemaps.i b/qpid/cpp/bindings/swig_python_typemaps.i index b69784a6de..18bfd48f72 100644 --- a/qpid/cpp/bindings/swig_python_typemaps.i +++ b/qpid/cpp/bindings/swig_python_typemaps.i @@ -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++ |