forked from GitHub/gf-core
Add Transaction type to Python bindings, get first tests working.
This commit is contained in:
@@ -5,9 +5,43 @@
|
||||
#include <pgf/pgf.h>
|
||||
#include "./expr.h"
|
||||
#include "./ffi.h"
|
||||
#include "./transactions.h"
|
||||
|
||||
TransactionObject *
|
||||
PGF_newTransaction(PGFObject *self, PyObject *args)
|
||||
{
|
||||
PgfText *name = NULL;
|
||||
// PgfText *name = PyUnicode_AsPgfText(PyUnicode_FromString("transient"));
|
||||
|
||||
PgfExn err;
|
||||
PgfRevision rev = pgf_clone_revision(self->db, self->revision, name, &err);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
TransactionObject *trans = (TransactionObject *)pgf_TransactionType.tp_alloc(&pgf_TransactionType, 0);
|
||||
trans->pgf = self;
|
||||
trans->revision = rev;
|
||||
|
||||
return trans;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PGF_createFunction(PGFObject *self, PyObject *args)
|
||||
Transaction_commit(TransactionObject *self, PyObject *args)
|
||||
{
|
||||
PgfExn err;
|
||||
pgf_commit_revision(self->pgf->db, self->revision, &err);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
self->pgf->revision = self->revision;
|
||||
|
||||
Py_RETURN_TRUE;
|
||||
}
|
||||
|
||||
PyObject *
|
||||
Transaction_createFunction(TransactionObject *self, PyObject *args)
|
||||
{
|
||||
const char *s;
|
||||
Py_ssize_t size;
|
||||
@@ -22,7 +56,7 @@ PGF_createFunction(PGFObject *self, PyObject *args)
|
||||
fname->size = size;
|
||||
|
||||
PgfExn err;
|
||||
pgf_create_function(self->db, self->revision, fname, (PgfType) type, arity, prob, &marshaller, &err);
|
||||
pgf_create_function(self->pgf->db, self->revision, fname, (PgfType) type, arity, prob, &marshaller, &err);
|
||||
PyMem_Free(fname);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
@@ -32,7 +66,7 @@ PGF_createFunction(PGFObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PGF_dropFunction(PGFObject *self, PyObject *args)
|
||||
Transaction_dropFunction(TransactionObject *self, PyObject *args)
|
||||
{
|
||||
const char *s;
|
||||
Py_ssize_t size;
|
||||
@@ -44,7 +78,7 @@ PGF_dropFunction(PGFObject *self, PyObject *args)
|
||||
fname->size = size;
|
||||
|
||||
PgfExn err;
|
||||
pgf_drop_category(self->db, self->revision, fname, &err);
|
||||
pgf_drop_category(self->pgf->db, self->revision, fname, &err);
|
||||
PyMem_Free(fname);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
@@ -54,7 +88,7 @@ PGF_dropFunction(PGFObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PGF_createCategory(PGFObject *self, PyObject *args)
|
||||
Transaction_createCategory(TransactionObject *self, PyObject *args)
|
||||
{
|
||||
const char *s;
|
||||
Py_ssize_t size;
|
||||
@@ -70,7 +104,7 @@ PGF_createCategory(PGFObject *self, PyObject *args)
|
||||
PgfTypeHypo *context = NULL;
|
||||
|
||||
PgfExn err;
|
||||
pgf_create_category(self->db, self->revision, catname, n_hypos, context, prob, &marshaller, &err);
|
||||
pgf_create_category(self->pgf->db, self->revision, catname, n_hypos, context, prob, &marshaller, &err);
|
||||
PyMem_Free(catname);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
@@ -80,7 +114,7 @@ PGF_createCategory(PGFObject *self, PyObject *args)
|
||||
}
|
||||
|
||||
PyObject *
|
||||
PGF_dropCategory(PGFObject *self, PyObject *args)
|
||||
Transaction_dropCategory(TransactionObject *self, PyObject *args)
|
||||
{
|
||||
const char *s;
|
||||
Py_ssize_t size;
|
||||
@@ -92,7 +126,7 @@ PGF_dropCategory(PGFObject *self, PyObject *args)
|
||||
catname->size = size;
|
||||
|
||||
PgfExn err;
|
||||
pgf_drop_function(self->db, self->revision, catname, &err);
|
||||
pgf_drop_function(self->pgf->db, self->revision, catname, &err);
|
||||
PyMem_Free(catname);
|
||||
if (handleError(err) != PGF_EXN_NONE) {
|
||||
return NULL;
|
||||
@@ -100,3 +134,81 @@ PGF_dropCategory(PGFObject *self, PyObject *args)
|
||||
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
// static void
|
||||
// Transaction_dealloc(PGFObject* self)
|
||||
// {
|
||||
// Py_TYPE(self)->tp_free((PyObject*)self);
|
||||
// }
|
||||
|
||||
static PyGetSetDef Transaction_getseters[] = {
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
// static PyMemberDef Transaction_members[] = {
|
||||
// {NULL} /* Sentinel */
|
||||
// };
|
||||
|
||||
static PyMethodDef Transaction_methods[] = {
|
||||
{"commit", (PyCFunction)Transaction_commit, METH_VARARGS,
|
||||
"Commit transaction"
|
||||
},
|
||||
|
||||
{"createFunction", (PyCFunction)Transaction_createFunction, METH_VARARGS,
|
||||
"Create function"
|
||||
},
|
||||
{"dropFunction", (PyCFunction)Transaction_dropFunction, METH_VARARGS,
|
||||
"Drop function"
|
||||
},
|
||||
{"createCategory", (PyCFunction)Transaction_createCategory, METH_VARARGS,
|
||||
"Create category"
|
||||
},
|
||||
{"dropCategory", (PyCFunction)Transaction_dropCategory, METH_VARARGS,
|
||||
"Drop category"
|
||||
},
|
||||
{NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
PyTypeObject pgf_TransactionType = {
|
||||
PyVarObject_HEAD_INIT(NULL, 0)
|
||||
//0, /*ob_size*/
|
||||
"pgf.Transaction", /*tp_name*/
|
||||
sizeof(PGFObject), /*tp_basicsize*/
|
||||
0, /*tp_itemsize*/
|
||||
0, //(destructor)Transaction_dealloc, /*tp_dealloc*/
|
||||
0, /*tp_print*/
|
||||
0, /*tp_getattr*/
|
||||
0, /*tp_setattr*/
|
||||
0, /*tp_compare*/
|
||||
0, /*tp_repr*/
|
||||
0, /*tp_as_number*/
|
||||
0, /*tp_as_sequence*/
|
||||
0, /*tp_as_mapping*/
|
||||
0, /*tp_hash */
|
||||
0, /*tp_call*/
|
||||
0, // (reprfunc) Transaction_str, /*tp_str*/
|
||||
0, /*tp_getattro*/
|
||||
0, /*tp_setattro*/
|
||||
0, /*tp_as_buffer*/
|
||||
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
|
||||
"PGF transaction", /*tp_doc*/
|
||||
0, /*tp_traverse */
|
||||
0, /*tp_clear */
|
||||
0, /*tp_richcompare */
|
||||
0, /*tp_weaklistoffset */
|
||||
0, /*tp_iter */
|
||||
0, /*tp_iternext */
|
||||
Transaction_methods, /*tp_methods */
|
||||
0, //Transaction_members, /*tp_members */
|
||||
Transaction_getseters, /*tp_getset */
|
||||
0, /*tp_base */
|
||||
0, /*tp_dict */
|
||||
0, /*tp_descr_get */
|
||||
0, /*tp_descr_set */
|
||||
0, /*tp_dictoffset */
|
||||
0, /*tp_init */
|
||||
0, /*tp_alloc */
|
||||
0, /*tp_new */
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user