Skip to content

Commit 6ce4ac4

Browse files
timfelfangerer
authored andcommitted
further unify optional arg handling
1 parent d489d7d commit 6ce4ac4

File tree

1 file changed

+44
-62
lines changed

1 file changed

+44
-62
lines changed

graalpython/com.oracle.graal.python.cext/src/modsupport.c

Lines changed: 44 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -64,54 +64,36 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
6464
return NULL;
6565
}
6666

67-
#define PyTruffle_WriteOutImmediate(n, T, arg) { \
67+
#define PyTruffle_WriteOut(n, T, arg) { \
6868
T __oai = arg; \
6969
if (PyErr_Occurred()) { \
7070
return 0; \
7171
} \
7272
switch(n) { \
73-
case 0: *((T*)v0) = (T)__oai; break; \
74-
case 1: *((T*)v1) = (T)__oai; break; \
75-
case 2: *((T*)v2) = (T)__oai; break; \
76-
case 3: *((T*)v3) = (T)__oai; break; \
77-
case 4: *((T*)v4) = (T)__oai; break; \
78-
case 5: *((T*)v5) = (T)__oai; break; \
79-
case 6: *((T*)v6) = (T)__oai; break; \
80-
case 7: *((T*)v7) = (T)__oai; break; \
81-
case 8: *((T*)v8) = (T)__oai; break; \
82-
case 9: *((T*)v9) = (T)__oai; break; \
83-
case 10: *((T*)v10) = (T)__oai; break; \
84-
case 11: *((T*)v11) = (T)__oai; break; \
85-
case 12: *((T*)v12) = (T)__oai; break; \
86-
case 13: *((T*)v13) = (T)__oai; break; \
87-
case 14: *((T*)v14) = (T)__oai; break; \
88-
case 15: *((T*)v15) = (T)__oai; break; \
89-
case 16: *((T*)v16) = (T)__oai; break; \
90-
case 17: *((T*)v17) = (T)__oai; break; \
91-
case 18: *((T*)v18) = (T)__oai; break; \
92-
case 19: *((T*)v19) = (T)__oai; break; \
73+
case 0: *((T*)v0) = __oai; break; \
74+
case 1: *((T*)v1) = __oai; break; \
75+
case 2: *((T*)v2) = __oai; break; \
76+
case 3: *((T*)v3) = __oai; break; \
77+
case 4: *((T*)v4) = __oai; break; \
78+
case 5: *((T*)v5) = __oai; break; \
79+
case 6: *((T*)v6) = __oai; break; \
80+
case 7: *((T*)v7) = __oai; break; \
81+
case 8: *((T*)v8) = __oai; break; \
82+
case 9: *((T*)v9) = __oai; break; \
83+
case 10: *((T*)v10) = __oai; break; \
84+
case 11: *((T*)v11) = __oai; break; \
85+
case 12: *((T*)v12) = __oai; break; \
86+
case 13: *((T*)v13) = __oai; break; \
87+
case 14: *((T*)v14) = __oai; break; \
88+
case 15: *((T*)v15) = __oai; break; \
89+
case 16: *((T*)v16) = __oai; break; \
90+
case 17: *((T*)v17) = __oai; break; \
91+
case 18: *((T*)v18) = __oai; break; \
92+
case 19: *((T*)v19) = __oai; break; \
9393
} \
9494
n++; \
9595
} while(0);
9696

97-
#define PyTruffle_WriteOut(n, T, arg, optional) { \
98-
T __oa = arg; \
99-
if (__oa == NULL) { \
100-
if (!optional) { \
101-
PyErr_Format(PyExc_TypeError, \
102-
"not enough arguments " \
103-
"(expected at least %d, got %d)", \
104-
n, \
105-
n - 1); \
106-
return 0; \
107-
} else { \
108-
n++; \
109-
} \
110-
} else { \
111-
PyTruffle_WriteOutImmediate(n, T, __oa); \
112-
} \
113-
} while(0);
114-
11597
#define PyTruffle_ArgN(n) (((n) == 0) ? v0 : (((n) == 1) ? v1 : (((n) == 2) ? v2 : (((n) == 3) ? v3 : (((n) == 4) ? v4 : (((n) == 5) ? v5 : (((n) == 6) ? v6 : (((n) == 7) ? v7 : (((n) == 8) ? v8 : (((n) == 9) ? v9 : (((n) == 10) ? v10 : (((n) == 11) ? v11 : (((n) == 12) ? v12 : (((n) == 13) ? v13 : (((n) == 14) ? v14 : (((n) == 15) ? v15 : (((n) == 16) ? v16 : (((n) == 17) ? v17 : (((n) == 18) ? v18 : (((n) == 19) ? v19 : NULL))))))))))))))))))))
11698

