Skip to content

Commit 58ab6ec

Browse files
committed
add some tests for new format arguments
1 parent 7510fd6 commit 58ab6ec

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

graalpython/com.oracle.graal.python.test/src/tests/cpyext/test_modsupport.py

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,131 @@ def compile_module(self, name):
394394
cmpfunc=unhandled_error_compare
395395
)
396396

397+
test_parseargs_y = CPyExtFunction(
398+
lambda args: args[0][0].decode(),
399+
lambda: (
400+
((b'', ), ),
401+
((b'helloworld', ), ),
402+
),
403+
code='''
404+
static const char * wrap_PyArg_ParseTuple(PyObject* argTuple) {
405+
const char *out = NULL;
406+
Py_INCREF(argTuple);
407+
if (PyArg_ParseTuple(argTuple, "y", &out) == 0) {
408+
return NULL;
409+
}
410+
Py_DECREF(argTuple);
411+
return out;
412+
}
413+
''',
414+
resultspec="s",
415+
argspec="O",
416+
arguments=["PyObject* argTuple"],
417+
callfunction="wrap_PyArg_ParseTuple",
418+
cmpfunc=unhandled_error_compare
419+
)
420+
421+
test_parseargs_y_hash = CPyExtFunction(
422+
lambda args: (args[0][0].decode(), len(args[0][0])),
423+
lambda: (
424+
((b'', ), ),
425+
((b'helloworld', ), ),
426+
),
427+
code='''
428+
static PyObject * wrap_PyArg_ParseTuple(PyObject* argTuple) {
429+
const char *out = NULL;
430+
Py_ssize_t cnt = 0;
431+
Py_INCREF(argTuple);
432+
if (PyArg_ParseTuple(argTuple, "y#", &out, &cnt) == 0) {
433+
return NULL;
434+
}
435+
Py_DECREF(argTuple);
436+
return Py_BuildValue("si", out, cnt);
437+
}
438+
''',
439+
resultspec="O",
440+
argspec="O",
441+
arguments=["PyObject* argTuple"],
442+
callfunction="wrap_PyArg_ParseTuple",
443+
cmpfunc=unhandled_error_compare
444+
)
445+
446+
test_parseargs_y_star = CPyExtFunction(
447+
lambda args: (args[0][0].decode(), len(args[0][0])),
448+
lambda: (
449+
((b'', ), ),
450+
((b'helloworld', ), ),
451+
),
452+
code='''
453+
static PyObject * wrap_PyArg_ParseTuple(PyObject* argTuple) {
454+
Py_buffer view;
455+
Py_INCREF(argTuple);
456+
if (PyArg_ParseTuple(argTuple, "y*", &view) == 0) {
457+
return NULL;
458+
}
459+
Py_DECREF(argTuple);
460+
PyObject *result = Py_BuildValue("si", (char*)view.buf, view.len);
461+
PyBuffer_Release(&view);
462+
return result;
463+
}
464+
''',
465+
resultspec="O",
466+
argspec="O",
467+
arguments=["PyObject* argTuple"],
468+
callfunction="wrap_PyArg_ParseTuple",
469+
cmpfunc=unhandled_error_compare
470+
)
471+
472+
test_parseargs_es = CPyExtFunction(
473+
lambda args: args[0][0].decode() if isinstance(args[0][0], bytes) else args[0][0],
474+
lambda: (
475+
(('helloworld', ), ),
476+
),
477+
code='''
478+
static PyObject * wrap_PyArg_ParseTuple(PyObject* argTuple) {
479+
const char * out;
480+
Py_INCREF(argTuple);
481+
if (PyArg_ParseTuple(argTuple, "es", "UTF-8", &out) == 0) {
482+
return NULL;
483+
}
484+
Py_DECREF(argTuple);
485+
PyObject *result = PyUnicode_FromString(out);
486+
PyMem_Free(out);
487+
return result;
488+
}
489+
''',
490+
resultspec="O",
491+
argspec="O",
492+
arguments=["PyObject* argTuple"],
493+
callfunction="wrap_PyArg_ParseTuple",
494+
cmpfunc=unhandled_error_compare
495+
)
496+
497+
test_parseargs_et = CPyExtFunction(
498+
lambda args: args[0][0].decode() if isinstance(args[0][0], bytes) else args[0][0],
499+
lambda: (
500+
(('helloworld', ), ),
501+
),
502+
code='''
503+
static PyObject * wrap_PyArg_ParseTuple(PyObject* argTuple) {
504+
const char * out;
505+
Py_INCREF(argTuple);
506+
if (PyArg_ParseTuple(argTuple, "es", "UTF-8", &out) == 0) {
507+
return NULL;
508+
}
509+
Py_DECREF(argTuple);
510+
PyObject *result = PyUnicode_FromString(out);
511+
PyMem_Free(out);
512+
return result;
513+
}
514+
''',
515+
resultspec="O",
516+
argspec="O",
517+
arguments=["PyObject* argTuple"],
518+
callfunction="wrap_PyArg_ParseTuple",
519+
cmpfunc=unhandled_error_compare
520+
)
521+
397522
test_parseargs_valist = CPyExtFunction(
398523
lambda args: args[0],
399524
lambda: (

0 commit comments

Comments
 (0)