mopidy/pyspotify — pull request #60 by jodal (2012-03-17 16:25:27+00:00)
Title: spotify.capi: Low-level libspotify wrapper using ctypes

This work has just begun, but I'm opening this pull request to track the development and discussion of the new low-level API.

The idea is to provide the thinnest possible Python wrapper for libspotify, using just pure Python and the ctypes module from Python's standard library. This low-level wrapper (aka `spotify.capi` or C API) has several advantages over the current pyspotify bindings written in C using CPython's C API:

- It is pure Python, easing the maintenance burden.

- It is pure Python, using ctypes, so it should work on all/most Python implementations, including CPython 2.x, CPython 3.x, PyPy, and Jython.

- It introduces a low-level API available to users of pyspotify, which makes it easier to make new libspotify features quickly available to end users without spending time figuring out how to shoehorn the new features into the existing high-level and pythonic pyspotify API.

- It introduces a low-level API, which can be used to build different high-level and pythonic abstractions for using libspotify from Python.

- It eases debugging by doing extensive type checking in Python, avoiding many, but probably not all, segfaults from using the libspotify library improperly.

The major disadvantage is that a ctypes wrapper will be slower than a pure C wrapper.

Even though I say "thinnest possible Python wrapper for libspotify possible", we should consider padding the wrapper in some places:

- `spotify.capi` could do the encoding/decoding between the UTF-8 encoded strings libspotify uses, and only expose unicode objects to users of `spotify.capi`.

- `spotify.capi` could try to conceal the use of ctypes. Users of the module should e.g. not need to wrap arguments to `spotify.capi` functions with `ctypes.pointer()`.

- `spotify.capi` could convert error return codes to Python exceptions.

The first milestone is making the full libspotify API available through the `spotify.capi` module. The second milestone is to port the existing pyspotify API from using the C extension `spotify._spotify` to using `spotify.capi`. The intention of this pull request is to reach the first milestone.