summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2010-02-27 13:57:32 +0100
committerJulian Andres Klode <jak@debian.org>2010-02-27 13:57:32 +0100
commit13361b7bfa6aa00c02ab9639317495bd2c4ab0a5 (patch)
treea7df3123ea6b10306b1cc246eaa9fa9f6ae6a77e /python
parent83521b397a31165de60ad50fe73e684290c52a43 (diff)
downloadpython-apt-13361b7bfa6aa00c02ab9639317495bd2c4ab0a5.tar.gz
* python/cache.cc:
- Implement Cache.__len__() and Cache.__contains__() (Closes: #571443).
Diffstat (limited to 'python')
-rw-r--r--python/cache.cc27
1 files changed, 22 insertions, 5 deletions
diff --git a/python/cache.cc b/python/cache.cc
index fe6e8b68..5743d9df 100644
--- a/python/cache.cc
+++ b/python/cache.cc
@@ -253,6 +253,16 @@ static PyObject *CacheMapOp(PyObject *Self,PyObject *Arg)
return CppPyObject_NEW<pkgCache::PkgIterator>(Self,&PyPackage_Type,Pkg);
}
+// Check whether the cache contains a package with a given name.
+static int CacheContains(PyObject *Self,PyObject *Arg)
+{
+ // Get the name of the package, unicode and normal strings.
+ const char *Name = PyObject_AsString(Arg);
+ if (Name == NULL)
+ return 0;
+ return (GetCpp<pkgCache *>(Self)->FindPkg(Name).end() == false);
+}
+
static PyObject *PkgCacheNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
{
PyObject *pyCallbackInst = 0;
@@ -305,14 +315,21 @@ static PyObject *PkgCacheNew(PyTypeObject *type,PyObject *Args,PyObject *kwds)
return CacheObj;
}
+static Py_ssize_t CacheMapLen(PyObject *Self)
+{
+ return GetCpp<pkgCache*>(Self)->HeaderP->PackageCount;
+}
+
static char *doc_PkgCache = "Cache([progress]) -> Cache() object.\n\n"
"The cache provides access to the packages and other stuff.\n\n"
"The optional parameter *progress* can be used to specify an \n"
"apt.progress.OpProgress() object (or similar) which displays\n"
- "the opening progress.\n\n"
- "If not specified, the progress is displayed in simple text form.";
-
-static PyMappingMethods CacheMap = {0,CacheMapOp,0};
+ "the opening progress. If not specified, the progress is\n"
+ "displayed in simple text form.\n\n"
+ "The cache can be used like a mapping of package names to Package\n"
+ "objects.";
+static PySequenceMethods CacheSeq = {0,0,0,0,0,0,0,CacheContains,0,0};
+static PyMappingMethods CacheMap = {CacheMapLen,CacheMapOp,0};
PyTypeObject PyCache_Type =
{
PyVarObject_HEAD_INIT(&PyType_Type, 0)
@@ -327,7 +344,7 @@ PyTypeObject PyCache_Type =
0, // tp_compare
0, // tp_repr
0, // tp_as_number
- 0, // tp_as_sequence
+ &CacheSeq, // tp_as_sequence
&CacheMap, // tp_as_mapping
0, // tp_hash
0, // tp_call