Commits

Lenard Lindstrom  committed 9126dee

adapt joystick and cdrom modules for Python 3.1

  • Participants
  • Parent commits 3d14cd2

Comments (0)

Files changed (8)

File examples/testsprite.py

 from random import randint
 from time import time
 import pygame.joystick
+from pygame.compat import xrange_
 
 ##import FastRenderGroup as FRG
 import pygame.sprite as FRG
         else:
             sprites = pygame.sprite.Group()
 
-    for i in xrange(0, numsprites):
+    for i in xrange_(0, numsprites):
         if use_static and i%2==0:
             sprites.add(Static())
         sprites.add(Thingy())

File lib/compat.py

     unicode_ = unicode
 except NameError:
     unicode_ = str
+
+try:
+    raw_input_ = raw_input
+except NameError:
+    raw_input_ = input

File python3/README

 lib/__init__.py files with these, then run "python config.py" and 
 "python setup.py ..." as usual.
 
-Though all Python modules (.py file) will install and compile without
-error, some may still not work correctly. installed modules or programs
-will work. But the following modules do: display, image, mixer, mixer.music,
-transform, gfxdraw, font, event, key, sprites, mask, cursor and fastevent.
-The Surface, Rect and Color types are also available. The following example
-programs have also been ported: aliens.py, chimp.py, stars.py, fonty.py,
-scaletest.py, scroll.py, mask.py, fastevents.py, cursors.py and moveit.py.
+Though all Python modules (.py files) will install and compile without 
+error, some may still not work correctly. But the following modules do: 
+display, image, mixer, mixer.music, transform, gfxdraw, font, event, 
+key, sprites, mask, cursor, fastevent, joystick and cdrom. The Surface, 
+Rect and Color types are also available. The following example programs 
+have also been ported: aliens.py, chimp.py, stars.py, fonty.py, 
+scaletest.py, scroll.py, mask.py, fastevents.py, cursors.py, moveit.py 
+and spritetest.py.
 
 May 5, 2009
 

File python3/Setup.in

 #SDL as a dependency. these should not be altered
 
 base src/base.c $(SDL) $(DEBUG)
-#cdrom src/cdrom.c $(SDL) $(DEBUG)
+cdrom src/cdrom.c $(SDL) $(DEBUG)
 color src/color.c $(SDL) $(DEBUG)
 constants src/constants.c $(SDL) $(DEBUG)
 display src/display.c $(SDL) $(DEBUG)
 surface src/surface.c src/alphablit.c src/surface_fill.c $(SDL) $(DEBUG)
 surflock src/surflock.c $(SDL) $(DEBUG)
 time src/time.c $(SDL) $(DEBUG)
-#joystick src/joystick.c $(SDL) $(DEBUG)
+joystick src/joystick.c $(SDL) $(DEBUG)
 draw src/draw.c $(SDL) $(DEBUG)
 image src/image.c $(SDL) $(DEBUG)
 overlay src/overlay.c $(SDL) $(DEBUG)

File python3/__init__.py

 #first, the "required" modules
 from pygame.base import *
 from pygame.constants import *
-###from pygame.version import *
+from pygame.version import *
 from pygame.rect import Rect
 from pygame.compat import geterror
 import pygame.rwobject
 import pygame.surflock
 import pygame.color
 Color = color.Color
-#__version__ = ver
+__version__ = ver
 
 #next, the "standard" modules
 #we still allow them to be missing for stripped down pygame distributions
-###try: import pygame.cdrom
-###except (ImportError,IOError):cdrom=MissingModule("cdrom", geterror(), 1)
+try: import pygame.cdrom
+except (ImportError,IOError):cdrom=MissingModule("cdrom", geterror(), 1)
 
 try: import pygame.cursors
 except (ImportError,IOError):cursors=MissingModule("cursors", geterror(), 1)
 try: import pygame.image
 except (ImportError,IOError):image=MissingModule("image", geterror(), 1)
 
