bastienleonard / pysfml-cython

A Python 2/3 binding for SFML 2, written with Cython
http://pysfml2-cython.readthedocs.org/
Other
68 stars 8 forks source link

Cannot use arguments in ctor when inheriting sf.Sprite #26

Closed abodelot closed 12 years ago

abodelot commented 12 years ago

When inheriting sf.Sprite, the sub-class cannot be initialized if the constructor is invoked with an argument. The code bellow reproduces the bug:

import sfml as sf

class Entity(sf.Sprite):

    def __init__(self, hp=1):
        print "Entity.__init__ called"
        super(Entity, self).__init__()
        self.hp = hp

Entity(5) # ERROR

A TypeError exception is raised:

Entity(5) # ERROR
  File "sfml.pyx", line 2280, in sfml.Sprite.__cinit__ (src\sfml.cpp:25311)
TypeError: Argument 'texture' has incorrect type (expected sfml.Texture, got int)

The line print "Entity.__init__ called" is not executed, I assume Entity.__init__ is bypassed and sf.Sprite.__init__ is directly called instead.

By removing the argument in the last line (replacing Entity(5) with Entity()), the code runs correctly and enters Entity.__init__ as expected.

Environment: Windows XP / Python 2.7 / pySFML-0.1.win32-py2.7 (installer)

bastienleonard commented 12 years ago

Thanks. This was fixed in commit 78743d8c8cf5de18e1057b0abbbeb0c2d3f0b7e3, and in source release 0.1.1. The installers on this page should include the change: http://www.lfd.uci.edu/~gohlke/pythonlibs/#pysfml2.

The problem was that Cython lets you define a special __cinit__ method that will always automatically get called with the arguments passed to the child class' constructor. So as you said, it gets called before your own constructor with invalid arguments and raised an exception. I replaced it with a normal __init__ constructor.

abodelot commented 12 years ago

Oh, sorry for the false flag. I'll make sure I'm up to date for the next time!