File: leveldb_ext.cc

package info (click to toggle)
python-leveldb 0~svn68-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 168 kB
  • ctags: 167
  • sloc: cpp: 1,252; python: 333; sh: 113; makefile: 18
file content (125 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// Copyright (c) Arni Mar Jonsson.
// See LICENSE for details.

// The Python 2/3 compatability code was found in cporting.rst

#include "leveldb_ext.h"

static PyMethodDef leveldb_extension_methods[] =
{
	{ (char*)"RepairDB",  (PyCFunction)pyleveldb_repair_db,  METH_VARARGS | METH_KEYWORDS, (char*)pyleveldb_repair_db_doc  },
	{ (char*)"DestroyDB", (PyCFunction)pyleveldb_destroy_db, METH_VARARGS, (char*)pyleveldb_destroy_db_doc },
	{NULL, NULL},
};

PyObject* leveldb_exception = 0;

#if PY_MAJOR_VERSION >= 3

struct leveldb_extension_state {
};

static int leveldb_extension_traverse(PyObject* m, visitproc visit, void* arg)
{
	return 0;
}

static int leveldb_extension_clear(PyObject* m)
{
	return 0;
}

static struct PyModuleDef leveldb_extension_def = {
	PyModuleDef_HEAD_INIT,
	"leveldb",
	NULL,
	sizeof(struct leveldb_extension_state),
	leveldb_extension_methods,
	NULL,
	leveldb_extension_traverse,
	leveldb_extension_clear,
	NULL
};

#define INITERROR return NULL

extern "C" PyObject* PyInit_leveldb(void)

#else

#define INITERROR return

extern "C" void initleveldb(void)

#endif
{
#if PY_MAJOR_VERSION >= 3
	PyObject* leveldb_module = PyModule_Create(&leveldb_extension_def);
#else
	PyObject* leveldb_module = Py_InitModule3((char*)"leveldb", leveldb_extension_methods, 0);
#endif

	if (leveldb_module == 0)
		INITERROR;

	// add custom exception
	leveldb_exception = PyErr_NewException((char*)"leveldb.LevelDBError", 0, 0);

	if (leveldb_exception == 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	if (PyModule_AddObject(leveldb_module, (char*)"LevelDBError", leveldb_exception) != 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	if (PyType_Ready(&PyLevelDB_Type) < 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	if (PyType_Ready(&PyLevelDBSnapshot_Type) < 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	if (PyType_Ready(&PyWriteBatch_Type) < 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	if (PyType_Ready(&PyLevelDBIter_Type) < 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	// add custom types to the different modules
	Py_INCREF(&PyLevelDB_Type);

	if (PyModule_AddObject(leveldb_module, (char*)"LevelDB", (PyObject*)&PyLevelDB_Type) != 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	Py_INCREF(&PyLevelDBSnapshot_Type);

	if (PyModule_AddObject(leveldb_module, (char*)"Snapshot", (PyObject*)&PyLevelDBSnapshot_Type) != 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	Py_INCREF(&PyWriteBatch_Type);

	if (PyModule_AddObject(leveldb_module, (char*)"WriteBatch", (PyObject*)&PyWriteBatch_Type) != 0) {
		Py_DECREF(leveldb_module);
		INITERROR;
	}

	PyEval_InitThreads();

	#if PY_MAJOR_VERSION >= 3
	return leveldb_module;
	#endif
}