Skip to content

Commit 52a01c8

Browse files
committed
Implement C API function PyObject_CallMethodObjArgs.
1 parent 9f5b4b8 commit 52a01c8

File tree

5 files changed

+27
-5
lines changed

5 files changed

+27
-5
lines changed

graalpython/com.oracle.graal.python.cext/src/capi.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ extern PyObject* wrapped_null;
331331

332332
/* STR */
333333
__attribute__((always_inline)) PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va, void **args, int argc);
334+
__attribute__((always_inline)) PyObject* PyTruffle_Tuple_Pack(int dummy, va_list va, void **args, int argc);
334335

335336
/* BYTES, BYTEARRAY */
336337
int bytes_buffer_getbuffer(PyBytesObject *self, Py_buffer *view, int flags);
@@ -361,7 +362,7 @@ int bufferdecorator_getbuffer(PyBufferDecorator *self, Py_buffer *view, int flag
361362
for (int i = off; i < __poly_argc; i++) { \
362363
__poly_args[i - off] = polyglot_get_arg(i); \
363364
} \
364-
result = function(__VA_ARGS__, __va_list, __poly_args, 0)
365+
result = function(__VA_ARGS__, __va_list, __poly_args, __poly_argc)
365366
#else
366367
/*
367368
* (tfel): Just skip the optimization with using a managed malloc and use

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,9 @@ PyObject* PyTruffle_GetArg(positional_argstack* p, PyObject* kwds, char** kwdnam
102102

103103
/* argparse */
104104
UPCALL_ID(__bool__);
105-
MUST_INLINE static int _PyTruffleArg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const char *format, char **kwdnames, va_list va, void** poly_args, int offset) {
105+
MUST_INLINE static int _PyTruffleArg_ParseTupleAndKeywords(PyObject *argv, PyObject *kwds, const char *format, char **kwdnames, va_list va, void** poly_args, int argc) {
106106
PyObject* arg;
107+
int offset = 0;
107108
int format_idx = 0;
108109
unsigned char rest_optional = 0;
109110
unsigned char rest_keywords_only = 0;
@@ -485,8 +486,9 @@ typedef struct _build_stack {
485486
struct _build_stack* prev;
486487
} build_stack;
487488

488-
MUST_INLINE static PyObject* _PyTruffle_BuildValue(const char* format, va_list va, void** poly_args, int offset) {
489+
MUST_INLINE static PyObject* _PyTruffle_BuildValue(const char* format, va_list va, void** poly_args, int argc) {
489490
PyObject* (*converter)(void*) = NULL;
491+
int offset = 0;
490492
char argchar[2] = {'\0'};
491493
unsigned int format_idx = 0;
492494
build_stack *v = (build_stack*)calloc(1, sizeof(build_stack));

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ PyObject* PyObject_CallFunctionObjArgs(PyObject *callable, ...) {
274274
}
275275

276276
UPCALL_ID(PyObject_CallMethod);
277+
NO_INLINE
277278
PyObject* PyObject_CallMethod(PyObject* object, const char* method, const char* fmt, ...) {
278279
PyObject* args;
279280
if (fmt == NULL || fmt[0] == '\0') {
@@ -284,6 +285,14 @@ PyObject* PyObject_CallMethod(PyObject* object, const char* method, const char*
284285
return UPCALL_CEXT_O(_jls_PyObject_CallMethod, native_to_java(object), polyglot_from_string(method, SRC_CS), native_to_java(args));
285286
}
286287

288+
NO_INLINE
289+
PyObject* PyObject_CallMethodObjArgs(PyObject *callable, PyObject *name, ...) {
290+
PyObject* args;
291+
CallWithPolyglotArgs(args, name, 2, PyTruffle_Tuple_Pack, 0);
292+
return UPCALL_CEXT_O(_jls_PyObject_CallMethod, native_to_java(callable), native_to_java(name), native_to_java(args));
293+
}
294+
295+
NO_INLINE
287296
PyObject* _PyObject_CallMethod_SizeT(PyObject* object, const char* method, const char* fmt, ...) {
288297
PyObject* args;
289298
if (fmt == NULL || fmt[0] == '\0') {

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ PyObject* PyTuple_Pack(Py_ssize_t n, ...) {
8585
return result;
8686
}
8787

88+
MUST_INLINE
89+
PyObject* PyTruffle_Tuple_Pack(int dummy, va_list vlist, void **argv, int argc) {
90+
PyObject *result = PyTuple_New(argc);
91+
for (int i = 0; i < argc; i++) {
92+
PyTuple_SetItem(result, i, (PyObject*) argv[i]);
93+
}
94+
return result;
95+
}
96+
8897
MUST_INLINE
8998
static PyObject * tuple_create(PyObject *iterable) {
9099
if (iterable == NULL) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ MUST_INLINE PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va,
132132
char* fmtcpy = strdup(fmt);
133133
char* c = fmtcpy;
134134
int use_valist = args == NULL;
135+
int pos = 0;
135136

136137
int remaining_space = 2047;
137138
char* buffer = (char*)calloc(sizeof(char), remaining_space + 1);
@@ -177,7 +178,7 @@ MUST_INLINE PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va,
177178
case 'R':
178179
if (converter == NULL) converter = PyObject_Repr;
179180
c[1] = 's';
180-
allocated = variable = as_char_pointer(converter(use_valist ? va_arg(va, PyObject*) : (PyObject*)(args[argc++])));
181+
allocated = variable = as_char_pointer(converter(use_valist ? va_arg(va, PyObject*) : (PyObject*)(args[pos++])));
181182
break;
182183
case '%':
183184
// literal %
@@ -189,7 +190,7 @@ MUST_INLINE PyObject* PyTruffle_Unicode_FromFormat(const char *fmt, va_list va,
189190
// if we're reading args from a void* array, read it now,
190191
// otherwise there's nothing to do
191192
if (args != NULL) {
192-
variable = args[argc++];
193+
variable = args[pos++];
193194
}
194195
}
195196
// skip over next char, we checked it

0 commit comments

Comments
 (0)