diff --git a/src/runtime/python/compat.h b/src/runtime/python/compat.h deleted file mode 100644 index cc0ad2312..000000000 --- a/src/runtime/python/compat.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef PYPGF_COMPAT_H_ -#define PYPGF_COMPAT_H_ - -// #if PY_MAJOR_VERSION >= 3 - // #define PyIntObject PyLongObject - // #define PyInt_Type PyLong_Type - // #define PyInt_Check(op) PyLong_Check(op) - // #define PyInt_CheckExact(op) PyLong_CheckExact(op) - // #define PyInt_FromString PyLong_FromString - // #define PyInt_FromUnicode PyLong_FromUnicode - // #define PyInt_FromLong PyLong_FromLong - // #define PyInt_FromSize_t PyLong_FromSize_t - // #define PyInt_FromSsize_t PyLong_FromSsize_t - // #define PyInt_AsLong PyLong_AsLong - // #define PyInt_AS_LONG PyLong_AS_LONG - // #define PyInt_AsSsize_t PyLong_AsSsize_t - // #define PyInt_AsUnsignedLongMask PyLong_AsUnsignedLongMask - // #define PyInt_AsUnsignedLongLongMask PyLong_AsUnsignedLongLongMask - - // #define PyStringObject PyUnicodeObject - // #define PyString_Check PyUnicode_Check - // #define PyString_FromStringAndSize PyUnicode_FromStringAndSize - // #define PyString_FromFormat PyUnicode_FromFormat - // #define PyString_Concat(ps,s) {PyObject* tmp = *(ps); *(ps) = PyUnicode_Concat(tmp,s); Py_DECREF(tmp);} - // #define PyString_Concat PyUnicode_Concat - // #define PyString_Compare PyUnicode_Compare -// #endif - -#endif // PYPGF_COMPAT_H_ diff --git a/src/runtime/python/expr.c b/src/runtime/python/expr.c index 2f2474bf3..95f048f43 100644 --- a/src/runtime/python/expr.c +++ b/src/runtime/python/expr.c @@ -2,7 +2,7 @@ #include #include -#include "./compat.h" +#include #include "./expr.h" #include "./marshaller.h" @@ -245,6 +245,9 @@ ExprAbs_init(ExprAbsObject *self, PyObject *args, PyObject *kwds) self->bindType = bindType; self->var = var; self->expr = expr; + Py_INCREF(bindType); + Py_INCREF(var); + Py_INCREF(expr); return 0; } @@ -331,9 +334,10 @@ ExprApp_init(ExprAppObject *self, PyObject *args, PyObject *kwds) if (!PyArg_ParseTuple(args, "O!O!", &pgf_ExprType, &e1, &pgf_ExprType, &e2)) { return -1; } - self->e1 = (ExprObject *)e1; self->e2 = (ExprObject *)e2; + Py_INCREF(e1); + Py_INCREF(e2); return 0; } @@ -420,6 +424,7 @@ ExprLit_init(ExprLitObject *self, PyObject *args, PyObject *kwds) } if (PyLong_Check(lit) || PyFloat_Check(lit) || PyUnicode_Check(lit)) { self->value = lit; + Py_INCREF(lit); return 0; } else { PyErr_SetString(PyExc_TypeError, "invalid argument in ExprLit_init"); @@ -528,6 +533,7 @@ ExprMeta_init(ExprMetaObject *self, PyObject *args, PyObject *kwds) return 0; } else if (PyLong_Check(lit)) { self->id = lit; + Py_INCREF(lit); return 0; } else { PyErr_SetString(PyExc_TypeError, "invalid argument in ExprMeta_init"); @@ -616,6 +622,7 @@ ExprFun_init(ExprFunObject *self, PyObject *args, PyObject *kwds) return -1; } self->name = lit; + Py_INCREF(lit); return 0; } @@ -704,6 +711,7 @@ ExprVar_init(ExprVarObject *self, PyObject *args, PyObject *kwds) return 0; } else if (PyLong_Check(lit)) { self->index = lit; + Py_INCREF(lit); return 0; } else { PyErr_SetString(PyExc_TypeError, "invalid argument in ExprVar_init"); @@ -794,6 +802,8 @@ ExprTyped_init(ExprTypedObject *self, PyObject *args, PyObject *kwds) } self->expr = expr; self->type = type; + Py_INCREF(expr); + Py_INCREF(type); return 0; } @@ -879,6 +889,7 @@ ExprImplArg_init(ExprImplArgObject *self, PyObject *args, PyObject *kwds) return -1; } self->expr = expr; + Py_INCREF(expr); return 0; } diff --git a/src/runtime/python/expr.h b/src/runtime/python/expr.h index e062516b1..dbbdf1bde 100644 --- a/src/runtime/python/expr.h +++ b/src/runtime/python/expr.h @@ -6,8 +6,6 @@ #include -#include "./compat.h" - typedef struct { PyObject_HEAD PyObject *hypos; // PyListObject of PyTupleObject: (bind_type: int, cid: string, type: TypeObject) diff --git a/src/runtime/python/marshaller.c b/src/runtime/python/marshaller.c index 4d847bf45..562d7853f 100644 --- a/src/runtime/python/marshaller.c +++ b/src/runtime/python/marshaller.c @@ -4,8 +4,8 @@ #include #include -#include "./compat.h" #include "./expr.h" +#include "./marshaller.h" // ---------------------------------------------------------------------------- diff --git a/src/runtime/python/marshaller.h b/src/runtime/python/marshaller.h index 110334f21..032b0f90b 100644 --- a/src/runtime/python/marshaller.h +++ b/src/runtime/python/marshaller.h @@ -1,17 +1,15 @@ #ifndef PYPGF_MARSHALLER_H_ #define PYPGF_MARSHALLER_H_ -// #define PY_SSIZE_T_CLEAN -// #include +#define PY_SSIZE_T_CLEAN +#include #include PgfText *PyUnicode_AsPgfText(PyObject *pystr); - PyObject *PyUnicode_FromPgfText(PgfText *text); extern PgfUnmarshaller unmarshaller; - extern PgfMarshaller marshaller; #endif // PYPGF_MARSHALLER_H_ diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index bbba72906..2f91c5a62 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -3,7 +3,6 @@ #include #include -#include "./compat.h" #include "./expr.h" #include "./marshaller.h" @@ -2524,7 +2523,7 @@ pgf_bootNGF(PyObject *self, PyObject *args) static PGFObject* pgf_readNGF(PyObject *self, PyObject *args) { -const char *fpath; + const char *fpath; if (!PyArg_ParseTuple(args, "s", &fpath)) return NULL; diff --git a/src/runtime/python/test_suite.py b/src/runtime/python/test_suite.py index a7db80f72..9c9e0c69f 100644 --- a/src/runtime/python/test_suite.py +++ b/src/runtime/python/test_suite.py @@ -282,14 +282,37 @@ def test_showExpr_evar_4(): # expressions: lambda abstractions - # ,TestCase (assertEqual "show expression 8" "\\w->w" (showExpr ["z","y","x"] (EAbs Explicit "w" (EVar 0)))) - # ,TestCase (assertEqual "show expression 9" "\\v,w->z" (showExpr ["z","y","x"] (EAbs Explicit "v" (EAbs Explicit "w" (EVar 2))))) - # ,TestCase (assertEqual "show expression 10" "\\v,{w}->z" (showExpr ["z","y","x"] (EAbs Explicit "v" (EAbs Implicit "w" (EVar 2))))) - # ,TestCase (assertEqual "show expression 11" "\\v,{w},z->z" (showExpr ["y","x"] (EAbs Explicit "v" (EAbs Implicit "w" (EAbs Explicit "z" (EVar 0)))))) - # ,TestCase (assertEqual "show expression 12" "\\v,{w,z}->v" (showExpr ["y","x"] (EAbs Explicit "v" (EAbs Implicit "w" (EAbs Implicit "z" (EVar 2)))))) - # ,TestCase (assertEqual "show expression 13" "\\v,{w,z},t->v" (showExpr ["y","x"] (EAbs Explicit "v" (EAbs Implicit "w" (EAbs Implicit "z" (EAbs Explicit "t" (EVar 3))))))) - # ,TestCase (assertEqual "show expression 14" "\\u,v,{w,z},t->v" (showExpr ["y","x"] (EAbs Explicit "u" (EAbs Explicit "v" (EAbs Implicit "w" (EAbs Implicit "z" (EAbs Explicit "t" (EVar 3)))))))) - # ,TestCase (assertEqual "show expression 15" "f (\\x->x)" (showExpr [] (EApp (EFun "f") (EAbs Explicit "x" (EVar 0))))) +def test_showExpr_eabs_1(): + expr = pgf.ExprAbs(0, "w", pgf.ExprVar(0)) + assert pgf.showExpr(["z", "y", "x"], expr) == "\\w->w" + +def test_showExpr_eabs_2(): + expr = pgf.ExprAbs(0, "v", pgf.ExprAbs(0, "w", pgf.ExprVar(2))) + assert pgf.showExpr(["z", "y", "x"], expr) == "\\v,w->z" + +def test_showExpr_eabs_3(): + expr = pgf.ExprAbs(0, "v", pgf.ExprAbs(1, "w", pgf.ExprVar(2))) + assert pgf.showExpr(["z", "y", "x"], expr) == "\\v,{w}->z" + +def test_showExpr_eabs_4(): + expr = pgf.ExprAbs(0, "v", pgf.ExprAbs(1, "w", pgf.ExprAbs(0, "z", pgf.ExprVar(0)))) + assert pgf.showExpr(["y", "x"], expr) == "\\v,{w},z->z" + +def test_showExpr_eabs_5(): + expr = pgf.ExprAbs(0, "v", pgf.ExprAbs(1, "w", pgf.ExprAbs(1, "z", pgf.ExprVar(2)))) + assert pgf.showExpr(["y", "x"], expr) == "\\v,{w,z}->v" + +def test_showExpr_eabs_6(): + expr = pgf.ExprAbs(0, "v", pgf.ExprAbs(1, "w", pgf.ExprAbs(1, "z", pgf.ExprAbs(0, "t", pgf.ExprVar(3))))) + assert pgf.showExpr(["y", "x"], expr) == "\\v,{w,z},t->v" + +def test_showExpr_eabs_7(): + expr = pgf.ExprAbs(0, "u", pgf.ExprAbs(0, "v", pgf.ExprAbs(1, "w", pgf.ExprAbs(1, "z", pgf.ExprAbs(0, "t", pgf.ExprVar(3)))))) + assert pgf.showExpr(["y", "x"], expr) == "\\u,v,{w,z},t->v" + +def test_showExpr_eabs_8(): + expr = pgf.ExprApp(pgf.ExprFun("f"), pgf.ExprAbs(0, "x", pgf.ExprVar(0))) + assert pgf.showExpr([], expr) == "f (\\x->x)" # expressions: meta variables @@ -313,5 +336,3 @@ def test_readExpr_emeta_equality(): def test_readExpr_emeta_str(): assert str(pgf.readExpr("")) == "" - - # ,TestCase (assertEqual "show expression 18" "" (showExpr [] (ETyped (EFun "z") (DTyp [] "N" []))))