diff options
| -rw-r--r-- | debian/changelog | 3 | ||||
| -rw-r--r-- | python/acquire-item.cc | 48 | ||||
| -rw-r--r-- | python/acquire.cc | 124 | ||||
| -rw-r--r-- | python/apt_pkgmodule.h | 1 | ||||
| -rw-r--r-- | python/progress.cc | 47 | ||||
| -rw-r--r-- | python/progress.h | 1 |
6 files changed, 64 insertions, 160 deletions
diff --git a/debian/changelog b/debian/changelog index 276f1e89..9b505a58 100644 --- a/debian/changelog +++ b/debian/changelog @@ -12,6 +12,9 @@ python-apt (0.7.93.1) UNRELEASED; urgency=low - Do not segfault if TarFile.go() is called without a member name. - Clone all pkgDirStream::Item's so apt_pkg.TarMember object can be used outside of the callback function passed to go(). + * Drop the segfault prevention measures from the Acquire code, as they fail + to work. A replacement will be added once destruction callbacks are added + in APT. -- Julian Andres Klode <jak@debian.org> Sat, 23 Jan 2010 15:35:55 +0100 diff --git a/python/acquire-item.cc b/python/acquire-item.cc index 24780f1c..059f1802 100644 --- a/python/acquire-item.cc +++ b/python/acquire-item.cc @@ -28,22 +28,6 @@ using namespace std; - - -struct PyAcquireItems { - CppOwnedPyObject<pkgAcqFile*> *file; - CppOwnedPyObject<pkgAcquire::Item*> *item; - CppOwnedPyObject<pkgAcquire::ItemDesc*> *desc; -}; - -typedef map<pkgAcquire::Item*,PyAcquireItems> item_map; - -// Keep a vector to PyAcquireItemObject pointers, so we can set the Object -// pointers to NULL when deallocating the main object (mostly AcquireFile). -struct PyAcquireObject : public CppPyObject<pkgAcquire*> { - item_map items; -}; - inline pkgAcquire::Item *acquireitem_tocpp(PyObject *self) { pkgAcquire::Item *itm = GetCpp<pkgAcquire::Item*>(self); @@ -163,45 +147,18 @@ static PyObject *acquireitem_repr(PyObject *Self) pkgAcquire::Item *Itm = acquireitem_tocpp(Self); if (Itm == 0) return 0; - return PyString_FromFormat("<%s object: " "Status: %i Complete: %i Local: %i IsTrusted: %i " "FileSize: %lu DestFile:'%s' " "DescURI: '%s' ID:%lu ErrorText: '%s'>", Self->ob_type->tp_name, Itm->Status, Itm->Complete, Itm->Local, Itm->IsTrusted(), - Itm->FileSize, Itm->DestFile.c_str(), Itm->DescURI().c_str(), + Itm->FileSize, Itm->DestFile.c_str(), Itm->DescURI().c_str(), Itm->ID,Itm->ErrorText.c_str()); } static void acquireitem_dealloc(PyObject *self) { - pkgAcquire::Item *item = PyAcquireItem_ToCpp(self); - PyAcquireObject *Owner = (PyAcquireObject *)GetOwner<pkgAcquire::Item*>(self); - if (Owner != NULL) { - PyAcquireItems item_struct = Owner->items[item]; - // TODO: Unregister the object in the owner. - if (!((CppOwnedPyObject<pkgAcquire::Item*>*)self)->NoDelete) { - if (item_struct.file != 0 && item_struct.file != self) - item_struct.file->Object = 0; - if (item_struct.item != 0 && item_struct.item != self) { - item_struct.item->Object = 0; - Py_DECREF(item_struct.item); - } - if (item_struct.desc != 0) { - item_struct.desc->Object = 0; - Py_DECREF(item_struct.desc); - } - Owner->items.erase(item); - } - else { - if (item_struct.file == self) - item_struct.file = 0; - if (item_struct.item == self) - item_struct.item = 0; - } - } - CppOwnedDeallocPtr<pkgAcquire::Item*>(self); } @@ -267,9 +224,6 @@ static PyObject *acquirefile_new(PyTypeObject *type, PyObject *Args, PyObject * destFile); // short-desc CppOwnedPyObject<pkgAcqFile*> *AcqFileObj = CppOwnedPyObject_NEW<pkgAcqFile*>(pyfetcher, type); AcqFileObj->Object = af; - - - ((PyAcquireObject *)pyfetcher)->items[af].file = AcqFileObj; return AcqFileObj; } diff --git a/python/acquire.cc b/python/acquire.cc index 789f994e..bcb76d67 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -30,43 +30,18 @@ #include <apt-pkg/acquire-worker.h> -typedef CppOwnedPyObject<pkgAcquire::Worker*> PyAcquireWorkerObject; -struct PyAcquireItems { - CppOwnedPyObject<pkgAcqFile*> *file; - CppOwnedPyObject<pkgAcquire::Item*> *item; - CppOwnedPyObject<pkgAcquire::ItemDesc*> *desc; -}; - -typedef map<pkgAcquire::Item*,PyAcquireItems> item_map; -typedef map<pkgAcquire::Worker*,PyAcquireWorkerObject*> worker_map; - -// Keep a vector to PyAcquireItemObject pointers, so we can set the Object -// pointers to NULL when deallocating the main object (mostly AcquireFile). -struct PyAcquireObject : public CppPyObject<pkgAcquire*> { - item_map items; - worker_map workers; -}; - - static PyObject *acquireworker_get_current_item(PyObject *self, void *closure) { pkgAcquire::Worker *worker = GetCpp<pkgAcquire::Worker*>(self); - - if (worker->CurrentItem == NULL) { + pkgAcquire::ItemDesc *desc = worker->CurrentItem; + if (desc == NULL) { Py_RETURN_NONE; } - - PyObject *PyAcquire = GetOwner<pkgAcquire::Worker*>(self); - - if (PyAcquire) - return PyAcquire_GetItemDesc(PyAcquire, worker->CurrentItem); - else { - PyObject *PyItem = PyAcquireItem_FromCpp(worker->CurrentItem->Owner); - PyObject *ret = PyAcquireItemDesc_FromCpp(worker->CurrentItem,false, - PyItem); - Py_DECREF(PyItem); - return ret; - } + PyObject *PyAcq = GetOwner<pkgAcquire::Worker*>(self); + PyObject *PyItem = PyAcquireItem_FromCpp(desc->Owner, false, PyAcq); + PyObject *PyDesc = PyAcquireItemDesc_FromCpp(desc, false, PyItem); + Py_XDECREF(PyItem); + return PyDesc; } static PyObject *acquireworker_get_status(PyObject *self, void *closure) @@ -134,17 +109,28 @@ PyTypeObject PyAcquireWorker_Type = { acquireworker_getset, // tp_getset }; + +static pkgAcquire::ItemDesc* acquireitemdesc_tocpp(PyObject *self) { + pkgAcquire::ItemDesc *item = GetCpp<pkgAcquire::ItemDesc*>(self); + if (item == NULL) + PyErr_SetString(PyExc_ValueError, "Acquire has been shutdown"); + return item; +} + static PyObject *acquireitemdesc_get_uri(PyObject *self, void *closure) { - return CppPyString(GetCpp<pkgAcquire::ItemDesc*>(self)->URI); + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->URI) : NULL; } static PyObject *acquireitemdesc_get_description(PyObject *self, void *closure) { - return CppPyString(GetCpp<pkgAcquire::ItemDesc*>(self)->Description); + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->Description) : NULL; } static PyObject *acquireitemdesc_get_shortdesc(PyObject *self, void *closure) { - return CppPyString(GetCpp<pkgAcquire::ItemDesc*>(self)->ShortDesc); + pkgAcquire::ItemDesc *item = acquireitemdesc_tocpp(self); + return item ? CppPyString(item->ShortDesc) : NULL; } static PyObject *acquireitemdesc_get_owner(CppOwnedPyObject<pkgAcquire::ItemDesc*> *self, void *closure) { @@ -153,7 +139,7 @@ static PyObject *acquireitemdesc_get_owner(CppOwnedPyObject<pkgAcquire::ItemDesc return self->Owner; } else if (self->Object) { - self->Owner = PyAcquireItem_FromCpp(self->Object->Owner); + self->Owner = PyAcquireItem_FromCpp(self->Object->Owner, false, NULL); Py_INCREF(self->Owner); return self->Owner; } @@ -214,31 +200,6 @@ PyTypeObject PyAcquireItemDesc_Type = { 0, // tp_new }; - -// 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) { pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); @@ -252,34 +213,19 @@ static PyObject *PkgAcquireRun(PyObject *Self,PyObject *Args) return HandleErrors(Py_BuildValue("i",run)); } + static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) { pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); - if (PyArg_ParseTuple(Args, "") == 0) return 0; - fetcher->Shutdown(); - - // TODO: Delete all objects here - item_map &items = ((PyAcquireObject *)Self)->items; - for (item_map::iterator I = items.begin(); I != items.end(); I++) { - if ((*I).second.file) - (*I).second.file->Object = NULL; - if ((*I).second.item) { - (*I).second.item->Object = NULL; - Py_DECREF((*I).second.item); - } - if ((*I).second.desc) { - (*I).second.desc->Object = NULL; - Py_DECREF((*I).second.desc); - } - } - items.clear(); Py_INCREF(Py_None); return HandleErrors(Py_None); } + + static PyMethodDef PkgAcquireMethods[] = { {"run",PkgAcquireRun,METH_VARARGS,"Run the fetcher"}, {"shutdown",PkgAcquireShutdown, METH_VARARGS,"Shutdown the fetcher"}, @@ -312,8 +258,7 @@ static PyObject *PkgAcquireGetWorkers(PyObject *self, void *closure) CppOwnedPyObject<pkgAcquire::Worker*> *PyWorker = NULL; for (pkgAcquire::Worker *Worker = Owner->WorkersBegin(); Worker != 0; Worker = Owner->WorkerStep(Worker)) { - PyWorker = CppOwnedPyObject_NEW<pkgAcquire::Worker*>(self,&PyAcquireWorker_Type, Worker); - PyWorker->NoDelete = true; + PyWorker = PyAcquireWorker_FromCpp(Worker, false, self); PyList_Append(List, PyWorker); Py_DECREF(PyWorker); } @@ -326,7 +271,7 @@ static PyObject *PkgAcquireGetItems(PyObject *Self,void*) PyObject *Obj; for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); I != fetcher->ItemsEnd(); I++) { - Obj = PyAcquire_GetItem(Self, *I); + Obj = PyAcquireItem_FromCpp(*I, false, Self); PyList_Append(List,Obj); Py_DECREF(Obj); } @@ -368,14 +313,11 @@ static PyObject *PkgAcquireNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) fetcher = new pkgAcquire(); } - PyAcquireObject *FetcherObj = (PyAcquireObject *) - CppPyObject_NEW<pkgAcquire*>(type, fetcher); + PyObject *FetcherObj = CppPyObject_NEW<pkgAcquire*>(type, fetcher); if (progress != 0) progress->setPyAcquire(FetcherObj); // prepare our map of items. - new (&FetcherObj->items) item_map(); - new (&FetcherObj->workers) worker_map(); return FetcherObj; } @@ -383,11 +325,9 @@ static PyObject *PkgAcquireNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) * Create a new apt_pkg.Acquire Python object from the pkgAcquire object. */ PyObject *PyAcquire_FromCpp(pkgAcquire *fetcher, bool Delete) { - PyAcquireObject *FetcherObj = (PyAcquireObject *)CppPyObject_NEW<pkgAcquire*>(&PyAcquire_Type, fetcher); - new (&FetcherObj->items) item_map(); - new (&FetcherObj->workers) worker_map(); - FetcherObj->NoDelete = (!Delete); - return FetcherObj; + CppPyObject<pkgAcquire*> *obj = CppPyObject_NEW<pkgAcquire*>(&PyAcquire_Type, fetcher); + obj->NoDelete = (!Delete); + return obj; } static char *doc_PkgAcquire = @@ -399,7 +339,7 @@ static char *doc_PkgAcquire = PyTypeObject PyAcquire_Type = { PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Acquire", // tp_name - sizeof(PyAcquireObject), // tp_basicsize + sizeof(CppPyObject<pkgAcquire*>), // tp_basicsize 0, // tp_itemsize // Methods CppDeallocPtr<pkgAcquire*>, // tp_dealloc diff --git a/python/apt_pkgmodule.h b/python/apt_pkgmodule.h index 5a5a6c6f..ec6cf10e 100644 --- a/python/apt_pkgmodule.h +++ b/python/apt_pkgmodule.h @@ -123,6 +123,7 @@ extern PyTypeObject PySystemLock_Type; extern PyTypeObject PyFileLock_Type; PyObject *PyAcquire_FromCpp(pkgAcquire *fetcher, bool Delete); +PyObject *PyAcquireItem_FromCpp(pkgAcquire::Item *item, bool Delete, PyObject *owner); #include "python-apt.h" #endif diff --git a/python/progress.cc b/python/progress.cc index 0fc01085..b69149d5 100644 --- a/python/progress.cc +++ b/python/progress.cc @@ -17,7 +17,6 @@ #include "generic.h" #include "apt_pkgmodule.h" - /** * Set an attribute on an object, after creating the value with * Py_BuildValue(fmt, arg). Afterwards, decrease its refcount and return @@ -118,6 +117,16 @@ void PyOpProgress::Done() // apt interface +PyObject *PyFetchProgress::GetDesc(pkgAcquire::ItemDesc *item) { + if (!pyAcquire && item->Owner && item->Owner->GetOwner()) { + pyAcquire = PyAcquire_FromCpp(item->Owner->GetOwner(), false); + } + PyObject *pyItem = PyAcquireItem_FromCpp(item->Owner, false, pyAcquire); + PyObject *pyDesc = PyAcquireItemDesc_FromCpp(item, false, pyItem); + Py_DECREF(pyItem); + return pyDesc; +} + bool PyFetchProgress::MediaChange(string Media, string Drive) { PyCbObj_END_ALLOW_THREADS @@ -171,7 +180,7 @@ void PyFetchProgress::IMSHit(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_HasAttrString(callbackInst, "ims_hit")) - RunSimpleCallback("ims_hit", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); + RunSimpleCallback("ims_hit", TUPLEIZE(GetDesc(&Itm))); else UpdateStatus(Itm, DLHit); PyCbObj_BEGIN_ALLOW_THREADS @@ -181,7 +190,7 @@ void PyFetchProgress::Fetch(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_HasAttrString(callbackInst, "fetch")) - RunSimpleCallback("fetch", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); + RunSimpleCallback("fetch", TUPLEIZE(GetDesc(&Itm))); else UpdateStatus(Itm, DLQueued); PyCbObj_BEGIN_ALLOW_THREADS @@ -191,7 +200,7 @@ void PyFetchProgress::Done(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_HasAttrString(callbackInst, "done")) - RunSimpleCallback("done", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); + RunSimpleCallback("done", TUPLEIZE(GetDesc(&Itm))); else UpdateStatus(Itm, DLDone); PyCbObj_BEGIN_ALLOW_THREADS @@ -201,7 +210,7 @@ void PyFetchProgress::Fail(pkgAcquire::ItemDesc &Itm) { PyCbObj_END_ALLOW_THREADS if (PyObject_HasAttrString(callbackInst, "fail")) { - RunSimpleCallback("fail", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); + RunSimpleCallback("fail", TUPLEIZE(GetDesc(&Itm))); PyCbObj_BEGIN_ALLOW_THREADS return; } @@ -219,7 +228,7 @@ void PyFetchProgress::Fail(pkgAcquire::ItemDesc &Itm) if (PyObject_HasAttrString(callbackInst, "fail")) - RunSimpleCallback("fail", TUPLEIZE(PyAcquire_GetItemDesc(pyAcquire, &Itm))); + RunSimpleCallback("fail", TUPLEIZE(GetDesc(&Itm))); else UpdateStatus(Itm, DLFailed); PyCbObj_BEGIN_ALLOW_THREADS @@ -279,24 +288,15 @@ bool PyFetchProgress::Pulse(pkgAcquire * Owner) setattr(callbackInst, "elapsed_time", "k", ElapsedTime); setattr(callbackInst, "current_items", "k", CurrentItems); setattr(callbackInst, "total_items", "k", TotalItems); -#ifdef COMPAT_0_7 - setattr(callbackInst, "currentCPS", "d", CurrentCPS); - setattr(callbackInst, "currentBytes", "d", CurrentBytes); - setattr(callbackInst, "totalBytes", "d", TotalBytes); - setattr(callbackInst, "fetchedBytes", "d", FetchedBytes); - setattr(callbackInst, "currentItems", "k", CurrentItems); - setattr(callbackInst, "totalItems", "k", TotalItems); -#endif // New style -#ifdef COMPAT_0_7 if (!PyObject_HasAttrString(callbackInst, "updateStatus")) { -#else - { -#endif PyObject *result1; bool res1 = true; + if (pyAcquire == NULL) { + pyAcquire = PyAcquire_FromCpp(Owner, false); + } Py_INCREF(pyAcquire); if (RunSimpleCallback("pulse", TUPLEIZE(pyAcquire) , &result1)) { @@ -308,11 +308,14 @@ bool PyFetchProgress::Pulse(pkgAcquire * Owner) } PyCbObj_BEGIN_ALLOW_THREADS return true; - - - } #ifdef COMPAT_0_7 + setattr(callbackInst, "currentCPS", "d", CurrentCPS); + setattr(callbackInst, "currentBytes", "d", CurrentBytes); + setattr(callbackInst, "totalBytes", "d", TotalBytes); + setattr(callbackInst, "fetchedBytes", "d", FetchedBytes); + setattr(callbackInst, "currentItems", "k", CurrentItems); + setattr(callbackInst, "totalItems", "k", TotalItems); // Go through the list of items and add active items to the // activeItems vector. map<pkgAcquire::Worker *, pkgAcquire::ItemDesc *> activeItemMap; @@ -401,6 +404,8 @@ bool PyFetchProgress::Pulse(pkgAcquire * Owner) PyCbObj_BEGIN_ALLOW_THREADS // fetching can be canceld by returning false return res; +#else + return false; #endif } diff --git a/python/progress.h b/python/progress.h index 7e75652b..4e66c771 100644 --- a/python/progress.h +++ b/python/progress.h @@ -64,6 +64,7 @@ struct PyFetchProgress : public pkgAcquireStatus, public PyCallbackObj { protected: PyObject *pyAcquire; + PyObject *GetDesc(pkgAcquire::ItemDesc *item); public: enum { DLDone, DLQueued, DLFailed, DLHit, DLIgnored |