-###try: import pygame.joystick
-###except (ImportError,IOError):joystick=MissingModule("joystick", geterror(), 1)
+try: import pygame.joystick
+except (ImportError,IOError):joystick=MissingModule("joystick", geterror(), 1)
 
 try: import pygame.key
 except (ImportError,IOError):key=MissingModule("key", geterror(), 1)
 
 #define PYGAMEAPI_CDROM_INTERNAL
 #include "pygame.h"
+#include "pgcompat.h"
 #include "pygamedocs.h"
 
 #define CDROM_MAXDRIVES 32
 static SDL_CD* cdrom_drivedata[CDROM_MAXDRIVES] = {NULL};
-staticforward PyTypeObject PyCD_Type;
+static PyTypeObject PyCD_Type;
 static PyObject* PyCD_New(int id);
 #define PyCD_Check(x) ((x)->ob_type == &PyCD_Type)
 
 cdrom_autoquit (void)
 {
     int loop;
-    for (loop = 0; loop < CDROM_MAXDRIVES; ++loop)
-    {
-        if (cdrom_drivedata[loop])
-        {
+    for (loop = 0; loop < CDROM_MAXDRIVES; ++loop) {
+        if (cdrom_drivedata[loop]) {
             SDL_CDClose (cdrom_drivedata[loop]);
             cdrom_drivedata[loop] = NULL;
         }
     }
 
-    if (SDL_WasInit (SDL_INIT_CDROM))
+    if (SDL_WasInit (SDL_INIT_CDROM)) {
         SDL_QuitSubSystem (SDL_INIT_CDROM);
+    }
 }
 
 static PyObject*
 cdrom_autoinit (PyObject* self)
 {
-    if (!SDL_WasInit (SDL_INIT_CDROM))
-    {
-        if (SDL_InitSubSystem (SDL_INIT_CDROM))
+    if (!SDL_WasInit (SDL_INIT_CDROM)) {
+        if (SDL_InitSubSystem (SDL_INIT_CDROM)) {
             return PyInt_FromLong (0);
+        }
         PyGame_RegisterQuit (cdrom_autoquit);
     }
     return PyInt_FromLong (1);
     result = cdrom_autoinit (self);
     istrue = PyObject_IsTrue (result);
     Py_DECREF (result);
-    if (!istrue)
+    if (!istrue) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
     Py_RETURN_NONE;
 }
 
 CD (PyObject* self, PyObject* args)
 {
     int id;
-    if (!PyArg_ParseTuple (args, "i", &id))
+    if (!PyArg_ParseTuple (args, "i", &id)) {
         return NULL;
+    }
 
     CDROM_INIT_CHECK ();
     return PyCD_New (id);
     int cd_id = PyCD_AsID (self);
 
     CDROM_INIT_CHECK ();
-    if (!cdrom_drivedata[cd_id])
-    {
+    if (!cdrom_drivedata[cd_id]) {
         cdrom_drivedata[cd_id] = SDL_CDOpen (cd_id);
-        if (!cdrom_drivedata[cd_id])
+        if (!cdrom_drivedata[cd_id]) {
             return RAISE (PyExc_SDLError, "Cannot initialize device");
+        }
     }
     Py_RETURN_NONE;
 }
 
     CDROM_INIT_CHECK ();
 
-    if (cdrom_drivedata[cd_id])
-    {
+    if (cdrom_drivedata[cd_id]) {
         SDL_CDClose (cdrom_drivedata[cd_id]);
         cdrom_drivedata[cd_id] = NULL;
     }
     float start=0.0f, end=0.0f;
     PyObject *endobject=NULL;
 
-    if (!PyArg_ParseTuple (args, "i|fO", &track, &start, &endobject))
+    if (!PyArg_ParseTuple (args, "i|fO", &track, &start, &endobject)) {
         return NULL;
-    if (endobject == Py_None)
+    }
+    if (endobject == Py_None) {
         playforever = 1;
-    else if (!PyArg_ParseTuple (args, "i|ff", &track, &start, &end))
+    }
+    else if (!PyArg_ParseTuple (args, "i|ff", &track, &start, &end)) {
         return NULL;
+    }
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
     SDL_CDStatus (cdrom);
-    if (track < 0 || track >= cdrom->numtracks)
+    if (track < 0 || track >= cdrom->numtracks) {
         return RAISE (PyExc_IndexError, "Invalid track number");
-    if (cdrom->track[track].type != SDL_AUDIO_TRACK)
+    }
+    if (cdrom->track[track].type != SDL_AUDIO_TRACK) {
         return RAISE (PyExc_SDLError, "CD track type is not audio");
+    }
 	
     /*validate times*/
-    if (playforever)
+    if (playforever) {
         end = start;
-    else if (start == end && start != 0.0f)
+    }
+    else if (start == end && start != 0.0f) {
         Py_RETURN_NONE;
+    }
 	
     startframe = (int)(start * CD_FPS);
     numframes = 0;
-    if (startframe < 0)
+    if (startframe < 0) {
         startframe = 0;
-    if (end)
+    }
+    if (end) {
         numframes = (int) ((end-start) * CD_FPS);
-    else
+    }
+    else {
         numframes = cdrom->track[track].length - startframe;
+    }
     if (numframes < 0 ||
-        startframe > (int) (cdrom->track[track].length * CD_FPS))
+        startframe > (int) (cdrom->track[track].length * CD_FPS)) {
         Py_RETURN_NONE;
+    }
 
     result = SDL_CDPlayTracks (cdrom, track, startframe, 0, numframes);
-    if (result == -1)
+    if (result == -1) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
 
     Py_RETURN_NONE;
 }
     int result;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     result = SDL_CDPause (cdrom);
-    if (result == -1)
+    if (result == -1) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
     Py_RETURN_NONE;
 }
 
     int result;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     result = SDL_CDResume (cdrom);
-    if (result == -1)
+    if (result == -1) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
     Py_RETURN_NONE;
 }
 
     int result;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     result = SDL_CDStop (cdrom);
-    if (result == -1)
+    if (result == -1) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
     Py_RETURN_NONE;
 }
 
     int result;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     result = SDL_CDEject (cdrom);
