From 909982b1b5d2bccbf28ecfb38064ab86e09d4523 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 30 Sep 2009 16:45:46 +0300 Subject: [PATCH] Deprecate custom foo.count() methods, support len() instead --- python/rpmds-py.c | 6 +++--- python/rpmmi-py.c | 20 +++++++++++++------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/python/rpmds-py.c b/python/rpmds-py.c index 67904ba..7ad9fd4 100644 --- a/python/rpmds-py.c +++ b/python/rpmds-py.c @@ -63,7 +63,8 @@ void rpmds_ParseEVR(char * evr, static PyObject * rpmds_Count(rpmdsObject * s) { - return Py_BuildValue("i", rpmdsCount(s->ds)); + DEPRECATED_METHOD; + return Py_BuildValue("i", PyDict_Size((PyObject *)s)); } static PyObject * @@ -383,8 +384,7 @@ rpmds_dealloc(rpmdsObject * s) s->ob_type->tp_free((PyObject *)s); } -static int -rpmds_length(rpmdsObject * s) +static Py_ssize_t rpmds_length(rpmdsObject * s) { return rpmdsCount(s->ds); } diff --git a/python/rpmmi-py.c b/python/rpmmi-py.c index 70584ff..8a5aa6b 100644 --- a/python/rpmmi-py.c +++ b/python/rpmmi-py.c @@ -93,12 +93,8 @@ rpmmi_Instance(rpmmiObject * s) static PyObject * rpmmi_Count(rpmmiObject * s) { - int rc = 0; - - if (s->mi != NULL) - rc = rpmdbGetIteratorCount(s->mi); - - return Py_BuildValue("i", rc); + DEPRECATED_METHOD; + return Py_BuildValue("i", PyDict_Size((PyObject *)s)); } static PyObject * @@ -136,6 +132,16 @@ static void rpmmi_dealloc(rpmmiObject * s) s->ob_type->tp_free((PyObject *)s); } +static Py_ssize_t rpmmi_length(rpmmiObject * s) +{ + return s->mi ? rpmdbGetIteratorCount(s->mi) : 0; +} + +PyMappingMethods rpmmi_as_mapping = { + (lenfunc) rpmmi_length, /* mp_length */ + 0, +}; + static char rpmmi_doc[] = ""; @@ -153,7 +159,7 @@ PyTypeObject rpmmi_Type = { 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ - 0, /* tp_as_mapping */ + &rpmmi_as_mapping, /* tp_as_mapping */ 0, /* tp_hash */ 0, /* tp_call */ 0, /* tp_str */ -- 2.7.4