Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_listbuilder.c

raw symbols SCIP
1 #include <stddef.h>
2 #include <Python.h>
3 #include "hpy.h"
4
5 #ifndef HPY_ABI_CPYTHON
6 // for _h2py and _py2h
7 # include "handles.h"
8 #endif
9
10
11 _HPy_HIDDEN HPyListBuilder
12 ctx_ListBuilder_New(HPyContext *ctx, HPy_ssize_t size)
13 {
14 PyObject *lst = PyList_New(size);
15 if (lst == NULL)
16 PyErr_Clear(); /* delay the MemoryError */
17 return (HPyListBuilder){(HPy_ssize_t)lst};
18 }
19
20 _HPy_HIDDEN void
21 ctx_ListBuilder_Set(HPyContext *ctx, HPyListBuilder builder,
22 HPy_ssize_t index, HPy h_item)
23 {
24 PyObject *lst = (PyObject *)builder._lst;
25 if (lst != NULL) {
26 PyObject *item = _h2py(h_item);
27 assert(index >= 0 && index < PyList_GET_SIZE(lst));
28 assert(PyList_GET_ITEM(lst, index) == NULL);
29 Py_INCREF(item);
30 PyList_SET_ITEM(lst, index, item);
31 }
32 }
33
34 _HPy_HIDDEN HPy
35 ctx_ListBuilder_Build(HPyContext *ctx, HPyListBuilder builder)
36 {
37 PyObject *lst = (PyObject *)builder._lst;
38 if (lst == NULL) {
39 PyErr_NoMemory();
40 return HPy_NULL;
41 }
42 builder._lst = 0;
43 return _py2h(lst);
44 }
45
46 _HPy_HIDDEN void
47 ctx_ListBuilder_Cancel(HPyContext *ctx, HPyListBuilder builder)
48 {
49 PyObject *lst = (PyObject *)builder._lst;
50 if (lst == NULL) {
51 // we don't report the memory error here: the builder
52 // is being cancelled (so the result of the builder is not being used)
53 // and likely it's being cancelled during the handling of another error
54 return;
55 }
56 builder._lst = 0;
57 Py_XDECREF(lst);
58 }