-    if (result == -1)
+    if (result == -1) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
 
     Py_RETURN_NONE;
 }
     int status;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     status = SDL_CDStatus (cdrom);
     return PyInt_FromLong (status == CD_TRAYEMPTY);
     int status;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     status = SDL_CDStatus (cdrom);
     return PyInt_FromLong (status == CD_PLAYING);
     int status;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     status = SDL_CDStatus (cdrom);
     return PyInt_FromLong (status == CD_PAUSED);
     float seconds;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     SDL_CDStatus (cdrom);
     track = cdrom->cur_track;
     SDL_CD* cdrom = cdrom_drivedata[cd_id];
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     SDL_CDStatus (cdrom);
     return PyInt_FromLong (cdrom->numtracks);
 {
     int cd_id = PyCD_AsID (self);
     CDROM_INIT_CHECK ();
-    return PyString_FromString (SDL_CDName (cd_id));
+    return Text_FromUTF8 (SDL_CDName (cd_id));
 }
 
 static PyObject*
     SDL_CD* cdrom = cdrom_drivedata[cd_id];
     int track;
 
-    if (!PyArg_ParseTuple (args, "i", &track))
+    if (!PyArg_ParseTuple (args, "i", &track)) {
         return NULL;
+    }
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
     SDL_CDStatus (cdrom);
-    if (track < 0 || track >= cdrom->numtracks)
+    if (track < 0 || track >= cdrom->numtracks) {
         return RAISE (PyExc_IndexError, "Invalid track number");
+    }
 
     return PyInt_FromLong (cdrom->track[track].type == SDL_AUDIO_TRACK);
 }
     SDL_CD* cdrom = cdrom_drivedata[cd_id];
     int track;
 
-    if (!PyArg_ParseTuple (args, "i", &track))
+    if (!PyArg_ParseTuple (args, "i", &track)) {
         return NULL;
+    }
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
     SDL_CDStatus (cdrom);
-    if (track < 0 || track >= cdrom->numtracks)
+    if (track < 0 || track >= cdrom->numtracks) {
         return RAISE (PyExc_IndexError, "Invalid track number");
-    if (cdrom->track[track].type != SDL_AUDIO_TRACK)
+    }
+    if (cdrom->track[track].type != SDL_AUDIO_TRACK) {
         return PyFloat_FromDouble (0.0);
+    }
 
     return PyFloat_FromDouble (cdrom->track[track].length / (double) CD_FPS);
 }
     SDL_CD* cdrom = cdrom_drivedata[cd_id];
     int track;
 
-    if (!PyArg_ParseTuple (args, "i", &track))
+    if (!PyArg_ParseTuple (args, "i", &track)) {
         return NULL;
+    }
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
     SDL_CDStatus (cdrom);
-    if (track < 0 || track >= cdrom->numtracks)
+    if (track < 0 || track >= cdrom->numtracks) {
         return RAISE (PyExc_IndexError, "Invalid track number");
+    }
 
     return PyFloat_FromDouble (cdrom->track[track].offset / (double) CD_FPS);
 }
     PyObject *tuple, *item;
 
     CDROM_INIT_CHECK ();
-    if (!cdrom)
+    if (!cdrom) {
         return RAISE (PyExc_SDLError, "CD drive not initialized");
+    }
 
     SDL_CDStatus (cdrom);
     tuple = PyTuple_New (cdrom->numtracks);
-    if (!tuple)
+    if (!tuple) {
         return NULL;
-    for (track=0; track < cdrom->numtracks; track++)
-    {
+    }
+    for (track=0; track < cdrom->numtracks; track++) {
         int audio = cdrom->track[track].type == SDL_AUDIO_TRACK;
         double start = cdrom->track[track].offset / (double) CD_FPS;
         double length = cdrom->track[track].length / (double) CD_FPS;
         double end = start + length;
         item = PyTuple_New (4);
-        if (!item)
-        {
+        if (!item) {
             Py_DECREF (tuple);
             return NULL;
         }
     return tuple;
 }
 
-static PyMethodDef cd_builtins[] =
+static PyMethodDef cd_methods[] =
 {
     { "init", (PyCFunction) cd_init, METH_NOARGS, DOC_CDINIT },
     { "quit", (PyCFunction) cd_quit, METH_NOARGS, DOC_CDQUIT },
     { NULL, NULL, 0, NULL }
 };
 
-static PyObject*
-cd_getattr (PyObject* self, char* attrname)
-{
-    return Py_FindMethod (cd_builtins, self, attrname);
-}
-
 static PyTypeObject PyCD_Type =
 {
-    PyObject_HEAD_INIT(NULL)
-    0,
-    "CD",
-    sizeof(PyCDObject),
-    0,
-    cd_dealloc,
-    0,
-    cd_getattr,
-    0,
-    0,
-    0,
-    0,
-    NULL,
-    0,
-    (hashfunc)NULL,
-    (ternaryfunc)NULL,
-    (reprfunc)NULL,
-    0L,0L,0L,0L,
-    DOC_PYGAMECDROMCD /* Documentation string */
+    TYPE_HEAD (NULL, 0)
+    "CD",                       /* name */
+    sizeof(PyCDObject),         /* basic size */
+    0,                          /* itemsize */
+    cd_dealloc,                 /* dealloc */
+    0,                          /* print */
+    0,                          /* getattr */
+    0,                          /* setattr */
+    0,                          /* compare */
+    0,                          /* repr */
+    0,                          /* as_number */
+    0,                          /* as_sequence */
+    0,                          /* as_mapping */
+    0,                          /* hash */
+    0,                          /* call */
+    0,                          /* str */
+    0,                          /* tp_getattro */
+    0,                          /* tp_setattro */
+    0,                          /* tp_as_buffer */
+    0,                          /* flags */
+    DOC_PYGAMECDROMCD,          /* Documentation string */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,	                        /* tp_iter */
+    0,                          /* tp_iternext */
+    cd_methods,                 /* tp_methods */
+    0,                          /* tp_members */
+    0,                          /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    0,                          /* tp_init */
+    0,				/* tp_alloc */
+    0,			        /* tp_new */
 };
 
 static PyObject*
 {
     PyCDObject* cd;
 
-    if (id < 0 || id >= CDROM_MAXDRIVES || id >= SDL_CDNumDrives ())
+    if (id < 0 || id >= CDROM_MAXDRIVES || id >= SDL_CDNumDrives ()) {
         return RAISE (PyExc_SDLError, "Invalid cdrom device number");
+    }
 
     cd = PyObject_NEW (PyCDObject, &PyCD_Type);
-    if(!cd)
+    if(!cd) {
         return NULL;
+    }
 
     cd->id = id;
 
     return (PyObject*)cd;
 }
 
-static PyMethodDef cdrom_builtins[] =
+static PyMethodDef _cdrom_methods[] =
 {
     { "__PYGAMEinit__", (PyCFunction) cdrom_autoinit, METH_NOARGS,
       "auto initialize function" },
     { NULL, NULL, 0, NULL }
 };
 
-PYGAME_EXPORT
-void initcdrom (void)
+MODINIT_DEFINE (cdrom)
 {
     PyObject *module, *dict, *apiobj;
+    int ecode;
     static void* c_api[PYGAMEAPI_CDROM_NUMSLOTS];
 
+#if PY3
+    static struct PyModuleDef _module = {
+        PyModuleDef_HEAD_INIT,
+        "cdrom",
+        DOC_PYGAMECDROM,
+        -1,
+        _cdrom_methods,
+        NULL, NULL, NULL, NULL
+    };
+#endif
+
     /* imported needed apis; Do this first so if there is an error
        the module is not loaded.
     */
     import_pygame_base ();
     if (PyErr_Occurred ()) {
-	return;
+	MODINIT_ERROR;
     }
 
     /* type preparation */
-    PyType_Init (PyCD_Type);
+    if (PyType_Ready (&PyCD_Type) == -1) {
+        MODINIT_ERROR;
+    }
 
     /* create the module */
-    module = Py_InitModule3 ("cdrom", cdrom_builtins, DOC_PYGAMECDROM);
+#if PY3
+    module = PyModule_Create (&_module);
+#else
+    module = Py_InitModule3 ("cdrom", _cdrom_methods, DOC_PYGAMECDROM);
+#endif
+    if (module == NULL) {
+        MODINIT_ERROR;
+    }
     dict = PyModule_GetDict (module);
 
-    PyDict_SetItemString (dict, "CDType", (PyObject *)&PyCD_Type);
+    if (PyDict_SetItemString (dict, "CDType", (PyObject *)&PyCD_Type) == -1) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
 
     /* export the c api */
     c_api[0] = &PyCD_Type;
     c_api[1] = PyCD_New;
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
-    PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
+    if (apiobj == NULL) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
+    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
+    if (ecode == -1) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
+    MODINIT_RETURN (module);
 }

File src/joystick.c

 
 #define PYGAMEAPI_JOYSTICK_INTERNAL
 #include "pygame.h"
+#include "pgcompat.h"
 #include "pygamedocs.h"
 
 #define JOYSTICK_MAXSTICKS 32
 static SDL_Joystick* joystick_stickdata[JOYSTICK_MAXSTICKS] = {NULL};
-staticforward PyTypeObject PyJoystick_Type;
+static PyTypeObject PyJoystick_Type;
 static PyObject* PyJoystick_New (int);
 #define PyJoystick_Check(x) ((x)->ob_type == &PyJoystick_Type)
 
 joy_autoquit (void)
 {
     int loop;
-    for (loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop)
-    {
-        if (joystick_stickdata[loop])
-        {
+    for (loop = 0; loop < JOYSTICK_MAXSTICKS; ++loop) {
+        if (joystick_stickdata[loop]) {
             SDL_JoystickClose (joystick_stickdata[loop]);
             joystick_stickdata[loop] = NULL;
         }
     }
 
-    if (SDL_WasInit (SDL_INIT_JOYSTICK))
-    {
+    if (SDL_WasInit (SDL_INIT_JOYSTICK)) {
         SDL_JoystickEventState (SDL_ENABLE);
         SDL_QuitSubSystem (SDL_INIT_JOYSTICK);
     }
 static PyObject*
 joy_autoinit (PyObject* self)
 {
-    if (!SDL_WasInit (SDL_INIT_JOYSTICK))
-    {
-        if (SDL_InitSubSystem (SDL_INIT_JOYSTICK))
+    if (!SDL_WasInit (SDL_INIT_JOYSTICK)) {
+        if (SDL_InitSubSystem (SDL_INIT_JOYSTICK)) {
             return PyInt_FromLong (0);
+        }
         SDL_JoystickEventState (SDL_ENABLE);
         PyGame_RegisterQuit (joy_autoquit);
     }
     result = joy_autoinit (self);
     istrue = PyObject_IsTrue (result);
     Py_DECREF (result);
-    if (!istrue)
+    if (!istrue) {
         return RAISE (PyExc_SDLError, SDL_GetError ());
+    }
     Py_RETURN_NONE;
 }
 
 Joystick (PyObject* self, PyObject* args)
 {
     int id;	
-    if (!PyArg_ParseTuple (args, "i", &id))
+    if (!PyArg_ParseTuple (args, "i", &id)) {
         return NULL;
+    }
 
     JOYSTICK_INIT_CHECK ();
 
     int joy_id = PyJoystick_AsID (self);
 
     JOYSTICK_INIT_CHECK ();
-    if (!joystick_stickdata[joy_id])
-    {
+    if (!joystick_stickdata[joy_id]) {
         joystick_stickdata[joy_id] = SDL_JoystickOpen (joy_id);
-        if (!joystick_stickdata[joy_id])
+        if (!joystick_stickdata[joy_id]) {
             return RAISE (PyExc_SDLError, SDL_GetError ());
+        }
     }
     Py_RETURN_NONE;
 }
 
     JOYSTICK_INIT_CHECK ();
 
-    if (joystick_stickdata[joy_id])
-    {
+    if (joystick_stickdata[joy_id]) {
         SDL_JoystickClose (joystick_stickdata[joy_id]);
         joystick_stickdata[joy_id] = NULL;
     }
 {
     int joy_id = PyJoystick_AsID (self);
     JOYSTICK_INIT_CHECK ();
-    return PyString_FromString (SDL_JoystickName (joy_id));
+    return Text_FromUTF8 (SDL_JoystickName (joy_id));
 }
 
 static PyObject*
     SDL_Joystick* joy = joystick_stickdata[joy_id];
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
+    }
 
     return PyInt_FromLong (SDL_JoystickNumAxes (joy));
 }
     SDL_Joystick* joy = joystick_stickdata[joy_id];
     int axis, value;
 	
-    if (!PyArg_ParseTuple (args, "i", &axis))
+    if (!PyArg_ParseTuple (args, "i", &axis)) {
         return NULL;
+    }
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
-    if (axis < 0 || axis >= SDL_JoystickNumAxes (joy))
+    }
+    if (axis < 0 || axis >= SDL_JoystickNumAxes (joy)) {
         return RAISE (PyExc_SDLError, "Invalid joystick axis");
+    }
 
     value = SDL_JoystickGetAxis (joy, axis);
     return PyFloat_FromDouble (value / 32768.0);
     SDL_Joystick* joy = joystick_stickdata[joy_id];
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
+    }
 
     return PyInt_FromLong (SDL_JoystickNumButtons (joy));
 }
     SDL_Joystick* joy = joystick_stickdata[joy_id];
     int _index, value;
 	
-    if (!PyArg_ParseTuple (args, "i", &_index))
+    if (!PyArg_ParseTuple (args, "i", &_index)) {
         return NULL;
+    }
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
-    if (_index < 0 || _index >= SDL_JoystickNumButtons (joy))
+    }
+    if (_index < 0 || _index >= SDL_JoystickNumButtons (joy)) {
         return RAISE (PyExc_SDLError, "Invalid joystick button");
+    }
 
     value = SDL_JoystickGetButton (joy, _index);
     return PyInt_FromLong (value);
     SDL_Joystick* joy = joystick_stickdata[joy_id];
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
+    }
 
     return PyInt_FromLong (SDL_JoystickNumBalls (joy));
 }
     SDL_Joystick* joy = joystick_stickdata[joy_id];
     int _index, dx, dy;
 	
-    if (!PyArg_ParseTuple (args, "i", &_index))
+    if (!PyArg_ParseTuple (args, "i", &_index)) {
         return NULL;
+    }
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
-    if (_index < 0 || _index >= SDL_JoystickNumBalls (joy))
+    }
+    if (_index < 0 || _index >= SDL_JoystickNumBalls (joy)) {
         return RAISE (PyExc_SDLError, "Invalid joystick trackball");
+    }
 
     SDL_JoystickGetBall (joy, _index, &dx, &dy);
     return Py_BuildValue ("(ii)", dx, dy);
     SDL_Joystick* joy = joystick_stickdata[joy_id];
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
+    }
 
     return PyInt_FromLong (SDL_JoystickNumHats (joy));
 }
     int _index, px, py;
     Uint8 value;
 
