Skip to content

Commit d489d7d

Browse files
timfelfangerer
authored andcommitted
further refactoring
1 parent b7654f8 commit d489d7d

File tree

1 file changed

+66
-27
lines changed

1 file changed

+66
-27
lines changed

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

Lines changed: 66 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,7 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
5858
const char* kwdname = kwdnames[p->argnum];
5959
if (kwdname != NULL) {
6060
void* kwarg = PyDict_GetItem(kwds, to_sulong(truffle_read_string(kwdname)));
61-
if (kwarg == NULL) {
62-
if (PyErr_Occurred()) {
63-
return NULL;
64-
}
65-
return Py_None;
66-
} else {
67-
return kwarg;
68-
}
61+
return kwarg;
6962
}
7063
}
7164
return NULL;
@@ -121,6 +114,12 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
121114

122115
#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))))))))))))))))))))
123116

117+
#define PyTruffle_SkipOptionalArg(n, arg, optional) \
118+
if (arg == NULL && optional) { \
119+
n++; \
120+
break; \
121+
}
122+
124123
/* argparse */
125124
int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const char *format, char** kwdnames, int outc, void *v0, void *v1, void *v2, void *v3, void *v4, void *v5, void *v6, void *v7, void *v8, void *v9, void *v10, void *v11, void *v12, void *v13, void *v14, void *v15, void *v16, void *v17, void *v18, void *v19) {
126125
PyObject* arg;
@@ -141,6 +140,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
141140
case 'z':
142141
case 'y':
143142
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
143+
v->argnum++;
144144
if (format[format_idx + 1] == '*') {
145145
format_idx++; // skip over '*'
146146
PyErr_Format(PyExc_TypeError, "%c* not supported", c);
@@ -157,6 +157,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
157157
return 0;
158158
}
159159
} else {
160+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
160161
PyTruffle_WriteOut(output_idx, const char*, as_char_pointer(arg), rest_optional);
161162
if (format[format_idx + 1] == '#') {
162163
format_idx++;
@@ -166,6 +167,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
166167
break;
167168
case 'S':
168169
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
170+
v->argnum++;
171+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
169172
if (!PyBytes_Check(arg)) {
170173
PyErr_Format(PyExc_TypeError, "expected bytes, got %R", Py_TYPE(arg));
171174
return 0;
@@ -174,6 +177,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
174177
break;
175178
case 'Y':
176179
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
180+
v->argnum++;
181+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
177182
if (!PyByteArray_Check(arg)) {
178183
PyErr_Format(PyExc_TypeError, "expected bytearray, got %R", Py_TYPE(arg));
179184
return 0;
@@ -186,6 +191,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
186191
return 0;
187192
case 'U':
188193
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
194+
v->argnum++;
195+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
189196
if (!PyUnicode_Check(arg)) {
190197
PyErr_Format(PyExc_TypeError, "expected str, got %R", Py_TYPE(arg));
191198
return 0;
@@ -208,43 +215,59 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
208215
return 0;
209216
case 'b':
210217
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
218+
v->argnum++;
219+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
211220
if (_PyLong_Sign(arg) < 0) {
212221
PyErr_Format(PyExc_TypeError, "expected non-negative integer");
213222
return 0;
214223
}
215224
PyTruffle_WriteOutImmediate(output_idx, unsigned char, as_uchar(arg));
216225
break;
217226
case 'B':
218-
PyTruffle_WriteOutImmediate(output_idx, unsigned char,
219-
as_uchar(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
227+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
228+
v->argnum++;
229+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
230+
PyTruffle_WriteOutImmediate(output_idx, unsigned char, as_uchar(arg));
220231
break;
221232
case 'h':
222233
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
234+
v->argnum++;
235+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
223236
if (_PyLong_Sign(arg) < 0) {
224237
PyErr_Format(PyExc_TypeError, "expected non-negative integer");
225238
return 0;
226239
}
227240
PyTruffle_WriteOutImmediate(output_idx, short int, as_short(arg));
228241
break;
229242
case 'H':
230-
PyTruffle_WriteOutImmediate(output_idx, short int,
231-
as_short(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
243+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
244+
v->argnum++;
245+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
246+
PyTruffle_WriteOutImmediate(output_idx, short int, as_short(arg));
232247
break;
233248
case 'i':
234-
PyTruffle_WriteOutImmediate(output_idx, int,
235-
as_int(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
249+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
250+
v->argnum++;
251+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
252+
PyTruffle_WriteOutImmediate(output_idx, int, as_int(arg));
236253
break;
237254
case 'I':
238-
PyTruffle_WriteOutImmediate(output_idx, unsigned int,
239-
as_int(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
255+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
256+
v->argnum++;
257+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
258+
PyTruffle_WriteOutImmediate(output_idx, unsigned int, as_int(arg));
240259
break;
241260
case 'l':
242-
PyTruffle_WriteOutImmediate(output_idx, long,
243-
as_long(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
261+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
262+
v->argnum++;
263+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
264+
PyTruffle_WriteOutImmediate(output_idx, long, as_long(arg));
244265
break;
245266
case 'k':
246-
PyTruffle_WriteOutImmediate(output_idx, unsigned long,
247-
as_long(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
267+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
268+
v->argnum++;
269+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
270+
PyTruffle_WriteOutImmediate(output_idx, unsigned long, as_long(arg));
248271
break;
249272
case 'L':
250273
PyErr_Format(PyExc_TypeError, "long long argument parsing not yet supported");
@@ -253,11 +276,15 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
253276
PyErr_Format(PyExc_TypeError, "long long argument parsing not yet supported");
254277
return 0;
255278
case 'n':
256-
PyTruffle_WriteOutImmediate(output_idx, Py_ssize_t,
257-
as_long(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
279+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
280+
v->argnum++;
281+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
282+
PyTruffle_WriteOutImmediate(output_idx, Py_ssize_t, as_long(arg));
258283
break;
259284
case 'c':
260285
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
286+
v->argnum++;
287+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
261288
if (!(PyBytes_Check(arg) || PyByteArray_Check(arg))) {
262289
PyErr_Format(PyExc_TypeError, "expted bytes or bytearray, got %R", Py_TYPE(arg));
263290
return 0;
@@ -270,6 +297,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
270297
break;
271298
case 'C':
272299
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
300+
v->argnum++;
301+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
273302
if (!PyUnicode_Check(arg)) {
274303
PyErr_Format(PyExc_TypeError, "expted bytes or bytearray, got %R", Py_TYPE(arg));
275304
return 0;
@@ -281,22 +310,28 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
281310
PyTruffle_WriteOutImmediate(output_idx, int, as_int(polyglot_invoke(to_java(arg), "__getitem__", 0)));
282311
break;
283312
case 'f':
284-
PyTruffle_WriteOutImmediate(output_idx, float,
285-
as_float(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
313+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
314+
v->argnum++;
315+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
316+
PyTruffle_WriteOutImmediate(output_idx, float, as_float(arg));
286317
break;
287318
case 'd':
288-
PyTruffle_WriteOutImmediate(output_idx, double,
289-
as_double(PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only)));
319+
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
320+
v->argnum++;
321+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
322+
PyTruffle_WriteOutImmediate(output_idx, double, as_double(arg));
290323
break;
291324
case 'D':
292325
PyErr_Format(PyExc_TypeError, "converting complex arguments not implemented, yet");
293326
return 0;
294327
case 'O':
295328
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
329+
v->argnum++;
296330
if (format[format_idx + 1] == '!') {
297331
format_idx++;
298332
PyTypeObject* typeobject = (PyTypeObject*)PyTruffle_ArgN(output_idx);
299333
output_idx++;
334+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
300335
if (!PyType_IsSubtype(Py_TYPE(arg), typeobject)) {
301336
PyErr_Format(PyExc_TypeError, "expected object of type %R, got %R", typeobject, Py_TYPE(arg));
302337
return 0;
@@ -308,6 +343,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
308343
output_idx++;
309344
void* output = PyTruffle_ArgN(output_idx);
310345
output_idx++;
346+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
311347
int status = converter(arg, output);
312348
if (!status) {
313349
if (!PyErr_Occurred()) {
@@ -322,10 +358,14 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
322358
break;
323359
case 'p':
324360
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
361+
v->argnum++;
362+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
325363
PyTruffle_WriteOutImmediate(output_idx, int, as_int(truffle_invoke(to_java(arg), "__bool__")));
326364
break;
327365
case '(':
328366
arg = PyTruffle_GetArg(v, kwds, kwdnames, rest_keywords_only);
367+
v->argnum++;
368+
PyTruffle_SkipOptionalArg(output_idx, arg, rest_optional);
329369
if (!PyTuple_Check(arg)) {
330370
PyErr_Format(PyExc_TypeError, "expected tuple, got %R", Py_TYPE(arg));
331371
return 0;
@@ -361,7 +401,6 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
361401
PyErr_Format(PyExc_TypeError, "unrecognized format char in arguments parsing: %c", c);
362402
}
363403
c = format[++format_idx];
364-
v->argnum++;
365404
}
366405

367406
free(v);

0 commit comments

Comments
 (0)