Author: Armin Rigo <[email protected]>
Branch: _PySet_NextEntry
Changeset: r97907:4b97719a1d45
Date: 2019-10-31 14:24 +0100
http://bitbucket.org/pypy/pypy/changeset/4b97719a1d45/

Log:    in-progress

diff --git a/pypy/module/cpyext/include/Python.h 
b/pypy/module/cpyext/include/Python.h
--- a/pypy/module/cpyext/include/Python.h
+++ b/pypy/module/cpyext/include/Python.h
@@ -108,6 +108,7 @@
 #include "descrobject.h"
 #include "tupleobject.h"
 #include "dictobject.h"
+#include "setobject.h"
 #include "longobject.h"
 #include "listobject.h"
 #include "longobject.h"
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
@@ -1,39 +1,43 @@
 from pypy.interpreter.error import OperationError, oefmt
 from rpython.rtyper.lltypesystem import rffi, lltype
-from pypy.module.cpyext.api import (cpython_api, Py_ssize_t, CANNOT_FAIL,
-                                    build_type_checkers)
+from pypy.module.cpyext.api import (
+    cpython_api, Py_ssize_t, Py_ssize_tP, CANNOT_FAIL, build_type_checkers,
+    PyObjectFields, cpython_struct, bootstrap_function, slot_function)
 from pypy.module.cpyext.pyobject import (PyObject, PyObjectP,
-    make_ref, from_ref)
+    make_ref, from_ref, make_typedescr)
 from pypy.module.cpyext.pyerrors import PyErr_BadInternalCall
 from pypy.objspace.std.setobject import W_SetObject, W_FrozensetObject, newset
 
 PySetObjectStruct = lltype.ForwardReference()
 PySetObject = lltype.Ptr(PySetObjectStruct)
 PySetObjectFields = PyObjectFields + \
-    (("_tmpset", PyObject),)
+    (("_tmplist", PyObject),)
 cpython_struct("PySetObject", PySetObjectFields, PySetObjectStruct)
 
 @bootstrap_function
 def init_dictobject(space):
-    "Type description of PyDictObject"
-    make_typedescr(space.w_dict.layout.typedef,
+    "Type description of PySetObject"
+    make_typedescr(space.w_set.layout.typedef,
                    basestruct=PySetObject.TO,
                    attach=set_attach,
-                   dealloc=set_dealloc,
-                  )
+                   dealloc=set_dealloc)
+    make_typedescr(space.w_frozenset.layout.typedef,   # same as 'set'
+                   basestruct=PySetObject.TO,
+                   attach=set_attach,
+                   dealloc=set_dealloc)
 
 def set_attach(space, py_obj, w_obj, w_userdata=None):
     """
     Fills a newly allocated PySetObject with the given set object.
     """
-    py_set = rffi.cast(PySettObject, py_obj)
-    py_set.c__tmpset = lltype.nullptr(PyObject.TO)
+    py_set = rffi.cast(PySetObject, py_obj)
+    py_set.c__tmplist = lltype.nullptr(PyObject.TO)
 
 @slot_function([PyObject], lltype.Void)
-def dict_dealloc(space, py_obj):
+def set_dealloc(space, py_obj):
     py_set = rffi.cast(PySetObject, py_obj)
-    decref(space, py_set.c__tmpset)
-    py_set.c__tmpset = lltype.nullptr(PyObject.TO)
+    decref(space, py_set.c__tmplist)
+    py_set.c__tmplist = lltype.nullptr(PyObject.TO)
     _dealloc(space, py_obj)
 
 PySet_Check, PySet_CheckExact = build_type_checkers("Set")
@@ -164,15 +168,15 @@
         return -1
     py_obj = as_pyobj(space, w_set)
     py_set = rffi.cast(PySetObject, py_obj)
-    if not py_set.c__tmpkeys:
+    if not py_set.c__tmplist:
         w_keyview = space.call_method(space.w_set, "list", w_set)
         # w_keys must use the object strategy in order to keep the keys alive
         w_keys = space.newlist(space.listview(w_keyview))
         w_keys.switch_to_object_strategy()
-        py_set.c__tmpkeys = create_ref(space, w_keys)
-        incref(space, py_set.c__tmpkeys)
+        py_set.c__tmplist = create_ref(space, w_keys)
+        incref(space, py_set.c__tmplist)
     else:
-        w_keys = from_ref(space, py_set.c__tmpkeys)
+        w_keys = from_ref(space, py_set.c__tmplist)
     pos = pos[0]
     if pos >= space.len_w(w_keys):
         return 0
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
@@ -3,6 +3,7 @@
 from pypy.module.cpyext.setobject import (
     PySet_Check, PyFrozenSet_Check, PyFrozenSet_CheckExact,
     PySet_Add, PySet_Size, PySet_GET_SIZE)
+from pypy.module.cpyext.api import Py_ssize_tP, PyObjectP
 
 
 class TestTupleObject(BaseApiTest):
@@ -62,6 +63,22 @@
         """)
         assert api.PyAnySet_Check(w_instance)
 
+    def test_pyset_next(self, space, api):
+        w_set = space.call_function(space.w_set, space.newtext("ab"))
+        with rffi.scoped_alloc(Py_ssize_tP.TO, 1) as pos_p:
+            with rffi.scoped_alloc(PyObjectP.TO, 1) as result_p:
+                pos_p[0] = 0
+                res = api._PySet_Next(w_set, pos_p, result_p)
+                assert res == 1
+                letter1 = space.text_w(result_p[0])
+                res = api._PySet_Next(w_set, pos_p, result_p)
+                assert res == 1
+                letter2 = space.text_w(result_p[0])
+                res = api._PySet_Next(w_set, pos_p, result_p)
+                assert res == 0
+        assert set([letter1, letter2]) == set("ab")
+
+
 class AppTestSetObject(AppTestCpythonExtensionBase):
     def test_set_macro_cast(self):
         module = self.import_extension('foo', [
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit

Reply via email to