1. Marcus von Appen
  2. py-sdl2
Issue #9 resolved

bind_function fails if function is not found

Steven Johnson
created an issue

Based on commit 03c0926, bind_function is called:

# sdl2/hints.py
_bind("SDL_AddHintCallback", [c_char_p, SDL_HintCallback, c_void_p])

# sdl2/dll.py (_DLL class)
def bind_function(self, funcname, args=None, returns=None, optfunc=None):
    """Binds the passed argument and return value types to the specified
    function."""
    func = getattr(self._dll, funcname, None)
    if not func:
        func = optfunc
    func.argtypes = args
    func.restype = returns
    return func

bind_function checks if the function is found, if not it uses the supplied optfunc regardless if it's none (None by default)

I'm not sure whether it should return a dummy function, raise an error, or just do nothing. But currently a non-descriptive error occurs:

>>> import sdl2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.3/dist-packages/sdl2/__init__.py", line 15, in <module>
    from .hints import *
  File "/usr/local/lib/python3.3/dist-packages/sdl2/hints.py", line 45, in <module>
    SDL_AddHintCallback = _bind("SDL_AddHintCallback", [c_char_p, SDL_HintCallback, c_void_p])
  File "/usr/local/lib/python3.3/dist-packages/sdl2/dll.py", line 73, in bind_function
    func.argtypes = args
AttributeError: 'NoneType' object has no attribute 'argtypes'

Comments (6)

  1. Steven Johnson reporter

    For now, I'm doing this:

    def bind_function(self, funcname, args=None, returns=None, optfunc=None):
        """Binds the passed argument and return value types to the specified
        function."""
        func = getattr(self._dll, funcname, None)
        if not func and optfunc:
            func = optfunc
        if func:
            func.argtypes = args
            func.restype = returns
        else:
            warnings.warn("%s not found in %s" % (funcname, self._dll))
        return func
    
  2. Marcus von Appen repo owner

    Ideally, it should look like this as long as SDL2 is not officially released:

        def bind_function(self, funcname, args=None, returns=None, optfunc=None):
            """Binds the passed argument and return value types to the specified
            function."""
            func = getattr(self._dll, funcname, None)
            if not func:
                if optfunc:
                    warnings.warn\
                        ("function '%s' not found in %r, using replacement" %
                         (funcname, self._dll))
                    func = optfunc
                else:
                    raise ValueError("could not find function '%s' in %r" %
                                     (funcname, self._dll))
            func.argtypes = args
            func.restype = returns
            return func
    

    Especially since current mercurial version of py-sdl2 is tightly bound to SDL2's changes. I'll commit that change later on.

  3. Log in to comment