-    if (!PyArg_ParseTuple (args, "i", &_index))
+    if (!PyArg_ParseTuple (args, "i", &_index)) {
         return NULL;
+    }
 
     JOYSTICK_INIT_CHECK ();
-    if (!joy)
+    if (!joy) {
         return RAISE (PyExc_SDLError, "Joystick not initialized");
-    if (_index < 0 || _index >= SDL_JoystickNumHats (joy))
+    }
+    if (_index < 0 || _index >= SDL_JoystickNumHats (joy)) {
         return RAISE(PyExc_SDLError, "Invalid joystick hat");
+    }
 
     px = py = 0;
     value = SDL_JoystickGetHat (joy, _index);
-    if (value & SDL_HAT_UP)
+    if (value & SDL_HAT_UP) {
         py = 1;
-    else if (value & SDL_HAT_DOWN)
+    }
+    else if (value & SDL_HAT_DOWN) {
         py = -1;
-    if (value & SDL_HAT_RIGHT)
+    }
+    if (value & SDL_HAT_RIGHT) {
         px = 1;
-    else if (value & SDL_HAT_LEFT)
+    }
+    else if (value & SDL_HAT_LEFT) {
         px = -1;
+    }
 	
     return Py_BuildValue ("(ii)", px, py);
 }
 
