From 33293ab8c53a9b1427012c297f288972e11ea3bb Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 8 Jun 2009 17:47:46 +0200 Subject: Add apt_pkg.HashString and apt_pkg.IndexRecords (Closes: #456141) --- python/hashstring.cc | 140 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 python/hashstring.cc (limited to 'python/hashstring.cc') diff --git a/python/hashstring.cc b/python/hashstring.cc new file mode 100644 index 00000000..02a80210 --- /dev/null +++ b/python/hashstring.cc @@ -0,0 +1,140 @@ +/* hashstring.cc - Wrapper around HashString + * + * Copyright 2009 Julian Andres Klode + * + * 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 +#include "generic.h" +#include "apt_pkgmodule.h" +#include + +static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, + PyObject *kwds) +{ + const char *Hash; + const char *Type; + char *kwlist[] = {"type", "hash", NULL}; + if (PyArg_ParseTupleAndKeywords(Args, kwds, "s|s", kwlist, &Type, + &Hash) == 0) + return 0; + CppPyObject *PyObj = CppPyObject_NEW(type); + if (Hash) + PyObj->Object = new HashString(Type,Hash); + else // Type is the combined form now (i.e. type:hash) + PyObj->Object = new HashString(Type); + return PyObj; +} + +PyObject *PyHashString_FromCpp(HashString *obj) +{ + return CppPyObject_NEW(&PyHashString_Type, obj); +} + +static PyObject *HashString_Repr(PyObject *self) +{ + HashString *hash = GetCpp(self); + char repr[100]; + sprintf(repr, "<%s: \"%s\">", self->ob_type->tp_name, hash->toStr().c_str()); + return PyString_FromString(repr); +} + +static PyObject *HashString_ToStr(PyObject *self) +{ + HashString *hash = GetCpp(self); + return CppPyString(hash->toStr()); +} + +static PyObject *HashString_HashType(PyObject *self) +{ + HashString *hash = GetCpp(self); + return CppPyString(hash->HashType()); +} + +static const char *HashString_VerifyFile_doc = + "verify_file(filename: str) --> bool\n\n" + "Verify that the file indicated by filename matches the hash."; + +static PyObject *HashString_VerifyFile(PyObject *self,PyObject *args) +{ + HashString *hash = GetCpp(self); + char *filename; + if (PyArg_ParseTuple(args, "s", &filename) == 0) + return 0; + return PyBool_FromLong(hash->VerifyFile(filename)); +} + +static PyMethodDef HashString_Methods[] = { + {"to_str",(PyCFunction)HashString_ToStr,METH_NOARGS, + "to_str() --> str\n\nReturn a string, consisting of type:hash"}, + {"verify_file",HashString_VerifyFile,METH_VARARGS, + HashString_VerifyFile_doc}, + {"hash_type",(PyCFunction)HashString_HashType,METH_NOARGS, + "hash_type() --> str\n\nReturn the type of hash (MD5Sum,SHA1,SHA256)"}, + {} +}; + +static const char *HashString_doc = + "HashString(type, hash) OR HashString('type:hash')\n\n" + "Create a new HashString object. The first form allows you to specify\n" + "a type and a hash, and the second form a single string where type and\n" + "hash are seperated by a colon, e.g.::\n\n" + " HashString('MD5Sum', '6cc1b6e6655e3555ac47e5b5fe26d04e')\n\n" + "Valid options for 'type' are: MD5Sum, SHA1, SHA256."; +PyTypeObject PyHashString_Type = { + PyObject_HEAD_INIT(&PyType_Type) +#if PY_MAJOR_VERSION < 3 + 0, // ob_size +#endif + "apt_pkg.HashString", // tp_name + sizeof(CppPyObject), // tp_basicsize + 0, // tp_itemsize + // Methods + CppDealloc, // tp_dealloc + 0, // tp_print + 0, // tp_getattr + 0, // tp_setattr + 0, // tp_compare + HashString_Repr, // tp_repr + 0, // tp_as_number + 0, // tp_as_sequence + 0, // tp_as_mapping + 0, // tp_hash + 0, // tp_call + HashString_ToStr, // tp_str + 0, // tp_getattro + 0, // tp_setattro + 0, // tp_as_buffer + Py_TPFLAGS_DEFAULT, // tp_flags + HashString_doc, // tp_doc + 0, // tp_traverse + 0, // tp_clear + 0, // tp_richcompare + 0, // tp_weaklistoffset + 0, // tp_iter + 0, // tp_iternext + HashString_Methods, // tp_methods + 0, // tp_members + 0, // tp_getset + 0, // tp_base + 0, // tp_dict + 0, // tp_descr_get + 0, // tp_descr_set + 0, // tp_dictoffset + 0, // tp_init + 0, // tp_alloc + HashString_NEW, // tp_new +}; -- cgit v1.2.3 From 09d94116e3aae865d2b04020725f90814bd7cb3c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Mon, 8 Jun 2009 18:22:32 +0200 Subject: Fix build failures with python2.4-dbg (const) --- python/hashstring.cc | 4 ++-- python/indexrecords.cc | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'python/hashstring.cc') diff --git a/python/hashstring.cc b/python/hashstring.cc index 02a80210..71ae5bf4 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -64,7 +64,7 @@ static PyObject *HashString_HashType(PyObject *self) return CppPyString(hash->HashType()); } -static const char *HashString_VerifyFile_doc = +static char *HashString_VerifyFile_doc = "verify_file(filename: str) --> bool\n\n" "Verify that the file indicated by filename matches the hash."; @@ -87,7 +87,7 @@ static PyMethodDef HashString_Methods[] = { {} }; -static const char *HashString_doc = +static char *HashString_doc = "HashString(type, hash) OR HashString('type:hash')\n\n" "Create a new HashString object. The first form allows you to specify\n" "a type and a hash, and the second form a single string where type and\n" diff --git a/python/indexrecords.cc b/python/indexrecords.cc index 917c3bbf..35e41c59 100644 --- a/python/indexrecords.cc +++ b/python/indexrecords.cc @@ -44,7 +44,7 @@ static PyObject *IndexRecords_Load(PyObject *self,PyObject *args) return HandleErrors(Py_BuildValue("i", records->Load(filename))); } -static const char *IndexRecords_Lookup_doc = "lookup(metakey)\n\n" +static char *IndexRecords_Lookup_doc = "lookup(metakey)\n\n" "Lookup the filename given by metakey, return a tuple (hash, size).\n" "The hash part is a HashString() object."; static PyObject *IndexRecords_Lookup(PyObject *self,PyObject *args) -- cgit v1.2.3 From f652213b76f72382bab21e730bf0ccc4419a1267 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 9 Jun 2009 18:10:19 +0200 Subject: Allow types providing __new__() to be subclassed. --- debian/changelog | 3 ++- python/acquire.cc | 6 ++++-- python/cache.cc | 3 ++- python/cdrom.cc | 3 ++- python/configuration.cc | 3 ++- python/depcache.cc | 9 ++++++--- python/hashstring.cc | 3 ++- python/indexrecords.cc | 3 ++- python/pkgmanager.cc | 3 ++- python/pkgrecords.cc | 3 ++- python/pkgsrcrecords.cc | 3 ++- python/sourcelist.cc | 3 ++- python/tag.cc | 6 ++++-- 13 files changed, 34 insertions(+), 17 deletions(-) (limited to 'python/hashstring.cc') diff --git a/debian/changelog b/debian/changelog index 1a80d081..818bf630 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,8 +1,9 @@ python-apt (0.7.92) experimental; urgency=low * Add apt_pkg.HashString and apt_pkg.IndexRecords (Closes: #456141) + * Allow types providing __new__() to be subclassed. - -- Julian Andres Klode Mon, 08 Jun 2009 17:23:37 +0200 + -- Julian Andres Klode Tue, 09 Jun 2009 18:09:53 +0200 python-apt (0.7.91) experimental; urgency=low diff --git a/python/acquire.cc b/python/acquire.cc index 05bffc65..702d48ce 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -276,7 +276,8 @@ PyTypeObject PkgAcquireType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_PkgAcquire, // tp_doc 0, // tp_traverse 0, // tp_clear @@ -366,7 +367,8 @@ PyTypeObject PkgAcquireFileType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_PkgAcquireFile, // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/cache.cc b/python/cache.cc index c988145f..14484104 100644 --- a/python/cache.cc +++ b/python/cache.cc @@ -331,7 +331,8 @@ PyTypeObject PkgCacheType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT , // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_PkgCache, // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/cdrom.cc b/python/cdrom.cc index 3e63a89c..e98947d1 100644 --- a/python/cdrom.cc +++ b/python/cdrom.cc @@ -108,7 +108,8 @@ PyTypeObject PkgCdromType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "Cdrom Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/configuration.cc b/python/configuration.cc index 3c6ea88c..365a35fd 100644 --- a/python/configuration.cc +++ b/python/configuration.cc @@ -526,7 +526,8 @@ PyTypeObject ConfigurationType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "Configuration Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/depcache.cc b/python/depcache.cc index a69e0723..f1c34fef 100644 --- a/python/depcache.cc +++ b/python/depcache.cc @@ -675,7 +675,8 @@ PyTypeObject PkgDepCacheType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_PkgDepCache, // tp_doc 0, // tp_traverse 0, // tp_clear @@ -858,7 +859,8 @@ PyTypeObject PkgProblemResolverType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "ProblemResolver Object", // tp_doc 0, // tp_traverse 0, // tp_clear @@ -975,7 +977,8 @@ PyTypeObject PkgActionGroupType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_PkgActionGroup, // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/hashstring.cc b/python/hashstring.cc index 71ae5bf4..58bcca9e 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -118,7 +118,8 @@ PyTypeObject PyHashString_Type = { 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), HashString_doc, // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/indexrecords.cc b/python/indexrecords.cc index 35e41c59..61ff07fc 100644 --- a/python/indexrecords.cc +++ b/python/indexrecords.cc @@ -104,7 +104,8 @@ PyTypeObject PyIndexRecords_Type = { 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), IndexRecords_doc, // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/pkgmanager.cc b/python/pkgmanager.cc index 7752ce5b..f47e77ad 100644 --- a/python/pkgmanager.cc +++ b/python/pkgmanager.cc @@ -153,7 +153,8 @@ PyTypeObject PkgManagerType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "PackageManager Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/pkgrecords.cc b/python/pkgrecords.cc index 6022097a..8beefffd 100644 --- a/python/pkgrecords.cc +++ b/python/pkgrecords.cc @@ -186,7 +186,8 @@ PyTypeObject PkgRecordsType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "Records Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/pkgsrcrecords.cc b/python/pkgsrcrecords.cc index 50445432..e6b78a83 100644 --- a/python/pkgsrcrecords.cc +++ b/python/pkgsrcrecords.cc @@ -228,7 +228,8 @@ PyTypeObject PkgSrcRecordsType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "SourceRecords Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/sourcelist.cc b/python/sourcelist.cc index 1c05d83e..5e5838d8 100644 --- a/python/sourcelist.cc +++ b/python/sourcelist.cc @@ -138,7 +138,8 @@ PyTypeObject PkgSourceListType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), "pkgSourceList Object", // tp_doc 0, // tp_traverse 0, // tp_clear diff --git a/python/tag.cc b/python/tag.cc index 31491c90..7e7eb21c 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -434,7 +434,8 @@ PyTypeObject TagSecType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_TagSec, // tp_doc 0, // tp_traverse 0, // tp_clear @@ -521,7 +522,8 @@ PyTypeObject TagFileType = 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - Py_TPFLAGS_DEFAULT, // tp_flags + (Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE), doc_TagFile, // tp_doc 0, // tp_traverse 0, // tp_clear -- cgit v1.2.3 From 395b33f9f8e93223f933c625bacbf1e2d23c6673 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 12 Jun 2009 18:56:23 +0200 Subject: Bugfix: Delete pointers correctly, fixing memory leaks. (LP: #370149) We previously called the destructor of the pointer. This resulted in no object using pointers being deallocated. This patch introduces CppDeallocPtr() and CppOwnedDeallocPtr() which do the same as the other CppDealloc() and CppOwnedDealloc(), but use 'delete' on the pointer instead of the deconstructor. Furthermore, this patch also changes AcquireFile to be a CppOwnedPyObject, owned by the Acquire object. Without this change, deleting the Acquire object would cause a crash when AcquireFile is deallocated. --- debian/changelog | 5 +++-- python/acquire.cc | 8 ++++---- python/cache.cc | 4 ++-- python/depcache.cc | 6 +++--- python/generic.h | 19 +++++++++++++++++++ python/hashstring.cc | 2 +- python/indexfile.cc | 2 +- python/indexrecords.cc | 2 +- python/metaindex.cc | 2 +- python/pkgmanager.cc | 2 +- python/policy.cc | 2 +- python/sourcelist.cc | 2 +- 12 files changed, 38 insertions(+), 18 deletions(-) (limited to 'python/hashstring.cc') diff --git a/debian/changelog b/debian/changelog index 3e85c5be..9f9838c3 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,10 +1,11 @@ -python-apt (0.7.92) experimental; urgency=low +python-apt (0.7.92) UNRELEASED; urgency=low * Add apt_pkg.HashString and apt_pkg.IndexRecords (Closes: #456141) * Add apt_pkg.Policy class (Closes: #382725) * Allow types providing __new__() to be subclassed. + * Bugfix: Delete pointers correctly, fixing memory leaks. (LP: #370149) - -- Julian Andres Klode Tue, 09 Jun 2009 20:39:39 +0200 + -- Julian Andres Klode Fri, 12 Jun 2009 18:42:02 +0200 python-apt (0.7.91) experimental; urgency=low diff --git a/python/acquire.cc b/python/acquire.cc index 702d48ce..6b091479 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -261,7 +261,7 @@ PyTypeObject PkgAcquireType = sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr @@ -329,7 +329,7 @@ static PyObject *PkgAcquireFileNew(PyTypeObject *type, PyObject *Args, PyObject shortDescr, destDir, destFile); // short-desc - CppPyObject *AcqFileObj = CppPyObject_NEW(type); + CppOwnedPyObject *AcqFileObj = CppOwnedPyObject_NEW(pyfetcher, type); AcqFileObj->Object = af; return AcqFileObj; @@ -349,10 +349,10 @@ PyTypeObject PkgAcquireFileType = 0, // ob_size #endif "apt_pkg.AcquireFile", // tp_name - sizeof(CppPyObject),// tp_basicsize + sizeof(CppOwnedPyObject),// tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/cache.cc b/python/cache.cc index 14484104..22ed9ecc 100644 --- a/python/cache.cc +++ b/python/cache.cc @@ -247,7 +247,7 @@ void PkgCacheFileDealloc(PyObject *Self) PyObject *CacheFilePy = GetOwner(Self); pkgCacheFile *CacheF = GetCpp(CacheFilePy); CacheF->Close(); - CppOwnedDealloc(Self); + CppOwnedDeallocPtr(Self); } static PyObject *PkgCacheNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) @@ -365,7 +365,7 @@ PyTypeObject PkgCacheFileType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/depcache.cc b/python/depcache.cc index f1c34fef..9bbda527 100644 --- a/python/depcache.cc +++ b/python/depcache.cc @@ -660,7 +660,7 @@ PyTypeObject PkgDepCacheType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr @@ -844,7 +844,7 @@ PyTypeObject PkgProblemResolverType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr,// tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr @@ -962,7 +962,7 @@ PyTypeObject PkgActionGroupType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/generic.h b/python/generic.h index df4d8755..b7b958f5 100644 --- a/python/generic.h +++ b/python/generic.h @@ -153,6 +153,25 @@ void CppOwnedDealloc(PyObject *iObj) iObj->ob_type->tp_free(iObj); } +// Pointer deallocation +// Generic Dealloc type functions +template +void CppDeallocPtr(PyObject *Obj) +{ + delete GetCpp(Obj); + Obj->ob_type->tp_free(Obj); +} + +template +void CppOwnedDeallocPtr(PyObject *iObj) +{ + CppOwnedPyObject *Obj = (CppOwnedPyObject *)iObj; + delete Obj->Object; + if (Obj->Owner != 0) + Py_DECREF(Obj->Owner); + iObj->ob_type->tp_free(iObj); +} + inline PyObject *CppPyString(std::string Str) { return PyString_FromStringAndSize(Str.c_str(),Str.length()); diff --git a/python/hashstring.cc b/python/hashstring.cc index 58bcca9e..23212a4b 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -103,7 +103,7 @@ PyTypeObject PyHashString_Type = { sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/indexfile.cc b/python/indexfile.cc index 795931fb..78a4f513 100644 --- a/python/indexfile.cc +++ b/python/indexfile.cc @@ -99,7 +99,7 @@ PyTypeObject PackageIndexFileType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/indexrecords.cc b/python/indexrecords.cc index 61ff07fc..fcb2b85d 100644 --- a/python/indexrecords.cc +++ b/python/indexrecords.cc @@ -89,7 +89,7 @@ PyTypeObject PyIndexRecords_Type = { sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/metaindex.cc b/python/metaindex.cc index b451f0b5..b5d194b4 100644 --- a/python/metaindex.cc +++ b/python/metaindex.cc @@ -80,7 +80,7 @@ PyTypeObject MetaIndexType = sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/pkgmanager.cc b/python/pkgmanager.cc index f47e77ad..0fd2cd92 100644 --- a/python/pkgmanager.cc +++ b/python/pkgmanager.cc @@ -138,7 +138,7 @@ PyTypeObject PkgManagerType = sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/policy.cc b/python/policy.cc index 89604e9a..992a1192 100644 --- a/python/policy.cc +++ b/python/policy.cc @@ -151,7 +151,7 @@ PyTypeObject PyPolicy_Type = { sizeof(CppOwnedPyObject),// tp_basicsize 0, // tp_itemsize // Methods - CppOwnedDealloc, // tp_dealloc + CppOwnedDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr diff --git a/python/sourcelist.cc b/python/sourcelist.cc index 5e5838d8..e53fccd3 100644 --- a/python/sourcelist.cc +++ b/python/sourcelist.cc @@ -123,7 +123,7 @@ PyTypeObject PkgSourceListType = sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize // Methods - CppDealloc, // tp_dealloc + CppDeallocPtr, // tp_dealloc 0, // tp_print 0, // tp_getattr 0, // tp_setattr -- cgit v1.2.3 From e80500e7173387acc7a6b6be9d7424a4343ec036 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Thu, 25 Jun 2009 18:53:47 +0200 Subject: python: Use PyVarObject_HEAD_INIT() instead of PyObject_HEAD_INIT(). This is related to PEP 3123 and fixes some compiler warnings. --- python/acquire.cc | 17 ++++------------- python/cache.cc | 45 +++++++++------------------------------------ python/cdrom.cc | 5 +---- python/configuration.cc | 15 +++------------ python/depcache.cc | 15 +++------------ python/generic.h | 1 + python/hashstring.cc | 5 +---- python/indexfile.cc | 5 +---- python/indexrecords.cc | 5 +---- python/metaindex.cc | 5 +---- python/pkgmanager.cc | 5 +---- python/pkgrecords.cc | 5 +---- python/pkgsrcrecords.cc | 5 +---- python/policy.cc | 5 +---- python/sourcelist.cc | 5 +---- python/tag.cc | 12 ++++-------- 16 files changed, 34 insertions(+), 121 deletions(-) (limited to 'python/hashstring.cc') diff --git a/python/acquire.cc b/python/acquire.cc index a36012bc..6c6c2ea4 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -105,10 +105,7 @@ static PyObject *AcquireItemRepr(PyObject *Self) PyTypeObject AcquireItemType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.AcquireItem", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -267,10 +264,7 @@ static char *doc_PkgAcquire = "Acquire(progress) -> Acquire() object.\n\n" PyTypeObject PkgAcquireType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Acquire", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize @@ -360,11 +354,8 @@ static char *doc_PkgAcquireFile = PyTypeObject PkgAcquireFileType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif - "apt_pkg.AcquireFile", // tp_name + PyVarObject_HEAD_INIT(&PyType_Type, 0) + "apt_pkg.AcquireFile", // tp_name sizeof(CppOwnedPyObject),// tp_basicsize 0, // tp_itemsize // Methods diff --git a/python/cache.cc b/python/cache.cc index 0f5e27eb..a571788f 100644 --- a/python/cache.cc +++ b/python/cache.cc @@ -299,10 +299,7 @@ static char *doc_PkgCache = "Cache([progress]) -> Cache() object.\n\n" static PyMappingMethods CacheMap = {0,CacheMapOp,0}; PyTypeObject PkgCacheType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Cache", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -349,10 +346,7 @@ PyTypeObject PkgCacheType = // --------------------------------------------------------------------- PyTypeObject PkgCacheFileType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "pkgCacheFile", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -425,10 +419,7 @@ static PySequenceMethods PkgListSeq = PyTypeObject PkgListType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.PackageList", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -550,10 +541,7 @@ static PyObject *PackageRepr(PyObject *Self) PyTypeObject PackageType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Package", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -641,10 +629,7 @@ static PyObject *DescriptionRepr(PyObject *Self) PyTypeObject DescriptionType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Description", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -904,10 +889,7 @@ static PyGetSetDef VersionGetSet[] = { PyTypeObject VersionType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Version", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -1068,10 +1050,7 @@ static PyGetSetDef PackageFileGetSet[] = { }; PyTypeObject PackageFileType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.PackageFile", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -1243,10 +1222,7 @@ static PyGetSetDef DependencyGetSet[] = { PyTypeObject DependencyType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Dependency", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -1331,10 +1307,7 @@ static PySequenceMethods RDepListSeq = PyTypeObject RDepListType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.DependencyList", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/cdrom.cc b/python/cdrom.cc index e2651670..492afd90 100644 --- a/python/cdrom.cc +++ b/python/cdrom.cc @@ -85,10 +85,7 @@ static PyObject *PkgCdromNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) PyTypeObject PkgCdromType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Cdrom", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/configuration.cc b/python/configuration.cc index 9ef5967b..07f55957 100644 --- a/python/configuration.cc +++ b/python/configuration.cc @@ -503,10 +503,7 @@ static PySequenceMethods ConfigurationSeq = {0,0,0,0,0,0,0,CnfContains,0,0}; static PyMappingMethods ConfigurationMap = {0,CnfMap,CnfMapSet}; PyTypeObject ConfigurationType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Configuration", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize @@ -550,10 +547,7 @@ PyTypeObject ConfigurationType = PyTypeObject ConfigurationPtrType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.ConfigurationPtr", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize @@ -589,10 +583,7 @@ PyTypeObject ConfigurationPtrType = PyTypeObject ConfigurationSubType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.ConfigurationSub", // tp_name sizeof(SubConfiguration), // tp_basicsize 0, // tp_itemsize diff --git a/python/depcache.cc b/python/depcache.cc index 9c0c9a6f..2ce7a595 100644 --- a/python/depcache.cc +++ b/python/depcache.cc @@ -667,10 +667,7 @@ static char *doc_PkgDepCache = "DepCache(cache) -> DepCache() object\n\n" "The parameter *cache* refers to an apt_pkg.Cache() object."; PyTypeObject PkgDepCacheType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.DepCache", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -858,10 +855,7 @@ static PyMethodDef PkgProblemResolverMethods[] = PyTypeObject PkgProblemResolverType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.ProblemResolver", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize @@ -977,10 +971,7 @@ static char *doc_PkgActionGroup = "ActionGroup(depcache)\n\n" PyTypeObject PkgActionGroupType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.ActionGroup", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/generic.h b/python/generic.h index 182b1b53..118917cb 100644 --- a/python/generic.h +++ b/python/generic.h @@ -63,6 +63,7 @@ typedef int Py_ssize_t; #define PyBytes_Check PyString_Check #define PyBytes_AsString PyString_AsString #define PyBytes_AsStringAndSize PyString_AsStringAndSize +#define PyVarObject_HEAD_INIT(type, size) PyObject_HEAD_INIT(type) size, #endif #endif diff --git a/python/hashstring.cc b/python/hashstring.cc index 23212a4b..6f70ac7e 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -95,10 +95,7 @@ static char *HashString_doc = " HashString('MD5Sum', '6cc1b6e6655e3555ac47e5b5fe26d04e')\n\n" "Valid options for 'type' are: MD5Sum, SHA1, SHA256."; PyTypeObject PyHashString_Type = { - PyObject_HEAD_INIT(&PyType_Type) -#if PY_MAJOR_VERSION < 3 - 0, // ob_size -#endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.HashString", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/indexfile.cc b/python/indexfile.cc index 73f76ce6..5b526b87 100644 --- a/python/indexfile.cc +++ b/python/indexfile.cc @@ -90,10 +90,7 @@ static PyGetSetDef PackageIndexFileGetSet[] = { PyTypeObject PackageIndexFileType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.PackageIndexFile", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/indexrecords.cc b/python/indexrecords.cc index fcb2b85d..588cf883 100644 --- a/python/indexrecords.cc +++ b/python/indexrecords.cc @@ -81,10 +81,7 @@ static PyMethodDef IndexRecords_Methods[] = { static char *IndexRecords_doc = "IndexRecords()\n\n" "Representation of a release file."; PyTypeObject PyIndexRecords_Type = { - PyObject_HEAD_INIT(&PyType_Type) -#if PY_MAJOR_VERSION < 3 - 0, // ob_size -#endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.IndexRecords", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/metaindex.cc b/python/metaindex.cc index b5d194b4..87b1e59a 100644 --- a/python/metaindex.cc +++ b/python/metaindex.cc @@ -72,10 +72,7 @@ static PyObject *MetaIndexRepr(PyObject *Self) PyTypeObject MetaIndexType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.MetaIndex", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/pkgmanager.cc b/python/pkgmanager.cc index 668f7cfc..9e9cc32d 100644 --- a/python/pkgmanager.cc +++ b/python/pkgmanager.cc @@ -133,10 +133,7 @@ static PyGetSetDef PkgManagerGetSet[] = { PyTypeObject PkgManagerType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.PackageManager", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/pkgrecords.cc b/python/pkgrecords.cc index 4a3c80db..886bdb58 100644 --- a/python/pkgrecords.cc +++ b/python/pkgrecords.cc @@ -163,10 +163,7 @@ static PyObject *PkgRecordsNew(PyTypeObject *type,PyObject *Args,PyObject *kwds) PyTypeObject PkgRecordsType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.PackageRecords", // tp_name sizeof(CppOwnedPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/pkgsrcrecords.cc b/python/pkgsrcrecords.cc index 9707dce2..187050b7 100644 --- a/python/pkgsrcrecords.cc +++ b/python/pkgsrcrecords.cc @@ -204,10 +204,7 @@ static PyObject *PkgSrcRecordsNew(PyTypeObject *type,PyObject *args,PyObject *kw PyTypeObject PkgSrcRecordsType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.SourceRecords", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/policy.cc b/python/policy.cc index 5150af91..91ed5454 100644 --- a/python/policy.cc +++ b/python/policy.cc @@ -143,10 +143,7 @@ static char *Policy_doc = "Policy(cache)\n\n" "may be some cases where a special policy class is needed."; PyTypeObject PyPolicy_Type = { - PyObject_HEAD_INIT(&PyType_Type) -#if PY_MAJOR_VERSION < 3 - 0, // ob_size -#endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.Policy", // tp_name sizeof(CppOwnedPyObject),// tp_basicsize 0, // tp_itemsize diff --git a/python/sourcelist.cc b/python/sourcelist.cc index 2cf5eea9..0820d5bf 100644 --- a/python/sourcelist.cc +++ b/python/sourcelist.cc @@ -115,10 +115,7 @@ static PyObject *PkgSourceListNew(PyTypeObject *type,PyObject *args,PyObject *kw PyTypeObject PkgSourceListType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.SourceList", // tp_name sizeof(CppPyObject), // tp_basicsize 0, // tp_itemsize diff --git a/python/tag.cc b/python/tag.cc index 1ad0a465..2b71517d 100644 --- a/python/tag.cc +++ b/python/tag.cc @@ -175,6 +175,7 @@ static PyObject *TagSecKeys(PyObject *Self,PyObject *Args) return List; } +#if PY_MAJOR_VERSION < 3 static char *doc_Exists = "Exists(Name) -> integer"; static PyObject *TagSecExists(PyObject *Self,PyObject *Args) { @@ -188,6 +189,7 @@ static PyObject *TagSecExists(PyObject *Self,PyObject *Args) return Py_BuildValue("i",0); return Py_BuildValue("i",1); } +#endif static int TagSecContains(PyObject *Self,PyObject *Arg) { @@ -436,10 +438,7 @@ static char *doc_TagSec = "TagSection(text) -> Create a new object.\n\n" "to the functions provided by the C++ class (e.g. Find)"; PyTypeObject TagSecType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.TagSection", // tp_name sizeof(TagSecData), // tp_basicsize 0, // tp_itemsize @@ -525,10 +524,7 @@ static char *doc_TagFile = "TagFile(file) -> TagFile() object. \n\n" // Type for a Tag File PyTypeObject TagFileType = { - PyObject_HEAD_INIT(&PyType_Type) - #if PY_MAJOR_VERSION < 3 - 0, // ob_size - #endif + PyVarObject_HEAD_INIT(&PyType_Type, 0) "apt_pkg.TagFile", // tp_name sizeof(TagFileData), // tp_basicsize 0, // tp_itemsize -- cgit v1.2.3 From 7009c7dff71064526dafd0ed114f39c970448da0 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 15 Jul 2009 00:46:48 +0200 Subject: python/hashstring.cc: Do not use const char*, but use char* with default to NULL. --- python/hashstring.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'python/hashstring.cc') diff --git a/python/hashstring.cc b/python/hashstring.cc index 6f70ac7e..d303049a 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -25,8 +25,8 @@ static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, PyObject *kwds) { - const char *Hash; - const char *Type; + char *Type = NULL; + char *Hash = NULL; char *kwlist[] = {"type", "hash", NULL}; if (PyArg_ParseTupleAndKeywords(Args, kwds, "s|s", kwlist, &Type, &Hash) == 0) -- cgit v1.2.3 From 360477dcc304b8a18c467e59435d8c766bbb76bb Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Wed, 15 Jul 2009 14:14:44 +0200 Subject: python/python-apt.h: Introduce the C++ API The C++ API provides support for creating Python objects from C++ objects given by pointer or reference (depending on the implementation of the Python object) and for retrieving the underlying C++ object from the Python object and for checking the type of the Python object. --- debian/changelog | 1 + debian/control | 11 ++ debian/python-apt-dev.examples | 1 + debian/python-apt-dev.install | 2 + doc/client-example.cc | 46 ++++++++ python/apt_pkgmodule.cc | 38 ++++++ python/apt_pkgmodule.h | 4 +- python/hashstring.cc | 5 - python/python-apt.h | 259 +++++++++++++++++++++++++++++++++++++++++ 9 files changed, 360 insertions(+), 7 deletions(-) create mode 100644 debian/python-apt-dev.examples create mode 100644 debian/python-apt-dev.install create mode 100644 doc/client-example.cc create mode 100644 python/python-apt.h (limited to 'python/hashstring.cc') diff --git a/debian/changelog b/debian/changelog index 4ae6e574..415ebdd2 100644 --- a/debian/changelog +++ b/debian/changelog @@ -3,6 +3,7 @@ python-apt (0.7.92) UNRELEASED; urgency=low [ Julian Andres Klode ] * Add apt_pkg.HashString and apt_pkg.IndexRecords (Closes: #456141) * Add apt_pkg.Policy class (Closes: #382725) + * Provide a C++ API in the package python-apt-dev (Closes: #334923) * Allow types providing __new__() to be subclassed. * Bugfix: Delete pointers correctly, fixing memory leaks. (LP: #370149) * apt/package.py: Return VersionList objects in Package.versions, which diff --git a/debian/control b/debian/control index 10b9bc31..a16ae10e 100644 --- a/debian/control +++ b/debian/control @@ -54,3 +54,14 @@ Description: Python interface to libapt-pkg (debug extension) variety of functions. . This package contains the extension built for the Python debug interpreter. + +Package: python-apt-dev +Architecture: all +Depends: python-apt (>= ${source:Version}), libapt-pkg-dev (>= 0.7.10) +Description: Python interface to libapt-pkg (development files) + The apt_pkg Python interface will provide full access to the internal + libapt-pkg structures allowing Python programs to easily perform a + variety of functions. + . + This package contains the header files needed to use python-apt objects from + C++ applications. diff --git a/debian/python-apt-dev.examples b/debian/python-apt-dev.examples new file mode 100644 index 00000000..39f7bf97 --- /dev/null +++ b/debian/python-apt-dev.examples @@ -0,0 +1 @@ +doc/client-example.cc diff --git a/debian/python-apt-dev.install b/debian/python-apt-dev.install new file mode 100644 index 00000000..2a1405fd --- /dev/null +++ b/debian/python-apt-dev.install @@ -0,0 +1,2 @@ +python/python-apt.h usr/include/python-apt/ +python/generic.h usr/include/python-apt/ diff --git a/doc/client-example.cc b/doc/client-example.cc new file mode 100644 index 00000000..7fa6672f --- /dev/null +++ b/doc/client-example.cc @@ -0,0 +1,46 @@ +/* + * client-example.cc - A simple example for using the python-apt C++ API. + * + * Copyright 2009 Julian Andres Klode + * + * 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 + +int main(int argc, char *argv[]) { + Py_Initialize(); + if (import_apt_pkg() < 0) + return 1; + + // Initialize a module. + PyObject *Module = Py_InitModule("client", NULL); + + // Create a HashString, which will be added to the module. + HashString *hash = new HashString("0966a120bb936bdc6fdeac445707aa6b"); + // Create a Python object for the hashstring and add it to the module + PyModule_AddObject(Module, "hash", PyHashString_FromCpp(hash)); + + // Another example: Add the HashString type to the module. + Py_INCREF(&PyHashString_Type); + PyModule_AddObject(Module, "HashString", (PyObject*)(&PyHashString_Type)); + + // Run IPython, adding the client module to the namespace. + PySys_SetArgv(argc, argv); + PyRun_SimpleString("from IPython.Shell import start\n"); + PyRun_SimpleString("import client\n"); + PyRun_SimpleString("start(user_ns=dict(client=client)).mainloop()\n"); + Py_Finalize(); +} diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc index e4fd0dfc..bc2f4258 100644 --- a/python/apt_pkgmodule.cc +++ b/python/apt_pkgmodule.cc @@ -498,6 +498,38 @@ static PyMethodDef methods[] = {} }; +static struct _PyAptPkgAPIStruct API = { + &PyAcquire_Type, // acquire_type + &PyAcquireFile_Type, // acquirefile_type + &PyAcquireItem_Type, // acquireitem_type + &PyActionGroup_Type, // actiongroup_type + &PyCache_Type, // cache_type + &PyCacheFile_Type, // cachefile_type + &PyCdrom_Type, // cdrom_type + &PyConfiguration_Type, // configuration_type + &PyDepCache_Type, // depcache_type + &PyDependency_Type, // dependency_type + &PyDependencyList_Type, // dependencylist_type + &PyDescription_Type, // description_type + &PyHashes_Type, // hashes_type + &PyHashString_Type, // hashstring_type + &PyIndexRecords_Type, // indexrecords_type + &PyMetaIndex_Type, // metaindex_type + &PyPackage_Type, // package_type + &PyPackageFile_Type, // packagefile_type + &PyPackageIndexFile_Type, // packageindexfile_type + &PyPackageList_Type, // packagelist_type + &PyPackageManager_Type, // packagemanager_type + &PyPackageRecords_Type, // packagerecords_type + &PyPolicy_Type, // policy_type + &PyProblemResolver_Type, // problemresolver_type + &PySourceList_Type, // sourcelist_type + &PySourceRecords_Type, // sourcerecords_type + &PyTagFile_Type, // tagfile_type + &PyTagSection_Type, // tagsection_type + &PyVersion_Type, // version_type +}; + #define ADDTYPE(mod,name,type) { \ if (PyType_Ready(type) == -1) INIT_ERROR; \ @@ -596,6 +628,12 @@ extern "C" void initapt_pkg() PyModule_AddObject(Module,"REWRITE_SOURCE_ORDER", CharCharToList(TFRewriteSourceOrder)); +#if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 1 + PyObject *PyCapsule = PyCapsule_New(&API, "apt_pkg._C_API", NULL); +#else + PyObject *PyCapsule = PyCObject_FromVoidPtr(&API, NULL); +#endif + PyModule_AddObject(Module, "_C_API", PyCapsule); // Version.. PyModule_AddStringConstant(Module,"VERSION",(char *)pkgVersion); PyModule_AddStringConstant(Module,"LIB_VERSION",(char *)pkgLibVersion); diff --git a/python/apt_pkgmodule.h b/python/apt_pkgmodule.h index f77c5e73..97be5d5c 100644 --- a/python/apt_pkgmodule.h +++ b/python/apt_pkgmodule.h @@ -104,7 +104,6 @@ extern PyTypeObject PyPackageIndexFile_Type; extern PyTypeObject PyMetaIndex_Type; // HashString -PyObject *PyHashString_FromCpp(HashString *obj); extern PyTypeObject PyHashString_Type; // IndexRecord @@ -113,5 +112,6 @@ extern PyTypeObject PyIndexRecords_Type; // Policy extern PyTypeObject PyPolicy_Type; extern PyTypeObject PyHashes_Type; - +#include "python-apt.h" #endif + diff --git a/python/hashstring.cc b/python/hashstring.cc index d303049a..b76dc127 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -39,11 +39,6 @@ static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, return PyObj; } -PyObject *PyHashString_FromCpp(HashString *obj) -{ - return CppPyObject_NEW(&PyHashString_Type, obj); -} - static PyObject *HashString_Repr(PyObject *self) { HashString *hash = GetCpp(self); diff --git a/python/python-apt.h b/python/python-apt.h new file mode 100644 index 00000000..557f5a3b --- /dev/null +++ b/python/python-apt.h @@ -0,0 +1,259 @@ +/* + * python-apt.h - Header file for the public interface. + * + * Copyright 2009 Julian Andres Klode + * + * 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. + */ + +#ifndef PYTHON_APT_H +#define PYTHON_APT_H +#include +#include +#include "generic.h" + +struct _PyAptPkgAPIStruct { + PyTypeObject *acquire_type; + PyTypeObject *acquirefile_type; + PyTypeObject *acquireitem_type; + PyTypeObject *actiongroup_type; + PyTypeObject *cache_type; + PyTypeObject *cachefile_type; + PyTypeObject *cdrom_type; + PyTypeObject *configuration_type; + PyTypeObject *depcache_type; + PyTypeObject *dependency_type; + PyTypeObject *dependencylist_type; + PyTypeObject *description_type; + PyTypeObject *hashes_type; + PyTypeObject *hashstring_type; + PyTypeObject *indexrecords_type; + PyTypeObject *metaindex_type; + PyTypeObject *package_type; + PyTypeObject *packagefile_type; + PyTypeObject *packageindexfile_type; + PyTypeObject *packagelist_type; + PyTypeObject *packagemanager_type; + PyTypeObject *packagerecords_type; + PyTypeObject *policy_type; + PyTypeObject *problemresolver_type; + PyTypeObject *sourcelist_type; + PyTypeObject *sourcerecords_type; + PyTypeObject *tagfile_type; + PyTypeObject *tagsection_type; + PyTypeObject *version_type; +}; + +# ifndef APT_PKGMODULE_H +# define PyAcquire_Type *(_PyAptPkg_API->acquire_type) +# define PyAcquireFile_Type *(_PyAptPkg_API->acquirefile_type) +# define PyAcquireItem_Type *(_PyAptPkg_API->acquireitem_type) +# define PyActionGroup_Type *(_PyAptPkg_API->actiongroup_type) +# define PyCache_Type *(_PyAptPkg_API->cache_type) +# define PyCacheFile_Type *(_PyAptPkg_API->cachefile_type) +# define PyCdrom_Type *(_PyAptPkg_API->cdrom_type) +# define PyConfiguration_Type *(_PyAptPkg_API->configuration_type) +# define PyDepCache_Type *(_PyAptPkg_API->depcache_type) +# define PyDependency_Type *(_PyAptPkg_API->dependency_type) +# define PyDependencyList_Type *(_PyAptPkg_API->dependencylist_type) +# define PyDescription_Type *(_PyAptPkg_API->description_type) +# define PyHashes_Type *(_PyAptPkg_API->hashes_type) +# define PyHashString_Type *(_PyAptPkg_API->hashstring_type) +# define PyIndexRecords_Type *(_PyAptPkg_API->indexrecords_type) +# define PyMetaIndex_Type *(_PyAptPkg_API->metaindex_type) +# define PyPackage_Type *(_PyAptPkg_API->package_type) +# define PyPackageFile_Type *(_PyAptPkg_API->packagefile_type) +# define PyPackageIndexFile_Type *(_PyAptPkg_API->packageindexfile_type) +# define PyPackageList_Type *(_PyAptPkg_API->packagelist_type) +# define PyPackageManager_Type *(_PyAptPkg_API->packagemanager_type) +# define PyPackageRecords_Type *(_PyAptPkg_API->packagerecords_type) +# define PyPolicy_Type *(_PyAptPkg_API->policy_type) +# define PyProblemResolver_Type *(_PyAptPkg_API->problemresolver_type) +# define PySourceList_Type *(_PyAptPkg_API->sourcelist_type) +# define PySourceRecords_Type *(_PyAptPkg_API->sourcerecords_type) +# define PyTagFile_Type *(_PyAptPkg_API->tagfile_type) +# define PyTagSection_Type *(_PyAptPkg_API->tagsection_type) +# define PyVersion_Type *(_PyAptPkg_API->version_type) + +// Creating + +static struct _PyAptPkgAPIStruct *_PyAptPkg_API; + +static int import_apt_pkg(void) { +# if PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 1 + _PyAptPkg_API = (_PyAptPkgAPIStruct *)PyCapsule_Import("apt_pkg._C_API", 0); + return (_PyAptPkg_API != NULL) ? 0 : -1; +# else + + PyObject *module = PyImport_ImportModule("apt_pkg"); + + if (module == NULL) { + return -1; + } + if (module != NULL) { + PyObject *c_api_object = PyObject_GetAttrString(module, "_C_API"); + if (c_api_object == NULL) + return -1; + if (PyCObject_Check(c_api_object)) + _PyAptPkg_API = (struct _PyAptPkgAPIStruct *)PyCObject_AsVoidPtr(c_api_object); + Py_DECREF(c_api_object); + } + return 0; +# endif // PY_MAJOR_VERSION >= 3 && PY_MINOR_VERSION >= 1 +} +# endif // APT_PKGMODULE_H + +// Checking macros. +# define PyAcquire_Check(op) PyObject_TypeCheck(op, &PyAcquire_Type) +# define PyAcquireFile_Check(op) PyObject_TypeCheck(op, &PyAcquireFile_Type) +# define PyAcquireItem_Check(op) PyObject_TypeCheck(op, &PyAcquireItem_Type) +# define PyActionGroup_Check(op) PyObject_TypeCheck(op, &PyActionGroup_Type) +# define PyCache_Check(op) PyObject_TypeCheck(op, &PyCache_Type) +# define PyCacheFile_Check(op) PyObject_TypeCheck(op, &PyCacheFile_Type) +# define PyCdrom_Check(op) PyObject_TypeCheck(op, &PyCdrom_Type) +# define PyConfiguration_Check(op) PyObject_TypeCheck(op, &PyConfiguration_Type) +# define PyDepCache_Check(op) PyObject_TypeCheck(op, &PyDepCache_Type) +# define PyDependency_Check(op) PyObject_TypeCheck(op, &PyDependency_Type) +# define PyDependencyList_Check(op) PyObject_TypeCheck(op, &PyDependencyList_Type) +# define PyDescription_Check(op) PyObject_TypeCheck(op, &PyDescription_Type) +# define PyHashes_Check(op) PyObject_TypeCheck(op, &PyHashes_Type) +# define PyHashString_Check(op) PyObject_TypeCheck(op, &PyHashString_Type) +# define PyIndexRecords_Check(op) PyObject_TypeCheck(op, &PyIndexRecords_Type) +# define PyMetaIndex_Check(op) PyObject_TypeCheck(op, &PyMetaIndex_Type) +# define PyPackage_Check(op) PyObject_TypeCheck(op, &PyPackage_Type) +# define PyPackageFile_Check(op) PyObject_TypeCheck(op, &PyPackageFile_Type) +# define PyPackageIndexFile_Check(op) PyObject_TypeCheck(op, &PyPackageIndexFile_Type) +# define PyPackageList_Check(op) PyObject_TypeCheck(op, &PyPackageList_Type) +# define PyPackageManager_Check(op) PyObject_TypeCheck(op, &PyPackageManager_Type) +# define PyPackageRecords_Check(op) PyObject_TypeCheck(op, &PyPackageRecords_Type) +# define PyPolicy_Check(op) PyObject_TypeCheck(op, &PyPolicy_Type) +# define PyProblemResolver_Check(op) PyObject_TypeCheck(op, &PyProblemResolver_Type) +# define PySourceList_Check(op) PyObject_TypeCheck(op, &PySourceList_Type) +# define PySourceRecords_Check(op) PyObject_TypeCheck(op, &PySourceRecords_Type) +# define PyTagFile_Check(op) PyObject_TypeCheck(op, &PyTagFile_Type) +# define PyTagSection_Check(op) PyObject_TypeCheck(op, &PyTagSection_Type) +# define PyVersion_Check(op) PyObject_TypeCheck(op, &PyVersion_Type) +// Exact check macros. +# define PyAcquire_CheckExact(op) (Py_TYPE(op) == &PyAcquire_Type) +# define PyAcquireFile_CheckExact(op) (Py_TYPE(op) == &PyAcquireFile_Type) +# define PyAcquireItem_CheckExact(op) (Py_TYPE(op) == &PyAcquireItem_Type) +# define PyActionGroup_CheckExact(op) (Py_TYPE(op) == &PyActionGroup_Type) +# define PyCache_CheckExact(op) (Py_TYPE(op) == &PyCache_Type) +# define PyCacheFile_CheckExact(op) (Py_TYPE(op) == &PyCacheFile_Type) +# define PyCdrom_CheckExact(op) (Py_TYPE(op) == &PyCdrom_Type) +# define PyConfiguration_CheckExact(op) (Py_TYPE(op) == &PyConfiguration_Type) +# define PyDepCache_CheckExact(op) (Py_TYPE(op) == &PyDepCache_Type) +# define PyDependency_CheckExact(op) (Py_TYPE(op) == &PyDependency_Type) +# define PyDependencyList_CheckExact(op) (Py_TYPE(op) == &PyDependencyList_Type) +# define PyDescription_CheckExact(op) (Py_TYPE(op) == &PyDescription_Type) +# define PyHashes_CheckExact(op) (Py_TYPE(op) == &PyHashes_Type) +# define PyHashString_CheckExact(op) (Py_TYPE(op) == &PyHashString_Type) +# define PyIndexRecords_CheckExact(op) (Py_TYPE(op) == &PyIndexRecords_Type) +# define PyMetaIndex_CheckExact(op) (Py_TYPE(op) == &PyMetaIndex_Type) +# define PyPackage_CheckExact(op) (Py_TYPE(op) == &PyPackage_Type) +# define PyPackageFile_CheckExact(op) (Py_TYPE(op) == &PyPackageFile_Type) +# define PyPackageIndexFile_CheckExact(op) (Py_TYPE(op) == &PyPackageIndexFile_Type) +# define PyPackageList_CheckExact(op) (Py_TYPE(op) == &PyPackageList_Type) +# define PyPackageManager_CheckExact(op) (Py_TYPE(op) == &PyPackageManager_Type) +# define PyPackageRecords_CheckExact(op) (Py_TYPE(op) == &PyPackageRecords_Type) +# define PyPolicy_CheckExact(op) (Py_TYPE(op) == &PyPolicy_Type) +# define PyProblemResolver_CheckExact(op) (Py_TYPE(op) == &PyProblemResolver_Type) +# define PySourceList_CheckExact(op) (Py_TYPE(op) == &PySourceList_Type) +# define PySourceRecords_CheckExact(op) (Py_TYPE(op) == &PySourceRecords_Type) +# define PyTagFile_CheckExact(op) (Py_TYPE(op) == &PyTagFile_Type) +# define PyTagSection_CheckExact(op) (Py_TYPE(op) == &PyTagSection_Type) +# define PyVersion_CheckExact(op) (Py_TYPE(op) == &PyVersion_Type) + +// Get the underlying C++ reference or pointer from the Python object. +# define PyAcquire_ToCpp GetCpp +# define PyAcquireFile_ToCpp GetCpp +# define PyAcquireItem_ToCpp GetCpp +# define PyActionGroup_ToCpp GetCpp +# define PyCache_ToCpp GetCpp +# define PyCacheFile_ToCpp GetCpp +# define PyCdrom_ToCpp GetCpp +# define PyConfiguration_ToCpp GetCpp +# define PyDepCache_ToCpp GetCpp +# define PyDependency_ToCpp GetCpp +//# define PyDependencyList_ToCpp GetCpp // NOT EXPORTED +# define PyDescription_ToCpp GetCpp +# define PyHashes_ToCpp GetCpp +# define PyHashString_ToCpp GetCpp +# define PyIndexRecords_ToCpp GetCpp +# define PyMetaIndex_ToCpp GetCpp +# define PyPackage_ToCpp GetCpp +# define PyPackageFile_ToCpp GetCpp +# define PyPackageIndexFile_ToCpp GetCpp +//# define PyPackageList_ToCpp GetCpp // NOT EXPORTED. +# define PyPackageManager_ToCpp GetCpp +//# define PyPackageRecords_ToCpp GetCpp // NOT EXPORTED +# define PyPolicy_ToCpp GetCpp +# define PyProblemResolver_ToCpp GetCpp +# define PySourceList_ToCpp GetCpp +//# define PySourceRecords_ToCpp GetCpp // NOT EXPORTED +# define PyTagFile_ToCpp GetCpp +# define PyTagSection_ToCpp GetCpp +# define PyVersion_ToCpp GetCpp + +// 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) +{ + CppPyObject *Obj = CppPyObject_NEW(pytype, obj); + Obj->NoDelete = (!Delete); + return Obj; +} + +template +inline PyObject *FromCppOwned(PyTypeObject *pytype, Cpp const &obj, + bool Delete=false, PyObject *Owner=NULL) +{ + CppOwnedPyObject *Obj = CppOwnedPyObject_NEW(Owner, pytype, obj); + Obj->NoDelete = (!Delete); + return Obj; +} + +# define PyAcquire_FromCpp(...) FromCpp(&PyAcquire_Type, ##__VA_ARGS__) +# define PyAcquireFile_FromCpp(...) FromCppOwned(&PyAcquireFile_Type, ##__VA_ARGS__) +# define PyAcquireItem_FromCpp(...) FromCppOwned(&PyAcquireItemType,##__VA_ARGS__) +# define PyActionGroup_FromCpp(...) FromCppOwned(&PyActionGroup_Type,##__VA_ARGS__) +# define PyCache_FromCpp(...) FromCppOwned(&PyCache_Type,##__VA_ARGS__) +# define PyCacheFile_FromCpp(...) FromCpp(&PyCacheFile_Type,##__VA_ARGS__) +# define PyCdrom_FromCpp(...) FromCpp(&PyCdromType,##__VA_ARGS__) +# define PyConfiguration_FromCpp(...) FromCppOwned(&PyConfiguration_Type, ##__VA_ARGS__) +# define PyDepCache_FromCpp(...) FromCppOwned(&PyDepCache_Type,##__VA_ARGS__) +# define PyDependency_FromCpp(...) FromCppOwned(&PyDependency_Type,##__VA_ARGS__) +//# define PyDependencyList_FromCpp(...) FromCppOwned(&PyDependencyList_Type,##__VA_ARGS__) +# define PyDescription_FromCpp(...) FromCppOwned(&PyDescription_Type,##__VA_ARGS__) +# define PyHashes_FromCpp(...) FromCpp(&PyHashes_Type,##__VA_ARGS__) +# define PyHashString_FromCpp(...) FromCpp(&PyHashString_Type,##__VA_ARGS__) +# define PyIndexRecords_FromCpp(...) FromCpp(&PyIndexRecords_Type,##__VA_ARGS__) +# define PyMetaIndex_FromCpp(...) FromCppOwned(&PyMetaIndex_Type,##__VA_ARGS__) +# define PyPackage_FromCpp(...) FromCppOwned(&PyPackage_Type,##__VA_ARGS__) +# define PyPackageIndexFile_FromCpp(...) FromCppOwned(&PyPackageIndexFile_Type,##__VA_ARGS__) +# define PyPackageFile_FromCpp(...) FromCppOwned(&PyPackageFile_Type,##__VA_ARGS__) +//# define PyPackageList_FromCpp(...) FromCppOwned(&PyPackageList_Type,##__VA_ARGS__) +# define PyPackageManager_FromCpp(...) FromCpp(&PyPackageManagerType,##__VA_ARGS__) +//# define PyPackageRecords_FromCpp(...) FromCppOwned(&PyPackageRecords_Type,##__VA_ARGS__) +# define PyPolicy_FromCpp(...) FromCppOwned(&PyPolicy_Type,##__VA_ARGS__) +# define PyProblemResolver_FromCpp(...) FromCppOwned(&PyProblemResolver_Type,##__VA_ARGS__) +# define PySourceList_FromCpp(...) FromCpp(&PySourceList_Type,##__VA_ARGS__) +//# define PySourceRecords_FromCpp(...) FromCpp(&PySourceRecords_Type,##__VA_ARGS__) +//# define PyTagFile_FromCpp(...) FromCppOwned(&PyTagFile_Type,##__VA_ARGS__) +# define PyTagSection_FromCpp(...) FromCppOwned(&PyTagSection_Type,##__VA_ARGS__) +# define PyVersion_FromCpp(...) FromCppOwned(&PyVersion_Type,##__VA_ARGS__) +#endif -- cgit v1.2.3 From b34e27d4ebaf325df6d82fde17b28cf9a008dbfc Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 17 Jul 2009 16:07:03 +0200 Subject: python: Use PyString_FromFormat instead of snprintf. --- python/acquire.cc | 5 ++-- python/cache.cc | 67 +++++++++++++++++++++++----------------------------- python/generic.h | 1 + python/hashstring.cc | 5 ++-- python/indexfile.cc | 5 +--- python/metaindex.cc | 11 ++++----- 6 files changed, 40 insertions(+), 54 deletions(-) (limited to 'python/hashstring.cc') diff --git a/python/acquire.cc b/python/acquire.cc index 05429d1b..50bdc949 100644 --- a/python/acquire.cc +++ b/python/acquire.cc @@ -287,8 +287,8 @@ static PyObject *AcquireItemRepr(PyObject *Self) pkgAcquire::Item *Itm = acquireitem_tocpp(Self); if (Itm == 0) return 0; - char S[300]; - snprintf(S,sizeof(S),"<%s object: " + + return PyString_FromFormat("<%s object: " "Status: %i Complete: %i Local: %i IsTrusted: %i " "FileSize: %lu DestFile:'%s' " "DescURI: '%s' ID:%lu ErrorText: '%s'>", @@ -296,7 +296,6 @@ static PyObject *AcquireItemRepr(PyObject *Self) Itm->Status, Itm->Complete, Itm->Local, Itm->IsTrusted(), Itm->FileSize, Itm->DestFile.c_str(), Itm->DescURI().c_str(), Itm->ID,Itm->ErrorText.c_str()); - return PyString_FromString(S); } static void AcquireItemDealloc(PyObject *self) { diff --git a/python/cache.cc b/python/cache.cc index c160cf69..c1f9eb2b 100644 --- a/python/cache.cc +++ b/python/cache.cc @@ -228,7 +228,7 @@ static PyObject *CacheMapOp(PyObject *Self,PyObject *Arg) const char *Name = PyObject_AsString(Arg); if (Name == NULL) return 0; - + // Search for the package pkgCache::PkgIterator Pkg = Cache->FindPkg(Name); @@ -534,11 +534,9 @@ static PyObject *PackageRepr(PyObject *Self) { pkgCache::PkgIterator &Pkg = GetCpp(Self); - char S[300]; - snprintf(S,sizeof(S),"", - Pkg.Name(),Pkg.Section(),Pkg->ID,Pkg->Flags); - return PyString_FromString(S); + return PyString_FromFormat("<%s object: name:'%s' section: " + "'%s' id:%u>", Self->ob_type->tp_name, + Pkg.Name(), Pkg.Section(), Pkg->ID); } PyTypeObject PyPackage_Type = @@ -621,12 +619,9 @@ static PyGetSetDef DescriptionGetSet[] = { static PyObject *DescriptionRepr(PyObject *Self) { pkgCache::DescIterator &Desc = GetCpp(Self); - - char S[300]; - snprintf(S,sizeof(S), - "ob_type->tp_name, Desc.LanguageCode(), + Desc.md5()); } PyTypeObject PyDescription_Type = @@ -840,15 +835,14 @@ static PyObject *VersionGetIsTrusted(PyObject *Self, void*) { static PyObject *VersionRepr(PyObject *Self) { pkgCache::VerIterator &Ver = GetCpp(Self); - - char S[300]; - snprintf(S,sizeof(S),"", - Ver.ParentPkg().Name(),Ver.VerStr(),Ver.Section(),Ver.Arch(), - (unsigned long)Ver->Size,(unsigned long)Ver->InstalledSize, - Ver->Hash,Ver->ID,Ver->Priority); - return PyString_FromString(S); + return PyString_FromFormat("<%s object: Pkg:'%s' Ver:'%s' Section:'%s' " + " Arch:'%s' Size:%lu ISize:%lu Hash:%u ID:%u " + "Priority:%u>", Self->ob_type->tp_name, + Ver.ParentPkg().Name(), Ver.VerStr(), + Ver.Section(), Ver.Arch(), + (unsigned long)Ver->Size, + (unsigned long)Ver->InstalledSize, + Ver->Hash, Ver->ID, Ver->Priority); } static PyGetSetDef VersionGetSet[] = { @@ -1004,20 +998,22 @@ static PyObject *PackageFile_GetID(PyObject *Self,void*) return Py_BuildValue("i",File->ID); } +#define S(s) (s == NULL ? "" : s) static PyObject *PackageFileRepr(PyObject *Self) { pkgCache::PkgFileIterator &File = GetCpp(Self); - char S[300]; - snprintf(S,sizeof(S),"", - File.FileName(),File.Archive(),File.Component(),File.Version(), - File.Origin(),File.Label(),File.Architecture(),File.Site(), - File.IndexType(),File->Size,File->Flags,File->ID); - return PyString_FromString(S); + return PyString_FromFormat("<%s object: filename:'%s'" + " a=%s,c=%s,v=%s,o=%s,l=%s arch='%s' site='%s'" + " IndexType='%s' Size=%lu ID:%u>", + Self->ob_type->tp_name, File.FileName(), + S(File.Archive()), + S(File.Component()),S(File.Version()), + S(File.Origin()),S(File.Label()), + S(File.Architecture()),S(File.Site()), + S(File.IndexType()),File->Size,File->ID); } +#undef S static PyGetSetDef PackageFileGetSet[] = { {(char*)"architecture",PackageFile_GetArchitecture}, @@ -1089,13 +1085,10 @@ static PyObject *DependencyRepr(PyObject *Self) { pkgCache::DepIterator &Dep = GetCpp(Self); - char S[300]; - snprintf(S,sizeof(S),"", - Dep.TargetPkg().Name(), - (Dep.TargetVer() == 0?"":Dep.TargetVer()), - Dep.CompType()); - return PyString_FromString(S); + return PyString_FromFormat("<%s object: pkg:'%s' ver:'%s' comp:'%s'>", + Self->ob_type->tp_name, Dep.TargetPkg().Name(), + (Dep.TargetVer() == 0?"":Dep.TargetVer()), + Dep.CompType()); } static PyObject *DepSmartTargetPkg(PyObject *Self,PyObject *Args) diff --git a/python/generic.h b/python/generic.h index 7f3a3809..4a55e9bf 100644 --- a/python/generic.h +++ b/python/generic.h @@ -53,6 +53,7 @@ typedef int Py_ssize_t; #define PyString_FromString PyUnicode_FromString #define PyString_FromStringAndSize PyUnicode_FromStringAndSize #define PyString_AsString PyUnicode_AsString +#define PyString_FromFormat PyUnicode_FromFormat #define PyInt_Check PyLong_Check #define PyInt_AsLong PyLong_AsLong // Force 0.7 compatibility to be off in Python 3 builds diff --git a/python/hashstring.cc b/python/hashstring.cc index b76dc127..a8d97cd8 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -42,9 +42,8 @@ static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, static PyObject *HashString_Repr(PyObject *self) { HashString *hash = GetCpp(self); - char repr[100]; - sprintf(repr, "<%s: \"%s\">", self->ob_type->tp_name, hash->toStr().c_str()); - return PyString_FromString(repr); + return PyString_FromFormat("<%s object: \"%s\">", self->ob_type->tp_name, + hash->toStr().c_str()); } static PyObject *HashString_ToStr(PyObject *self) diff --git a/python/indexfile.cc b/python/indexfile.cc index 7bf646d0..74345ec9 100644 --- a/python/indexfile.cc +++ b/python/indexfile.cc @@ -58,16 +58,13 @@ static PyObject *PackageIndexFileGetIsTrusted(PyObject *Self,void*) { static PyObject *PackageIndexFileRepr(PyObject *Self) { pkgIndexFile *File = GetCpp(Self); - - char S[1024]; - snprintf(S,sizeof(S),"", File->GetType()->Label, File->Describe().c_str(), File->Exists(), File->HasPackages(), File->Size(), File->IsTrusted(), File->ArchiveURI("").c_str()); - return PyString_FromString(S); } static PyGetSetDef PackageIndexFileGetSet[] = { diff --git a/python/metaindex.cc b/python/metaindex.cc index d67c0ada..62ff6b90 100644 --- a/python/metaindex.cc +++ b/python/metaindex.cc @@ -64,13 +64,10 @@ static PyGetSetDef MetaIndexGetSet[] = { static PyObject *MetaIndexRepr(PyObject *Self) { metaIndex *meta = GetCpp(Self); - - char S[1024]; - snprintf(S,sizeof(S),"", - meta->GetType(), meta->GetURI().c_str(), meta->GetDist().c_str(), - meta->IsTrusted()); - return PyString_FromString(S); + return PyString_FromFormat("<%s object: type='%s', uri:'%s' dist='%s' " + "is_trusted='%i'>", Self->ob_type->tp_name, + meta->GetType(), meta->GetURI().c_str(), + meta->GetDist().c_str(), meta->IsTrusted()); } PyTypeObject PyMetaIndex_Type = -- cgit v1.2.3 From bc879c4d608aa62e3e69e2e4eb252f6e013a7752 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 24 Jul 2009 15:01:37 +0200 Subject: python/hashstring.cc: Make hashtype a descriptor, adjust coding style. --- python/hashstring.cc | 54 +++++++++++++++++++++++++++------------------------- 1 file changed, 28 insertions(+), 26 deletions(-) (limited to 'python/hashstring.cc') diff --git a/python/hashstring.cc b/python/hashstring.cc index a8d97cd8..90c80e4c 100644 --- a/python/hashstring.cc +++ b/python/hashstring.cc @@ -22,13 +22,13 @@ #include "apt_pkgmodule.h" #include -static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, +static PyObject *hashstring_new(PyTypeObject *type,PyObject *Args, PyObject *kwds) { char *Type = NULL; char *Hash = NULL; char *kwlist[] = {"type", "hash", NULL}; - if (PyArg_ParseTupleAndKeywords(Args, kwds, "s|s", kwlist, &Type, + if (PyArg_ParseTupleAndKeywords(Args, kwds, "s|s:__new__", kwlist, &Type, &Hash) == 0) return 0; CppPyObject *PyObj = CppPyObject_NEW(type); @@ -39,49 +39,51 @@ static PyObject *HashString_NEW(PyTypeObject *type,PyObject *Args, return PyObj; } -static PyObject *HashString_Repr(PyObject *self) +static PyObject *hashstring_repr(PyObject *self) { HashString *hash = GetCpp(self); return PyString_FromFormat("<%s object: \"%s\">", self->ob_type->tp_name, hash->toStr().c_str()); } -static PyObject *HashString_ToStr(PyObject *self) +static PyObject *hashstring_str(PyObject *self) { HashString *hash = GetCpp(self); return CppPyString(hash->toStr()); } -static PyObject *HashString_HashType(PyObject *self) +static PyObject *hashstring_get_hashtype(PyObject *self) { HashString *hash = GetCpp(self); return CppPyString(hash->HashType()); } -static char *HashString_VerifyFile_doc = - "verify_file(filename: str) --> bool\n\n" +static char *hashstring_verify_file_doc = + "verify_file(filename: str) -> bool\n\n" "Verify that the file indicated by filename matches the hash."; -static PyObject *HashString_VerifyFile(PyObject *self,PyObject *args) +static PyObject *hashstring_verify_file(PyObject *self,PyObject *args) { HashString *hash = GetCpp(self); char *filename; - if (PyArg_ParseTuple(args, "s", &filename) == 0) + if (PyArg_ParseTuple(args, "s:verify_file", &filename) == 0) return 0; return PyBool_FromLong(hash->VerifyFile(filename)); } -static PyMethodDef HashString_Methods[] = { - {"to_str",(PyCFunction)HashString_ToStr,METH_NOARGS, - "to_str() --> str\n\nReturn a string, consisting of type:hash"}, - {"verify_file",HashString_VerifyFile,METH_VARARGS, - HashString_VerifyFile_doc}, - {"hash_type",(PyCFunction)HashString_HashType,METH_NOARGS, - "hash_type() --> str\n\nReturn the type of hash (MD5Sum,SHA1,SHA256)"}, - {} +static PyMethodDef hashstring_methods[] = { + {"verify_file",hashstring_verify_file,METH_VARARGS, + hashstring_verify_file_doc}, + {NULL} }; -static char *HashString_doc = +static PyGetSetDef hashstring_getset[] = { + {"hashtype",(getter)hashstring_get_hashtype,0, + "The type of the hash, as a string (possible: MD5Sum,SHA1,SHA256)."}, + {NULL} +}; + +static char *hashstring_doc = "HashString(type, hash) OR HashString('type:hash')\n\n" "Create a new HashString object. The first form allows you to specify\n" "a type and a hash, and the second form a single string where type and\n" @@ -99,28 +101,28 @@ PyTypeObject PyHashString_Type = { 0, // tp_getattr 0, // tp_setattr 0, // tp_compare - HashString_Repr, // tp_repr + hashstring_repr, // tp_repr 0, // tp_as_number 0, // tp_as_sequence 0, // tp_as_mapping 0, // tp_hash 0, // tp_call - HashString_ToStr, // tp_str + hashstring_str, // tp_str 0, // tp_getattro 0, // tp_setattro 0, // tp_as_buffer - (Py_TPFLAGS_DEFAULT | // tp_flags - Py_TPFLAGS_BASETYPE), - HashString_doc, // tp_doc + Py_TPFLAGS_DEFAULT | // tp_flags + Py_TPFLAGS_BASETYPE, + hashstring_doc, // tp_doc 0, // tp_traverse 0, // tp_clear 0, // tp_richcompare 0, // tp_weaklistoffset 0, // tp_iter 0, // tp_iternext - HashString_Methods, // tp_methods + hashstring_methods, // tp_methods 0, // tp_members - 0, // tp_getset + hashstring_getset, // tp_getset 0, // tp_base 0, // tp_dict 0, // tp_descr_get @@ -128,5 +130,5 @@ PyTypeObject PyHashString_Type = { 0, // tp_dictoffset 0, // tp_init 0, // tp_alloc - HashString_NEW, // tp_new + hashstring_new, // tp_new }; -- cgit v1.2.3