Commits

Amaury Forgeot d'Arc  committed 2935df8

Implement PySet_Contains

  • Participants
  • Parent commits 917774b

Comments (0)

Files changed (3)

File pypy/module/cpyext/setobject.py

         raise OperationError(space.w_TypeError,
                              space.wrap("expected set object"))
     return PySet_GET_SIZE(space, ref)
+
+@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
+def PySet_Contains(space, w_obj, w_key):
+    """Return 1 if found, 0 if not found, and -1 if an error is encountered.  Unlike
+    the Python __contains__() method, this function does not automatically
+    convert unhashable sets into temporary frozensets.  Raise a TypeError if
+    the key is unhashable. Raise PyExc_SystemError if anyset is not a
+    set, frozenset, or an instance of a subtype."""
+    w_res = space.contains(w_obj, w_key)
+    return space.int_w(w_res)

File pypy/module/cpyext/stubs.py

     raise NotImplementedError
 
 @cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
-def PySet_Contains(space, anyset, key):
-    """Return 1 if found, 0 if not found, and -1 if an error is encountered.  Unlike
-    the Python __contains__() method, this function does not automatically
-    convert unhashable sets into temporary frozensets.  Raise a TypeError if
-    the key is unhashable. Raise PyExc_SystemError if anyset is not a
-    set, frozenset, or an instance of a subtype."""
-    raise NotImplementedError
-
-@cpython_api([PyObject, PyObject], rffi.INT_real, error=-1)
 def PySet_Add(space, set, key):
     """Add key to a set instance.  Does not apply to frozenset
     instances.  Return 0 on success or -1 on failure. Raise a TypeError if

File pypy/module/cpyext/test/test_setobject.py

         assert api.PySet_Size(w_set) == 5
         api.PySet_Discard(w_set, space.wrap(6))
         assert api.PySet_Size(w_set) == 4
+
+    def test_set_contains(self, space, api):
+        w_set = api.PySet_New(space.wrap([1,2,3,4]))
+        assert api.PySet_Contains(w_set, space.wrap(1))
+        assert not api.PySet_Contains(w_set, space.wrap(0))