Problem with parameters wrapping C++->Python

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with parameters wrapping C++->Python

Arnaud Megret
Hi,

I am using Swig 3.0.2 under Debian to wrap C++ classes to Python.
As I am using an IDE (PyCharm)  that enables auto completion, I would appreciate that my Python module files (.py) generated by swig would define the methods with all their arguments.
But instead I got a single *args arguments for any methods  (additionally to the self).
So when I type the method in the IDE, it displays only that *args parameter which is useless.
I have read that there where some specific cases when one uses default parameters values or virtual méthods.
But it is not my case.

Here is a very simple example :

My testPy.h file :

#ifdef SWIG
%module testPy
%{
        #include "testPy.h"
%}
%feature("autodoc","0");
#endif //SWIG
class IntRange
{
public:
  bool contains(int value) const;
};

I wrap it using

swig3.0 -python -c++ testPy.h

and here is the .py file, I get :

# This file was automatically generated by SWIG (http://www.swig.org).
# Version 3.0.2
#
# Do not make changes to this file unless you know what you are doing--modify
# the SWIG interface file instead.





from sys import version_info
if version_info >= (2,6,0):
    def swig_import_helper():
        from os.path import dirname
        import imp
        fp = None
        try:
            fp, pathname, description = imp.find_module('_testPy', [dirname(__file__)])
        except ImportError:
            import _testPy
            return _testPy
        if fp is not None:
            try:
                _mod = imp.load_module('_testPy', fp, pathname, description)
            finally:
                fp.close()
            return _mod
    _testPy = swig_import_helper()
    del swig_import_helper
else:
    import _testPy
del version_info
try:
    _swig_property = property
except NameError:
    pass # Python < 2.2 doesn't have 'property'.
def _swig_setattr_nondynamic(self,class_type,name,value,static=1):
    if (name == "thisown"): return self.this.own(value)
    if (name == "this"):
        if type(value).__name__ == 'SwigPyObject':
            self.__dict__[name] = value
            return
    method = class_type.__swig_setmethods__.get(name,None)
    if method: return method(self,value)
    if (not static):
        self.__dict__[name] = value
    else:
        raise AttributeError("You cannot add attributes to %s" % self)

def _swig_setattr(self,class_type,name,value):
    return _swig_setattr_nondynamic(self,class_type,name,value,0)

def _swig_getattr(self,class_type,name):
    if (name == "thisown"): return self.this.own()
    method = class_type.__swig_getmethods__.get(name,None)
    if method: return method(self)
    raise AttributeError(name)

def _swig_repr(self):
    try: strthis = "proxy of " + self.this.__repr__()
    except: strthis = ""
    return "<%s.%s; %s >" % (self.__class__.__module__, self.__class__.__name__, strthis,)

try:
    _object = object
    _newclass = 1
except AttributeError:
    class _object : pass
    _newclass = 0


class IntRange(_object):
    """Proxy of C++ IntRange class"""
    __swig_setmethods__ = {}
    __setattr__ = lambda self, name, value: _swig_setattr(self, IntRange, name, value)
    __swig_getmethods__ = {}
    __getattr__ = lambda self, name: _swig_getattr(self, IntRange, name)
    __repr__ = _swig_repr
    def contains(self, *args):
        """contains(self, value) -> bool"""
        return _testPy.IntRange_contains(self, *args)

    def __init__(self):
        """__init__(self) -> IntRange"""
        this = _testPy.new_IntRange()
        try: self.this.append(this)
        except: self.this = this
    __swig_destroy__ = _testPy.delete_IntRange
    __del__ = lambda self : None;
IntRange_swigregister = _testPy.IntRange_swigregister
IntRange_swigregister(IntRange)

# This file is compatible with both classic and new-style classes.



The problem stands in the line : def contains(self, *args).
If my method has several parameters, I lost them and auto-completion is not helpful.
One strange thing is that other people with the same example, have a different output that correctly lists the details of the arguments in the def line.

How can I fixe that ?

Thank you a lot,

Arnaud








------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user
Reply | Threaded
Open this post in threaded view
|

Re: Problem with parameters wrapping C++->Python

William S Fulton
On 3 June 2016 at 13:06, Arnaud Megret <[hidden email]> wrote:

> Hi,
>
> I am using Swig 3.0.2 under Debian to wrap C++ classes to Python.
> As I am using an IDE (PyCharm)  that enables auto completion, I would
> appreciate that my Python module files (.py) generated by swig would define
> the methods with all their arguments.
> But instead I got a single *args arguments for any methods  (additionally to
> the self).
> So when I type the method in the IDE, it displays only that *args parameter
> which is useless.
> I have read that there where some specific cases when one uses default
> parameters values or virtual méthods.
> But it is not my case.
Later versions of SWIG (try 3.0.8) expand the *args where possible
into named parameters. Also try the autodoc documentation feature,
maybe PyCharm will use be able to parse the doc comments?

William

------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are
consuming the most bandwidth. Provides multi-vendor support for NetFlow,
J-Flow, sFlow and other flows. Make informed decisions using capacity
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Swig-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/swig-user