tsocket: try to fix the build on solaris FIONREAD was missing
[ira/wip.git] / lib / tdb / pytdb.c
index d6d0625686f1dc31ec0360e35d2f898548d59e69..159bc4dce5fa367005d07c76dcec98fd0a41d902 100644 (file)
    License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */
 
+#include "replace.h"
+#include "system/filesys.h"
+
 #include <Python.h>
-#ifdef HAVE_FSTAT
-#undef HAVE_FSTAT
+#ifndef Py_RETURN_NONE
+#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
 #endif
 
 /* Include tdb headers */
-#include <stdint.h>
-#include <signal.h>
 #include <tdb.h>
-#include <fcntl.h>
 
 typedef struct {
        PyObject_HEAD
        TDB_CONTEXT *ctx;
+       bool closed;
 } PyTdbObject;
 
 PyAPI_DATA(PyTypeObject) PyTdb;
@@ -59,7 +60,7 @@ static TDB_DATA PyString_AsTDB_DATA(PyObject *data)
 static PyObject *PyString_FromTDB_DATA(TDB_DATA data)
 {
        if (data.dptr == NULL && data.dsize == 0) {
-               return Py_None;
+               Py_RETURN_NONE;
        } else {
                PyObject *ret = PyString_FromStringAndSize((const char *)data.dptr, 
                                                                                                   data.dsize);
@@ -93,6 +94,7 @@ static PyObject *py_tdb_open(PyTypeObject *type, PyObject *args, PyObject *kwarg
 
        ret = PyObject_New(PyTdbObject, &PyTdb);
        ret->ctx = ctx;
+       ret->closed = false;
        return (PyObject *)ret;
 }
 
@@ -100,70 +102,74 @@ static PyObject *obj_transaction_cancel(PyTdbObject *self)
 {
        int ret = tdb_transaction_cancel(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_transaction_commit(PyTdbObject *self)
 {
        int ret = tdb_transaction_commit(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_transaction_recover(PyTdbObject *self)
 {
        int ret = tdb_transaction_recover(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_transaction_start(PyTdbObject *self)
 {
        int ret = tdb_transaction_start(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_reopen(PyTdbObject *self)
 {
        int ret = tdb_reopen(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_lockall(PyTdbObject *self)
 {
        int ret = tdb_lockall(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_unlockall(PyTdbObject *self)
 {
        int ret = tdb_unlockall(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_lockall_read(PyTdbObject *self)
 {
        int ret = tdb_lockall_read(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_unlockall_read(PyTdbObject *self)
 {
        int ret = tdb_unlockall_read(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_close(PyTdbObject *self)
 {
-       int ret = tdb_close(self->ctx);
+       int ret;
+       if (self->closed)
+               Py_RETURN_NONE;
+       ret = tdb_close(self->ctx);
+       self->closed = true;
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_get(PyTdbObject *self, PyObject *args)
@@ -191,7 +197,7 @@ static PyObject *obj_append(PyTdbObject *self, PyObject *args)
 
        ret = tdb_append(self->ctx, key, data);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_firstkey(PyTdbObject *self)
@@ -222,7 +228,7 @@ static PyObject *obj_delete(PyTdbObject *self, PyObject *args)
        key = PyString_AsTDB_DATA(py_key);
        ret = tdb_delete(self->ctx, key);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyObject *obj_has_key(PyTdbObject *self, PyObject *args)
@@ -257,7 +263,7 @@ static PyObject *obj_store(PyTdbObject *self, PyObject *args)
 
        ret = tdb_store(self->ctx, key, value, flag);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 
@@ -309,7 +315,7 @@ static PyObject *obj_clear(PyTdbObject *self)
 {
        int ret = tdb_wipe_all(self->ctx);
        PyErr_TDB_ERROR_IS_ERR_RAISE(ret, self->ctx);
-       return Py_None;
+       Py_RETURN_NONE;
 }
 
 static PyMethodDef tdb_object_methods[] = {
@@ -395,7 +401,8 @@ static PyObject *tdb_object_repr(PyTdbObject *self)
 
 static void tdb_object_dealloc(PyTdbObject *self)
 {
-       tdb_close(self->ctx);
+       if (!self->closed)
+               tdb_close(self->ctx);
        PyObject_Del(self);
 }