forked from GitHub/gf-core
support for transparent pickling/unpickling of abstract expressions in Python
This commit is contained in:
@@ -74,6 +74,9 @@ Expr_unpack(ExprObject* self, PyObject *args);
|
||||
static PyObject*
|
||||
Expr_visit(ExprObject* self, PyObject *args);
|
||||
|
||||
static PyObject*
|
||||
Expr_reduce_ex(ExprObject* self, PyObject *args);
|
||||
|
||||
static int
|
||||
Expr_init(ExprObject *self, PyObject *args, PyObject *kwds)
|
||||
{
|
||||
@@ -150,6 +153,9 @@ static PyMethodDef Expr_methods[] = {
|
||||
"If the method doesn't exist then the method self.default(e) "
|
||||
"is called."
|
||||
},
|
||||
{"__reduce_ex__", (PyCFunction)Expr_reduce_ex, METH_VARARGS,
|
||||
"This method allows for transparent pickling/unpickling of expressions."
|
||||
},
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
@@ -538,6 +544,36 @@ Expr_visit(ExprObject* self, PyObject *args)
|
||||
return PyObject_CallMethod(py_visitor, "default", "O", self);
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
Expr_reduce_ex(ExprObject* 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_readExpr = PyObject_GetAttrString(myModule, "readExpr");
|
||||
Py_DECREF(myModule);
|
||||
if (py_readExpr == NULL)
|
||||
return NULL;
|
||||
|
||||
PyObject* py_str = Expr_repr(self);
|
||||
if (py_str == NULL) {
|
||||
Py_DECREF(py_readExpr);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
PyObject* py_tuple =
|
||||
Py_BuildValue("O(O)", py_readExpr, py_str);
|
||||
|
||||
Py_DECREF(py_str);
|
||||
Py_DECREF(py_readExpr);
|
||||
|
||||
return py_tuple;
|
||||
}
|
||||
|
||||
static PyObject*
|
||||
Expr_getattro(ExprObject *self, PyObject *attr_name) {
|
||||
const char* name = PyString_AsString(attr_name);
|
||||
|
||||
Reference in New Issue
Block a user