Debian Codegraph @

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

raw symbols SCIP
1 #include "hpy.h"
2
3 /**
4 * Create a type and add it as an attribute on the given object. The type is
5 * created using :c:func:`HPyType_FromSpec`. The object is often a module that the type
6 * is being added to.
7 *
8 * :param ctx:
9 * The execution context.
10 * :param obj:
11 * A handle to the object the type is being added to (often a module).
12 * :param name:
13 * The name of the attribute on the object to assign the type to.
14 * :param hpyspec:
15 * The type spec to use to create the type.
16 * :param params:
17 * The type spec parameters to use to create the type.
18 *
19 * :returns: ``0`` on failure, ``1`` on success.
20 *
21 * Examples:
22 *
23 * Using ``HPyHelpers_AddType`` without any :c:struct:`HPyType_SpecParam`
24 * parameters:
25 *
26 * .. code-block:: c
27 *
28 * if (!HPyHelpers_AddType(ctx, module, "MyType", hpyspec, NULL))
29 * return HPy_NULL;
30 * ...
31 *
32 * Using `HPyHelpers_AddType` with `HPyType_SpecParam` parameters:
33 *
34 * .. code-block:: c
35 *
36 * HPyType_SpecParam params[] = {
37 * { HPyType_SpecParam_Base, ctx->h_LongType },
38 * { 0 }
39 * };
40 *
41 * if (!HPyHelpers_AddType(ctx, module, "MyType", hpyspec, params))
42 * return HPy_NULL;
43 * ...
44 */
45 HPyAPI_HELPER int
46 HPyHelpers_AddType(HPyContext *ctx, HPy obj, const char *name,
47 HPyType_Spec *hpyspec, HPyType_SpecParam *params)
48 {
49 HPy h_type = HPyType_FromSpec(ctx, hpyspec, params);
50 if (HPy_IsNull(h_type)) {
51 return 0;
52 }
53 if (HPy_SetAttr_s(ctx, obj, name, h_type) != 0) {
54 HPy_Close(ctx, h_type);
55 return 0;
56 }
57 HPy_Close(ctx, h_type);
58 return 1;
59 }
60
61 /**
62 * Convert positional/keyword argument vector to argument tuple and keywords
63 * dictionary.
64 *
65 * This helper function is useful to convert arguments from HPy's calling
66 * convention to the legacy CPython *tp_call* calling convention. HPy's calling
67 * convention is similar to CPython's fastcall/vectorcall calling convention
68 * where positional and keyword arguments are passed as a C array, the number of
69 * positional arguments is explicitly given by an argument and the names of the
70 * keyword arguments are provided in a tuple.
71 *
72 * For an example on how to use this function, see section
73 * :ref:`call-migration`.
74 *
75 * :param ctx:
76 * The execution context.
77 * :param args:
78 * A pointer to an array of positional and keyword arguments. This argument
79 * must not be ``NULL`` if ``nargs > 0`` or
80 * ``HPy_Length(ctx, kwnames) > 0``.
81 * :param nargs:
82 * The number of positional arguments in ``args``.
83 * :param kwnames:
84 * A handle to the tuple of keyword argument names (may be ``HPy_NULL``).
85 * The values of the keyword arguments are also passed in ``args`` appended
86 * to the positional arguments. Argument ``nargs`` does not include the
87 * keyword argument count.
88 * :param out_pos_args:
89 * A pointer to a variable where to write the created positional arguments
90 * tuple to. If there are no positional arguments (i.e. ``nargs == 0``),
91 * then ``HPy_NULL`` will be written. The pointer will not be used if any
92 * error occurs during conversion.
93 * :param out_kwd:
94 * A pointer to a variable where to write the created keyword arguments
95 * dictionary to. If there are not keyword arguments (i.e.
96 * ``HPy_Length(ctx, kwnames) == 0``), then ``HPy_NULL`` will be written.
97 * The pointer will not be used if any error occurs during conversion.
98 *
99 * :returns: ``0`` on failure, ``1`` on success.
100 */
101 HPyAPI_HELPER int
102 HPyHelpers_PackArgsAndKeywords(HPyContext *ctx, const HPy *args, size_t nargs,
103 HPy kwnames, HPy *out_pos_args, HPy *out_kwd)
104 {
105 HPy pos_args, kwd, tmp;
106 HPy_ssize_t nkw, i;
107
108 if (out_pos_args == NULL) {
109 HPyErr_SetString(ctx, ctx->h_SystemError,
110 "argument 'out_pos_args' must not be NULL");
111 return 0;
112 }
113 if (out_kwd == NULL) {
114 HPyErr_SetString(ctx, ctx->h_SystemError,
115 "argument 'out_kwd' must not be NULL");
116 return 0;
117 }
118
119 nkw = HPy_IsNull(kwnames) ? 0 : HPy_Length(ctx, kwnames);
120 if (nkw < 0) {
121 return 0;
122 } else if (nkw > 0) {
123 kwd = HPyDict_New(ctx);
124 for (i=0; i < nkw; i++) {
125 tmp = HPy_GetItem_i(ctx, kwnames, i);
126 if (HPy_IsNull(tmp)) {
127 HPy_Close(ctx, kwd);
128 return 0;
129 }
130 if (HPy_SetItem(ctx, kwd, tmp, args[nargs + i]) < 0) {
131 HPy_Close(ctx, tmp);
132 HPy_Close(ctx, kwd);
133 return 0;
134 }
135 HPy_Close(ctx, tmp);
136 }
137 } else {
138 assert(nkw == 0);
139 kwd = HPy_NULL;
140 }
141 if (nargs > 0) {
142 pos_args = HPyTuple_FromArray(ctx, (HPy *)args, nargs);
143 if (HPy_IsNull(pos_args)) {
144 HPy_Close(ctx, kwd);
145 return 0;
146 }
147 } else {
148 pos_args = HPy_NULL;
149 }
150 // only write to output variables if everything was successful
151 *out_pos_args = pos_args;
152 *out_kwd = kwd;
153 return 1;
154 }