Overloaded C++ static methods and Python
Jim Easterbrook <[email protected]> Thu, 23 Feb 2023 11:15:59 +0000
| Newsgroups | gmane.comp.programming.swig |
|---|---|
| Message-ID | <[email protected]> |
I've encountered an interesting problem with the SWIG generated Python
interface of overloaded C++ methods. This is my test case:
> %module overload_test
>
> class class_A {
> public:
> int method_1();
> int method_1(int x);
> int method_2();
> static int method_2(int x);
> };
The Python interface for method_1 is as you'd expect, but method_2()
becomes a static method requiring a class_A object to be passed as a
parameter. I'm running swig as follows:
swig -c++ -python -builtin -fastdispatch overload_test.i
The relevant parts of the wrapper file are
> (void)self;
> if (!(argc = SWIG_Python_UnpackTuple(args, "class_A_method_1", 0, 2, argv+1))) SWIG_fail;
> argv[0] = self;
> if (argc == 1) {
> PyObject *retobj = _wrap_class_A_method_1__SWIG_0(self, argc, argv);
> if (!SWIG_Python_TypeErrorOccurred(retobj)) return retobj;
> SWIG_fail;
> }
> if (argc == 2) {
> PyObject *retobj = _wrap_class_A_method_1__SWIG_1(self, argc, argv);
> if (!SWIG_Python_TypeErrorOccurred(retobj)) return retobj;
> SWIG_fail;
> }
and
> (void)self;
> if (!(argc = SWIG_Python_UnpackTuple(args, "class_A_method_2", 0, 1, argv))) SWIG_fail;
> --argc;
> if (argc == 1) {
> int _v = 0;
> {
> void *vptr = 0;
> int res = SWIG_ConvertPtr(argv[0], &vptr, SWIGTYPE_p_class_A, 0);
> _v = SWIG_CheckState(res);
> }
> if (!_v) goto check_1;
> return _wrap_class_A_method_2__SWIG_0(self, argc, argv);
> }
> check_1:
>
> if (argc == 1) {
> PyObject *retobj = _wrap_class_A_method_2__SWIG_1(self, argc, argv);
> if (!SWIG_Python_TypeErrorOccurred(retobj)) return retobj;
> SWIG_fail;
> }
Is this a known problem? Is there a workaround?
--
Jim Easterbrook <http://www.jim-easterbrook.me.uk/>