11799
#define PyTruffle_SkipOptionalArg(n, arg, optional) \
@@ -147,21 +129,21 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
147129
return 0;
148130
} else if (arg == Py_None) {
149131
if (c == 'z') {
150-
PyTruffle_WriteOut(output_idx, const char*, NULL, rest_optional);
132+
PyTruffle_WriteOut(output_idx, const char*, NULL);
151133
if (format[format_idx + 1] == '#') {
152134
format_idx++; // skip over '#'
153-
PyTruffle_WriteOutImmediate(output_idx, int, 0);
135+
PyTruffle_WriteOut(output_idx, int, 0);
154136
}
155137
} else {
156138
PyErr_Format(PyExc_TypeError, "expected str or bytes-like, got None");
157139
return 0;
158140
}
159141
} else {
160142
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
161-
PyTruffle_WriteOut(output_idx, const char*, as_char_pointer(arg), rest_optional);
143+
PyTruffle_WriteOut(output_idx, const char*, as_char_pointer(arg));
162144
if (format[format_idx + 1] == '#') {
163145
format_idx++;
164-
PyTruffle_WriteOutImmediate(output_idx, int, Py_SIZE(arg));
146+
PyTruffle_WriteOut(output_idx, int, Py_SIZE(arg));
165147
}
166148
}
167149
break;
@@ -173,7 +155,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
173155
PyErr_Format(PyExc_TypeError, "expected bytes, got %R", Py_TYPE(arg));
174156
return 0;
175157
}
176-
PyTruffle_WriteOut(output_idx, PyObject*, arg, rest_optional);
158+
PyTruffle_WriteOut(output_idx, PyObject*, arg);
177159
break;
178160
case 'Y':
179161
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
@@ -183,7 +165,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
183165
PyErr_Format(PyExc_TypeError, "expected bytearray, got %R", Py_TYPE(arg));
184166
return 0;
185167
}
186-
PyTruffle_WriteOut(output_idx, PyObject*, arg, rest_optional);
168+
PyTruffle_WriteOut(output_idx, PyObject*, arg);
187169
break;
188170
case 'u':
189171
case 'Z':
@@ -197,7 +179,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
197179
PyErr_Format(PyExc_TypeError, "expected str, got %R", Py_TYPE(arg));
198180
return 0;
199181
}
200-
PyTruffle_WriteOut(output_idx, PyObject*, arg, rest_optional);
182+
PyTruffle_WriteOut(output_idx, PyObject*, arg);
201183
break;
202184
case 'w':
203185
PyErr_Format(PyExc_TypeError, "'w' format specifier in argument parsing not supported");
@@ -221,13 +203,13 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
221203
PyErr_Format(PyExc_TypeError, "expected non-negative integer");
222204
return 0;
223205
}
224-
PyTruffle_WriteOutImmediate(output_idx, unsigned char, as_uchar(arg));
206+
PyTruffle_WriteOut(output_idx, unsigned char, as_uchar(arg));
225207
break;
226208
case 'B':
227209
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
228210
v->argnum++;
229211
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
230-
PyTruffle_WriteOutImmediate(output_idx, unsigned char, as_uchar(arg));
212+
PyTruffle_WriteOut(output_idx, unsigned char, as_uchar(arg));
231213
break;
232214
case 'h':
233215
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
@@ -237,37 +219,37 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
237219
PyErr_Format(PyExc_TypeError, "expected non-negative integer");
238220
return 0;
239221
}
240-
PyTruffle_WriteOutImmediate(output_idx, short int, as_short(arg));
222+
PyTruffle_WriteOut(output_idx, short int, as_short(arg));
241223
break;
242224
case 'H':
243225
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
244226
v->argnum++;
245227
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
246-
PyTruffle_WriteOutImmediate(output_idx, short int, as_short(arg));
228+
PyTruffle_WriteOut(output_idx, short int, as_short(arg));
247229
break;
248230
case 'i':
249231
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
250232
v->argnum++;
251233
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
252-
PyTruffle_WriteOutImmediate(output_idx, int, as_int(arg));
234+
PyTruffle_WriteOut(output_idx, int, as_int(arg));
253235
break;
254236
case 'I':
255237
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
256238
v->argnum++;
257239
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
258-
PyTruffle_WriteOutImmediate(output_idx, unsigned int, as_int(arg));
240+
PyTruffle_WriteOut(output_idx, unsigned int, as_int(arg));
259241
break;
260242
case 'l':
261243
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
262244
v->argnum++;
263245
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
264-
PyTruffle_WriteOutImmediate(output_idx, long, as_long(arg));
246+
PyTruffle_WriteOut(output_idx, long, as_long(arg));
265247
break;
266248
case 'k':
267249
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
268250
v->argnum++;
269251
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
270-
PyTruffle_WriteOutImmediate(output_idx, unsigned long, as_long(arg));
252+
PyTruffle_WriteOut(output_idx, unsigned long, as_long(arg));
271253
break;
272254
case 'L':
273255
PyErr_Format(PyExc_TypeError, "long long argument parsing not yet supported");
@@ -279,7 +261,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
279261
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
280262
v->argnum++;
281263
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
282-
PyTruffle_WriteOutImmediate(output_idx, Py_ssize_t, as_long(arg));
264+
PyTruffle_WriteOut(output_idx, Py_ssize_t, as_long(arg));
283265
break;
284266
case 'c':
285267
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
@@ -293,7 +275,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
293275
PyErr_Format(PyExc_TypeError, "expted bytes or bytearray of length 1, was length %d", Py_SIZE(arg));
294276
return 0;
295277
}
296-
PyTruffle_WriteOutImmediate(output_idx, char, as_char(polyglot_invoke(to_java(arg), "__getitem__", 0)));
278+
PyTruffle_WriteOut(output_idx, char, as_char(polyglot_invoke(to_java(arg), "__getitem__", 0)));
297279
break;
298280
case 'C':
299281
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
@@ -307,19 +289,19 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
307289
PyErr_Format(PyExc_TypeError, "expted str of length 1, was length %d", Py_SIZE(arg));
308290
return 0;
309291
}
310-
PyTruffle_WriteOutImmediate(output_idx, int, as_int(polyglot_invoke(to_java(arg), "__getitem__", 0)));
292+
PyTruffle_WriteOut(output_idx, int, as_int(polyglot_invoke(to_java(arg), "__getitem__", 0)));
311293
break;
312294
case 'f':
313295
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
314296
v->argnum++;
315297
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
316-
PyTruffle_WriteOutImmediate(output_idx, float, as_float(arg));
298+
PyTruffle_WriteOut(output_idx, float, as_float(arg));
317299
break;
318300
case 'd':
319301
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
320302
v->argnum++;
321303
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
322-
PyTruffle_WriteOutImmediate(output_idx, double, as_double(arg));
304+
PyTruffle_WriteOut(output_idx, double, as_double(arg));
323305
break;
324306
case 'D':
325307
PyErr_Format(PyExc_TypeError, "converting complex arguments not implemented, yet");
@@ -336,7 +318,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
336318
PyErr_Format(PyExc_TypeError, "expected object of type %R, got %R", typeobject, Py_TYPE(arg));
337319
return 0;
338320
}
339-
PyTruffle_WriteOut(output_idx, PyObject*, arg, rest_optional);
321+
PyTruffle_WriteOut(output_idx, PyObject*, arg);
340322
} else if (format[format_idx + 1] == '&') {
341323
format_idx++;
342324
void* (*converter)(PyObject*,void*) = PyTruffle_ArgN(output_idx);
@@ -353,14 +335,14 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
353335
return 0;
354336
}
355337
} else {
356-
PyTruffle_WriteOut(output_idx, PyObject*, arg, rest_optional);
338+
PyTruffle_WriteOut(output_idx, PyObject*, arg);
357339
}
358340
break;
359341
case 'p':
360342
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
361343
v->argnum++;
362344
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
363-
PyTruffle_WriteOutImmediate(output_idx, int, as_int(truffle_invoke(to_java(arg), "__bool__")));
345+
PyTruffle_WriteOut(output_idx, int, as_int(truffle_invoke(to_java(arg), "__bool__")));
364346
break;
365347
case '(':
366348
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);

0 commit comments

Comments
 (0)