To_Host for composite types to Python. Implement To_Host conversion for Hash -> Python dict and Vector -> Python list.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/bebc7589 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/bebc7589 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/bebc7589 Branch: refs/heads/master Commit: bebc75899ec1dbbbd040fbf6ff21b1b6c4b9be97 Parents: 3e4952c Author: Marvin Humphrey <[email protected]> Authored: Mon Jan 25 17:02:25 2016 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Tue Feb 23 18:22:03 2016 -0800 ---------------------------------------------------------------------- runtime/python/cfext/CFBind.c | 39 ++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/bebc7589/runtime/python/cfext/CFBind.c ---------------------------------------------------------------------- diff --git a/runtime/python/cfext/CFBind.c b/runtime/python/cfext/CFBind.c index ef0d094..f524b78 100644 --- a/runtime/python/cfext/CFBind.c +++ b/runtime/python/cfext/CFBind.c @@ -31,6 +31,7 @@ #include "Clownfish/ByteBuf.h" #include "Clownfish/Err.h" #include "Clownfish/Hash.h" +#include "Clownfish/HashIterator.h" #include "Clownfish/Method.h" #include "Clownfish/Num.h" #include "Clownfish/String.h" @@ -572,16 +573,42 @@ CFISH_BB_To_Host_IMP(cfish_ByteBuf *self) { void* CFISH_Vec_To_Host_IMP(cfish_Vector *self) { - CFISH_UNUSED_VAR(self); - CFISH_THROW(CFISH_ERR, "TODO"); - CFISH_UNREACHABLE_RETURN(void*); + uint32_t num_elems = CFISH_Vec_Get_Size(self); + PyObject *list = PyList_New(num_elems); + + // Iterate over vector items. + for (uint32_t i = 0; i < num_elems; i++) { + cfish_Obj *val = CFISH_Vec_Fetch(self, i); + PyObject *item = CFBind_cfish_to_py(val); + PyList_SET_ITEM(list, i, item); + } + + return list; } void* CFISH_Hash_To_Host_IMP(cfish_Hash *self) { - CFISH_UNUSED_VAR(self); - CFISH_THROW(CFISH_ERR, "TODO"); - CFISH_UNREACHABLE_RETURN(void*); + PyObject *dict = PyDict_New(); + + // Iterate over key-value pairs. + cfish_HashIterator *iter = cfish_HashIter_new(self); + while (CFISH_HashIter_Next(iter)) { + cfish_String *key = (cfish_String*)CFISH_HashIter_Get_Key(iter); + if (!cfish_Obj_is_a((cfish_Obj*)key, CFISH_STRING)) { + CFISH_THROW(CFISH_ERR, "Non-string key: %o", + cfish_Obj_get_class_name((cfish_Obj*)key)); + } + size_t size = CFISH_Str_Get_Size(key); + const char *ptr = CFISH_Str_Get_Ptr8(key); + PyObject *py_key = PyUnicode_FromStringAndSize(ptr, size); + PyObject *py_val = CFBind_cfish_to_py(CFISH_HashIter_Get_Value(iter)); + PyDict_SetItem(dict, py_key, py_val); + Py_DECREF(py_key); + Py_DECREF(py_val); + } + CFISH_DECREF(iter); + + return dict; } void*