-static PyMethodDef joy_builtins[] =
+static PyMethodDef joy_methods[] =
 {
     { "init", (PyCFunction) joy_init, METH_NOARGS, DOC_JOYSTICKINIT },
     { "quit", (PyCFunction) joy_quit, METH_NOARGS, DOC_JOYSTICKQUIT },
     { NULL, NULL, 0, NULL }
 };
 
-static PyObject*
-joy_getattr (PyObject* self, char* attrname)
-{
-    return Py_FindMethod (joy_builtins, self, attrname);
-}
-
 static PyTypeObject PyJoystick_Type =
 {
-    PyObject_HEAD_INIT(NULL)
-    0,
-    "Joystick",
-    sizeof(PyJoystickObject),
-    0,
-    joy_dealloc,
-    0,
-    joy_getattr,
-    0,
-    0,
-    0,
-    0,
-    NULL,
-    0, 
-    (hashfunc)NULL,
-    (ternaryfunc)NULL,
-    (reprfunc)NULL,
-    0L,0L,0L,0L,
-    DOC_PYGAMEJOYSTICKJOYSTICK /* Documentation string */
+    TYPE_HEAD (NULL, 0)
+    "Joystick",                 /* name */
+    sizeof(PyJoystickObject),   /* basic size */
+    0,                          /* itemsize */
+    joy_dealloc,                /* dealloc */
+    0,                          /* print */
+    0,                          /* getattr */
+    0,                          /* setattr */
+    0,                          /* compare */
+    0,                          /* repr */
+    0,                          /* as_number */
+    0,                          /* as_sequence */
+    0,                          /* as_mapping */
+    0,                          /* hash */
+    0,                          /* call */
+    0,                          /* str */
+    0,                          /* tp_getattro */
+    0,                          /* tp_setattro */
+    0,                          /* tp_as_buffer */
+    0,                          /* flags */
+    DOC_PYGAMEJOYSTICKJOYSTICK, /* Documentation string */
+    0,                          /* tp_traverse */
+    0,                          /* tp_clear */
+    0,                          /* tp_richcompare */
+    0,                          /* tp_weaklistoffset */
+    0,	                        /* tp_iter */
+    0,                          /* tp_iternext */
+    joy_methods,                /* tp_methods */
+    0,                          /* tp_members */
+    0,                          /* tp_getset */
+    0,                          /* tp_base */
+    0,                          /* tp_dict */
+    0,                          /* tp_descr_get */
+    0,                          /* tp_descr_set */
+    0,                          /* tp_dictoffset */
+    0,                          /* tp_init */
+    0,				/* tp_alloc */
+    0,			        /* tp_new */
 };
 
 static PyObject*
 {
     PyJoystickObject* joy;
 
-    if (id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks ())
+    if (id < 0 || id >= JOYSTICK_MAXSTICKS || id >= SDL_NumJoysticks ()) {
         return RAISE (PyExc_SDLError, "Invalid joystick device number");
+    }
 	
     joy = PyObject_NEW (PyJoystickObject, &PyJoystick_Type);
-    if (!joy)
+    if (!joy) {
         return NULL;
+    }
 
     joy->id = id;
 
     return (PyObject*)joy;
 }
 
