Menu

#1100 [Python] overloading dispatch function error msg incorrect

closed-fixed
nobody
python (260)
5
2010-09-27
2010-07-09
No

If a C++ function with multiple signatures is wrapped, the dispatch function has only one error message, namely " Wrong number of arguments for overloaded function 'f'.", which is displayed not only when the number of arguments is in fact wrong but also if one of the type checks failed.

*** Example: ***

--8<---- test.i ---
%module test

%{
void f( int a = 0 ) {};
%}

void f( int a = 0 );
---8<--------------

I wrap this with 'swig -python -c++ test.i' and test it in Python:

---8<--------
>>> import test
>>> test.f( 3 )
>>> test.f( 3, 5 )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: Wrong number of arguments for overloaded function 'f'.
Possible C/C++ prototypes are:
f(int)
f()

>>> test.f( "foo" )
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NotImplementedError: Wrong number of arguments for overloaded function 'f'.
Possible C/C++ prototypes are:
f(int)
f()
---8<---

In the later case, the error message should read " Argument one is of incorrect type." or something like that.

*** Version: ****

I am using Cython 0.12.1 on Ubuntu.

*** Suggested Fix: ***

A quick fix would be to reword the error message to "Wrong number or type of arguments for overloaded function 'f'." As the signatures are then listed, that would do it.

Here is the generated dispatch function. As one can see, it is easy to generate a more informative message by adding an "else" clause to the "if( _v )" statement.

--8<--------
SWIGINTERN PyObject *_wrap_f(PyObject *self, PyObject *args) {
int argc;
PyObject *argv[2];
int ii;

if (!PyTuple_Check(args)) SWIG_fail;
argc = (int)PyObject_Length(args);
for (ii = 0; (ii < argc) && (ii < 1); ii++) {
argv[ii] = PyTuple_GET_ITEM(args,ii);
}
if (argc == 0) {
return _wrap_f__SWIG_1(self, args);
}
if (argc == 1) {
int _v;
{
int res = SWIG_AsVal_int(argv[0], NULL);
_v = SWIG_CheckState(res);
}
if (_v) {
return _wrap_f__SWIG_0(self, args);
}
}

fail:
SWIG_SetErrorMsg(PyExc_NotImplementedError,"Wrong number of arguments for overloaded function 'f'.\n"
" Possible C/C++ prototypes are:\n"
" f(int)\n"
" f()\n");
return NULL;
}
---8<---

Discussion

  • Olly Betts

    Olly Betts - 2010-09-27

    I improve the message as suggested in trunk r12229, so this should appear in SWIG 2.0.1.

     
  • Olly Betts

    Olly Betts - 2010-09-27
    • summary: overloading dispatch function gives incorrect error message --> [Python] overloading dispatch function error msg incorrect
    • labels: 505206 --> python
    • status: open --> closed-fixed
     

Log in to post a comment.