Debian Codegraph @

lib_pypy/hpy/devel/src/runtime/ctx_tuplebuilder.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 HPyTupleBuilder
12 ctx_TupleBuilder_New(HPyContext *ctx, HPy_ssize_t size)
13 {
14 PyObject *tup = PyTuple_New(size);
15 if (tup == NULL) {
16 PyErr_Clear(); /* delay the MemoryError */
17 /* note: it's done this way so that the caller doesn't need to
18 check if HPyTupleBuilder_New() or every HPyTupleBuilder_Set()
19 raised. If there is a rare error like a MemoryError somewhere,
20 further calls to the HPyTupleBuilder are ignored. The final
21 HPyTupleBuilder_Build() will re-raise the MemoryError and so
22 it's enough for the caller to check at that point. */
23 }
24 return (HPyTupleBuilder){(HPy_ssize_t)tup};
25 }
26
27 _HPy_HIDDEN void
28 ctx_TupleBuilder_Set(HPyContext *ctx, HPyTupleBuilder builder,
29 HPy_ssize_t index, HPy h_item)
30 {
31 PyObject *tup = (PyObject *)builder._tup;
32 if (tup != NULL) {
33 PyObject *item = _h2py(h_item);
34 assert(index >= 0 && index < PyTuple_GET_SIZE(tup));
35 assert(PyTuple_GET_ITEM(tup, index) == NULL);
36 Py_INCREF(item);
37 PyTuple_SET_ITEM(tup, index, item);
38 }
39 }
40
41 _HPy_HIDDEN HPy
42 ctx_TupleBuilder_Build(HPyContext *ctx, HPyTupleBuilder builder)
43 {
44 PyObject *tup = (PyObject *)builder._tup;
45 if (tup == NULL) {
46 PyErr_NoMemory();
47 return HPy_NULL;
48 }
49 builder._tup = 0;
50 return _py2h(tup);
51 }
52
53 _HPy_HIDDEN void
54 ctx_TupleBuilder_Cancel(HPyContext *ctx, HPyTupleBuilder builder)
55 {
56 PyObject *tup = (PyObject *)builder._tup;
57 if (tup == NULL) {
58 // we don't report the memory error here: the builder
59 // is being cancelled (so the result of the builder is not being used)
60 // and likely it's being cancelled during the handling of another error
61 return;
62 }
63 builder._tup = 0;
64 Py_XDECREF(tup);
65 }