Skip to main content


One of the RaspberryPi's goals is to be a fun toolkit for school children (and adults!) to learn programming and electronics with. Python and pygame are part of this toolkit. Recently the RaspberryPi Foundation funded parts of the effort of porting of pypy to the Pi -- making Python programs on the Pi faster!

Unfortunately pygame is written as a Python C extension that wraps SDL which means performance of pygame under pypy remains mediocre. To fix this pygame needs to be rewritten using cffi to wrap SDL instead.

RaspberryPi sponsored a CTPUG (Cape Town Python User Group) hackathon to put together a proof-of-concept pygame-cffi. The day was quite successful - we got a basic version of the bub'n'bros client working on pygame-cffi (and on PyPy). The results can be found on github with contributions from the five people present at the sprint.

While far from complete, the proof of concept does show that there are no major obstacles to porting pygame to cffi and that cffi is a great way to bind your Python package to C libraries.

Amazingly, we managed to have machines running all three major platforms (OS X, Linux and Windows) at the hackathon so the code runs on all of them!

We would like to thank the Praekelt foundation for providing the venue and The Raspberry Pi foundation for providing food and drinks!

Simon Cross, Jeremy Thurgood, Neil Muller, David Sharpe and fijal.


René Dudfield wrote on 2013-12-09 14:21:

Why not use the ctypes based pysdl2?

Maciej Fijalkowski wrote on 2013-12-09 16:19:

first of all pygame depends on SDL 1. Second ctypes kinda suck and I don't quite buy it's stability (especially with changing APIs, though it can be less of an issue with SDL). It's also slow on pypy

René Dudfield wrote on 2013-12-09 17:09:

Ah, ok. Very nice work anyway. It's impressive what you all managed to get done in the sprint :)

Here's some information from pygame land about where the project is heading.

SDL 1 is the past, and the SDL developers are no longer putting out releases. However, I think many people will continue to patch it up for many years. SDL 2 is the future and after many years finally has a release out (2 now). pysdl2 is part of the future of pygame. pysdl2 matches the SDL 2 API as closely as possible. A pygame API ontop of pysdl2 is the future of pygame.

ctypes is no good for some platforms like iOS, and the web and pypy apparently. Although note, that pysdl2 already 'works' on top of pypy.

Happy hacking :)

Anonymous wrote on 2013-12-09 18:56:

Amazing - you consider a messy cffi implementation (sometimes it builds on platform X, sometimes it does not, sometimes it works, sometimes it does not) a better choice over ctypes?

Maciej Fijalkowski wrote on 2013-12-09 19:16:

@Anonymous - your comment is pretty loaded, but we do think cffi is better than ctypes on all platforms, that's why we came up with cffi in the first place. I think cffi FAQ contains an answer to that.

Armin Rigo wrote on 2013-12-10 09:30:

@Rene: if pysdl2 is a bare-metal ctypes wrapper, writing a similar cffi wrapper instead should be very straightforward (even more than the current pygame-cffi). But do you know if pygame is really going that route, and if so, how soon?

Unknown wrote on 2013-12-10 23:33:

I've been looking at cffi since it was first mentioned on our Pygame mailing list. It does look promising. I see only two, buffer related, issues that need to be resolved.

First, PyPy lacks an array export mechanism comparable to the CPython PEP 3113 buffer protocol. Instead, only the NumPy Array Interface, version: 3 is available. Though Pygame supports both the Python and C sides of the interface, it relies on CPython's reference counting for timely buffer release [1]. Periodic garbage collection is too unpredictable.

Second, the cffi module does not support CPython api function calls. So a cffi Pygame could not support the buffer protocol on CPython.

A possible solution to the first issue is for PyPy to use an extended array interface that includes a PEP 3118 like buffer release callback. I am working to resolve the second issue: [Issue13797] Allow objects implemented in pure Python to export PEP 3118 buffers.

[1] Add PEP 3118 (new) buffer support to Pygame surfaces

Anonymous wrote on 2013-12-15 21:32:

Hm, I can't get this to work on Ubuntu 12.04 doing the following

virtualenv -p /usr/bin/pypy pypy
cd pypy
source bin/activate
pip install git+
pip install hg+
pip install hg+
git clone
cd pygame_cffi/
import pygame

>>>> import pygame
Traceback (most recent call last):
File "", line 1, in
File "pygame/", line 9, in
from pygame.color import Color
File "pygame/", line 3, in
from pygame._sdl import ffi, sdl
File "pygame/", line 6, in
ffi = cffi.FFI()
File "/home/me/Documents/python/pygame/pypy/site-packages/cffi/", line 56, in __init__
import _cffi_backend as backend
ImportError: No module named _cffi_backend

dpkg -l pypy
ii pypy 1.8+dfsg-2 fast alternative implementation of Python - PyPy interpreter

Do I need a newer pypy? Am I missing something else?

Maciej Fijalkowski wrote on 2013-12-15 21:48:

yes, you need a vastly newer pypy

Unknown wrote on 2013-12-16 18:49:

I am +1 on porting PySDL2 to CFFI instead of pygame.

Unknown wrote on 2016-05-03 01:01:

great! what's current status of it? I really can't wait to use Pygame on a PI through pypy.
Armin Rigo wrote on 2016-05-04 10:16:

Development occurs at nowadays.