Kaydet (Commit) e6f7d18e authored tarafından Guido van Rossum's avatar Guido van Rossum

Added count() method.

Changed some conditional INCREFs into XINCREFs.
üst c64d04de
...@@ -94,21 +94,18 @@ setlistitem(op, i, newitem) ...@@ -94,21 +94,18 @@ setlistitem(op, i, newitem)
{ {
register object *olditem; register object *olditem;
if (!is_listobject(op)) { if (!is_listobject(op)) {
if (newitem != NULL) XDECREF(newitem);
DECREF(newitem);
err_badcall(); err_badcall();
return -1; return -1;
} }
if (i < 0 || i >= ((listobject *)op) -> ob_size) { if (i < 0 || i >= ((listobject *)op) -> ob_size) {
if (newitem != NULL) XDECREF(newitem);
DECREF(newitem);
err_setstr(IndexError, "list assignment index out of range"); err_setstr(IndexError, "list assignment index out of range");
return -1; return -1;
} }
olditem = ((listobject *)op) -> ob_item[i]; olditem = ((listobject *)op) -> ob_item[i];
((listobject *)op) -> ob_item[i] = newitem; ((listobject *)op) -> ob_item[i] = newitem;
if (olditem != NULL) XDECREF(olditem);
DECREF(olditem);
return 0; return 0;
} }
...@@ -177,8 +174,7 @@ list_dealloc(op) ...@@ -177,8 +174,7 @@ list_dealloc(op)
{ {
int i; int i;
for (i = 0; i < op->ob_size; i++) { for (i = 0; i < op->ob_size; i++) {
if (op->ob_item[i] != NULL) XDECREF(op->ob_item[i]);
DECREF(op->ob_item[i]);
} }
if (op->ob_item != NULL) if (op->ob_item != NULL)
free((ANY *)op->ob_item); free((ANY *)op->ob_item);
...@@ -551,6 +547,25 @@ listindex(self, args) ...@@ -551,6 +547,25 @@ listindex(self, args)
return NULL; return NULL;
} }
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 * static object *
listremove(self, args) listremove(self, args)
listobject *self; listobject *self;
...@@ -577,6 +592,7 @@ listremove(self, args) ...@@ -577,6 +592,7 @@ listremove(self, args)
static struct methodlist list_methods[] = { static struct methodlist list_methods[] = {
{"append", listappend}, {"append", listappend},
{"count", listcount},
{"index", listindex}, {"index", listindex},
{"insert", listinsert}, {"insert", listinsert},
{"sort", listsort}, {"sort", listsort},
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment