Add ParseTuple converters for composite types. Provide ParseTuple-compatible C functions which convert Python list to Clownfish Vector and Python dict to Clownfish Hash.
Project: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/commit/2b65f360 Tree: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/tree/2b65f360 Diff: http://git-wip-us.apache.org/repos/asf/lucy-clownfish/diff/2b65f360 Branch: refs/heads/master Commit: 2b65f36064ae0f733123a82202dc97f18a6dd325 Parents: 99d6bc0 Author: Marvin Humphrey <[email protected]> Authored: Wed Jan 27 16:43:55 2016 -0800 Committer: Marvin Humphrey <[email protected]> Committed: Tue Feb 23 18:22:04 2016 -0800 ---------------------------------------------------------------------- runtime/python/cfext/CFBind.c | 78 ++++++++++++++++++++++++++++++++++++++ runtime/python/cfext/CFBind.h | 14 +++++++ 2 files changed, 92 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2b65f360/runtime/python/cfext/CFBind.c ---------------------------------------------------------------------- diff --git a/runtime/python/cfext/CFBind.c b/runtime/python/cfext/CFBind.c index 501c342..e7d15ed 100644 --- a/runtime/python/cfext/CFBind.c +++ b/runtime/python/cfext/CFBind.c @@ -282,6 +282,84 @@ CFBind_py_to_cfish_noinc(PyObject *py_obj, cfish_Class *klass, } static int +S_convert_hash(PyObject *py_obj, cfish_Hash **hash_ptr, bool nullable) { + if (py_obj == NULL) { // Py_CLEANUP_SUPPORTED cleanup + CFISH_DECREF(*hash_ptr); + return 1; + } + + if (py_obj == Py_None) { + if (nullable) { + return Py_CLEANUP_SUPPORTED; + } + else { + PyErr_SetString(PyExc_TypeError, "Required argument cannot be None"); + return 0; + } + } + else if (PyDict_CheckExact(py_obj)) { + *hash_ptr = S_py_dict_to_hash(py_obj); + return Py_CLEANUP_SUPPORTED; + } + else if (S_py_obj_is_a(py_obj, CFISH_HASH)) { + *hash_ptr = (cfish_Hash*)CFISH_INCREF(py_obj); + return Py_CLEANUP_SUPPORTED; + } + else { + return 0; + } +} + +int +CFBind_convert_hash(PyObject *py_obj, cfish_Hash **hash_ptr) { + return S_convert_hash(py_obj, hash_ptr, false); +} + +int +CFBind_maybe_convert_hash(PyObject *py_obj, cfish_Hash **hash_ptr) { + return S_convert_hash(py_obj, hash_ptr, true); +} + +static int +S_convert_vec(PyObject *py_obj, cfish_Vector **vec_ptr, bool nullable) { + if (py_obj == NULL) { // Py_CLEANUP_SUPPORTED cleanup + CFISH_DECREF(*vec_ptr); + return 1; + } + + if (py_obj == Py_None) { + if (nullable) { + return Py_CLEANUP_SUPPORTED; + } + else { + PyErr_SetString(PyExc_TypeError, "Required argument cannot be None"); + return 0; + } + } + else if (PyList_CheckExact(py_obj)) { + *vec_ptr = S_py_list_to_vector(py_obj); + return Py_CLEANUP_SUPPORTED; + } + else if (S_py_obj_is_a(py_obj, CFISH_VECTOR)) { + *vec_ptr = (cfish_Vector*)CFISH_INCREF(py_obj); + return Py_CLEANUP_SUPPORTED; + } + else { + return 0; + } +} + +int +CFBind_convert_vec(PyObject *py_obj, cfish_Vector **vec_ptr) { + return S_convert_vec(py_obj, vec_ptr, false); +} + +int +CFBind_maybe_convert_vec(PyObject *py_obj, cfish_Vector **vec_ptr) { + return S_convert_vec(py_obj, vec_ptr, true); +} + +static int S_convert_sint(PyObject *py_obj, void *ptr, bool nullable, unsigned width) { if (py_obj == Py_None) { if (nullable) { http://git-wip-us.apache.org/repos/asf/lucy-clownfish/blob/2b65f360/runtime/python/cfext/CFBind.h ---------------------------------------------------------------------- diff --git a/runtime/python/cfext/CFBind.h b/runtime/python/cfext/CFBind.h index e4e0f23..d7eb0ba 100644 --- a/runtime/python/cfext/CFBind.h +++ b/runtime/python/cfext/CFBind.h @@ -100,6 +100,20 @@ cfish_Obj* CFBind_py_to_cfish_noinc(PyObject *py_obj, cfish_Class *klass, void *allocation); +/* ParseTuple conversion routines for reference types. + * + * If `input` is `None`, the "maybe_convert" variants will leave `ptr` + * untouched, while the "convert" routines will raise a TypeError. + */ +int +CFBind_convert_hash(PyObject *input, cfish_Hash **ptr); +int +CFBind_convert_vec(PyObject *input, cfish_Vector **ptr); +int +CFBind_maybe_convert_hash(PyObject *input, cfish_Hash **ptr); +int +CFBind_maybe_convert_vec(PyObject *input, cfish_Vector **ptr); + /* ParseTuple conversion routines for primitive numeric types. * * If the value of `input` is out of range for the an integer C type, an
