0

I am using the Eric IDE for Python. It is using autocompletion and it should be very useful. But, we develop python scripts that use objects from a C++ library that we convert using Swig. Unfortunately, swig create a .py file that maps each C++ object's method replacing all arguments with an *args unique argument and so Eric's autocompletion is not as useful as it may be as it does not display the list of parameters but only that args which is useless.

For instance, I have a C++ .h file :

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

I generate the .py file by :

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

and the generated .py file contains :

  def contains(self, *args):
    """contains(self, value) -> bool"""
    return _testPy.IntRange_contains(self, *args)

So the autodoc part is fine. Unfortunately, it is not used by Eric and the arguments of the method have been replaced by *args.

Is there a way to ask swig to keep the arguments' names?

N.B : I am using Python 2.7 and swig 3.0.2

1
  • @Ashish please don't use inline code markup for anything other than code. Commented Jun 3, 2016 at 6:25

2 Answers 2

1

According the the Swig Changelog, Python named parameters are available from version 3.0.3.

One thing that may make autocompletion more intelligent is to have docstrings generated. I am not familiar with Eric so I don't know if it uses these in autocompletion, but some editors do and show you original type information if that information is in the doc string of a method. You enable this by setting the autodoc feature:

%feature("autodoc", "0");

The number can go up to 3 and determines how verbose/informative the docstrings are. For instance, with level three, the following C++ method:

class IntRange
{
public:
  bool contains(int value) const;
};

results in the following Python code:

def contains(self, value):
    """
    contains(IntRange self, int value) -> bool

    Parameters
    ----------
    value: int

    """
    return _foo.IntRange_contains(self, value)
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. I tried it but it added and extra kwargs parameters in the .py file : def construireFormatGrid(self, *args, **kwargs): return _reseauEntropiquePy.ReseauEntropiquePy_construireFormatGrid(self, *args, **kwargs)
You are right, I had %feature("kwargs") set and had misread an email thread on the Swig mailing list which made me mistakenly remember that was it. I removed that part of my answer.
I tried your simple example (I edited my question with it) and still got he *args replacement. I migrated from swig 2.?? to 3.0.2. Where do you thing stand the difference with your test ? compilation command line, difference between swig versions (I use Debian and cannot get easily more recent) or the fact that I am under Linux OS ...
With exactly your code in the edit of your question I get def contains(self, value):, really not sure what's the difference.. Which exact version of Swig 3 do you have? check by running swig3.0 -version.
Seems like you have to upgrade just one patch version further: according to github.com/swig/swig/blob/master/CHANGES#L772-L778 you get named arguments starting from 3.0.3.
|
0

I just tried your example (with just this one function), and for me SWIG does preserve the argument names (SWIG 3.0.9).

However, if your function is an overloaded function, then SWIG will always create a wrapper taking *args as you describe, and perform the dispatching in C++.

Using the "autodoc" feature is indeed very useful in this case, since the method signature (with argument names) is placed into the docstring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.