Skip to content

Commit

Permalink
First cut implementation of dumping control block to string
Browse files Browse the repository at this point in the history
  • Loading branch information
alisaifee committed Apr 17, 2015
1 parent 222aaf8 commit 5493f84
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
32 changes: 31 additions & 1 deletion pycrm114/pycrm114_module.c
Expand Up @@ -269,11 +269,38 @@ CB_load(PyObject *type, PyObject *args) {
return (PyObject *)self;
}

static PyObject *
CB_dumps(CB_Object *self, PyObject *args) {
FILE * fp;
char *buffer = 0;
size_t size = 0;
fp = open_memstream(&buffer, &size);
if (NULL == fp){
PyErr_Format(ErrorObject, "unable to allocate memory for dumping control block");
return NULL;
}
if (FALSE==crm114_cb_write_text_fp(self->p_cb, fp)) {
PyErr_Format(ErrorObject, "error storing control block");
if ( NULL!=buffer){
free(buffer);
}
return NULL;
}
fclose(fp);
PyObject * dump = PyString_FromString(buffer);
if ( NULL != buffer ){
free(buffer);
}
return dump;
}

static PyMethodDef CB_methods[] = {
{"dump", (PyCFunction)CB_dump, METH_VARARGS,
"store data block into a file"},
{"load", (PyCFunction)CB_load, METH_CLASS | METH_VARARGS,
"load data block from a file"},
{"dumps", (PyCFunction)CB_dumps, METH_VARARGS,
"store data block into a file"},
{NULL} /* sentinel */
};

Expand Down Expand Up @@ -411,7 +438,10 @@ DB_dump(DB_Object *self, PyObject *args) {
fflush(fp);
Py_RETURN_NONE;
}

static PyObject *
DB_dumps(DB_Object *self, PyObject *args) {
Py_RETURN_NONE;
}
static PyObject *
DB_load(PyObject *type, PyObject *args) {
PyObject *fpo;
Expand Down
12 changes: 12 additions & 0 deletions tests/test_simple_demo.py
Expand Up @@ -77,3 +77,15 @@ def test_classification(self):

s = db.classify_text(Willows_frag)
self.assertEqual(s.best_match(), "Alice")

def test_dumps_control_block(self):
cb = binding.ControlBlock(flags=(binding.CRM114_SVM | binding.CRM114_STRING),
classes=[("Alice", True), ("Macbeth", False)],
start_mem = 8000000)
data = cb.dumps()
output = tempfile.mktemp()
with open(output, 'w') as out:
out.write(data)
with open(output, 'r') as inp:
cb = binding.ControlBlock.load(inp)
print cb

0 comments on commit 5493f84

Please sign in to comment.