From 544f14e0f2fb70b5a1f30786a93023a42d88290d Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 22 Jul 2009 18:16:28 +0200 Subject: python: 2nd part of the acquire fixes (one PyObject per C++ object). --- python/acquire-item.cc | 14 +++++++---- python/acquire.cc | 63 +++++++++++++++++++++++++------------------------- python/apt_pkgmodule.h | 5 ++++ python/progress.cc | 10 ++++---- python/python-apt.h | 5 ++-- 5 files changed, 54 insertions(+), 43 deletions(-) (limited to 'python') diff --git a/python/acquire-item.cc b/python/acquire-item.cc index cf0a628e..1fb66080 100644 --- a/python/acquire-item.cc +++ b/python/acquire-item.cc @@ -176,13 +176,11 @@ static PyObject *acquireitem_repr(PyObject *Self) static void acquireitem_dealloc(PyObject *self) { + pkgAcquire::Item *item = PyAcquireItem_ToCpp(self); + PyAcquireObject *Owner = (PyAcquireObject *)GetOwner(self); + PyAcquireItems item_struct = Owner->items[item]; // TODO: Unregister the object in the owner. if (!((CppOwnedPyObject*)self)->NoDelete) { - pkgAcquire::Item *item = PyAcquireItem_ToCpp(self); - PyAcquireObject *Owner = (PyAcquireObject *)GetOwner(self); - - PyAcquireItems item_struct = Owner->items[item]; - if (item_struct.file != 0 && item_struct.file != self) item_struct.file->Object = 0; if (item_struct.item != 0 && item_struct.item != self) { @@ -195,6 +193,12 @@ static void acquireitem_dealloc(PyObject *self) } Owner->items.erase(item); } + else { + if (item_struct.file == self) + item_struct.file = 0; + if (item_struct.item == self) + item_struct.item = 0; + } CppOwnedDeallocPtr(self); } diff --git a/python/acquire.cc b/python/acquire.cc index 5e03586e..1085d0a2 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -40,26 +40,17 @@ static PyObject *acquireworker_get_current_item(PyObject *self, void *closure) Py_RETURN_NONE; } - PyAcquireObject *PyAcquire = (PyAcquireObject *)GetOwner(self); + PyObject *PyAcquire = GetOwner(self); - pkgAcquire::Item *Item = worker->CurrentItem->Owner; - - PyObject *PyItem; - // FIXME: PyAcquire_FromCpp needs to initialize item_map. - if (PyAcquire && false && PyAcquire->items[Item].item) { - Py_INCREF(PyItem); - PyItem = PyAcquire->items[Item].item; - } + if (PyAcquire) + return PyAcquire_GetItemDesc(PyAcquire, worker->CurrentItem); else { - PyItem = PyAcquireItem_FromCpp(Item,false,PyAcquire); - // FIXME: PyAcquire_FromCpp needs to initialize item_map. - if (PyAcquire && false) - PyAcquire->items[Item].item = (CppOwnedPyObject*)PyItem; + PyObject *PyItem = PyAcquireItem_FromCpp(worker->CurrentItem->Owner); + PyObject *ret = PyAcquireItemDesc_FromCpp(worker->CurrentItem,false, + PyItem); + Py_DECREF(PyItem); + return ret; } - - PyObject *ret = PyAcquireItemDesc_FromCpp(worker->CurrentItem,false,PyItem); - Py_DECREF(PyItem); - return ret; } static PyObject *acquireworker_get_status(PyObject *self, void *closure) @@ -146,7 +137,7 @@ static PyObject *acquireitemdesc_get_owner(CppOwnedPyObjectOwner); return self->Owner; } - else if (self->Object && self->Object->Owner != NULL) { + else if (self->Object) { self->Owner = PyAcquireItem_FromCpp(self->Object->Owner); Py_INCREF(self->Owner); return self->Owner; @@ -210,7 +201,27 @@ PyTypeObject PyAcquireItemDesc_Type = }; +// Acquire + +PyObject *PyAcquire_GetItem(PyObject *self, pkgAcquire::Item *item) { + PyAcquireItems &item_struct = ((PyAcquireObject *)self)->items[item]; + if (! item_struct.item) { + item_struct.item = PyAcquireItem_FromCpp(item,false,self); + } + Py_INCREF(item_struct.item); + return item_struct.item; +} +PyObject *PyAcquire_GetItemDesc(PyObject *self, pkgAcquire::ItemDesc *item) { + PyAcquireItems &item_struct = ((PyAcquireObject *)self)->items[item->Owner]; + if (! item_struct.item) + item_struct.item = PyAcquireItem_FromCpp(item->Owner,false,self); + if (! item_struct.desc) + item_struct.desc = PyAcquireItemDesc_FromCpp(item,false, + item_struct.item); + Py_INCREF(item_struct.desc); + return item_struct.desc; +} static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) { @@ -294,23 +305,13 @@ static PyObject *PkgAcquireGetItems(PyObject *Self,void*) { pkgAcquire *fetcher = GetCpp(Self); PyObject *List = PyList_New(0); - CppOwnedPyObject *Obj; + PyObject *Obj; for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); I != fetcher->ItemsEnd(); I++) { - - if (((PyAcquireObject *)Self)->items[*I].item) - PyList_Append(List, ((PyAcquireObject *)Self)->items[*I].item); - else { - Obj = CppOwnedPyObject_NEW(Self,&PyAcquireItem_Type,*I); - Obj->NoDelete = true; - + Obj = PyAcquire_GetItem(Self, *I); PyList_Append(List,Obj); - ((PyAcquireObject *)Self)->items[*I].item = Obj; - - - // Not DECREFING it, we want to manage it somewhere else. - } + Py_DECREF(Obj); } return List; } diff --git a/python/apt_pkgmodule.h b/python/apt_pkgmodule.h index 04bce2cc..3edba5d2 100644 --- a/python/apt_pkgmodule.h +++ b/python/apt_pkgmodule.h @@ -12,6 +12,7 @@ #include #include +#include // Configuration Stuff #define Configuration_Check(op) ((op)->ob_type == &PyConfiguration_Type) @@ -49,6 +50,10 @@ PyObject *StrTimeRFC1123(PyObject *self,PyObject *Args); PyObject *StrStrToTime(PyObject *self,PyObject *Args); PyObject *StrCheckDomainList(PyObject *Self,PyObject *Args); +PyObject *PyAcquire_GetItem(PyObject *self, pkgAcquire::Item *item); +PyObject *PyAcquire_GetItemDesc(PyObject *self, pkgAcquire::ItemDesc *item); +bool PyAcquire_DropItem(PyObject *self, pkgAcquire::Item *item); + // Cache Stuff extern PyTypeObject PyCache_Type; extern PyTypeObject PyCacheFile_Type; diff --git a/python/progress.cc b/python/progress.cc index 1b135a75..7873d894 100644 --- a/python/progress.cc +++ b/python/progress.cc @@ -175,7 +175,7 @@ void PyFetchProgress::IMSHit(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_TypeCheck(callbackInst,&PyAcquireProgress_Type)) - RunSimpleCallback("ims_hit", TUPLEIZE(PyAcquireItemDesc_FromCpp(&Itm))); + RunSimpleCallback("ims_hit", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); else UpdateStatus(Itm, DLHit); PyCbObj_BEGIN_ALLOW_THREADS @@ -185,7 +185,7 @@ void PyFetchProgress::Fetch(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_TypeCheck(callbackInst,&PyAcquireProgress_Type)) - RunSimpleCallback("fetch", TUPLEIZE(PyAcquireItemDesc_FromCpp(&Itm))); + RunSimpleCallback("fetch", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); else UpdateStatus(Itm, DLQueued); PyCbObj_BEGIN_ALLOW_THREADS @@ -195,7 +195,7 @@ void PyFetchProgress::Done(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_TypeCheck(callbackInst,&PyAcquireProgress_Type)) - RunSimpleCallback("done", TUPLEIZE(PyAcquireItemDesc_FromCpp(&Itm))); + RunSimpleCallback("done", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); else UpdateStatus(Itm, DLDone); PyCbObj_BEGIN_ALLOW_THREADS @@ -205,7 +205,7 @@ void PyFetchProgress::Fail(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_TypeCheck(callbackInst,&PyAcquireProgress_Type)) { - RunSimpleCallback("fail", TUPLEIZE(PyAcquireItemDesc_FromCpp(&Itm))); + RunSimpleCallback("fail", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); return; } @@ -220,7 +220,7 @@ void PyFetchProgress::Fail(pkgAcquire::ItemDesc &Itm) if (PyObject_TypeCheck(callbackInst,&PyAcquireProgress_Type)) - RunSimpleCallback("fail", TUPLEIZE(PyAcquireItemDesc_FromCpp(&Itm))); + RunSimpleCallback("fail", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); else UpdateStatus(Itm, DLFailed); PyCbObj_BEGIN_ALLOW_THREADS diff --git a/python/python-apt.h b/python/python-apt.h index 80ad03bd..fb84b394 100644 --- a/python/python-apt.h +++ b/python/python-apt.h @@ -220,7 +220,8 @@ static int import_apt_pkg(void) { // Python object creation, using two inline template functions and one variadic // macro per type. template -inline PyObject *FromCpp(PyTypeObject *pytype, Cpp obj, bool Delete=false) +inline CppPyObject *FromCpp(PyTypeObject *pytype, Cpp obj, + bool Delete=false) { CppPyObject *Obj = CppPyObject_NEW(pytype, obj); Obj->NoDelete = (!Delete); @@ -228,7 +229,7 @@ inline PyObject *FromCpp(PyTypeObject *pytype, Cpp obj, bool Delete=false) } template -inline PyObject *FromCppOwned(PyTypeObject *pytype, Cpp const &obj, +inline CppOwnedPyObject *FromCppOwned(PyTypeObject *pytype, Cpp const &obj, bool Delete=false, PyObject *Owner=NULL) { CppOwnedPyObject *Obj = CppOwnedPyObject_NEW(Owner, pytype, obj); -- cgit v1.2.3