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(-) 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