-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpymod.c
123 lines (103 loc) · 2.05 KB
/
pymod.c
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
/*
* Python module code...
* Copyright 2016 Darrick J. Wong.
* Licensed under the GPLv2.
*/
#include <Python.h>
#include "filemapper.h"
#include "compdb.h"
#include "compress.h"
#define MOD_NAME "compdb"
/* Get a list of supported compression algorithms. */
static PyObject *
compdb_compressors_py(
PyObject *self,
PyObject *args)
{
char *s;
PyObject *o;
s = compdb_compressors();
o = Py_BuildValue("s", s);
free(s);
return o;
}
/* Register a compressed-VFS */
static PyObject *
compdb_register_py(
PyObject *self,
PyObject *args)
{
char *under;
char *name;
char *compr;
int err;
if (!PyArg_ParseTuple(args, "zsz", &under, &name, &compr))
return NULL;
err = compdb_register(under, name, compr);
if (err)
PyErr_SetString(PyExc_RuntimeError, strerror(err));
return Py_BuildValue("i", err);
}
/* Unregister a VFS */
static PyObject *
compdb_unregister_py(
PyObject *self,
PyObject *args)
{
sqlite3_vfs *vfs;
char *name;
int err;
if (!PyArg_ParseTuple(args, "z", &name))
return NULL;
vfs = sqlite3_vfs_find(name);
if (!vfs) {
err = ENOENT;
PyErr_SetString(PyExc_RuntimeError, strerror(err));
goto out;
}
err = sqlite3_vfs_unregister(vfs);
if (err != SQLITE_OK) {
err = EIO;
PyErr_SetString(PyExc_RuntimeError, strerror(err));
}
out:
return Py_BuildValue("i", err);
}
static PyMethodDef compdb_methods[] = {
{"register", compdb_register_py, METH_VARARGS, NULL},
{"unregister", compdb_unregister_py, METH_VARARGS, NULL},
{"compressors", compdb_compressors_py, METH_NOARGS, NULL},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
MOD_NAME,
NULL,
0,
compdb_methods,
NULL,
NULL,
NULL,
NULL
};
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit_compdb(void)
{
PyObject *m;
m = PyModule_Create(&moduledef);
if (!m)
return NULL;
return m;
}
#else
PyMODINIT_FUNC
init_compdb(void)
{
PyObject *m;
m = Py_InitModule(MOD_NAME, compdb_methods);
if (!m)
return;
return;
}
#endif /* PY_MAJOR_VERSION */