Commits

Marcus von Appen committed 2d6920d

- add sdl2.filesystem, the upcoming filesystem API for SDL2
- add warning functions for the released SDL2 API and the
current development version

  • Participants
  • Parent commits 1c2d466

Comments (0)

Files changed (4)

File sdl2/__init__.py

 from .endian import *
 from .error import *
 from .events import *
+from .filesystem import *
 from .gamecontroller import *
 from .gesture import *
 from .haptic import *
         pattern = "lib%s.dylib"
     else:
         pattern = "lib%s.so"
-
     searchfor = libnames
     if type(libnames) is dict:
         # different library names for the platforms
                 warnings.warn\
                     ("function '%s' not found in %r, using replacement" %
                      (funcname, self._dll))
-                func = optfunc
+                func = _nonexistent(funcname, optfunc)
             else:
                 raise ValueError("could not find function '%s' in %r" %
                                  (funcname, self._dll))
         """Gets the filename of the loaded library."""
         return self._libfile
 
+def _nonexistent(funcname, func):
+    """A simple wrapper to mark functions and methods as nonexistent."""
+    def wrapper(*fargs, **kw):
+        warnings.warn("%s does not exist" % funcname,
+                      category=RuntimeWarning, stacklevel=2)
+        return func(*fargs, **kw)
+    wrapper.__name__ = func.__name__
+    return wrapper
+
+
 try:
     dll = _DLL("SDL2", ["SDL2", "SDL2-2.0"], os.getenv("PYSDL2_DLL_PATH"))
 except RuntimeError as exc:
     raise ImportError(exc)
 
+def nullfunc(*args):
+    """A simple no-op function to be used as dll replacement."""
+    return
+
 def get_dll_file():
     """Gets the file name of the loaded SDL2 library."""
     return dll.libfile

File sdl2/filesystem.py

+from ctypes import c_char, c_char_p, POINTER
+from .dll import _bind, nullfunc
+
+__all__ = ["SDL_GetBasePath", "SDL_GetPrefPath"]
+
+# The filesystem API came in after the 2.0 release
+SDL_GetBasePath = _bind("SDL_GetBasePath", None, POINTER(c_char), nullfunc)
+SDL_GetPrefPath = _bind("SDL_GetPrefPath", [c_char_p, c_char_p], POINTER(c_char),
+                        nullfunc)

File sdl2/test/filesystem_test.py

+import os
+import sys
+import unittest
+from ctypes import cast, c_char_p, addressof
+from .. import filesystem, SDL_free
+
+
+class SDLFileSystemTest(unittest.TestCase):
+    __tags__ = ["sdl"]
+
+    def test_SDL_GetBasePath(self):
+        execpath = os.path.dirname(sys.executable) + os.sep
+        path = filesystem.SDL_GetBasePath()
+        ppath = cast(path, c_char_p).value
+        self.assertEqual(execpath, ppath.decode("utf-8"))
+        SDL_free(path)
+
+    @unittest.skip("not implemented")
+    def test_SDL_GetPrefPath(self):
+        pass
+
+
+if __name__ == '__main__':
+    sys.exit(unittest.main())