[pypy-svn] rev 690 - pypy/trunk/src/pypy/objspace/std

tismer at codespeak.net tismer at codespeak.net
Thu May 29 17:01:46 CEST 2003


Author: tismer
Date: Thu May 29 17:01:45 2003
New Revision: 690

Modified:
   pypy/trunk/src/pypy/objspace/std/listobject.py
Log:
list.count works

Modified: pypy/trunk/src/pypy/objspace/std/listobject.py
==============================================================================
--- pypy/trunk/src/pypy/objspace/std/listobject.py	(original)
+++ pypy/trunk/src/pypy/objspace/std/listobject.py	Thu May 29 17:01:45 2003
@@ -43,6 +43,9 @@
     def index(w_self, w_any):
         return list_index(w_self.space, w_self, w_any)
 
+    def count(w_self, w_any):
+        return list_count(w_self.space, w_self, w_any)
+
 def list_unwrap(space, w_list):
     items = [space.unwrap(w_item) for w_item in w_list.ob_item[:w_list.ob_size]]
     return list(items)
@@ -205,6 +208,9 @@
     if space.is_true(space.eq(w_attr, space.wrap('index'))):
         w_builtinfn = make_builtin_func(space, W_ListObject.index)
         return W_InstMethObject(space, w_list, w_builtinfn)
+    if space.is_true(space.eq(w_attr, space.wrap('count'))):
+        w_builtinfn = make_builtin_func(space, W_ListObject.count)
+        return W_InstMethObject(space, w_list, w_builtinfn)
     raise FailedToImplement(space.w_AttributeError)
 
 StdObjSpace.getattr.register(getattr_list, W_ListObject, W_ANY)
@@ -354,6 +360,15 @@
     raise OperationError(space.w_ValueError,
                          space.wrap("list.index(x): x not in list"))
 
+def list_count(space, w_list, w_any):
+    eq = space.eq
+    items = w_list.ob_item
+    count = r_int(0)
+    for i in range(w_list.ob_size):
+        cmp = eq(items[i], w_any)
+        if space.is_true(cmp):
+            count += 1
+    return space.wrap(count)
 
 """
 static PyMethodDef list_methods[] = {


More information about the Pypy-commit mailing list