Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#326 Python: remove SwigPyObject_print and _str

None
closed
general (37)
5
2013-08-07
2012-09-19
Kris Thielemans
No

Hi

this patch (effective only when using the -builtin option) removes SwigPyObject_print and SwigPyObject_str, and makes the generated wrapper use the default python implementations, which will fall back to repr.

Advantages:
- it avoids the swig user having to jump through hoops to get print to work as expected when redefining repr/str slots.
- typing the name of a variable on the python prompt now prints the result of a (possibly redefined) repr, without the swig user having to do any extra work.
- when redefining repr, the swig user doesn't necessarily have to redefine str as it will call the redefined repr
- the behaviour is exactly the same as without the -builtin option while requiring no extra work by the user (aside from adding the %feature("python:slot...) statements of course)
- the patch simplifies pyrun.swg a tiny bit.

Disadvantage:
- default str() will give different (but clearer?) output on swigged classes compared to unpatched swig

I've also attached a test case where class A defines str and repr, while class B defines only repr. Below is the output of a few Python lines in the 2 cases to illustrate the changes in python results caused by this patch.

-------- swig-2.0.7 (or svn revision 13840) with -builtin--------
>>> from testKT import *
>>> a=A(1)
>>> a
<Swig Object of type 'A *' at 0x7f2049efba78>
>>> print(a)
<Swig Object of type 'A *' at 0x7f2049efba78>
>>> str(a)
'in str'
>>> repr(a)
'in repr'
>>>
>>> b=B(1)
>>> b
<Swig Object of type 'B *' at 0x7f2049efba40>
>>> print(b)
<Swig Object of type 'B *' at 0x7f2049efba40>
>>> str(b)
'_6089440100000000_p_B'
>>> repr(b)
'in repr'

-------- patched swig with -builtin--------
>>> from testKT import *
>>> a=A(1)
>>> a
in repr
>>> print(a)
in str
>>> str(a)
'in str'
>>> repr(a)
'in repr'
>>>
>>> b=B(1)
>>> b
in repr
>>> print(b)
in repr
>>> str(b)
'in repr'
>>> repr(b)
'in repr'
>>>

Discussion

  • zipp with patch file and code example

     
  • William Fulton
    William Fulton
    2013-08-07

    • status: open --> closed
    • assigned_to: William Fulton
    • Group: -->
     
  • William Fulton
    William Fulton
    2013-08-07

    Added for swig-2.0.11. Thanks for patch. As mentioned on the swig-devel mailing list ...

    In your example showing the problem, you have this:

    a=A(1)
    a
    <Swig Object="" of="" type="" 'A="" *'="" at="" 0x7f2049efba78="">
    print(a)
    <Swig Object="" of="" type="" 'A="" *'="" at="" 0x7f2049efba78="">

    Can you think of a way to rewrite this for a test that doesn't involve stdout, ie we can put it into a regression test in the test-suite? Unfortunately just using str and repr, behave differently as you show. Maybe the print chevron for the 2nd case, but I don't know about the first one.

    If you can think of something, let me know and I'll add it as a testcase.