Debian Codegraph @

pypy/module/cpyext/test/test_setobject.py

raw symbols SCIP
1 from rpython.rtyper.lltypesystem import lltype
2 from pypy.module.cpyext.test.test_api import BaseApiTest, raises_w
3 from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase
4 from pypy.module.cpyext.setobject import (
5 PySet_Check, PyFrozenSet_Check, PyFrozenSet_CheckExact,
6 PySet_Add, PySet_Size, PySet_GET_SIZE)
7 from pypy.module.cpyext.api import Py_ssize_tP, PyObjectP
8 from pypy.module.cpyext.pyobject import from_ref
9
10
11 class TestTupleObject(BaseApiTest):
12 def test_setobj(self, space):
13 assert not PySet_Check(space, space.w_None)
14 assert not PyFrozenSet_Check(space, space.w_None)
15 with raises_w(space, SystemError):
16 PySet_Add(space, space.w_None, space.w_None)
17 w_set = space.call_function(space.w_set)
18 assert not PyFrozenSet_CheckExact(space, w_set)
19 space.call_method(w_set, 'update', space.wrap([1, 2, 3, 4]))
20 assert PySet_Size(space, w_set) == 4
21 assert PySet_GET_SIZE(space, w_set) == 4
22 with raises_w(space, TypeError):
23 PySet_Size(space, space.newlist([]))
24
25 def test_set_add_discard(self, space, api):
26 w_set = api.PySet_New(None)
27 assert api.PySet_Size(w_set) == 0
28 w_set = api.PyFrozenSet_New(space.wrap([1, 2, 3, 4]))
29 assert api.PySet_Size(w_set) == 4
30 w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
31 assert api.PySet_Size(w_set) == 4
32 api.PySet_Add(w_set, space.wrap(6))
33 assert api.PySet_Size(w_set) == 5
34 res = api.PySet_Discard(w_set, space.wrap(6))
35 assert res == 1
36 assert api.PySet_Size(w_set) == 4
37 res = api.PySet_Discard(w_set, space.wrap(6))
38 assert res == 0
39 assert api.PySet_Size(w_set) == 4
40
41 def test_frozenset_add(self, space, api):
42 w_set = api.PyFrozenSet_New(None)
43 api.PySet_Add(w_set, space.wrap(4))
44 assert api.PySet_Size(w_set) == 1
45 api.PySet_Add(w_set, space.wrap(5))
46 assert api.PySet_Size(w_set) == 2
47 assert space.hash_w(w_set) != 0 # makes the set really frozen
48 with raises_w(space, SystemError):
49 api.PySet_Add(w_set, space.wrap(6))
50
51 def test_set_contains(self, space, api):
52 w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
53 assert api.PySet_Contains(w_set, space.wrap(1))
54 assert not api.PySet_Contains(w_set, space.wrap(0))
55
56 def test_set_pop_clear(self, space, api):
57 w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
58 w_obj = api.PySet_Pop(w_set)
59 assert space.int_w(w_obj) in (1, 2, 3, 4)
60 assert space.len_w(w_set) == 3
61 api.PySet_Clear(w_set)
62 assert space.len_w(w_set) == 0
63
64 def test_anyset_check(self, space, api):
65 w_set = api.PySet_New(space.wrap([1, 2, 3, 4]))
66 w_frozenset = space.newfrozenset([space.wrap(i) for i in [1, 2, 3, 4]])
67 assert api.PyAnySet_CheckExact(w_set)
68 assert api.PyAnySet_CheckExact(w_frozenset)
69 assert api.PyAnySet_Check(w_set)
70 assert api.PyAnySet_Check(w_frozenset)
71 w_instance = space.appexec([], """():
72 class MySet(set):
73 pass
74 return MySet()
75 """)
76 assert api.PyAnySet_Check(w_instance)
77
78 def test_pyset_next(self, space, api):
79 w_set = space.call_function(space.w_set, space.newtext("ab"))
80 with lltype.scoped_alloc(Py_ssize_tP.TO, 1) as pos_p:
81 with lltype.scoped_alloc(PyObjectP.TO, 1) as result_p:
82 pos_p[0] = 0
83 res = api._PySet_Next(w_set, pos_p, result_p)
84 assert res == 1
85 letter1 = space.text_w(from_ref(space, result_p[0]))
86 res = api._PySet_Next(w_set, pos_p, result_p)
87 assert res == 1
88 letter2 = space.text_w(from_ref(space, result_p[0]))
89 res = api._PySet_Next(w_set, pos_p, result_p)
90 assert res == 0
91 assert set([letter1, letter2]) == set("ab")
92
93 def test_pyset_nextentry(self, space, api):
94 w_set = space.call_function(space.w_set, space.newtext("ab"))
95 with lltype.scoped_alloc(Py_ssize_tP.TO, 1) as pos_p:
96 with lltype.scoped_alloc(PyObjectP.TO, 1) as result_p:
97 with lltype.scoped_alloc(Py_ssize_tP.TO, 1) as hash_p:
98 pos_p[0] = 0
99 res = api._PySet_NextEntry(w_set, pos_p, result_p, hash_p)
100 assert res == 1
101 w_obj = from_ref(space, result_p[0])
102 letter1 = space.text_w(w_obj)
103 assert hash_p[0] == space.hash_w(w_obj)
104 res = api._PySet_NextEntry(w_set, pos_p, result_p, hash_p)
105 assert res == 1
106 w_obj = from_ref(space, result_p[0])
107 letter2 = space.text_w(w_obj)
108 assert hash_p[0] == space.hash_w(w_obj)
109 res = api._PySet_NextEntry(w_set, pos_p, result_p, hash_p)
110 assert res == 0
111 assert set([letter1, letter2]) == set("ab")
112
113
114 class AppTestSetObject(AppTestCpythonExtensionBase):
115 def test_set_macro_cast(self):
116 module = self.import_extension('foo', [
117 ("test_macro_cast", "METH_NOARGS",
118 """
119 PyObject* o = PySet_New(NULL);
120 // no PySetObject
121 char* dumb_pointer = (char*) o;
122
123 PySet_GET_SIZE(o);
124 PySet_GET_SIZE(dumb_pointer);
125
126 return o;
127 """)
128 ])