From 8c3f9c8d7350d86803a59fb189ff39f0060dca0c Mon Sep 17 00:00:00 2001 From: "John J. Camilleri" Date: Mon, 27 Sep 2021 15:37:33 +0200 Subject: [PATCH] Use PyBool instead of PyLong for bind_type --- src/runtime/python/expr.c | 20 +++++++------------- src/runtime/python/expr.h | 4 ++-- src/runtime/python/ffi.c | 9 +++++---- src/runtime/python/pypgf.c | 29 ++++++++++++++++++++--------- 4 files changed, 34 insertions(+), 28 deletions(-) diff --git a/src/runtime/python/expr.c b/src/runtime/python/expr.c index b6ceff16a..1d710374c 100644 --- a/src/runtime/python/expr.c +++ b/src/runtime/python/expr.c @@ -166,27 +166,21 @@ PyTypeObject pgf_TypeType = { static HypoObject * Hypo_new(PyTypeObject *subtype, PyObject *args, PyObject *kwds) { - HypoObject* self = (HypoObject *)subtype->tp_alloc(subtype, 0); + HypoObject *self = (HypoObject *)subtype->tp_alloc(subtype, 0); return self; } static int Hypo_init(HypoObject *self, PyObject *args, PyObject *kwds) { - int bind_type; - PyObject* cid; - TypeObject* type; - if (!PyArg_ParseTuple(args, "iUO!", &bind_type, &cid, &pgf_TypeType, &type)) { - return -1; - } - - if (bind_type == 0 || bind_type == 1) { - self->bind_type = PyLong_FromLong(bind_type); - } else { - PyErr_SetString(PyExc_TypeError, "invalid bind type in hypo initialisation"); + PyObject *bind_type; + PyObject *cid; + TypeObject *type; + if (!PyArg_ParseTuple(args, "O!UO!", &PyBool_Type, &bind_type, &cid, &pgf_TypeType, &type)) { return -1; } + self->bind_type = bind_type; self->cid = cid; self->type = type; Py_INCREF(self->bind_type); @@ -567,7 +561,7 @@ ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds) PyObject* bind_type = NULL; PyObject* name = NULL; ExprObject* body = NULL; - if (!PyArg_ParseTuple(args, "O!UO!", &PyLong_Type, &bind_type, &name, &pgf_ExprType, &body)) { + if (!PyArg_ParseTuple(args, "O!UO!", &PyBool_Type, &bind_type, &name, &pgf_ExprType, &body)) { return -1; } self->bind_type = bind_type; diff --git a/src/runtime/python/expr.h b/src/runtime/python/expr.h index 471d0f9d2..156c8bca1 100644 --- a/src/runtime/python/expr.h +++ b/src/runtime/python/expr.h @@ -17,7 +17,7 @@ PyTypeObject pgf_TypeType; typedef struct { PyObject_HEAD - PyObject *bind_type; // PyLongObject + PyObject *bind_type; // PyBool PyObject *cid; // PyUnicodeObject TypeObject *type; } HypoObject; @@ -30,7 +30,7 @@ typedef struct { typedef struct { PyObject_HEAD - PyObject *bind_type; // PyLongObject + PyObject *bind_type; // PyBool PyObject *name; // PyUnicodeObject ExprObject *body; } ExprAbsObject; diff --git a/src/runtime/python/ffi.c b/src/runtime/python/ffi.c index dd7db9e16..195183a6d 100644 --- a/src/runtime/python/ffi.c +++ b/src/runtime/python/ffi.c @@ -78,7 +78,7 @@ PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos) return NULL; } HypoObject *hypo = (HypoObject *)item; - hypos[i].bind_type = PyLong_AsLong(hypo->bind_type); + hypos[i].bind_type = hypo->bind_type == Py_True ? 0 : 1; hypos[i].cid = PyUnicode_AsPgfText(hypo->cid); hypos[i].type = (PgfType) hypo->type; Py_INCREF(hypos[i].type); @@ -97,7 +97,7 @@ PyList_FromHypos(PgfTypeHypo *hypos, const size_t n_hypos) for (size_t i = 0; i < n_hypos; i++) { HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = PyLong_FromLong(hypos[i].bind_type); + hypo->bind_type = hypos[i].bind_type == 0 ? Py_True : Py_False; hypo->cid = PyUnicode_FromStringAndSize(hypos[i].cid->text, hypos[i].cid->size); hypo->type = (TypeObject *)hypos[i].type; // Py_INCREF(hypo->type); @@ -157,7 +157,7 @@ static PgfExpr eabs(PgfUnmarshaller *this, PgfBindType btype, PgfText *name, PgfExpr body) { ExprAbsObject *pyexpr = (ExprAbsObject *)pgf_ExprAbsType.tp_alloc(&pgf_ExprAbsType, 0); - pyexpr->bind_type = PyLong_FromLong(btype); + pyexpr->bind_type = btype == 0 ? Py_True : Py_False; pyexpr->name = PyUnicode_FromPgfText(name); pyexpr->body = (ExprObject *)body; // Py_INCREF(body); @@ -367,7 +367,8 @@ match_expr(PgfMarshaller *this, PgfUnmarshaller *u, PgfExpr expr) if (PyObject_TypeCheck(pyobj, &pgf_ExprAbsType)) { ExprAbsObject *eabs = (ExprAbsObject *)expr; - return u->vtbl->eabs(u, PyLong_AsLong(eabs->bind_type), PyUnicode_AsPgfText(eabs->name), (PgfExpr) eabs->body); + long bt = eabs->bind_type == Py_True ? 0 : 1; + return u->vtbl->eabs(u, bt, PyUnicode_AsPgfText(eabs->name), (PgfExpr) eabs->body); } else if (PyObject_TypeCheck(pyobj, &pgf_ExprAppType)) { ExprAppObject *eapp = (ExprAppObject *)expr; diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index b40747978..9c329b8b6 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -619,9 +619,11 @@ pgf_mkHypo(PyObject *self, PyObject *args) return NULL; HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = PyLong_FromLong(0); // explicit + hypo->bind_type = Py_True; // explicit hypo->cid = PyUnicode_FromStringAndSize("_", 1); hypo->type = type; + Py_INCREF(hypo->bind_type); + Py_INCREF(hypo->cid); Py_INCREF(hypo->type); return hypo; @@ -636,9 +638,10 @@ pgf_mkDepHypo(PyObject *self, PyObject *args) return NULL; HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = PyLong_FromLong(0); // explicit + hypo->bind_type = Py_True; // explicit hypo->cid = var; hypo->type = type; + Py_INCREF(hypo->bind_type); Py_INCREF(hypo->cid); Py_INCREF(hypo->type); @@ -654,9 +657,10 @@ pgf_mkImplHypo(PyObject *self, PyObject *args) return NULL; HypoObject *hypo = PyObject_New(HypoObject, &pgf_HypoType); - hypo->bind_type = PyLong_FromLong(1); // implicit + hypo->bind_type = Py_False; // implicit hypo->cid = var; hypo->type = type; + Py_INCREF(hypo->bind_type); Py_INCREF(hypo->cid); Py_INCREF(hypo->type); @@ -711,14 +715,22 @@ static PyMethodDef module_methods[] = { if (PyType_Ready(&type) < 0) \ return MOD_ERROR_VAL; -#define ADD_TYPE(desc, type) \ +#define ADD_TYPE(name, type) \ Py_INCREF(&type); \ - if (PyModule_AddObject(m, desc, (PyObject *)&type) < 0) { \ + if (PyModule_AddObject(m, name, (PyObject *)&type) < 0) { \ Py_DECREF(&type); \ Py_DECREF(m); \ return NULL; \ } +#define ADD_TYPE_DIRECT(name, type) \ + Py_INCREF(type); \ + if (PyModule_AddObject(m, name, (PyObject *)type) < 0) { \ + Py_DECREF(type); \ + Py_DECREF(m); \ + return NULL; \ + } + MOD_INIT(pgf) { PyObject *m; @@ -742,8 +754,7 @@ MOD_INIT(pgf) return MOD_ERROR_VAL; PGFError = PyErr_NewException("pgf.PGFError", NULL, NULL); - PyModule_AddObject(m, "PGFError", PGFError); - Py_INCREF(PGFError); + ADD_TYPE_DIRECT("PGFError", PGFError); ADD_TYPE("PGF", pgf_PGFType); ADD_TYPE("Transaction", pgf_TransactionType); @@ -759,8 +770,8 @@ MOD_INIT(pgf) ADD_TYPE("Type", pgf_TypeType); ADD_TYPE("Hypo", pgf_HypoType); - PyModule_AddIntConstant(m, "BIND_TYPE_EXPLICIT", 0); - PyModule_AddIntConstant(m, "BIND_TYPE_IMPLICIT", 1); + ADD_TYPE_DIRECT("BIND_TYPE_EXPLICIT", Py_True); + ADD_TYPE_DIRECT("BIND_TYPE_IMPLICIT", Py_False); return MOD_SUCCESS_VAL(m); }