diff options
| author | Julian Andres Klode <jak@debian.org> | 2009-07-22 17:21:08 +0200 |
|---|---|---|
| committer | Julian Andres Klode <jak@debian.org> | 2009-07-22 17:21:08 +0200 |
| commit | 6ba42d2e31f161fc0ebe5405cf63b616c3e822b4 (patch) | |
| tree | e9083ea32a3dd8940de54c3d529d646d6fa3f751 | |
| parent | 8e6ce0ce4761404cf13bea468d925fc906ab2143 (diff) | |
| download | python-apt-6ba42d2e31f161fc0ebe5405cf63b616c3e822b4.tar.gz | |
python: First step of fixing acquire stuff.
Basically, we only want to have on PyAcquireItem per pkgAcquire::Item,
and one PyAcquireItemDesc per pkgAcquire::ItemDesc. Therefore, we store
them so we can return them at a later time.
| -rw-r--r-- | python/acquire-item.cc | 357 | ||||
| -rw-r--r-- | python/acquire.cc | 356 | ||||
| -rw-r--r-- | python/apt_pkgmodule.cc | 32 | ||||
| -rw-r--r-- | python/progress.cc | 12 | ||||
| -rw-r--r-- | python/progress.h | 7 | ||||
| -rw-r--r-- | setup.py | 3 |
6 files changed, 451 insertions, 316 deletions
diff --git a/python/acquire-item.cc b/python/acquire-item.cc new file mode 100644 index 00000000..cf0a628e --- /dev/null +++ b/python/acquire-item.cc @@ -0,0 +1,357 @@ +/* + * acquire-item.cc - Wrapper around pkgAcquire::Item and pkgAcqFile. + * + * Copyright 2004-2009 Canonical Ltd. + * Copyright 2009 Julian Andres Klode <jak@debian.org> + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301, USA. + */ + +#include "generic.h" +#include "apt_pkgmodule.h" + +#include <apt-pkg/acquire-item.h> +#include <map> + +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); + if (itm == 0) + PyErr_SetString(PyExc_ValueError, "Acquire() has been shut down or " + "the AcquireFile() object has been deallocated."); + return itm; +} + +static PyObject *acquireitem_get_complete(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? PyBool_FromLong(item->Complete) : 0; +} + +static PyObject *acquireitem_get_desc_uri(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? CppPyString(item->DescURI()) : 0; +} + +static PyObject *acquireitem_get_destfile(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? CppPyString(item->DestFile) : 0; +} + + +static PyObject *acquireitem_get_error_text(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? CppPyString(item->ErrorText) : 0; +} + +static PyObject *acquireitem_get_filesize(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? Py_BuildValue("i", item->FileSize) : 0; +} + +static PyObject *acquireitem_get_id(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? Py_BuildValue("k", item->ID) : 0; +} + +static PyObject *acquireitem_get_mode(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? Py_BuildValue("s", item->Mode) : 0; +} + +static PyObject *acquireitem_get_is_trusted(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? PyBool_FromLong(item->IsTrusted()) : 0; +} + +static PyObject *acquireitem_get_local(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? PyBool_FromLong(item->Local) : 0; +} + +static PyObject *acquireitem_get_status(PyObject *self, void *closure) +{ + pkgAcquire::Item *item = acquireitem_tocpp(self); + return item ? Py_BuildValue("i", item->Status) : 0; +} + +static int acquireitem_set_id(PyObject *self, PyObject *value, void *closure) +{ + pkgAcquire::Item *Itm = acquireitem_tocpp(self); + if (Itm == 0) + return -1; + if (PyLong_Check(value)) { + Itm->ID = PyLong_AsLong(value); + } + else if (PyInt_Check(value)) { + Itm->ID = PyInt_AsLong(value); + } + else { + PyErr_SetString(PyExc_TypeError, "value must be integer."); + return -1; + } + return 0; +} + + +static PyGetSetDef acquireitem_getset[] = { + {"complete",acquireitem_get_complete}, + {"desc_uri",acquireitem_get_desc_uri}, + {"destfile",acquireitem_get_destfile}, + {"error_text",acquireitem_get_error_text}, + {"filesize",acquireitem_get_filesize}, + {"id",acquireitem_get_id,acquireitem_set_id}, + {"mode",acquireitem_get_mode}, + {"is_trusted",acquireitem_get_is_trusted}, + {"local",acquireitem_get_local}, + {"status",acquireitem_get_status}, +#ifdef COMPAT_0_7 + {"Complete",acquireitem_get_complete}, + {"DescURI",acquireitem_get_desc_uri}, + {"DestFile",acquireitem_get_destfile}, + {"ErrorText",acquireitem_get_error_text}, + {"FileSize",acquireitem_get_filesize}, + {"ID",acquireitem_get_id}, + {"IsTrusted",acquireitem_get_is_trusted}, + {"Local",acquireitem_get_local}, + {"Status",acquireitem_get_status}, +#endif + {} +}; + +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->ID,Itm->ErrorText.c_str()); +} + +static void acquireitem_dealloc(PyObject *self) +{ + // TODO: Unregister the object in the owner. + if (!((CppOwnedPyObject<pkgAcquire::Item*>*)self)->NoDelete) { + pkgAcquire::Item *item = PyAcquireItem_ToCpp(self); + PyAcquireObject *Owner = (PyAcquireObject *)GetOwner<pkgAcquire::Item*>(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) { + 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); + } + + CppOwnedDeallocPtr<pkgAcquire::Item*>(self); +} + +PyTypeObject PyAcquireItem_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.AcquireItem", // tp_name + sizeof(CppOwnedPyObject<pkgAcquire::Item*>), // tp_basicsize + 0, // tp_itemsize + // Methods + acquireitem_dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + acquireitem_repr, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + "AcquireItem Object", // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + acquireitem_getset, // tp_getset +}; + +static PyObject *acquirefile_new(PyTypeObject *type, PyObject *Args, PyObject * kwds) +{ + PyObject *pyfetcher; + char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; + int size = 0; + uri = md5 = descr = shortDescr = destDir = destFile = ""; + + char *kwlist[] = {"owner","uri", "md5", "size", "descr", "short_descr", + "destdir", "destfile", NULL + }; + + if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist, + &PyAcquire_Type, &pyfetcher, &uri, &md5, + &size, &descr, &shortDescr, &destDir, &destFile) == 0) + return 0; + + pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher); + pkgAcqFile *af = new pkgAcqFile(fetcher, // owner + uri, // uri + md5, // md5 + size, // size + descr, // descr + shortDescr, + destDir, + destFile); // short-desc + CppOwnedPyObject<pkgAcqFile*> *AcqFileObj = CppOwnedPyObject_NEW<pkgAcqFile*>(pyfetcher, type); + AcqFileObj->Object = af; + + + ((PyAcquireObject *)pyfetcher)->items[af].file = AcqFileObj; + return AcqFileObj; +} + + +static char *acquirefile_doc = + "AcquireFile(owner, uri[, md5, size, descr, short_descr, destdir," + "destfile]) -> New AcquireFile() object\n\n" + "The parameter *owner* refers to an apt_pkg.Acquire() object. You can use\n" + "*destdir* OR *destfile* to specify the destination directory/file."; + +PyTypeObject PyAcquireFile_Type = { + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.AcquireFile", // tp_name + sizeof(CppOwnedPyObject<pkgAcqFile*>),// tp_basicsize + 0, // tp_itemsize + // Methods + acquireitem_dealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + 0, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + 0, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE | + Py_TPFLAGS_HAVE_GC, + acquirefile_doc, // tp_doc + CppOwnedTraverse<pkgAcqFile*>, // tp_traverse + CppOwnedClear<pkgAcqFile*>, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + 0, // tp_methods + 0, // tp_members + 0, // tp_getset + &PyAcquireItem_Type, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + acquirefile_new, // tp_new +}; + +#ifdef COMPAT_0_7 +char *doc_GetPkgAcqFile = + "GetPkgAcqFile(pkgAquire, uri[, md5, size, descr, shortDescr, destDir, destFile]) -> PkgAcqFile\n"; +PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) +{ + PyErr_WarnEx(PyExc_DeprecationWarning, "apt_pkg.GetPkgAcqFile() is " + "deprecated. Please see apt_pkg.AcquireFile() for the " + "replacement", 1); + PyObject *pyfetcher; + char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; + int size = 0; + uri = md5 = descr = shortDescr = destDir = destFile = ""; + + char * kwlist[] = {"owner","uri", "md5", "size", "descr", "shortDescr", + "destDir", "destFile", NULL + }; + + if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist, + &PyAcquire_Type, &pyfetcher, &uri, &md5, + &size, &descr, &shortDescr, &destDir, &destFile) == 0) + return 0; + + pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher); + pkgAcqFile *af = new pkgAcqFile(fetcher, // owner + uri, // uri + md5, // md5 + size, // size + descr, // descr + shortDescr, + destDir, + destFile); // short-desc + CppPyObject<pkgAcqFile*> *AcqFileObj = CppPyObject_NEW<pkgAcqFile*>(&PyAcquireFile_Type); + AcqFileObj->Object = af; + AcqFileObj->NoDelete = true; + + return AcqFileObj; +} +#endif diff --git a/python/acquire.cc b/python/acquire.cc index 78bd016e..5e03586e 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -13,23 +13,22 @@ #include <apt-pkg/acquire-item.h> #include <apt-pkg/acquire-worker.h> -typedef CppOwnedPyObject<pkgAcquire::Item*> PyAcquireItemObject; -typedef CppOwnedPyObject<pkgAcquire::ItemDesc*> PyAcquireItemDescObject; -typedef CppOwnedPyObject<pkgAcqFile*> PyAcquireFileObject; -typedef CppOwnedPyObject<pkgAcquire::Worker*> PyAcquireWorkerObject; - +typedef CppOwnedPyObject<pkgAcquire::Worker*> PyAcquireWorkerObject; struct PyAcquireItems { - PyAcquireFileObject *file; - PyAcquireItemObject *item; + 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*> { - map <pkgAcquire::Item*,PyAcquireItems> item_map; - map <pkgAcquire::ItemDesc*,PyAcquireItemDescObject*> itemdesc_map; - map <pkgAcquire::Worker*,PyAcquireWorkerObject*> worker_map; + item_map items; + worker_map workers; }; @@ -47,18 +46,20 @@ static PyObject *acquireworker_get_current_item(PyObject *self, void *closure) PyObject *PyItem; // FIXME: PyAcquire_FromCpp needs to initialize item_map. - if (PyAcquire && false && PyAcquire->item_map[Item].item) { + if (PyAcquire && false && PyAcquire->items[Item].item) { Py_INCREF(PyItem); - PyItem = PyAcquire->item_map[Item].item; + PyItem = PyAcquire->items[Item].item; } else { PyItem = PyAcquireItem_FromCpp(Item,false,PyAcquire); // FIXME: PyAcquire_FromCpp needs to initialize item_map. if (PyAcquire && false) - PyAcquire->item_map[Item].item = (PyAcquireItemObject*)PyItem; + PyAcquire->items[Item].item = (CppOwnedPyObject<pkgAcquire::Item*>*)PyItem; } - return PyAcquireItemDesc_FromCpp(worker->CurrentItem,false,PyItem); + PyObject *ret = PyAcquireItemDesc_FromCpp(worker->CurrentItem,false,PyItem); + Py_DECREF(PyItem); + return ret; } static PyObject *acquireworker_get_status(PyObject *self, void *closure) @@ -208,168 +209,6 @@ PyTypeObject PyAcquireItemDesc_Type = 0, // tp_new }; -inline pkgAcquire::Item *acquireitem_tocpp(PyObject *self) { - pkgAcquire::Item *itm = GetCpp<pkgAcquire::Item*>(self); - if (itm == 0) - PyErr_SetString(PyExc_ValueError, "Acquire() has been shut down or " - "the AcquireFile() object has been deallocated."); - return itm; -} - -#define MkGet(PyFunc,Ret) static PyObject *PyFunc(PyObject *Self,void*) \ -{ \ - pkgAcquire::Item *Itm = acquireitem_tocpp(Self); \ - if (Itm == 0) \ - return 0; \ - return Ret; \ -} - -// Define our getters -MkGet(AcquireItemGetComplete,Py_BuildValue("i",Itm->Complete)); -MkGet(AcquireItemGetDescURI,Safe_FromString(Itm->DescURI().c_str())); -MkGet(AcquireItemGetDestFile,Safe_FromString(Itm->DestFile.c_str())); -MkGet(AcquireItemGetErrorText,Safe_FromString(Itm->ErrorText.c_str())); -MkGet(AcquireItemGetFileSize,Py_BuildValue("i",Itm->FileSize)); -MkGet(AcquireItemGetID,Py_BuildValue("k",Itm->ID)); -MkGet(AcquireItemGetMode,Py_BuildValue("s",Itm->Mode)); -MkGet(AcquireItemGetIsTrusted,Py_BuildValue("i",Itm->IsTrusted())); -MkGet(AcquireItemGetLocal,Py_BuildValue("i",Itm->Local)); -MkGet(AcquireItemGetStatus,Py_BuildValue("i",Itm->Status)); - -// Constants -MkGet(AcquireItemGetStatIdle,Py_BuildValue("i", pkgAcquire::Item::StatIdle)); -MkGet(AcquireItemGetStatFetching,Py_BuildValue("i", pkgAcquire::Item::StatFetching)); -MkGet(AcquireItemGetStatDone,Py_BuildValue("i", pkgAcquire::Item::StatDone)); -MkGet(AcquireItemGetStatError,Py_BuildValue("i", pkgAcquire::Item::StatError)); -MkGet(AcquireItemGetStatAuthError,Py_BuildValue("i", pkgAcquire::Item::StatAuthError)); -#undef MkGet - -static int AcquireItemSetID(PyObject *self, PyObject *value, void *closure) -{ - pkgAcquire::Item *Itm = acquireitem_tocpp(self); - if (Itm == 0) - return -1; - if (PyLong_Check(value)) { - Itm->ID = PyLong_AsLong(value); - } - else if (PyInt_Check(value)) { - Itm->ID = PyInt_AsLong(value); - } - else { - PyErr_SetString(PyExc_TypeError, "value must be integer."); - return -1; - } - return 0; -} - - -static PyGetSetDef AcquireItemGetSet[] = { - {"complete",AcquireItemGetComplete}, - {"desc_uri",AcquireItemGetDescURI}, - {"destfile",AcquireItemGetDestFile}, - {"error_text",AcquireItemGetErrorText}, - {"filesize",AcquireItemGetFileSize}, - {"id",AcquireItemGetID,AcquireItemSetID}, - {"mode",AcquireItemGetMode}, - {"is_trusted",AcquireItemGetIsTrusted}, - {"local",AcquireItemGetLocal}, - {"status",AcquireItemGetStatus}, - {"stat_idle",AcquireItemGetStatIdle}, - {"stat_fetching",AcquireItemGetStatFetching}, - {"stat_done",AcquireItemGetStatDone}, - {"stat_error",AcquireItemGetStatError}, - {"stat_auth_error",AcquireItemGetStatAuthError}, -#ifdef COMPAT_0_7 - {"Complete",AcquireItemGetComplete}, - {"DescURI",AcquireItemGetDescURI}, - {"DestFile",AcquireItemGetDestFile}, - {"ErrorText",AcquireItemGetErrorText}, - {"FileSize",AcquireItemGetFileSize}, - {"ID",AcquireItemGetID}, - {"IsTrusted",AcquireItemGetIsTrusted}, - {"Local",AcquireItemGetLocal}, - {"Status",AcquireItemGetStatus}, - {"StatIdle",AcquireItemGetStatIdle}, - {"StatFetching",AcquireItemGetStatFetching}, - {"StatDone",AcquireItemGetStatDone}, - {"StatError",AcquireItemGetStatError}, - {"StatAuthError",AcquireItemGetStatAuthError}, -#endif - {} -}; - - - -static PyObject *AcquireItemRepr(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->ID,Itm->ErrorText.c_str()); -} - -static void AcquireItemDealloc(PyObject *self) { - pkgAcquire::Item *file = GetCpp<pkgAcquire::Item*>(self); - PyAcquireObject *owner = (PyAcquireObject *)GetOwner<pkgAcquire::Item*>(self); - - // Simply deallocate the object if we have no owner. - if (owner != NULL && !((CppPyObject<pkgAcquire::Item*> *)self)->NoDelete) { - PyAcquireItems &items = owner->item_map[file]; - - if (items.item && items.item != self) - items.item->Object = NULL; - if (items.file && items.item != self) - items.file->Object = NULL; - owner->item_map.erase(file); - } - - CppOwnedDeallocPtr<pkgAcquire::Item*>(self); -} - - - -PyTypeObject PyAcquireItem_Type = -{ - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "apt_pkg.AcquireItem", // tp_name - sizeof(CppOwnedPyObject<pkgAcquire::Item*>), // tp_basicsize - 0, // tp_itemsize - // Methods - AcquireItemDealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - AcquireItemRepr, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags - "AcquireItem Object", // tp_doc - 0, // tp_traverse - 0, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - 0, // tp_methods - 0, // tp_members - AcquireItemGetSet, // tp_getset -}; @@ -396,15 +235,20 @@ static PyObject *PkgAcquireShutdown(PyObject *Self,PyObject *Args) fetcher->Shutdown(); // TODO: Delete all objects here - map<pkgAcquire::Item*,PyAcquireItems> items = ((PyAcquireObject *)Self)->item_map; - for (map<pkgAcquire::Item*,PyAcquireItems>::iterator I = items.begin(); - I != items.end(); I++) { - (*I).second.file->Object = NULL; - (*I).second.item->Object = NULL; + 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); } @@ -442,6 +286,7 @@ static PyObject *PkgAcquireGetWorkers(PyObject *self, void *closure) PyWorker = CppOwnedPyObject_NEW<pkgAcquire::Worker*>(self,&PyAcquireWorker_Type, Worker); PyWorker->NoDelete = true; PyList_Append(List, PyWorker); + Py_DECREF(PyWorker); } return List; } @@ -449,19 +294,22 @@ static PyObject *PkgAcquireGetItems(PyObject *Self,void*) { pkgAcquire *fetcher = GetCpp<pkgAcquire*>(Self); PyObject *List = PyList_New(0); - PyAcquireItemObject *Obj; + CppOwnedPyObject<pkgAcquire::Item*> *Obj; for (pkgAcquire::ItemIterator I = fetcher->ItemsBegin(); I != fetcher->ItemsEnd(); I++) { - if (((PyAcquireObject *)Self)->item_map[*I].item) - PyList_Append(List, ((PyAcquireObject *)Self)->item_map[*I].item); + if (((PyAcquireObject *)Self)->items[*I].item) + PyList_Append(List, ((PyAcquireObject *)Self)->items[*I].item); else { Obj = CppOwnedPyObject_NEW<pkgAcquire::Item*>(Self,&PyAcquireItem_Type,*I); Obj->NoDelete = true; + PyList_Append(List,Obj); - ((PyAcquireObject *)Self)->item_map[*I].item = Obj; - Py_DECREF(Obj); + ((PyAcquireObject *)Self)->items[*I].item = Obj; + + + // Not DECREFING it, we want to manage it somewhere else. } } return List; @@ -506,9 +354,10 @@ static PyObject *PkgAcquireNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) if (PyArg_ParseTupleAndKeywords(Args,kwds,"|O",kwlist,&pyFetchProgressInst) == 0) return 0; + PyFetchProgress *progress = 0; if (pyFetchProgressInst != NULL) { // FIXME: memleak? - PyFetchProgress *progress = new PyFetchProgress(); + progress = new PyFetchProgress(); progress->setCallbackInst(pyFetchProgressInst); fetcher = new pkgAcquire(progress); } else { @@ -518,8 +367,11 @@ static PyObject *PkgAcquireNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) PyAcquireObject *FetcherObj = (PyAcquireObject *) CppPyObject_NEW<pkgAcquire*>(type, fetcher); + if (progress != 0) + progress->setPyAcquire(FetcherObj); // prepare our map of items. - new (&FetcherObj->item_map) map<pkgAcquire::Item*,PyAcquireItems>(); + new (&FetcherObj->items) item_map(); + new (&FetcherObj->workers) worker_map(); return FetcherObj; } @@ -581,125 +433,3 @@ PyObject *GetAcquire(PyObject *Self,PyObject *Args) } #endif -static PyObject *PkgAcquireFileNew(PyTypeObject *type, PyObject *Args, PyObject * kwds) -{ - PyObject *pyfetcher; - char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; - int size = 0; - uri = md5 = descr = shortDescr = destDir = destFile = ""; - - char * kwlist[] = {"owner","uri", "md5", "size", "descr", "short_descr", - "destdir", "destfile", NULL}; - - if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist, - &PyAcquire_Type, &pyfetcher, &uri, &md5, - &size, &descr, &shortDescr, &destDir, &destFile) == 0) - return 0; - - pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher); - pkgAcqFile *af = new pkgAcqFile(fetcher, // owner - uri, // uri - md5, // md5 - size, // size - descr, // descr - shortDescr, - destDir, - destFile); // short-desc - CppOwnedPyObject<pkgAcqFile*> *AcqFileObj = CppOwnedPyObject_NEW<pkgAcqFile*>(pyfetcher, type); - AcqFileObj->Object = af; - - // Register the file so we can remove it later. - ((PyAcquireObject *)pyfetcher)->item_map[af].file = AcqFileObj; - - return AcqFileObj; -} - - -static char *doc_PkgAcquireFile = - "AcquireFile(owner, uri[, md5, size, descr, short_descr, destdir," - "destfile]) -> New AcquireFile() object\n\n" - "The parameter *owner* refers to an apt_pkg.Acquire() object. You can use\n" - "*destdir* OR *destfile* to specify the destination directory/file."; - -PyTypeObject PyAcquireFile_Type = -{ - PyVarObject_HEAD_INIT(&PyType_Type, 0) - "apt_pkg.AcquireFile", // tp_name - sizeof(CppOwnedPyObject<pkgAcqFile*>),// tp_basicsize - 0, // tp_itemsize - // Methods - AcquireItemDealloc, // tp_dealloc - 0, // tp_print - 0, // tp_getattr - 0, // tp_setattr - 0, // tp_compare - 0, // tp_repr - 0, // tp_as_number - 0, // tp_as_sequence - 0, // tp_as_mapping - 0, // tp_hash - 0, // tp_call - 0, // tp_str - 0, // tp_getattro - 0, // tp_setattro - 0, // tp_as_buffer - (Py_TPFLAGS_DEFAULT | // tp_flags - Py_TPFLAGS_BASETYPE | - Py_TPFLAGS_HAVE_GC), - doc_PkgAcquireFile, // tp_doc - CppOwnedTraverse<pkgAcqFile*>, // tp_traverse - CppOwnedClear<pkgAcqFile*>, // tp_clear - 0, // tp_richcompare - 0, // tp_weaklistoffset - 0, // tp_iter - 0, // tp_iternext - 0, // tp_methods - 0, // tp_members - 0, // tp_getset - &PyAcquireItem_Type, // tp_base - 0, // tp_dict - 0, // tp_descr_get - 0, // tp_descr_set - 0, // tp_dictoffset - 0, // tp_init - 0, // tp_alloc - PkgAcquireFileNew, // tp_new -}; - -#ifdef COMPAT_0_7 -char *doc_GetPkgAcqFile = -"GetPkgAcqFile(pkgAquire, uri[, md5, size, descr, shortDescr, destDir, destFile]) -> PkgAcqFile\n"; -PyObject *GetPkgAcqFile(PyObject *Self, PyObject *Args, PyObject * kwds) -{ - PyErr_WarnEx(PyExc_DeprecationWarning, "apt_pkg.GetPkgAcqFile() is " - "deprecated. Please see apt_pkg.AcquireFile() for the " - "replacement", 1); - PyObject *pyfetcher; - char *uri, *md5, *descr, *shortDescr, *destDir, *destFile; - int size = 0; - uri = md5 = descr = shortDescr = destDir = destFile = ""; - - char * kwlist[] = {"owner","uri", "md5", "size", "descr", "shortDescr", - "destDir", "destFile", NULL}; - - if (PyArg_ParseTupleAndKeywords(Args, kwds, "O!s|sissss", kwlist, - &PyAcquire_Type, &pyfetcher, &uri, &md5, - &size, &descr, &shortDescr, &destDir, &destFile) == 0) - return 0; - - pkgAcquire *fetcher = GetCpp<pkgAcquire*>(pyfetcher); - pkgAcqFile *af = new pkgAcqFile(fetcher, // owner - uri, // uri - md5, // md5 - size, // size - descr, // descr - shortDescr, - destDir, - destFile); // short-desc - CppPyObject<pkgAcqFile*> *AcqFileObj = CppPyObject_NEW<pkgAcqFile*>(&PyAcquireFile_Type); - AcqFileObj->Object = af; - AcqFileObj->NoDelete = true; - - return AcqFileObj; -} -#endif diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index 5ee4015c..0a899efb 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -13,6 +13,7 @@ #include "generic.h" #include <apt-pkg/configuration.h> +#include <apt-pkg/acquire-item.h> #include <apt-pkg/version.h> #include <apt-pkg/deblistparser.h> #include <apt-pkg/pkgcache.h> @@ -606,6 +607,9 @@ extern "C" void initapt_pkg() PyModule_AddObject(Module,"Config",Config); #endif + + + // Add our classes. /* ============================ tag.cc ============================ */ ADDTYPE(Module,"TagSection",&PyTagSection_Type); @@ -661,6 +665,34 @@ extern "C" void initapt_pkg() PyModule_AddObject(Module,"REWRITE_SOURCE_ORDER", CharCharToList(TFRewriteSourceOrder)); + + // AcquireItem Constants. + + + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "stat_idle", + Py_BuildValue("i", pkgAcquire::Item::StatIdle)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "stat_fetching", + Py_BuildValue("i", pkgAcquire::Item::StatFetching)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "stat_done", + Py_BuildValue("i", pkgAcquire::Item::StatDone)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "stat_error", + Py_BuildValue("i", pkgAcquire::Item::StatError)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "stat_auth_error", + Py_BuildValue("i", pkgAcquire::Item::StatAuthError)); + +#ifdef COMPAT_0_7 + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "StatIdle", + Py_BuildValue("i", pkgAcquire::Item::StatIdle)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "StatFetching", + Py_BuildValue("i", pkgAcquire::Item::StatFetching)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "StatDone", + Py_BuildValue("i", pkgAcquire::Item::StatDone)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "StatError", + Py_BuildValue("i", pkgAcquire::Item::StatError)); + PyDict_SetItemString(PyAcquireItem_Type.tp_dict, "StatAuthError", + Py_BuildValue("i", pkgAcquire::Item::StatAuthError)); +#endif + #if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 1 PyObject *PyCapsule = PyCapsule_New(&API, "apt_pkg._C_API", NULL); #else diff --git a/python/progress.cc b/python/progress.cc index 305246b7..1b135a75 100644 --- a/python/progress.cc +++ b/python/progress.cc @@ -35,7 +35,12 @@ inline bool setattr(PyObject *object, const char *attr, const char *fmt, T arg) return result != -1; } -#define TUPLEIZE(op) Py_BuildValue("(O)", op) +inline PyObject *TUPLEIZE(PyObject *op) { + PyObject *ret = Py_BuildValue("(O)", op); + Py_DECREF(op); + return ret; +} + // generic bool PyCallbackObj::RunSimpleCallback(const char* method_name, PyObject *arglist, @@ -300,7 +305,10 @@ bool PyFetchProgress::Pulse(pkgAcquire * Owner) if (PyObject_TypeCheck(callbackInst, &PyAcquireProgress_Type)) { PyObject *result1; bool res1 = true; - if (RunSimpleCallback("pulse", TUPLEIZE(PyAcquire_FromCpp(Owner)), &result1)) { + + Py_INCREF(pyAcquire); + + if (RunSimpleCallback("pulse", TUPLEIZE(pyAcquire) , &result1)) { if (result1 != NULL && PyArg_Parse(result1, "b", &res1) && res1 == false) { // the user returned a explicit false here, stop PyCbObj_BEGIN_ALLOW_THREADS diff --git a/python/progress.h b/python/progress.h index e92933a7..bc1bd640 100644 --- a/python/progress.h +++ b/python/progress.h @@ -94,6 +94,9 @@ struct PyOpProgress : public OpProgress, public PyCallbackObj struct PyFetchProgress : public pkgAcquireStatus, public PyCallbackObj { + protected: + PyObject *pyAcquire; + public: enum { DLDone, DLQueued, DLFailed, DLHit, DLIgnored }; @@ -102,6 +105,10 @@ struct PyFetchProgress : public pkgAcquireStatus, public PyCallbackObj virtual bool MediaChange(string Media, string Drive); + void setPyAcquire(PyObject *o) { + pyAcquire = o; + } + /* apt stuff */ virtual void IMSHit(pkgAcquire::ItemDesc &Itm); virtual void Fetch(pkgAcquire::ItemDesc &Itm); @@ -35,7 +35,8 @@ files = ['apt_pkgmodule.cc', 'acquire.cc', 'cache.cc', 'cdrom.cc', 'hashstring.cc', 'indexfile.cc', 'indexrecords.cc', 'metaindex.cc', 'pkgmanager.cc', 'pkgrecords.cc', 'pkgsrcrecords.cc', 'policy.cc', 'progress.cc', 'sourcelist.cc', 'string.cc', 'tag.cc', - 'opprogress.cc', 'acquireprogress.cc', 'cdromprogress.cc', 'lock.cc'] + 'opprogress.cc', 'acquireprogress.cc', 'cdromprogress.cc', 'lock.cc', + 'acquire-item.cc'] files = sorted(['python/' + fname for fname in files]) apt_pkg = Extension("apt_pkg", files, libraries=["apt-pkg"]) |
