Skip to content

Commit fb42896

Browse files
committed
Implement C API function 'PySet_New' and 'PyFrozenSet_New'.
1 parent 82030ac commit fb42896

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ POLYGLOT_DECLARE_TYPE(PyBytesObject);
7474
POLYGLOT_DECLARE_STRUCT(_longobject);
7575
POLYGLOT_DECLARE_TYPE(PyCapsule);
7676
POLYGLOT_DECLARE_TYPE(PyMemoryViewObject);
77+
POLYGLOT_DECLARE_TYPE(PySetObject);
7778

7879

7980
extern void* to_java(PyObject* obj);

graalpython/com.oracle.graal.python.cext/src/setobject.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,19 @@
4040

4141
PyTypeObject PySet_Type = PY_TRUFFLE_TYPE("set", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC);
4242
PyTypeObject PyFrozenSet_Type = PY_TRUFFLE_TYPE("frozenset", &PyType_Type, Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC);
43+
44+
PyObject * PySet_New(PyObject *iterable) {
45+
void* result = polyglot_invoke(PY_TRUFFLE_CEXT, "PySet_New", to_java(iterable));
46+
if (result == ERROR_MARKER) {
47+
return NULL;
48+
}
49+
return to_sulong(result);
50+
}
51+
52+
PyObject * PyFrozenSet_New(PyObject *iterable) {
53+
void* result = polyglot_invoke(PY_TRUFFLE_CEXT, "PyFrozenSet_New", to_java(iterable));
54+
if (result == ERROR_MARKER) {
55+
return NULL;
56+
}
57+
return to_sulong(result);
58+
}

graalpython/lib-graalpython/python_cext.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,25 @@ def PyDict_DelItem(dictObj, key):
152152
return 0
153153

154154

155+
##################### SET, FROZENSET
156+
157+
158+
@may_raise
159+
def PySet_New(iterable):
160+
if iterable:
161+
return set(iterable)
162+
else:
163+
return set()
164+
165+
166+
@may_raise
167+
def PyFrozenSet_New(iterable):
168+
if iterable:
169+
return frozenset(iterable)
170+
else:
171+
return frozenset()
172+
173+
155174
##################### MAPPINGPROXY
156175

157176

0 commit comments

Comments
 (0)