Add Python C binding for CFCHierarchy.
Project: http://git-wip-us.apache.org/repos/asf/lucy/repo Commit: http://git-wip-us.apache.org/repos/asf/lucy/commit/f4377ad0 Tree: http://git-wip-us.apache.org/repos/asf/lucy/tree/f4377ad0 Diff: http://git-wip-us.apache.org/repos/asf/lucy/diff/f4377ad0 Branch: refs/heads/python-bindings-wip1 Commit: f4377ad0840bb3d450243dcc80c721e6799eb570 Parents: 3e8f569 Author: Marvin Humphrey <[email protected]> Authored: Fri Apr 5 16:06:19 2013 -0700 Committer: Marvin Humphrey <[email protected]> Committed: Fri Apr 5 16:10:54 2013 -0700 ---------------------------------------------------------------------- clownfish/compiler/python/clownfish/_cfc.c | 128 +++++++++++++++ .../compiler/python/clownfish/cfc/__init__.py | 2 + clownfish/compiler/python/setup.py | 5 +- 3 files changed, 134 insertions(+), 1 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/lucy/blob/f4377ad0/clownfish/compiler/python/clownfish/_cfc.c ---------------------------------------------------------------------- diff --git a/clownfish/compiler/python/clownfish/_cfc.c b/clownfish/compiler/python/clownfish/_cfc.c new file mode 100644 index 0000000..f4bd341 --- /dev/null +++ b/clownfish/compiler/python/clownfish/_cfc.c @@ -0,0 +1,128 @@ + +#include "Python.h" +#include "CFC.h" + +typedef struct { + PyObject_HEAD + void *cfc_obj; +} CFCPyWrapper; + +static CFCHierarchy* +S_extract_hierarchy(PyObject *wrapper) { + return (CFCHierarchy*)((CFCPyWrapper*)wrapper)->cfc_obj; +} + +static CFCPyWrapper* +S_CFCHierarchy_new(PyTypeObject *type, PyObject *args, + PyObject *keyword_args) { + char *dest = NULL; + char *keywords[] = {"dest", NULL}; + int result = PyArg_ParseTupleAndKeywords(args, keyword_args, "|s", + keywords, &dest); + if (!result) { return NULL; } + if (!dest) { + PyErr_SetString(PyExc_TypeError, "Missing required arg 'dest'"); + return NULL; + } + CFCPyWrapper *wrapper = (CFCPyWrapper*)type->tp_alloc(type, 0); + if (wrapper) { + wrapper->cfc_obj = CFCHierarchy_new(dest); + } + return wrapper; +} + +static void +S_CFCHierarchy_dealloc(CFCPyWrapper *wrapper) { + CFCBase *temp = (CFCBase*)wrapper->cfc_obj; + wrapper->cfc_obj = NULL; + CFCBase_decref(temp); + Py_TYPE(wrapper)->tp_free(wrapper); +} + +static PyObject* +S_CFCHierarchy_add_include_dir(PyObject *wrapper, PyObject *dir) { + CFCHierarchy *wrapped = S_extract_hierarchy(wrapper); + CFCHierarchy_add_include_dir(S_extract_hierarchy(wrapper), + PyUnicode_AsUTF8(dir)); + Py_RETURN_NONE; +} + +static PyModuleDef cfc_module_def = { + PyModuleDef_HEAD_INIT, + "clownfish.cfc", + "CFC: Clownfish compiler", + -1, + NULL, NULL, NULL, NULL, NULL +}; + +static PyModuleDef cfc_model_module_def = { + PyModuleDef_HEAD_INIT, + "clownfish.cfc.model", + "CFC classes which model language constructs", + -1, + NULL, NULL, NULL, NULL, NULL +}; + +static PyMethodDef hierarchy_methods[] = { + {"add_include_dir", (PyCFunction)S_CFCHierarchy_add_include_dir, METH_O, + NULL}, + {NULL} +}; + +static PyTypeObject CFCHierarchy_pytype = { + PyVarObject_HEAD_INIT(NULL, 0) + "clownfish.cfc.model.Hierarchy", // tp_name + sizeof(CFCPyWrapper), // tp_basicsize + 0, // tp_itemsize + (destructor)S_CFCHierarchy_dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_reserved + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "CFCHierarchy", // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + hierarchy_methods, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_allow + (newfunc)S_CFCHierarchy_new // tp_new +}; + +PyMODINIT_FUNC +PyInit__cfc(void) { + if (PyType_Ready(&CFCHierarchy_pytype) < 0) { + return NULL; + } + PyObject *cfc_module = PyModule_Create(&cfc_module_def); + PyObject *cfc_model_module = PyModule_Create(&cfc_model_module_def); + PyModule_AddObject(cfc_module, "model", (PyObject*)cfc_model_module); + Py_INCREF(&CFCHierarchy_pytype); + PyModule_AddObject(cfc_model_module, "Hierarchy", + (PyObject*)&CFCHierarchy_pytype); + + return cfc_module; +} + + http://git-wip-us.apache.org/repos/asf/lucy/blob/f4377ad0/clownfish/compiler/python/clownfish/cfc/__init__.py ---------------------------------------------------------------------- diff --git a/clownfish/compiler/python/clownfish/cfc/__init__.py b/clownfish/compiler/python/clownfish/cfc/__init__.py new file mode 100644 index 0000000..d539758 --- /dev/null +++ b/clownfish/compiler/python/clownfish/cfc/__init__.py @@ -0,0 +1,2 @@ +from clownfish._cfc import * + http://git-wip-us.apache.org/repos/asf/lucy/blob/f4377ad0/clownfish/compiler/python/setup.py ---------------------------------------------------------------------- diff --git a/clownfish/compiler/python/setup.py b/clownfish/compiler/python/setup.py index 819eb7e..6596f80 100644 --- a/clownfish/compiler/python/setup.py +++ b/clownfish/compiler/python/setup.py @@ -44,6 +44,7 @@ paths_to_clean = [ CHARMONY_H_PATH, '_charm*', ] +c_filepaths.append(os.path.join('clownfish', '_cfc.c')) for (dirpath, dirnames, files) in os.walk(CFC_SOURCE_DIR): for filename in files: if filename.endswith('.y'): @@ -159,7 +160,7 @@ class my_build(_build): self.run_command('parsers') _build.run(self) -cfc_extension = Extension('clownfish.cfc', +cfc_extension = Extension('clownfish._cfc', define_macros = [('CFCPYTHON', None)], include_dirs = [ CFC_INCLUDE_DIR, @@ -174,6 +175,8 @@ setup(name = 'clownfish-cfc', author = 'Apache Lucy Project', author_email = 'dev at lucy dot apache dot org', url = 'http://lucy.apache.org', + packages = ['clownfish.cfc', + ], cmdclass = { 'build': my_build, 'clean': my_clean,
