Skip to content

Commit 6a2319c

Browse files
committed
Use special wrapper for native 'nb_power' functions.
1 parent b0a25ec commit 6a2319c

File tree

1 file changed

+15
-1
lines changed
  • graalpython/com.oracle.graal.python.cext/src

1 file changed

+15
-1
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,20 @@ static PyObject* wrap_ssizeobjargproc(ssizeobjargproc f, PyObject* a, PyObject*
197197
return PyLong_FromLong(f(explicit_cast(a), PyLong_AsSsize_t(size), explicit_cast(b)));
198198
}
199199

200+
/* very special case: operator '**' has an optional third arg */
201+
static PyObject* wrap_pow(ternaryfunc f, ...) {
202+
int nargs = polyglot_get_arg_count();
203+
switch(nargs) {
204+
case 3:
205+
// TODO use 'native_to_java' on result
206+
return f(explicit_cast(polyglot_get_arg(1)), explicit_cast(polyglot_get_arg(2)), Py_None);
207+
case 4:
208+
// TODO use 'native_to_java' on result
209+
return f(explicit_cast(polyglot_get_arg(1)), explicit_cast(polyglot_get_arg(2)), explicit_cast(polyglot_get_arg(3)));
210+
}
211+
return native_to_java(NULL);
212+
}
213+
200214
int PyType_Ready(PyTypeObject* cls) {
201215
#define ADD_IF_MISSING(attr, def) if (!(attr)) { attr = def; }
202216
#define ADD_METHOD(m) ADD_METHOD_OR_SLOT(m.ml_name, get_method_flags_cwrapper(m.ml_flags), m.ml_meth, m.ml_flags, m.ml_doc)
@@ -382,7 +396,7 @@ int PyType_Ready(PyTypeObject* cls) {
382396
ADD_SLOT("__mul__", numbers->nb_multiply, -2);
383397
ADD_SLOT("__rem__", numbers->nb_remainder, -2);
384398
ADD_SLOT("__divmod__", numbers->nb_divmod, -2);
385-
ADD_SLOT("__pow__", numbers->nb_power, -2);
399+
ADD_SLOT_CONV("__pow__", wrap_pow, numbers->nb_power, -3);
386400
ADD_SLOT("__neg__", numbers->nb_negative, -1);
387401
ADD_SLOT("__pos__", numbers->nb_positive, -1);
388402
ADD_SLOT("__abs__", numbers->nb_absolute, -1);

0 commit comments

Comments
 (0)