diff --git a/pybindgen/cppclass.py b/pybindgen/cppclass.py index e192192..7d114fb 100644 --- a/pybindgen/cppclass.py +++ b/pybindgen/cppclass.py @@ -1712,13 +1712,18 @@ typedef struct { def try_wrap_sequence_method(py_name, slot_name): if py_name in self.methods: - assert len(self.methods[py_name].wrappers) == 1 - meth = self.methods[py_name].wrappers[0] + numwraps = len(self.methods[py_name].wrappers) + some_wrapper_is_function = max([isinstance(x, function.Function) for x in self.methods[py_name].wrappers]) + meth_wrapper_actual_name = self.methods[py_name].wrapper_actual_name wrapper_name = "%s__%s" % (self.mangled_full_name, slot_name) pysequencemethods.slots[slot_name] = wrapper_name - code_sink.writeln(pysequencemethods.FUNCTION_TEMPLATES[slot_name] % {'wrapper_name' : wrapper_name, - 'py_struct' : self._pystruct, - 'method_name' : meth.wrapper_actual_name}) + if py_name == "__len__" and (numwraps > 1 or some_wrapper_is_function): + template = pysequencemethods.FUNCTION_TEMPLATES[slot_name + "_ARGS"] + else: + template = pysequencemethods.FUNCTION_TEMPLATES[slot_name] + code_sink.writeln(template % {'wrapper_name' : wrapper_name, + 'py_struct' : self._pystruct, + 'method_name' : meth_wrapper_actual_name}) return for (py_name, slot_name) in [("__len__", "sq_length"), diff --git a/pybindgen/pytypeobject.py b/pybindgen/pytypeobject.py index 4ab4080..cc6b49d 100644 --- a/pybindgen/pytypeobject.py +++ b/pybindgen/pytypeobject.py @@ -244,6 +244,28 @@ static Py_ssize_t return result; } +''', + + # This hacky version is necessary 'cause if we're calling a function rather than a method + # or an overloaded wrapper the args parameter gets tacked into the call sequence. + "sq_length_ARGS" : ''' +static Py_ssize_t +%(wrapper_name)s (%(py_struct)s *py_self) +{ + PyObject *py_result; + PyObject *args; + + Py_ssize_t result; + + py_result = %(method_name)s(py_self, args, NULL); + if (py_result == NULL) { + return -1; + } + result = PyInt_AsSsize_t(py_result); + Py_DECREF(py_result); + return result; +} + ''', "sq_item" : '''