#10
authoradam <adamansky@gmail.com>
Fri, 8 Feb 2013 18:19:05 +0000 (01:19 +0700)
committeradam <adamansky@gmail.com>
Fri, 8 Feb 2013 18:19:05 +0000 (01:19 +0700)
pyejdb/nbproject/configurations.xml
pyejdb/pyejdb/EJDB.c
pyejdb/pyejdb/pyejdb.c
pyejdb/pyejdb/pyejdb.h

index 38b1daf..976fe33 100644 (file)
@@ -3,7 +3,7 @@
   <logicalFolder name="root" displayName="root" projectFiles="true" kind="ROOT">
     <df root="." name="pyejdb">
       <df name="pyejdb">
-        <in>ejdb.c</in>
+        <in>.stub</in>
         <in>pyejdb.c</in>
         <in>pyejdb.h</in>
       </df>
@@ -43,9 +43,7 @@
           </cTool>
         </makeTool>
       </makefileType>
-      <item path="pyejdb/ejdb.c" ex="false" tool="0" flavor2="2">
-        <cTool>
-        </cTool>
+      <item path="pyejdb/.stub" ex="false" tool="3" flavor2="0">
       </item>
       <item path="pyejdb/pyejdb.c" ex="false" tool="0" flavor2="2">
         <cTool>
index d090180..d6c70ff 100644 (file)
@@ -6,3 +6,6 @@ static PyTypeObject EJDBType = {
 
 };
 
+
+
+
index 4747317..8a41d42 100644 (file)
@@ -8,11 +8,59 @@ typedef struct {
 
 #include "EJDB.c"
 
+PyDoc_STRVAR(ejdb_m_doc, "EJDB http://ejdb.org");
+PyDoc_STRVAR(ejdb_version_doc, "version() -> str\n\n Returns the version string of the underlying EJDB library.");
+
+static PyObject* ejdb_version(PyObject *module) {
+    return PyString_FromString(tcversion);
+}
+
+/* cabinet_module.m_methods */
+static PyMethodDef pyejdb_m_methods[] = {
+    {"version", (PyCFunction) ejdb_version, METH_NOARGS, ejdb_version_doc},
+    {NULL} /* Sentinel */
+};
+
+
+#if PY_MAJOR_VERSION >= 3
+/* pyejdb_module */
+static PyModuleDef pyejdb_module = {
+    PyModuleDef_HEAD_INIT,
+    "pyejdb", /*m_name*/
+    ejdb_m_doc, /*m_doc*/
+    -1, /*m_size*/
+    pyejdb_m_methods, /*m_methods*/
+};
+#endif
+
 PyObject* init_pyejdb() {
     PyObject *pyejdb;
     if (PyType_Ready(&EJDBType)) {
         return NULL;
     }
+#if PY_MAJOR_VERSION >= 3
+    pyejdb = PyModule_Create(&pyejdb_module);
+#else
+    pyejdb = Py_InitModule3("pyejdb", pyejdb_m_methods, pyejdb_m_doc);
+#endif
+
+    if (!pyejdb) {
+        return NULL;
+    }
+    Error = PyErr_NewException("pyejdb.Error", NULL, NULL);
+    if (!Error || PyModule_AddObject(pyejdb, "Error", Error)) {
+        Py_XDECREF(Error);
+        goto fail;
+    }
+
+    
+
+    return pyejdb;
+
+fail:
+#if PY_MAJOR_VERSION >= 3
+    Py_DECREF(pyejdb);
+#endif
     return NULL;
 }
 
index ea1b516..942f1bd 100644 (file)
 #include <Python.h>
 #include <tcejdb/ejdb.h>
 
+/* Error */
+static PyObject *Error;
+
+/* PyUnicode to char * */
+char* PyUnicode_AsString(PyObject *unicode) {
+    PyObject *tmp;
+    char *result;
+    tmp = PyUnicode_EncodeUTF8(PyUnicode_AS_UNICODE(unicode),
+            PyUnicode_GET_SIZE(unicode), NULL);
+    if (!tmp) {
+        return NULL;
+    }
+    result = PyBytes_AS_STRING(tmp);
+    Py_DECREF(tmp);
+    return result;
+}
+
+char* PyUnicode_Object_AsString(PyObject *obj) {
+    if (PyUnicode_Check(obj)) {
+        return PyUnicode_AsString(obj);
+    }
+    if (PyBytes_Check(obj)) {
+        return PyBytes_AS_STRING(obj);
+    }
+    PyErr_SetString(PyExc_TypeError, "a bytes or unicode object is required");
+    return NULL;
+}
+
+#if PY_MAJOR_VERSION >= 3
+#define PyString_AsString PyUnicode_Object_AsString
+#define PyString_FromString PyUnicode_FromString
+#define PyInt_FromLong PyLong_FromLong
+#endif
+
+int PyModule_AddType(PyObject *module, const char *name, PyTypeObject *type) {
+    Py_INCREF(type);
+    if (PyModule_AddObject(module, name, (PyObject *) type)) {
+        Py_DECREF(type);
+        return -1;
+    }
+    return 0;
+}
+
 #endif /* PYEJDB_H */