From 75322d590497acb0af015c07b24a72f9769542e6 Mon Sep 17 00:00:00 2001 From: "jordi.saludes" Date: Thu, 22 Jul 2010 15:03:49 +0000 Subject: [PATCH] Refactoring py object constructors. --- contrib/py-bindings/gfmodule.c | 13 +++++-------- contrib/py-bindings/pygf.h | 8 ++++++-- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/contrib/py-bindings/gfmodule.c b/contrib/py-bindings/gfmodule.c index 768fb5037..7da294560 100644 --- a/contrib/py-bindings/gfmodule.c +++ b/contrib/py-bindings/gfmodule.c @@ -97,8 +97,7 @@ languageCode(PGF *self, PyObject *args) Lang *lang; if (!PyArg_ParseTuple(args, "O", &lang)) return NULL; - if (!checkType(lang, &LangType)) - return NULL; + if (!checkType(lang, &LangType)) return NULL; char* scode = gf_languageCode(self, lang); if (scode) { PyObject* result = PyString_FromString(scode); @@ -296,9 +295,9 @@ PyModule_AddObject(m, "gf", (PyObject *)&t); } -inline Lang* newLang() { +/* inline Lang* newLang() { return (Lang*)LangType.tp_new(&LangType,NULL,NULL); -} + } inline Tree* newTree() { return (Tree*)TreeType.tp_new(&TreeType,NULL,NULL); @@ -307,9 +306,7 @@ inline Tree* newTree() { inline CId* newCId() { return (CId*)CIdType.tp_new(&CIdType,NULL,NULL); } +*/ inline PyObject* newList() { return PyList_New(0); } - -void append(PyObject* l, PyObject* ob) { - PyList_Append(l, ob); -} +inline void append(PyObject* l, PyObject* ob) { PyList_Append(l, ob); } diff --git a/contrib/py-bindings/pygf.h b/contrib/py-bindings/pygf.h index 101674dfa..04f142bcf 100644 --- a/contrib/py-bindings/pygf.h +++ b/contrib/py-bindings/pygf.h @@ -1,5 +1,4 @@ #include -//#include "pgf.h" #include "HsFFI.h" #ifdef __GLASGOW_HASKELL__ @@ -38,6 +37,10 @@ typedef struct { GFTYPE obj; \ } OBJ; +#define PYTYPE(OBJ) OBJ ## Type +#define NEWCONSTRUCTOR(OBJ) inline OBJ* new ## OBJ () {\ + return (OBJ*)PYTYPE(OBJ).tp_new(&PYTYPE(OBJ),NULL,NULL); } + #define NEWTYPE(TYPE,NAME,OBJECT,DOC) static PyTypeObject TYPE = {\ PyObject_HEAD_INIT(NULL)\ 0, /*ob_size*/\ @@ -63,7 +66,8 @@ typedef struct { DOC, /* tp_doc */\ }; #define NEWGF(OBJ,GFTYPE,TYPE,NAME,DOC) NEWOBJECT(OBJ,GFTYPE) \ - NEWTYPE(TYPE,NAME,OBJ,DOC) + NEWTYPE(TYPE,NAME,OBJ,DOC)\ + NEWCONSTRUCTOR(OBJ) // NEWOBJECT(CID, GF_CId)