summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1991-10-20 20:20:40 (GMT)
committerGuido van Rossum <guido@python.org>1991-10-20 20:20:40 (GMT)
commite6f7d18e6b303f1e2613d0b016bd0dfb6fd337a1 (patch)
tree22b8ff0e06b71d42ab207f8d063e68a37fe51aed
parentc64d04dedd1800cfc02a72e0109f0b806f644535 (diff)
downloadcpython-e6f7d18e6b303f1e2613d0b016bd0dfb6fd337a1.zip
cpython-e6f7d18e6b303f1e2613d0b016bd0dfb6fd337a1.tar.gz
cpython-e6f7d18e6b303f1e2613d0b016bd0dfb6fd337a1.tar.bz2
Added count() method.
Changed some conditional INCREFs into XINCREFs.
-rw-r--r--Objects/listobject.c32
1 files changed, 24 insertions, 8 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 4026622..18bdbca 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -94,21 +94,18 @@ setlistitem(op, i, newitem)
{
register object *olditem;
if (!is_listobject(op)) {
- if (newitem != NULL)
- DECREF(newitem);
+ XDECREF(newitem);
err_badcall();
return -1;
}
if (i < 0 || i >= ((listobject *)op) -> ob_size) {
- if (newitem != NULL)
- DECREF(newitem);
+ XDECREF(newitem);
err_setstr(IndexError, "list assignment index out of range");
return -1;
}
olditem = ((listobject *)op) -> ob_item[i];
((listobject *)op) -> ob_item[i] = newitem;
- if (olditem != NULL)
- DECREF(olditem);
+ XDECREF(olditem);
return 0;
}
@@ -177,8 +174,7 @@ list_dealloc(op)
{
int i;
for (i = 0; i < op->ob_size; i++) {
- if (op->ob_item[i] != NULL)
- DECREF(op->ob_item[i]);
+ XDECREF(op->ob_item[i]);
}
if (op->ob_item != NULL)
free((ANY *)op->ob_item);
@@ -552,6 +548,25 @@ listindex(self, args)
}
static object *
+listcount(self, args)
+ listobject *self;
+ object *args;
+{
+ int count = 0;
+ int i;
+
+ if (args == NULL) {
+ err_badarg();
+ return NULL;
+ }
+ for (i = 0; i < self->ob_size; i++) {
+ if (cmpobject(self->ob_item[i], args) == 0)
+ count++;
+ }
+ return newintobject((long)count);
+}
+
+static object *
listremove(self, args)
listobject *self;
object *args;
@@ -577,6 +592,7 @@ listremove(self, args)
static struct methodlist list_methods[] = {
{"append", listappend},
+ {"count", listcount},
{"index", listindex},
{"insert", listinsert},
{"sort", listsort},