#include "pyejdb.h"
+PyObject* set_ejdb_error(EJDB *ejdb) {
+ int ecode;
+ ecode = ejdbecode(ejdb);
+ return set_error(Error, ejdberrmsg(ecode));
+}
-static PyTypeObject EJDBType = {
+/* EJDBType.tp_dealloc */
+static void EJDB_tp_dealloc(PEJDB *self) {
+ if (self->ejdb) {
+ ejdbdel(self->ejdb);
+ }
+ Py_TYPE(self)->tp_free((PyObject *) self);
+}
+/* EJDBType.tp_new */
+static PyObject* EJDB_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwargs) {
+ PEJDB *self = (PEJDB*) type->tp_alloc(type, 0);
+ if (!self) {
+ return NULL;
+ }
+ /* self->e */
+ self->ejdb = ejdbnew();
+ if (!self->ejdb) {
+ set_error(Error, "could not create EJDB");
+ Py_DECREF(self);
+ return NULL;
+ }
+ return (PyObject *) self;
+}
-};
+/* EJDBType.tp_doc */
+PyDoc_STRVAR(EJDB_tp_doc,
+ "EJDBType()\n\
+\n\
+EJDBType Database.\n\
+\n\
+See http://ejdb.org");
+
+/* HDB.open(path, mode) */
+PyDoc_STRVAR(EJDB_open_doc,
+ "open(path, mode)\n\
+\n\
+Open a database.\n\
+'path': path to the database file.\n\
+'mode': connection mode.");
+static PyObject* EJDB_open(PEJDB *self, PyObject *args) {
+ const char *path;
+ int mode;
+ if (!PyArg_ParseTuple(args, "si:open", &path, &mode)) {
+ return NULL;
+ }
+ if (!ejdbopen(self->ejdb, path, mode)) {
+ return set_ejdb_error(self->ejdb);
+ }
+ Py_RETURN_NONE;
+}
+/* EJDBType.tp_methods */
+static PyMethodDef EJDB_tp_methods[] = {
+ {"open", (PyCFunction) EJDB_open, METH_VARARGS, EJDB_open_doc},
+ //{"close", (PyCFunction) HDB_close, METH_NOARGS, HDB_close_doc},
+ {NULL}
+};
+static PyTypeObject EJDBType = {
+ PyVarObject_HEAD_INIT(NULL, 0)
+ "pyejdb.EJDB", /*tp_name*/
+ sizeof (EJDB), /*tp_basicsize*/
+ 0, /*tp_itemsize*/
+ (destructor) EJDB_tp_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, /*tp_str*/
+ 0, /*tp_getattro*/
+ 0, /*tp_setattro*/
+ 0, /*tp_as_buffer*/
+ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /*tp_flags*/
+ EJDB_tp_doc, /*tp_doc*/
+ 0, /*tp_traverse*/
+ 0, /*tp_clear*/
+ 0, /*tp_richcompare*/
+ 0, /*tp_weaklistoffset*/
+ 0, /*tp_iter*/
+ 0, /*tp_iternext*/
+ EJDB_tp_methods, /*tp_methods*/
+ 0, /*tp_members*/
+ 0, /*tp_getsets*/
+ 0, /*tp_base*/
+ 0, /*tp_dict*/
+ 0, /*tp_descr_get*/
+ 0, /*tp_descr_set*/
+ 0, /*tp_dictoffset*/
+ 0, /*tp_init*/
+ 0, /*tp_alloc*/
+ EJDB_tp_new, /*tp_new*/
+};
typedef struct {
PyObject_HEAD
EJDB *ejdb;
-} EJDB;
+} PEJDB;
#include "EJDB.c"
#else
pyejdb = Py_InitModule3("pyejdb", pyejdb_m_methods, pyejdb_m_doc);
#endif
-
if (!pyejdb) {
return NULL;
}
Error = PyErr_NewException("pyejdb.Error", NULL, NULL);
+ Py_XINCREF(Error);
if (!Error || PyModule_AddObject(pyejdb, "Error", Error)) {
Py_XDECREF(Error);
goto fail;
}
-
+ /* adding types and constants */
+ if (
+ PyModule_AddType(pyejdb, "EJDB", &EJDBType) ||
+
+ PyModule_AddIntMacro(pyejdb, JBOREADER) ||
+ PyModule_AddIntMacro(pyejdb, JBOWRITER) ||
+ PyModule_AddIntMacro(pyejdb, JBOCREAT) ||
+ PyModule_AddIntMacro(pyejdb, JBOTRUNC) ||
+ PyModule_AddIntMacro(pyejdb, JBONOLCK) ||
+ PyModule_AddIntMacro(pyejdb, JBOLCKNB) ||
+ PyModule_AddIntMacro(pyejdb, JBOTSYNC) ||
+
+ PyModule_AddIntMacro(pyejdb, JBIDXDROP) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXDROPALL) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXOP) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXOP) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXREBLD) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXNUM) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXSTR) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXARR) ||
+ PyModule_AddIntMacro(pyejdb, JBIDXISTR) ||
+
+ PyModule_AddIntMacro(pyejdb, JBQRYCOUNT)
+ ) {
+
+ goto fail;
+ }
return pyejdb;
#if PY_MAJOR_VERSION >= 3
-PyMODINIT_FUNC PyInit_cabinet(void) {
+PyMODINIT_FUNC PyInit_pyejdb(void) {
return init_pyejdb();
}
#else
-PyMODINIT_FUNC
-initcabinet(void) {
+PyMODINIT_FUNC initcabinet(void) {
init_pyejdb();
}
#endif