Author: Matti Picus <matti.pi...@gmail.com>
Branch: 
Changeset: r91613:776f717d31e0
Date: 2017-06-16 09:43 +0300
http://bitbucket.org/pypy/pypy/changeset/776f717d31e0/

Log:    support PyFrozenSet_New

diff --git a/pypy/module/cpyext/setobject.py b/pypy/module/cpyext/setobject.py
--- a/pypy/module/cpyext/setobject.py
+++ b/pypy/module/cpyext/setobject.py
@@ -9,6 +9,7 @@
 
 
 PySet_Check, PySet_CheckExact = build_type_checkers("Set")
+PyFrozenSet_Check, PyFrozenSet_CheckExact = build_type_checkers("FrozenSet")
 
 @cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
 def PyAnySet_Check(space, w_obj):
@@ -91,7 +92,7 @@
     """Return the length of a set or frozenset object. Equivalent to
     len(anyset).  Raises a PyExc_SystemError if anyset is not a set, frozenset,
     or an instance of a subtype."""
-    if not PySet_Check(space, ref):
+    if not PyAnySet_Check(space, ref):
         raise oefmt(space.w_TypeError, "expected set object")
     return PySet_GET_SIZE(space, ref)
 
@@ -104,3 +105,20 @@
     set, frozenset, or an instance of a subtype."""
     w_res = space.contains(w_obj, w_key)
     return space.int_w(w_res)
+
+@cpython_api([PyObject], PyObject)
+def PyFrozenSet_New(space, w_iterable):
+    """Return a new frozenset containing objects returned by the iterable.
+    The iterable may be NULL to create a new empty frozenset.  Return the new
+    set on success or NULL on failure.  Raise TypeError if iterable is
+    not actually iterable.
+
+    Now guaranteed to return a brand-new frozenset.  Formerly,
+    frozensets of zero-length were a singleton.  This got in the way of
+    building-up new frozensets with PySet_Add()."""
+    if w_iterable is None:
+        return space.call_function(space.w_frozenset)
+    else:
+        return space.call_function(space.w_frozenset, w_iterable)
+
+
diff --git a/pypy/module/cpyext/stubs.py b/pypy/module/cpyext/stubs.py
--- a/pypy/module/cpyext/stubs.py
+++ b/pypy/module/cpyext/stubs.py
@@ -1423,31 +1423,6 @@
     in your code for properly supporting 64-bit systems."""
     raise NotImplementedError
 
-@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyFrozenSet_Check(space, p):
-    """Return true if p is a frozenset object or an instance of a
-    subtype.
-    """
-    raise NotImplementedError
-
-@cpython_api([PyObject], rffi.INT_real, error=CANNOT_FAIL)
-def PyFrozenSet_CheckExact(space, p):
-    """Return true if p is a frozenset object but not an instance of a
-    subtype."""
-    raise NotImplementedError
-
-@cpython_api([PyObject], PyObject)
-def PyFrozenSet_New(space, iterable):
-    """Return a new frozenset containing objects returned by the iterable.
-    The iterable may be NULL to create a new empty frozenset.  Return the new
-    set on success or NULL on failure.  Raise TypeError if iterable is
-    not actually iterable.
-
-    Now guaranteed to return a brand-new frozenset.  Formerly,
-    frozensets of zero-length were a singleton.  This got in the way of
-    building-up new frozensets with PySet_Add()."""
-    raise NotImplementedError
-
 @cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP, rffi.CCHARP], PyObject)
 def PyString_Decode(space, s, size, encoding, errors):
     """Create an object by decoding size bytes of the encoded buffer s using 
the
diff --git a/pypy/module/cpyext/test/test_setobject.py 
b/pypy/module/cpyext/test/test_setobject.py
--- a/pypy/module/cpyext/test/test_setobject.py
+++ b/pypy/module/cpyext/test/test_setobject.py
@@ -9,9 +9,11 @@
 class TestTupleObject(BaseApiTest):
     def test_setobj(self, space, api):
         assert not api.PySet_Check(space.w_None)
+        assert not api.PyFrozenSet_Check(space.w_None)
         assert api.PySet_Add(space.w_None, space.w_None) == -1
         api.PyErr_Clear()
         w_set = space.call_function(space.w_set)
+        assert not api.PyFrozenSet_CheckExact(w_set)
         space.call_method(w_set, 'update', space.wrap([1,2,3,4]))
         assert api.PySet_Size(w_set) == 4
         assert api.PySet_GET_SIZE(w_set) == 4
@@ -21,6 +23,8 @@
     def test_set_add_discard(self, space, api):
         w_set = api.PySet_New(None)
         assert api.PySet_Size(w_set) == 0
+        w_set = api.PyFrozenSet_New(space.wrap([1,2,3,4]))
+        assert api.PySet_Size(w_set) == 4
         w_set = api.PySet_New(space.wrap([1,2,3,4]))
         assert api.PySet_Size(w_set) == 4
         api.PySet_Add(w_set, space.wrap(6))
_______________________________________________
pypy-commit mailing list
pypy-commit@python.org
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to