From db0727552744772f66ccab73a101a5ffc1c922e9 Mon Sep 17 00:00:00 2001 From: krasimir Date: Wed, 12 Aug 2015 10:11:00 +0000 Subject: [PATCH] pickling/unpickling for types --- src/runtime/python/pypgf.c | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index 3f950683c..bf58fd6c7 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -955,10 +955,43 @@ fail: return res; } +static PyObject* +Type_reduce_ex(TypeObject* self, PyObject *args) +{ + int protocol; + if (!PyArg_ParseTuple(args, "i", &protocol)) + return NULL; + + PyObject* myModule = PyImport_ImportModule("pgf"); + if (myModule == NULL) + return NULL; + PyObject* py_readType = PyObject_GetAttrString(myModule, "readType"); + Py_DECREF(myModule); + if (py_readType == NULL) + return NULL; + + PyObject* py_str = Type_repr(self); + if (py_str == NULL) { + Py_DECREF(py_readType); + return NULL; + } + + PyObject* py_tuple = + Py_BuildValue("O(O)", py_readType, py_str); + + Py_DECREF(py_str); + Py_DECREF(py_readType); + + return py_tuple; +} + static PyMethodDef Type_methods[] = { {"unpack", (PyCFunction)Type_unpack, METH_VARARGS, "Decomposes a type into its components" }, + {"__reduce_ex__", (PyCFunction)Type_reduce_ex, METH_VARARGS, + "This method allows for transparent pickling/unpickling of types." + }, {NULL} /* Sentinel */ };