Skip to content

Commit c728478

Browse files
committed
Implement dict clear C helper function
1 parent 72bb5c7 commit c728478

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

mypyc/lib-rt/CPy.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ PyObject *CPyDict_ItemsView(PyObject *dict);
347347
PyObject *CPyDict_Keys(PyObject *dict);
348348
PyObject *CPyDict_Values(PyObject *dict);
349349
PyObject *CPyDict_Items(PyObject *dict);
350+
void CPyDict_Clear(PyObject *dict);
350351
PyObject *CPyDict_GetKeysIter(PyObject *dict);
351352
PyObject *CPyDict_GetItemsIter(PyObject *dict);
352353
PyObject *CPyDict_GetValuesIter(PyObject *dict);

mypyc/lib-rt/dict_ops.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,14 @@ PyObject *CPyDict_Items(PyObject *dict) {
226226
return list;
227227
}
228228

229+
void CPyDict_Clear(PyObject *dict) {
230+
if (PyDict_CheckExact(dict)) {
231+
PyDict_Clear(dict);
232+
} else {
233+
PyObject_CallMethod(dict, "clear", NULL);
234+
}
235+
}
236+
229237
PyObject *CPyDict_GetKeysIter(PyObject *dict) {
230238
if (PyDict_CheckExact(dict)) {
231239
// Return dict itself to indicate we can use fast path instead.

mypyc/primitives/dict_ops.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@
143143
error_kind=ERR_MAGIC)
144144

145145
# dict.clear()
146-
c_method_op(
146+
method_op(
147147
name='clear',
148148
arg_types=[dict_rprimitive],
149149
return_type=void_rtype,
150-
c_function_name='PyDict_Clear',
150+
c_function_name='CPyDict_Clear',
151151
error_kind=ERR_NEVER)
152152

153153
# list(dict.keys())

mypyc/test-data/irbuild-dict.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,5 +321,5 @@ def f(d: Dict[int, int]) -> None:
321321
def f(d):
322322
d :: dict
323323
L0:
324-
PyDict_Clear(d)
324+
CPyDict_Clear(d)
325325
return 1

0 commit comments

Comments
 (0)