Skip to content

Commit 53fada3

Browse files
committed
Fix segfaults in benchmark for CPython.
1 parent 389a174 commit 53fada3

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

graalpython/benchmarks/src/micro/c-magic-iter.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,20 +57,25 @@
5757
int ci_init(PyObject* self, PyObject* args, PyObject* kwds);
5858
5959
PyObject* ci_iter(PyObject* self) {
60-
return ((NativeCustomIterableObject*)self)->it;
60+
PyObject* result = ((NativeCustomIterableObject*)self)->it;
61+
Py_INCREF(result);
62+
return result;
6163
}
6264
6365
PyObject* cit_iter(PyObject* self) {
66+
Py_INCREF(self);
6467
return self;
6568
}
6669
6770
PyObject* cit_next(PyObject* self) {
6871
NativeCustomIteratorObject* s = (NativeCustomIteratorObject*)self;
69-
return PyLong_FromLongLong(ci_item((PyObject*)(s->obj), (s->pos)++));
72+
return ci_item((PyObject*)(s->obj), (s->pos)++);
7073
}
7174
7275
PyObject* ci_item(PyObject* self, Py_ssize_t i) {
73-
return ((NativeCustomIterableObject*)self)->scale * i;
76+
PyObject* result = PyLong_FromSsize_t(((NativeCustomIterableObject*)self)->scale * i);
77+
Py_INCREF(result);
78+
return result;
7479
}
7580
7681
@@ -177,6 +182,8 @@
177182
};
178183
179184
int ci_init(PyObject* self, PyObject* args, PyObject* kwds) {
185+
Py_XINCREF(args);
186+
Py_XINCREF(kwds);
180187
static char *kwlist[] = {"scale", NULL};
181188
Py_ssize_t n = 0;
182189
@@ -187,7 +194,11 @@
187194
tself->scale = n + 1;
188195
189196
PyObject *argList = PyTuple_New(0);
197+
Py_INCREF(argList);
190198
PyObject *obj = PyObject_CallObject((PyObject *) &CustomIteratorType, argList);
199+
Py_DECREF(argList);
200+
Py_INCREF(obj);
201+
Py_INCREF(tself);
191202
((NativeCustomIteratorObject*)obj)->obj = tself;
192203
tself->it = obj;
193204
@@ -221,6 +232,7 @@
221232
Py_INCREF(&CustomIteratorType);
222233
PyModule_AddObject(m, "NativeCustomIterable", (PyObject *)&CustomIterableType);
223234
PyModule_AddObject(m, "NativeCustomIterator", (PyObject *)&CustomIteratorType);
235+
Py_INCREF(m);
224236
return m;
225237
}
226238

0 commit comments

Comments
 (0)