summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Andres Klode <jak@debian.org>2009-04-13 20:00:09 +0200
committerJulian Andres Klode <jak@debian.org>2009-04-13 20:00:09 +0200
commitccd98916ba50c7583f354e0d3487ffb0830103f0 (patch)
treef38068a3d3a5053600663c4e25fc2c0396a0e476
parentebedf54fd4c72bfbf5c5e60d3d908cd66acace95 (diff)
downloadpython-apt-ccd98916ba50c7583f354e0d3487ffb0830103f0.tar.gz
* python/configuration.cc: Support the 'in' operator for Configuration
Support the replacement of mapping.has_key() for Configuration,ConfigurationPtr and ConfigurationSub objects. This is implemented by extending the various types with the tp_as_sequence slot, which refers to a PySequenceMethods containing only this method. The CnfGetAttr() function has been removed and replaced by the use of the tp_method slot. This helps the py3k port because the previously used Py_FindMethod() is not avilable anymore. This completes the support of the 'in' operator in all python-apt objects, which makes it even easier to convert python-apt-using applications to py3k once python-apt supports it, as 2to3 converts 'm.has_key(k)' to 'k in m'. Also finalize the types in apt_pkgmodule.cc and add the new 'key in conf' description to the documentation.
-rw-r--r--debian/changelog4
-rw-r--r--doc/source/apt_pkg/cache.rst4
-rw-r--r--python/apt_pkgmodule.cc3
-rw-r--r--python/configuration.cc70
4 files changed, 64 insertions, 17 deletions
diff --git a/debian/changelog b/debian/changelog
index 29927989..261ff7bd 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -1,7 +1,7 @@
python-apt (0.7.11) UNRELEASED; urgency=low
- * python/tag.cc:
- - Support 'key in mapping' for TagSections
+ * Support the 'in' operator (e.g. "k in d") in Configuration{,Ptr,Sub}
+ objects (e.g. apt_pkg.Config) and in TagSections (apt_pkg.ParseSection())
* Replace support for file objects with a more generic support for any object
providing a fileno() method and for file descriptors (integers).
* Add support for the Breaks fields
diff --git a/doc/source/apt_pkg/cache.rst b/doc/source/apt_pkg/cache.rst
index 146c2c2a..3ecbf069 100644
--- a/doc/source/apt_pkg/cache.rst
+++ b/doc/source/apt_pkg/cache.rst
@@ -117,6 +117,10 @@ Classes in apt_pkg
The Configuration objects store the configuration of apt.
+ .. describe:: key in conf
+
+ Return ``True`` if *conf* has a key *key*, else ``False``.
+
.. describe:: conf[key]
Return the value of the option given key *key*. If it does not
diff --git a/python/apt_pkgmodule.cc b/python/apt_pkgmodule.cc
index 10d98911..3d043179 100644
--- a/python/apt_pkgmodule.cc
+++ b/python/apt_pkgmodule.cc
@@ -475,6 +475,9 @@ extern "C" void initapt_pkg()
// Finalize our types to add slots, etc.
if (PyType_Ready(&TagSecType) == -1) return;
if (PyType_Ready(&TagFileType) == -1) return;
+ if (PyType_Ready(&ConfigurationType) == -1) return;
+ if (PyType_Ready(&ConfigurationPtrType) == -1) return;
+ if (PyType_Ready(&ConfigurationSubType) == -1) return;
// Initialize the module
PyObject *Module = Py_InitModule("apt_pkg",methods);
diff --git a/python/configuration.cc b/python/configuration.cc
index 21f70bc1..a95ac029 100644
--- a/python/configuration.cc
+++ b/python/configuration.cc
@@ -128,6 +128,11 @@ static PyObject *CnfExists(PyObject *Self,PyObject *Args)
return Py_BuildValue("i",(int)GetSelf(Self).Exists(Name));
}
+static int CnfContains(PyObject *Self,PyObject *Arg)
+{
+ return (int)GetSelf(Self).Exists(PyString_AsString(Arg));
+}
+
static char *doc_Clear = "Clear(Name) -> None";
static PyObject *CnfClear(PyObject *Self,PyObject *Args)
{
@@ -470,34 +475,41 @@ static PyMethodDef CnfMethods[] =
{}
};
-// CnfGetAttr - Get an attribute - variable/method /*{{{*/
-// ---------------------------------------------------------------------
-/* */
-static PyObject *CnfGetAttr(PyObject *Self,char *Name)
-{
- return Py_FindMethod(CnfMethods,Self,Name);
-}
-
// Type for a Normal Configuration object
+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)
- 0, // ob_size
+ 0, // ob_size
"Configuration", // tp_name
sizeof(CppPyObject<Configuration>), // tp_basicsize
0, // tp_itemsize
// Methods
CppDealloc<Configuration>, // tp_dealloc
0, // tp_print
- CnfGetAttr, // tp_getattr
+ 0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
- 0, // tp_as_sequence
+ &ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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
+ "Configuration Object", // tp_doc
+ 0, // tp_traverse
+ 0, // tp_clear
+ 0, // tp_richcompare
+ 0, // tp_weaklistoffset
+ 0, // tp_iter
+ 0, // tp_iternext
+ CnfMethods, // tp_methods
};
PyTypeObject ConfigurationPtrType =
@@ -510,14 +522,28 @@ PyTypeObject ConfigurationPtrType =
// Methods
(destructor)PyObject_Free, // tp_dealloc
0, // tp_print
- CnfGetAttr, // tp_getattr
+ 0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
- 0, // tp_as_sequence
+ &ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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
+ "ConfigurationPtr Object", // tp_doc
+ 0, // tp_traverse
+ 0, // tp_clear
+ 0, // tp_richcompare
+ 0, // tp_weaklistoffset
+ 0, // tp_iter
+ 0, // tp_iternext
+ CnfMethods, // tp_methods
};
PyTypeObject ConfigurationSubType =
@@ -530,13 +556,27 @@ PyTypeObject ConfigurationSubType =
// Methods
CnfSubFree, // tp_dealloc
0, // tp_print
- CnfGetAttr, // tp_getattr
+ 0, // tp_getattr
0, // tp_setattr
0, // tp_compare
0, // tp_repr
0, // tp_as_number
- 0, // tp_as_sequence
+ &ConfigurationSeq, // tp_as_sequence
&ConfigurationMap, // 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
+ "ConfigurationSub Object", // tp_doc
+ 0, // tp_traverse
+ 0, // tp_clear
+ 0, // tp_richcompare
+ 0, // tp_weaklistoffset
+ 0, // tp_iter
+ 0, // tp_iternext
+ CnfMethods, // tp_methods
};