Interesting Swig Enhancements

(I'm resending this since I don't know whether I did it correctly before)

Hello,

I was surprised that to wrap C++ classes, Swig must first "flatten"
all methods in (say) "core_wrap.cpp", then rebuild the classes in
"core.py".

Since python 2.2, it is possible to build classes from the C API.

So I've made some changes to Swig, which I think could be interesting to
share.

I heavily changed the code in "Python.cxx", and now:
- classes and methods are all built from the "wrap.cpp" code
- "core.py" shrunk a lot: it contains a "from _core import *",
  plus all the %pythoncode...
- All the "thisown" stuff is done at the C level
- No more "xxxPtr" classes
- all objects derive from the "swig_base" type
- "pythonappend" and "pythonprepend" are included in "wrap.cpp"
- I removed "-modern" and "-classic" options
  (all is "supermodern" now :wink:

There are many advantages:
- my "windows.py" module is only 66 lines instead of 3260!
  (removing blank lines and comments)
- less memory: after "import wx",
  python.exe uses 11Mb instead of 14Mb before,
  82,251 python refs instead of 205,276 (python_d says)
- seems twice faster to load (Though I did not measure it precisely)
- function calls should be faster too.

Still, it it not an easy task, and the work far from complete.
Also, It's possible that I did not understand everyting correctly.
That's why I'm also asking for help...

Enough of the demo is working to be sure that we can work around
the remaining problems.

The main problem is that changes are not backward compatible.
But I guess that it's because wxPython codes is too much aware
of the underlying Swig structure.
So I consider this not so important.

Here are the current issues:
- Lot of Python objects are not correctly freed
  (problems in the garbage collector are not easy to correct...)
- Tested only on Windows 2000
- Removed the "_UnbornObject" and "_DeadObject" classes.
  I think this should be handled with a special attribute of the
  object instead of special classes.
- static methods are no more available as "Class_StaticMethod(args)".
  Use "Class.StaticMethod(args)" instead.
- "%pythoncode { asTuple=Get }" don't work any more.
  "%pythoncode { asTuple=Size.Get }" should work though.
- special methods (__nonzero__, __getitem__) don't work.
  We need some Swig magic to detect this and fill the appropriate
  slots of the PyTypeObject.
  I've done this for Size.__getitem__, but it is far from perfect.
- the Swig code (as well as libpy.c,helpers.cpp) needs tidying seriously...

I would attach the files I changed, but 600Kb seems big for a mailing list.
I'll send it to anybody who asks!
I'm not subscribed to the mailing list but I often check the archives.
(my name is amaury dot forgeotdarc and my domain name is ubitrade dot com)

···

--
Amaury Forgeot d'Arc

This sounds really interesting. Can I add that your method should also be a
fair bit faster as well. I think you should post this on the swig list. You
could probably get help with this there.

···

-----Original Message-----
From: amaury.forgeotdarc@ubitrade.com
[mailto:amaury.forgeotdarc@ubitrade.com]
Sent: 29 March 2004 17:05
To: wxPython-dev@lists.wxwidgets.org
Subject: [wxPython-dev] Interesting Swig Enhancements

(I'm resending this since I don't know whether I did it correctly before)

Hello,

I was surprised that to wrap C++ classes, Swig must first "flatten"
all methods in (say) "core_wrap.cpp", then rebuild the classes in
"core.py".

Since python 2.2, it is possible to build classes from the C API.

So I've made some changes to Swig, which I think could be interesting to
share.

I heavily changed the code in "Python.cxx", and now:
- classes and methods are all built from the "wrap.cpp" code
- "core.py" shrunk a lot: it contains a "from _core import *",
  plus all the %pythoncode...
- All the "thisown" stuff is done at the C level
- No more "xxxPtr" classes
- all objects derive from the "swig_base" type
- "pythonappend" and "pythonprepend" are included in "wrap.cpp"
- I removed "-modern" and "-classic" options
  (all is "supermodern" now :wink:

There are many advantages:
- my "windows.py" module is only 66 lines instead of 3260!
  (removing blank lines and comments)
- less memory: after "import wx",
  python.exe uses 11Mb instead of 14Mb before,
  82,251 python refs instead of 205,276 (python_d says)
- seems twice faster to load (Though I did not measure it precisely)
- function calls should be faster too.

Still, it it not an easy task, and the work far from complete.
Also, It's possible that I did not understand everyting correctly.
That's why I'm also asking for help...

Enough of the demo is working to be sure that we can work around
the remaining problems.

The main problem is that changes are not backward compatible.
But I guess that it's because wxPython codes is too much aware
of the underlying Swig structure.
So I consider this not so important.

Here are the current issues:
- Lot of Python objects are not correctly freed
  (problems in the garbage collector are not easy to correct...)
- Tested only on Windows 2000
- Removed the "_UnbornObject" and "_DeadObject" classes.
  I think this should be handled with a special attribute of the
  object instead of special classes.
- static methods are no more available as "Class_StaticMethod(args)".
  Use "Class.StaticMethod(args)" instead.
- "%pythoncode { asTuple=Get }" don't work any more.
  "%pythoncode { asTuple=Size.Get }" should work though.
- special methods (__nonzero__, __getitem__) don't work.
  We need some Swig magic to detect this and fill the appropriate
  slots of the PyTypeObject.
  I've done this for Size.__getitem__, but it is far from perfect.
- the Swig code (as well as libpy.c,helpers.cpp) needs tidying
seriously...

I would attach the files I changed, but 600Kb seems big for a
mailing list.
I'll send it to anybody who asks!
I'm not subscribed to the mailing list but I often check the archives.
(my name is amaury dot forgeotdarc and my domain name is ubitrade dot com)

--
Amaury Forgeot d'Arc

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-dev-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-dev-help@lists.wxwidgets.org

_____________________________________________________________________
This e-mail is confidential and may be privileged. It may be
read, copied and used only by the intended recipient. No
communication sent by e-mail to or from Eutechnyx is intended to
give rise to contractual or other legal liability, apart from
liability which cannot be excluded under English law.

This message has been checked for all known viruses by Star
Internet delivered through the MessageLabs Virus Control Centre.

www.eutechnyx.com Eutechnyx Limited. Registered in England No: 2172322

_____________________________________________________________________
This e-mail is confidential and may be privileged. It may be read, copied and used only by the intended recipient. No communication sent by e-mail to or from Eutechnyx is intended to give rise to contractual or other legal liability, apart from liability which cannot be excluded under English law.

This message has been checked for all known viruses by Star Internet delivered through the MessageLabs Virus Control Centre.

www.eutechnyx.com Eutechnyx Limited. Registered in England No: 2172322