commit 84a4d4b970bb9aaf540ad5fa82276b1caf5aa9bd Author: Andy Grover Date: Mon Oct 15 13:26:01 2012 -0700 python-lvm: Remove liblvm object Instead of requiring users to create a liblvm object, and then calling methods on it, the module acquires a liblvm handle as part of initialization. This makes it impossible to instantiate a liblvm object with a different systemdir, but there is an alternate envvar method for that obscure use case. Signed-off-by: Andy Grover --- python/liblvm.c | 269 ++++++++++++++++++++------------------------------------ 1 file changed, 97 insertions(+), 172 deletions(-) diff --git a/python/liblvm.c b/python/liblvm.c index cbfa170..4518cf4 100644 --- a/python/liblvm.c +++ b/python/liblvm.c @@ -24,39 +24,32 @@ #include #include "lvm2app.h" -typedef struct { - PyObject_HEAD - lvm_t libh; /* lvm lib handle */ -} lvmobject; +static lvm_t libh; + typedef struct { PyObject_HEAD vg_t vg; /* vg handle */ - lvmobject *lvm_obj; } vgobject; typedef struct { PyObject_HEAD lv_t lv; /* lv handle */ - lvmobject *lvm_obj; } lvobject; typedef struct { PyObject_HEAD pv_t pv; /* pv handle */ - lvmobject *lvm_obj; } pvobject; typedef struct { PyObject_HEAD lvseg_t lv_seg; /* lv segment handle */ - lvmobject *lvm_obj; } lvsegobject; typedef struct { PyObject_HEAD pvseg_t pv_seg; /* pv segment handle */ - lvmobject *lvm_obj; } pvsegobject; static PyTypeObject LibLVMvgType; @@ -67,100 +60,51 @@ static PyTypeObject LibLVMpvsegType; static PyObject *LibLVMError; - -/* ---------------------------------------------------------------------- - * LVM object initialization/deallocation - */ - -static int -liblvm_init(lvmobject *self, PyObject *arg) -{ - char *systemdir = NULL; - - if (!PyArg_ParseTuple(arg, "|s", &systemdir)) - return -1; - - self->libh = lvm_init(systemdir); - if (lvm_errno(self->libh)) { - PyErr_SetFromErrno(PyExc_OSError); - return -1; - } - - return 0; -} - -static void -liblvm_dealloc(lvmobject *self) -{ - /* if already closed, don't reclose it */ - if (self->libh != NULL){ - lvm_quit(self->libh); - } - - PyObject_Del(self); -} - -#define LVM_VALID(lvmobject) \ +#define LVM_VALID() \ do { \ - if (!lvmobject->libh) { \ - PyErr_SetString(PyExc_UnboundLocalError, "LVM object invalid"); \ + if (!libh) { \ + PyErr_SetString(PyExc_UnboundLocalError, "LVM handle invalid"); \ return NULL; \ } \ } while (0) static PyObject * -liblvm_get_last_error(lvmobject *self) +liblvm_get_last_error(void) { PyObject *info; - LVM_VALID(self); + LVM_VALID(); if((info = PyTuple_New(2)) == NULL) return NULL; - PyTuple_SetItem(info, 0, PyInt_FromLong((long) lvm_errno(self->libh))); - PyTuple_SetItem(info, 1, PyString_FromString(lvm_errmsg(self->libh))); + PyTuple_SetItem(info, 0, PyInt_FromLong((long) lvm_errno(libh))); + PyTuple_SetItem(info, 1, PyString_FromString(lvm_errmsg(libh))); return info; } static PyObject * -liblvm_library_get_version(lvmobject *self) +liblvm_library_get_version(void) { - LVM_VALID(self); + LVM_VALID(); return Py_BuildValue("s", lvm_library_get_version()); } - -static PyObject * -liblvm_close(lvmobject *self) -{ - LVM_VALID(self); - - /* if already closed, don't reclose it */ - if (self->libh != NULL) - lvm_quit(self->libh); - - self->libh = NULL; - - Py_INCREF(Py_None); - return Py_None; -} - static PyObject * -liblvm_lvm_list_vg_names(lvmobject *self) +liblvm_lvm_list_vg_names(void) { struct dm_list *vgnames; struct lvm_str_list *strl; PyObject * pytuple; int i = 0; - LVM_VALID(self); + LVM_VALID(); - vgnames = lvm_list_vg_names(self->libh); + vgnames = lvm_list_vg_names(libh); if (!vgnames) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -177,18 +121,18 @@ liblvm_lvm_list_vg_names(lvmobject *self) } static PyObject * -liblvm_lvm_list_vg_uuids(lvmobject *self) +liblvm_lvm_list_vg_uuids(void) { struct dm_list *uuids; struct lvm_str_list *strl; PyObject * pytuple; int i = 0; - LVM_VALID(self); + LVM_VALID(); - uuids = lvm_list_vg_uuids(self->libh); + uuids = lvm_list_vg_uuids(libh); if (!uuids) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -205,12 +149,12 @@ liblvm_lvm_list_vg_uuids(lvmobject *self) } static PyObject * -liblvm_lvm_percent_to_float(lvmobject *self, PyObject *arg) +liblvm_lvm_percent_to_float(PyObject *arg) { double converted; int percent; - LVM_VALID(self); + LVM_VALID(); if (!PyArg_ParseTuple(arg, "i", &percent)) return NULL; @@ -220,18 +164,18 @@ liblvm_lvm_percent_to_float(lvmobject *self, PyObject *arg) } static PyObject * -liblvm_lvm_vgname_from_pvid(lvmobject *self, PyObject *arg) +liblvm_lvm_vgname_from_pvid(PyObject *self, PyObject *arg) { const char *pvid; const char *vgname; - LVM_VALID(self); + LVM_VALID(); if (!PyArg_ParseTuple(arg, "s", &pvid)) return NULL; - if((vgname = lvm_vgname_from_pvid(self->libh, pvid)) == NULL) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + if((vgname = lvm_vgname_from_pvid(libh, pvid)) == NULL) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -239,18 +183,18 @@ liblvm_lvm_vgname_from_pvid(lvmobject *self, PyObject *arg) } static PyObject * -liblvm_lvm_vgname_from_device(lvmobject *self, PyObject *arg) +liblvm_lvm_vgname_from_device(PyObject *self, PyObject *arg) { const char *device; const char *vgname; - LVM_VALID(self); + LVM_VALID(); if (!PyArg_ParseTuple(arg, "s", &device)) return NULL; - if((vgname = lvm_vgname_from_device(self->libh, device)) == NULL) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + if((vgname = lvm_vgname_from_device(libh, device)) == NULL) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -259,18 +203,18 @@ liblvm_lvm_vgname_from_device(lvmobject *self, PyObject *arg) static PyObject * -liblvm_lvm_config_find_bool(lvmobject *self, PyObject *arg) +liblvm_lvm_config_find_bool(PyObject *self, PyObject *arg) { const char *config; int rval; PyObject *rc; - LVM_VALID(self); + LVM_VALID(); if (!PyArg_ParseTuple(arg, "s", &config)) return NULL; - if ((rval = lvm_config_find_bool(self->libh, config, -10)) == -10) { + if ((rval = lvm_config_find_bool(libh, config, -10)) == -10) { /* Retrieving error information yields no error in this case */ PyErr_Format(PyExc_ValueError, "config path not found"); return NULL; @@ -283,14 +227,14 @@ liblvm_lvm_config_find_bool(lvmobject *self, PyObject *arg) } static PyObject * -liblvm_lvm_config_reload(lvmobject *self) +liblvm_lvm_config_reload(void) { int rval; - LVM_VALID(self); + LVM_VALID(); - if((rval = lvm_config_reload(self->libh)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + if((rval = lvm_config_reload(libh)) == -1) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -300,14 +244,14 @@ liblvm_lvm_config_reload(lvmobject *self) static PyObject * -liblvm_lvm_scan(lvmobject *self) +liblvm_lvm_scan(void) { int rval; - LVM_VALID(self); + LVM_VALID(); - if((rval = lvm_scan(self->libh)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + if((rval = lvm_scan(libh)) == -1) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -316,18 +260,18 @@ liblvm_lvm_scan(lvmobject *self) } static PyObject * -liblvm_lvm_config_override(lvmobject *self, PyObject *arg) +liblvm_lvm_config_override(PyObject *self, PyObject *arg) { const char *config; int rval; - LVM_VALID(self); + LVM_VALID(); if (!PyArg_ParseTuple(arg, "s", &config)) return NULL; - if ((rval = lvm_config_override(self->libh, config)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self)); + if ((rval = lvm_config_override(libh, config)) == -1) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -340,14 +284,14 @@ liblvm_lvm_config_override(lvmobject *self, PyObject *arg) static PyObject * -liblvm_lvm_vg_open(lvmobject *lvm, PyObject *args) +liblvm_lvm_vg_open(PyObject *self, PyObject *args) { const char *vgname; const char *mode = NULL; - vgobject *self; + vgobject *vgobj; - LVM_VALID(lvm); + LVM_VALID(); if (!PyArg_ParseTuple(args, "s|s", &vgname, &mode)) { return NULL; @@ -356,42 +300,38 @@ liblvm_lvm_vg_open(lvmobject *lvm, PyObject *args) if (mode == NULL) mode = "r"; - if ((self = PyObject_New(vgobject, &LibLVMvgType)) == NULL) + if ((vgobj = PyObject_New(vgobject, &LibLVMvgType)) == NULL) return NULL; - if ((self->vg = lvm_vg_open(lvm->libh, vgname, mode, 0))== NULL) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(lvm)); - Py_DECREF(self); + if ((vgobj->vg = lvm_vg_open(libh, vgname, mode, 0))== NULL) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } - self->lvm_obj = lvm; - return (PyObject *)self; + return (PyObject *)vgobj; } static PyObject * -liblvm_lvm_vg_create(lvmobject *lvm, PyObject *args) +liblvm_lvm_vg_create(PyObject *self, PyObject *args) { const char *vgname; - vgobject *self; + vgobject *vgobj; - LVM_VALID(lvm); + LVM_VALID(); if (!PyArg_ParseTuple(args, "s", &vgname)) { return NULL; } - if ((self = PyObject_New(vgobject, &LibLVMvgType)) == NULL) + if ((vgobj = PyObject_New(vgobject, &LibLVMvgType)) == NULL) return NULL; - if ((self->vg = lvm_vg_create(lvm->libh, vgname))== NULL) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(lvm)); - Py_DECREF(self); + if ((vgobj->vg = lvm_vg_create(libh, vgname))== NULL) { + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } - self->lvm_obj = lvm; - return (PyObject *)self; + return (PyObject *)vgobj; } static void @@ -462,7 +402,7 @@ liblvm_lvm_vg_remove(vgobject *self) return Py_None; error: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -488,7 +428,7 @@ liblvm_lvm_vg_extend(vgobject *self, PyObject *args) return Py_None; error: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -514,7 +454,7 @@ liblvm_lvm_vg_reduce(vgobject *self, PyObject *args) return Py_None; error: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -538,7 +478,7 @@ liblvm_lvm_vg_add_tag(vgobject *self, PyObject *args) return Py_BuildValue("i", rval); error: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -564,7 +504,7 @@ liblvm_lvm_vg_remove_tag(vgobject *self, PyObject *args) return Py_None; error: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -658,13 +598,13 @@ liblvm_lvm_vg_get_free_extent_count(vgobject *self) /* Builds a python tuple ([string|number], bool) from a struct lvm_property_value */ static PyObject * -get_property(lvmobject *h, struct lvm_property_value *prop) +get_property(struct lvm_property_value *prop) { PyObject *pytuple; PyObject *setable; if( !prop->is_valid ) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(h)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -703,7 +643,7 @@ liblvm_lvm_vg_get_property(vgobject *self, PyObject *args) return NULL; prop_value = lvm_vg_get_property(self->vg, name); - return get_property(self->lvm_obj, &prop_value); + return get_property(&prop_value); } static PyObject * @@ -791,7 +731,7 @@ liblvm_lvm_vg_set_property(vgobject *self, PyObject *args) return Py_None; lvmerror: - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); bail: free(string_value); if( variant_type_arg ) { @@ -838,7 +778,7 @@ liblvm_lvm_vg_set_extent_size(vgobject *self, PyObject *args) } if ((rval = lvm_vg_set_extent_size(self->vg, new_size)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -875,7 +815,6 @@ liblvm_lvm_vg_list_lvs(vgobject *vg) } self->lv = lvl->lv; - self->lvm_obj = vg->lvm_obj; PyTuple_SET_ITEM(pytuple, i, (PyObject *) self); i++; } @@ -895,7 +834,7 @@ liblvm_lvm_vg_get_tags(vgobject *self) tags = lvm_vg_get_tags(self->vg); if (!tags) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -928,11 +867,10 @@ liblvm_lvm_vg_create_lv_linear(vgobject *vg, PyObject *args) return NULL; if ((self->lv = lvm_vg_create_lv_linear(vg->vg, vgname, size))== NULL) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(vg->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); Py_DECREF(self); return NULL; } - self->lvm_obj = vg->lvm_obj; return (PyObject *)self; } @@ -972,7 +910,6 @@ liblvm_lvm_vg_list_pvs(vgobject *vg) } self->pv = pvl->pv; - self->lvm_obj = vg->lvm_obj; PyTuple_SET_ITEM(pytuple, i, (PyObject *) self); i++; } @@ -997,7 +934,7 @@ liblvm_lvm_lv_from_N(vgobject *self, PyObject *arg, lv_fetch_by_N method) lv = method(self->vg, id); if( !lv ) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1007,7 +944,6 @@ liblvm_lvm_lv_from_N(vgobject *self, PyObject *arg, lv_fetch_by_N method) } rc->lv = lv; - rc->lvm_obj = self->lvm_obj; return (PyObject *)rc; } @@ -1037,7 +973,7 @@ liblvm_lvm_pv_from_N(vgobject *self, PyObject *arg, pv_fetch_by_N method) pv = method(self->vg, id); if( !pv ) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1047,7 +983,6 @@ liblvm_lvm_pv_from_N(vgobject *self, PyObject *arg, pv_fetch_by_N method) } rc->pv = pv; - rc->lvm_obj = self->lvm_obj; return (PyObject *)rc; } @@ -1104,7 +1039,7 @@ liblvm_lvm_lv_activate(lvobject *self) LV_VALID(self); if ((rval = lvm_lv_activate(self->lv)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1120,7 +1055,7 @@ liblvm_lvm_lv_deactivate(lvobject *self) LV_VALID(self); if ((rval = lvm_lv_deactivate(self->lv)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1136,7 +1071,7 @@ liblvm_lvm_vg_remove_lv(lvobject *self) LV_VALID(self); if ((rval = lvm_vg_remove_lv(self->lv)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1149,7 +1084,7 @@ liblvm_lvm_vg_remove_lv(lvobject *self) /* This will return a tuple of (value, bool) with the value being a string or integer and bool indicating if property is settable */ static PyObject * -liblvm_lvm_lv_get_property(lvobject *self, PyObject *args) +liblvm_lvm_lv_get_property(lvobject *self, PyObject *args) { const char *name; struct lvm_property_value prop_value; @@ -1160,7 +1095,7 @@ liblvm_lvm_lv_get_property(lvobject *self, PyObject *args) return NULL; prop_value = lvm_lv_get_property(self->lv, name); - return get_property(self->lvm_obj, &prop_value); + return get_property(&prop_value); } static PyObject * @@ -1210,7 +1145,7 @@ liblvm_lvm_lv_add_tag(lvobject *self, PyObject *args) } if ((rval = lvm_lv_add_tag(self->lv, tag)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1231,7 +1166,7 @@ liblvm_lvm_lv_remove_tag(lvobject *self, PyObject *args) } if ((rval = lvm_lv_remove_tag(self->lv, tag)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1251,7 +1186,7 @@ liblvm_lvm_lv_get_tags(lvobject *self) tags = lvm_lv_get_tags(self->lv); if (!tags) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1279,7 +1214,7 @@ liblvm_lvm_lv_rename(lvobject *self, PyObject *args) return NULL; if ((rval = lvm_lv_rename(self->lv, new_name)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1300,7 +1235,7 @@ liblvm_lvm_lv_resize(lvobject *self, PyObject *args) } if ((rval = lvm_lv_resize(self->lv, new_size)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1337,7 +1272,6 @@ liblvm_lvm_lv_list_lvsegs(lvobject *lv) } self->lv_seg = lvsegl->lvseg; - self->lvm_obj = lv->lvm_obj; PyTuple_SET_ITEM(pytuple, i, (PyObject *) self); i++; } @@ -1349,7 +1283,7 @@ liblvm_lvm_lv_list_lvsegs(lvobject *lv) #define PV_VALID(pvobject) \ do { \ - if (!pvobject->pv || !pvobject->lvm_obj) { \ + if (!pvobject->pv || !libh) { \ PyErr_SetString(PyExc_UnboundLocalError, "PV object invalid"); \ return NULL; \ } \ @@ -1385,7 +1319,7 @@ liblvm_lvm_pv_get_property(pvobject *self, PyObject *args) return NULL; prop_value = lvm_pv_get_property(self->pv, name); - return get_property(self->lvm_obj, &prop_value); + return get_property(&prop_value); } static PyObject * @@ -1417,7 +1351,7 @@ liblvm_lvm_pv_resize(pvobject *self, PyObject *args) } if ((rval = lvm_pv_resize(self->pv, new_size)) == -1) { - PyErr_SetObject(LibLVMError, liblvm_get_last_error(self->lvm_obj)); + PyErr_SetObject(LibLVMError, liblvm_get_last_error()); return NULL; } @@ -1454,7 +1388,6 @@ liblvm_lvm_lv_list_pvsegs(pvobject *pv) } self->pv_seg = pvsegl->pvseg; - self->lvm_obj = pv->lvm_obj; PyTuple_SET_ITEM(pytuple, i, (PyObject *) self); i++; } @@ -1480,7 +1413,7 @@ liblvm_lvm_lvseg_get_property(lvsegobject *self, PyObject *args) return NULL; prop_value = lvm_lvseg_get_property(self->lv_seg, name); - return get_property(self->lvm_obj, &prop_value); + return get_property(&prop_value); } /* PV seg methods */ @@ -1501,7 +1434,7 @@ liblvm_lvm_pvseg_get_property(pvsegobject *self, PyObject *args) return NULL; prop_value = lvm_pvseg_get_property(self->pv_seg, name); - return get_property(self->lvm_obj, &prop_value); + return get_property(&prop_value); } /* ---------------------------------------------------------------------- @@ -1513,7 +1446,6 @@ static PyMethodDef Liblvm_methods[] = { { "getVersion", (PyCFunction)liblvm_library_get_version, METH_NOARGS }, { "vgOpen", (PyCFunction)liblvm_lvm_vg_open, METH_VARARGS }, { "vgCreate", (PyCFunction)liblvm_lvm_vg_create, METH_VARARGS }, - { "close", (PyCFunction)liblvm_close, METH_NOARGS }, { "configFindBool", (PyCFunction)liblvm_lvm_config_find_bool, METH_VARARGS }, { "configReload", (PyCFunction)liblvm_lvm_config_reload, METH_NOARGS }, { "configOverride", (PyCFunction)liblvm_lvm_config_override, METH_VARARGS }, @@ -1606,18 +1538,6 @@ static PyMethodDef liblvm_pvseg_methods[] = { { NULL, NULL} /* sentinel */ }; -static PyTypeObject LiblvmType = { - PyObject_HEAD_INIT(&PyType_Type) - .tp_name = "liblvm.Liblvm", - .tp_basicsize = sizeof(lvmobject), - .tp_new = PyType_GenericNew, - .tp_dealloc = (destructor)liblvm_dealloc, - .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - .tp_doc = "Liblvm objects", - .tp_methods = Liblvm_methods, - .tp_init = (initproc)liblvm_init, -}; - static PyTypeObject LibLVMvgType = { PyObject_HEAD_INIT(&PyType_Type) .tp_name = "liblvm.Liblvm_vg", @@ -1673,13 +1593,20 @@ static PyTypeObject LibLVMpvsegType = { .tp_methods = liblvm_pvseg_methods, }; +static void +liblvm_cleanup(void) +{ + lvm_quit(libh); + libh = NULL; +} + PyMODINIT_FUNC initlvm(void) { PyObject *m; - if (PyType_Ready(&LiblvmType) < 0) - return; + libh = lvm_init(NULL); + if (PyType_Ready(&LibLVMvgType) < 0) return; if (PyType_Ready(&LibLVMlvType) < 0) @@ -1695,9 +1622,6 @@ initlvm(void) if (m == NULL) return; - Py_INCREF(&LiblvmType); - PyModule_AddObject(m, "Liblvm", (PyObject *)&LiblvmType); - LibLVMError = PyErr_NewException("Liblvm.LibLVMError", NULL, NULL); if (LibLVMError) { @@ -1708,4 +1632,5 @@ initlvm(void) PyModule_AddObject(m, "LibLVMError", LibLVMError); } + Py_AtExit(liblvm_cleanup); }