-static PyMethodDef joystick_builtins[] =
+static PyMethodDef _joystick_methods[] =
 {
     { "__PYGAMEinit__", (PyCFunction) joy_autoinit, METH_NOARGS,
       "auto initialize function for joystick" },
     { NULL, NULL, 0, NULL }
 };
 
-PYGAME_EXPORT
-void initjoystick (void)
+MODINIT_DEFINE (joystick)
 {
     PyObject *module, *dict, *apiobj;
+    int ecode;
     static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
 
+#if PY3
+    static struct PyModuleDef _module = {
+        PyModuleDef_HEAD_INIT,
+        "joystick",
+        DOC_PYGAMEJOYSTICK,
+        -1,
+        _joystick_methods,
+        NULL, NULL, NULL, NULL
+    };
+#endif
+
     /* imported needed apis; Do this first so if there is an error
        the module is not loaded.
     */
     import_pygame_base ();
     if (PyErr_Occurred ()) {
-	return;
+	MODINIT_ERROR;
     }
 
     /* type preparation */
-    PyType_Init (PyJoystick_Type);
+    if (PyType_Ready (&PyJoystick_Type) == -1) {
+        MODINIT_ERROR;
+    }
 
     /* create the module */
-    module = Py_InitModule3 ("joystick", joystick_builtins, DOC_PYGAMEJOYSTICK);
+#if PY3
+    module = PyModule_Create (&_module);
+#else
+    module = Py_InitModule3 ("joystick", _joystick_methods, DOC_PYGAMEJOYSTICK);
+#endif
+    if (module == NULL) {
+        MODINIT_ERROR;
+    }
     dict = PyModule_GetDict (module);
 
-    PyDict_SetItemString (dict, "JoystickType", (PyObject *)&PyJoystick_Type);
+    if (PyDict_SetItemString (dict, "JoystickType",
+                              (PyObject *)&PyJoystick_Type) == -1) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
 
     /* export the c api */
     c_api[0] = &PyJoystick_Type;
     c_api[1] = PyJoystick_New;
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
-    PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
+    if (apiobj == NULL) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
+    ecode = PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
+    if (ecode == -1) {
+        DECREF_MOD (module);
+        MODINIT_ERROR;
+    }
+    MODINIT_RETURN (module);
 }

File test/test_utils/__init__.py

 except NameError:
     xrange_ = range
 
+try:
+    raw_input_ = raw_input
+except NameError:
+    raw_input_ = input
+
 ################################################################################
 
 this_dir = os.path.dirname(os.path.abspath(__file__))
 ################################################################################
 
 def question(q):
-    return raw_input('%s ' % q.rstrip(' ')).lower().strip() == 'y'
+    return raw_input_('%s ' % q.rstrip(' ')).lower().strip() == 'y'
 
 def prompt(p):
-    return raw_input('%s (and press enter to continue) ' % p.rstrip(' '))
+    return raw_input_('%s (and press enter to continue) ' % p.rstrip(' '))
 
 #################################### HELPERS ###################################