@@ -58,14 +58,7 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
58
58
const char * kwdname = kwdnames [p -> argnum ];
59
59
if (kwdname != NULL ) {
60
60
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 ;
69
62
}
70
63
}
71
64
return NULL ;
@@ -121,6 +114,12 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
121
114
122
115
#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))))))))))))))))))))
123
116
117
+ #define PyTruffle_SkipOptionalArg (n , arg , optional ) \
118
+ if (arg == NULL && optional) { \
119
+ n++; \
120
+ break; \
121
+ }
122
+
124
123
/* argparse */
125
124
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 ) {
126
125
PyObject * arg ;
@@ -141,6 +140,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
141
140
case 'z' :
142
141
case 'y' :
143
142
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
143
+ v -> argnum ++ ;
144
144
if (format [format_idx + 1 ] == '*' ) {
145
145
format_idx ++ ; // skip over '*'
146
146
PyErr_Format (PyExc_TypeError , "%c* not supported" , c );
@@ -157,6 +157,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
157
157
return 0 ;
158
158
}
159
159
} else {
160
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
160
161
PyTruffle_WriteOut (output_idx , const char * , as_char_pointer (arg ), rest_optional );
161
162
if (format [format_idx + 1 ] == '#' ) {
162
163
format_idx ++ ;
@@ -166,6 +167,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
166
167
break ;
167
168
case 'S' :
168
169
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
170
+ v -> argnum ++ ;
171
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
169
172
if (!PyBytes_Check (arg )) {
170
173
PyErr_Format (PyExc_TypeError , "expected bytes, got %R" , Py_TYPE (arg ));
171
174
return 0 ;
@@ -174,6 +177,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
174
177
break ;
175
178
case 'Y' :
176
179
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
180
+ v -> argnum ++ ;
181
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
177
182
if (!PyByteArray_Check (arg )) {
178
183
PyErr_Format (PyExc_TypeError , "expected bytearray, got %R" , Py_TYPE (arg ));
179
184
return 0 ;
@@ -186,6 +191,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
186
191
return 0 ;
187
192
case 'U' :
188
193
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
194
+ v -> argnum ++ ;
195
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
189
196
if (!PyUnicode_Check (arg )) {
190
197
PyErr_Format (PyExc_TypeError , "expected str, got %R" , Py_TYPE (arg ));
191
198
return 0 ;
@@ -208,43 +215,59 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
208
215
return 0 ;
209
216
case 'b' :
210
217
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
218
+ v -> argnum ++ ;
219
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
211
220
if (_PyLong_Sign (arg ) < 0 ) {
212
221
PyErr_Format (PyExc_TypeError , "expected non-negative integer" );
213
222
return 0 ;
214
223
}
215
224
PyTruffle_WriteOutImmediate (output_idx , unsigned char , as_uchar (arg ));
216
225
break ;
217
226
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 ));
220
231
break ;
221
232
case 'h' :
222
233
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
234
+ v -> argnum ++ ;
235
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
223
236
if (_PyLong_Sign (arg ) < 0 ) {
224
237
PyErr_Format (PyExc_TypeError , "expected non-negative integer" );
225
238
return 0 ;
226
239
}
227
240
PyTruffle_WriteOutImmediate (output_idx , short int , as_short (arg ));
228
241
break ;
229
242
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 ));
232
247
break ;
233
248
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 ));
236
253
break ;
237
254
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 ));
240
259
break ;
241
260
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 ));
244
265
break ;
245
266
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 ));
248
271
break ;
249
272
case 'L' :
250
273
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
253
276
PyErr_Format (PyExc_TypeError , "long long argument parsing not yet supported" );
254
277
return 0 ;
255
278
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 ));
258
283
break ;
259
284
case 'c' :
260
285
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
286
+ v -> argnum ++ ;
287
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
261
288
if (!(PyBytes_Check (arg ) || PyByteArray_Check (arg ))) {
262
289
PyErr_Format (PyExc_TypeError , "expted bytes or bytearray, got %R" , Py_TYPE (arg ));
263
290
return 0 ;
@@ -270,6 +297,8 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
270
297
break ;
271
298
case 'C' :
272
299
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
300
+ v -> argnum ++ ;
301
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
273
302
if (!PyUnicode_Check (arg )) {
274
303
PyErr_Format (PyExc_TypeError , "expted bytes or bytearray, got %R" , Py_TYPE (arg ));
275
304
return 0 ;
@@ -281,22 +310,28 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
281
310
PyTruffle_WriteOutImmediate (output_idx , int , as_int (polyglot_invoke (to_java (arg ), "__getitem__" , 0 )));
282
311
break ;
283
312
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 ));
286
317
break ;
287
318
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 ));
290
323
break ;
291
324
case 'D' :
292
325
PyErr_Format (PyExc_TypeError , "converting complex arguments not implemented, yet" );
293
326
return 0 ;
294
327
case 'O' :
295
328
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
329
+ v -> argnum ++ ;
296
330
if (format [format_idx + 1 ] == '!' ) {
297
331
format_idx ++ ;
298
332
PyTypeObject * typeobject = (PyTypeObject * )PyTruffle_ArgN (output_idx );
299
333
output_idx ++ ;
334
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
300
335
if (!PyType_IsSubtype (Py_TYPE (arg ), typeobject )) {
301
336
PyErr_Format (PyExc_TypeError , "expected object of type %R, got %R" , typeobject , Py_TYPE (arg ));
302
337
return 0 ;
@@ -308,6 +343,7 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
308
343
output_idx ++ ;
309
344
void * output = PyTruffle_ArgN (output_idx );
310
345
output_idx ++ ;
346
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
311
347
int status = converter (arg , output );
312
348
if (!status ) {
313
349
if (!PyErr_Occurred ()) {
@@ -322,10 +358,14 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
322
358
break ;
323
359
case 'p' :
324
360
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
361
+ v -> argnum ++ ;
362
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
325
363
PyTruffle_WriteOutImmediate (output_idx , int , as_int (truffle_invoke (to_java (arg ), "__bool__" )));
326
364
break ;
327
365
case '(' :
328
366
arg = PyTruffle_GetArg (v , kwds , kwdnames , rest_keywords_only );
367
+ v -> argnum ++ ;
368
+ PyTruffle_SkipOptionalArg (output_idx , arg , rest_optional );
329
369
if (!PyTuple_Check (arg )) {
330
370
PyErr_Format (PyExc_TypeError , "expected tuple, got %R" , Py_TYPE (arg ));
331
371
return 0 ;
@@ -361,7 +401,6 @@ int PyTruffle_Arg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const ch
361
401
PyErr_Format (PyExc_TypeError , "unrecognized format char in arguments parsing: %c" , c );
362
402
}
363
403
c = format [++ format_idx ];
364
- v -> argnum ++ ;
365
404
}
366
405
367
406
free (v );
0 commit comments