1. pygame
  2. Untitled project
  3. pygame

Commits

marcus  committed e69b655

Added Python 3.x Unicode/String/Byte conversion routines in pygame2.base.
Added Python 3.x compatibility support in pgcompat.h.
Fixed string handling for Python 3.x.
Fixed file IO handling for Python 3.x.
Added simple pygame2.sdlext.draw example.
Fixed pygame2.sdlext.font module for Python 3.x.

  • Participants
  • Parent commits 06b0f17
  • Branches pgreloaded

Comments (0)

Files changed (31)

File Makefile

View file
-PYTHON = python
+PYTHON = python3.0
 top_srcdir = `pwd`
 SUBDIRS = \
 	$(top_srcdir)/config \

File TODO.txt

View file
 CRITICAL:
 =========
-* PyFile_* and PyString_* API under python 3.x (see pgcompat.h)
 * recheck and redesign X11 implementation for pygame.sdlext.scrap
 * merge transform/scale asm changes in rev. 1657:1669
 * complete physics collision and contacts and merge it back to the branch
 * check anything for possible integer/float overflows
 * use copy.copy and copy.deepcopy for consistent object copies
 * make use of PointFromObject and SizeFromObject where necessary
+* Merge transform smooth scale changes in rev. 1715:1717
 
 Things to ADD:
 ==============
 
 Things to FIX:
 ==============
+* check all string/unicode conversions, esp. file I/O with !UTF-8 and !ASCII
 * respect surface->clip_rect where necessary!
 * sdl.mouse.cursors, NOT pygame2.sdl.cursors or whatever
 * sdlext.transform.threshod_* -> clip_rect, surfaces with different sizes

File examples/sdl/draw.py

View file
+import sys
+import pygame2
+try:
+    import pygame2.sdl.constants as sdlconst
+    import pygame2.sdl.event as event
+    import pygame2.sdl.video as video
+    import pygame2.sdlext.draw as draw
+except ImportError:
+    print ("No pygame2.sdl support")
+    sys.exit ()
+
+
+white = pygame2.Color (255, 255, 255)
+black = pygame2.Color (0, 0, 0)
+red = pygame2.Color (255, 0, 0)
+green = pygame2.Color (0, 255, 0)
+blue = pygame2.Color (0, 0, 255)
+
+def draw_rect (screen):
+    draw.rect (screen, black, pygame2.Rect (20, 20, 100, 100))
+    draw.rect (screen, red, pygame2.Rect (160, 100, 160, 60))
+    draw.rect (screen, green, pygame2.Rect (180, 190, 180, 200))
+    draw.rect (screen, blue, pygame2.Rect (390, 120, 200, 140))
+
+def draw_circle (screen):
+    draw.circle (screen, black, (100, 100), 50)
+    draw.circle (screen, red, (200, 160), 80, 4)
+    draw.circle (screen, green, (370, 210), 100, 12)
+    draw.circle (screen, blue, (400, 400), 45, 40)
+
+def draw_arc (screen):
+    draw.arc (screen, black, pygame2.Rect (80, 100, 100, 100), 0, 30)
+
+def run ():
+    drawtypes = [ draw_rect, draw_circle, draw_arc ]
+    curtype = 0
+    video.init ()
+
+    screen = video.set_mode (640, 480)
+    screen.fill (white)
+    draw_rect (screen)
+    screen.flip ()
+
+    while True:
+        for ev in event.get ():
+            if ev.type == sdlconst.QUIT:
+                sys.exit ()
+            if ev.type == sdlconst.KEYDOWN and ev.key == sdlconst.K_ESCAPE:
+                sys.exit ()
+            if ev.type == sdlconst.MOUSEBUTTONDOWN:
+                curtype += 1
+                if curtype >= len (drawtypes):
+                    curtype = 0
+                screen.fill (white)
+                drawtypes[curtype] (screen)
+                screen.flip ()
+
+if __name__ == "__main__":
+    run ()

File lib/sdlext/font.py

View file
 from pygame2 import Rect, FRect, Color
 from pygame2.sdl.video import Surface
 
-DEFAULTMAP = [ u"0123456789",
-               u"ABCDEFGHIJ",
-               u"KLMNOPQRST",
-               u"UVWXYZ    ",
-               u"abcdefghij",
-               u"klmnopqrst",
-               u"uvwxyz    ",
-               u",;.:!?+-()" ]
+DEFAULTMAP = [ "0123456789",
+               "ABCDEFGHIJ",
+               "KLMNOPQRST",
+               "UVWXYZ    ",
+               "abcdefghij",
+               "klmnopqrst",
+               "uvwxyz    ",
+               ",;.:!?+-()" ]
 
 class BitmapFont (object):
     """BitmapFont (surface, size, mapping=None) -> BitmapFont

File src/base/basemod.c

View file
     return 0;
 }
 
+int
+ASCIIFromObject (PyObject *obj, char **text, PyObject **freeme)
+{
+    *freeme = NULL;
+    *text = NULL;
+
+    if (PyUnicode_Check (obj))
+    {
+        *freeme = PyUnicode_AsEncodedString (obj, "ascii", NULL);
+        if (!(*freeme))
+            return 0;
+        *text = Bytes_AS_STRING (*freeme);
+    }
+    else if (Bytes_Check (obj))
+        *text = Bytes_AS_STRING (obj);
+    else
+        return 0;
+
+    return 1;
+}
+
+int
+UTF8FromObject (PyObject *obj, char **text, PyObject **freeme)
+{
+    *freeme = NULL;
+    *text = NULL;
+
+    if (PyUnicode_Check (obj))
+    {
+        *freeme = PyUnicode_AsUTF8String (obj);
+        if (!(*freeme))
+            return 0;
+        *text = Bytes_AS_STRING (*freeme);
+    }
+    else if (Bytes_Check (obj))
+        *text = Bytes_AS_STRING (obj);
+    else
+        return 0;
+
+    return 1;
+}
+
 #if PY_VERSION_HEX >= 0x03000000
 PyMODINIT_FUNC PyInit_base (void)
 #else
     c_api[PYGAME_BASE_FIRSTSLOT+8] = SizeFromObject;
     c_api[PYGAME_BASE_FIRSTSLOT+9] = FPointFromObject;
     c_api[PYGAME_BASE_FIRSTSLOT+10] = FSizeFromObject;
+    c_api[PYGAME_BASE_FIRSTSLOT+11] = ASCIIFromObject;
+    c_api[PYGAME_BASE_FIRSTSLOT+12] = UTF8FromObject;
 
     color_export_capi (c_api);
     rect_export_capi (c_api);

File src/base/bufferproxy.c

View file
 static PyObject*
 _bufferproxy_get_raw (PyBufferProxy *self, void *closure)
 {
-#if PY_VERSION_HEX < 0x03000000
-    return PyString_FromStringAndSize (self->buffer, self->length);
-#else
-    return PyBytes_FromStringAndSize (self->buffer, self->length);
-#endif
+    return Bytes_FromStringAndSize (self->buffer, self->length);
 }
 
 /**
 #if PY_VERSION_HEX < 0x02050000
     return PyString_FromFormat ("<BufferProxy(%d)>", self->length);
 #else
-    return PyString_FromFormat ("<BufferProxy(%zd)>", self->length);
+    return Text_FromFormat ("<BufferProxy(%zd)>", self->length);
 #endif
 }
 

File src/base/color.c

View file
 static int
 _hexcolor (PyObject *color, pgbyte rgba[])
 {
+    int retval = 0;
     size_t len;
-    char *name = PyString_AsString (color);
-    if (!name)
+    char *name;
+    PyObject *tmp;
+
+    if (!ASCIIFromObject (color, &name, &tmp))
         return 0;
 
     len = strlen (name);
      * 0xRRGGBBAA
      */
     if (len < 7)
-        return 0;
+        goto fail;
 
     if (name[0] == '#')
     {
         if (len != 7 && len != 9)
-            return 0;
+            goto fail;
         if (!_hextoint (name + 1, &rgba[0]))
-            return 0;
+            goto fail;
         if (!_hextoint (name + 3, &rgba[1]))
-            return 0;
+            goto fail;
         if (!_hextoint (name + 5, &rgba[2]))
-            return 0;
+            goto fail;
         rgba[3] = 255;
         if (len == 9 && !_hextoint (name + 7, &rgba[3]))
-            return 0;
-        return 1;
+            goto fail;
+        goto success;
     }
     else if (name[0] == '0' && name[1] == 'x')
     {
         if (len != 8 && len != 10)
-            return 0;
+            goto fail;
         if (!_hextoint (name + 2, &rgba[0]))
-            return 0;
+            goto fail;
         if (!_hextoint (name + 4, &rgba[1]))
-            return 0;
+            goto fail;
         if (!_hextoint (name + 6, &rgba[2]))
-            return 0;
+            goto fail;
         rgba[3] = 255;
         if (len == 10 && !_hextoint (name + 8, &rgba[3]))
-            return 0;
-        return 1;
+            goto fail;
+        goto success;
     }
+fail:
+    Py_XDECREF (tmp);
     return 0;
+
+success:
+    Py_XDECREF (tmp);
+    return 1;
 }
 
 static int
     if (!PyArg_ParseTuple (args, "O|OOO", &obj, &obj1, &obj2, &obj3))
         return -1;
 
-    if (PyString_Check (obj))
+    if (IsTextObj (obj))
     {
         /* Named color */
         if (obj1 || obj2 || obj3)
     char buf[21];
     PyOS_snprintf (buf, sizeof (buf), "(%d, %d, %d, %d)",
         color->r, color->g, color->b, color->a);
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 /**
         PyOS_snprintf (buf, sizeof (buf), "0%lo", tmp);
     else
         PyOS_snprintf (buf, sizeof (buf), "0%loL", tmp);
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 /**
         PyOS_snprintf (buf, sizeof (buf), "0x%lXL", tmp);
 #endif
     }
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 /* Sequence protocol methods */

File src/base/floatrect.c

View file
 {
     PyFRect *r = (PyFRect*) self;
     char buf[256];
-    /* TODO: check for overflow */
+    /* TODO: overflow possible? */
     PyOS_snprintf (buf, sizeof (buf), "(%.3f, %.3f, %.3f, %.3f)",
         r->x, r->y, r->w, r->h);
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 /* FRect getters/setters */

File src/base/internals.h

View file
 int SizeFromObject (PyObject* obj, pgint32 *w, pgint32 *h);
 int FPointFromObject (PyObject* obj, double *x, double *y);
 int FSizeFromObject (PyObject* obj, double *w, double *h);
+int ASCIIFromObject (PyObject *obj, char **text, PyObject **freeme);
+int UTF8FromObject (PyObject *obj, char **text, PyObject **freeme);
 
 extern PyTypeObject PyColor_Type;
 #define PyColor_Check(x) (PyObject_TypeCheck(x, &PyColor_Type))

File src/base/pgbase.h

View file
 #endif
 
 #define PYGAME_BASE_FIRSTSLOT 0
-#define PYGAME_BASE_NUMSLOTS 11
+#define PYGAME_BASE_NUMSLOTS 13
 #ifndef PYGAME_BASE_INTERNAL
 #define PyExc_PyGameError ((PyObject*)PyGameBase_C_API[PYGAME_BASE_FIRSTSLOT])
 #define DoubleFromObj                                                   \
     (*(int(*)(PyObject*, double*, double*))PyGameBase_C_API[PYGAME_BASE_FIRSTSLOT+9])
 #define FSizeFromObject                                                  \
     (*(int(*)(PyObject*, double*, double*))PyGameBase_C_API[PYGAME_BASE_FIRSTSLOT+10])
+#define ASCIIFromObject                                                 \
+    (*(int(*)(PyObject*, char**, PyObject**))PyGameBase_C_API[PYGAME_BASE_FIRSTSLOT+11])
+#define UTF8FromObject                                                  \
+    (*(int(*)(PyObject*, char**, PyObject**))PyGameBase_C_API[PYGAME_BASE_FIRSTSLOT+12])
 #endif /* PYGAME_BASE_INTERNAL */
 
 typedef struct

File src/base/rect.c

View file
     char buf[256];
     PyOS_snprintf (buf, sizeof (buf), "(%d, %d, %u, %u)",
         r->x, r->y, r->w, r->h);
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 /* Rect getters/setters */

File src/pgcompat.h

View file
 
 /* Python 3.x compatibility */
 #if PY_VERSION_HEX >= 0x03000000
+#define IS_PYTHON_3
+
 /* Define some aliases for the removed PyInt_* functions */
 #define PyInt_Check(op) PyLong_Check(op)
 #define PyInt_FromString PyLong_FromString
 #define MODINIT_RETURN(x) return(x)
 #define TYPE_HEAD(x,y) PyVarObject_HEAD_INIT(x,y)
 
-/* TODO: REMOVE THOSE!
- * They are not safe - instead we will need the correct functions and
- * converters or whatever for them.
- */
-#define PyString_AsString PyUnicode_AS_DATA
-#define PyString_FromFormat PyUnicode_FromFormat
-#define PyString_FromString PyUnicode_FromString
-#define PyString_FromStringAndSize PyUnicode_FromStringAndSize
-#define PyString_Check PyUnicode_Check
-#define PyString_GET_SIZE PyUnicode_GET_SIZE
-#define PyString_Size PyUnicode_GetSize
-#define PyString_ConcatAndDel(x,y)
-
-#define PyFile_Check(x) 1
-#define PyFile_AsFile(x) PyObject_AsFileDescriptor(x)
-
-/* TODO END */
-
 #else /* PY_VERSION_HEX >= 0x03000000 */
 
 /* Used for the changed PyMODINIT_FUNC defines in 3.0 and 
 
 #endif /* PY_VERSION_HEX >= 0x03000000 */ 
 
+/**
+ * Text interfaces. Those assume the text is pure ASCII or UTF-8
+ */
+#if PY_VERSION_HEX >= 0x03000000
+#define Text_Check PyUnicode_Check
+#define Text_FromUTF8 PyUnicode_FromString
+#define Text_FromUTF8AndSize PyUnicode_FromStringAndSize
+#define Text_FromFormat PyUnicode_FromFormat
+#define Text_GetSize PyUnicode_GetSize
+#define Text_GET_SIZE PyUnicode_GET_SIZE
+
+#define Bytes_Check PyBytes_Check
+#define Bytes_Size PyBytes_Size
+#define Bytes_AsString PyBytes_AsString
+#define Bytes_AsStringAndSize PyBytes_AsStringAndSize
+#define Bytes_FromStringAndSize PyBytes_FromStringAndSize
+#define Bytes_AS_STRING PyBytes_AS_STRING
+#define Bytes_GET_SIZE PyBytes_GET_SIZE
+
+#define IsTextObj(x) (PyUnicode_Check(x) || PyBytes_Check(x))
+
+#else  /* PY_VERSION_HEX >= 0x03000000 */
+
+#define Text_Check PyString_Check
+#define Text_FromUTF8 PyString_FromString
+#define Text_FromUTF8AndSize PyString_FromStringAndSize
+#define Text_FromFormat PyString_FromFormat
+#define Text_GetSize PyString_GetSize
+#define Text_GET_SIZE PyString_GET_SIZE
+
+#define Bytes_Check PyString_Check
+#define Bytes_Size PyString_Size
+#define Bytes_AsString PyString_AsString
+#define Bytes_AsStringAndSize PyString_AsStringAndSize
+#define Bytes_FromStringAndSize PyString_FromStringAndSize
+#define Bytes_AS_STRING PyString_AS_STRING
+#define Bytes_GET_SIZE PyString_GET_SIZE
+
+#define IsTextObj(x) (PyString_Check(x) || PyUnicode_Check(x))
+
+#endif  /* PY_VERSION_HEX >= 0x03000000 */ 
+
 #endif /* _PYGAME_PYTHONCOMPAT_H_ */

File src/sdl/cdrom.c

View file
 _cd_getname (PyObject *self, void *closure)
 {
     ASSERT_CDROM_INIT(NULL);
-    return PyString_FromString (SDL_CDName (((PyCD*)self)->index));
+    return Text_FromUTF8 (SDL_CDName (((PyCD*)self)->index));
 }
 
 static PyObject*

File src/sdl/cdrommod.c

View file
         return NULL;
     }
     
-    return PyString_FromString (SDL_CDName (drive));
+    return Text_FromUTF8 (SDL_CDName (drive));
 }
 
 void

File src/sdl/event.c

View file
                 PyInt_FromLong (event-> syswm.msg->lParam)))
             goto failed;
 #elif defined(SDL_VIDEO_DRIVER_X11)
-        if (!_set_item (dict,  "event", PyString_FromStringAndSize
+        if (!_set_item (dict,  "event", Text_FromUTF8AndSize
                 ((char*) &(event->syswm.msg->event.xevent), sizeof (XEvent))))
             goto failed;
 #endif
     switch (((PyEvent*)self)->type)
     {
     case SDL_ACTIVEEVENT:
-        return PyString_FromString ("ActiveEvent");
+        return Text_FromUTF8 ("ActiveEvent");
     case SDL_KEYDOWN:
-        return PyString_FromString ("KeyDown");
+        return Text_FromUTF8 ("KeyDown");
     case SDL_KEYUP:
-        return PyString_FromString ("KeyUp");
+        return Text_FromUTF8 ("KeyUp");
     case SDL_MOUSEMOTION:
-        return PyString_FromString ("MouseMotion");
+        return Text_FromUTF8 ("MouseMotion");
     case SDL_MOUSEBUTTONDOWN:
-        return PyString_FromString ("MouseButtonDown");
+        return Text_FromUTF8 ("MouseButtonDown");
     case SDL_MOUSEBUTTONUP:
-        return PyString_FromString ("MouseButtonUp");
+        return Text_FromUTF8 ("MouseButtonUp");
     case SDL_JOYAXISMOTION:
-        return PyString_FromString ("JoyAxisMotion");
+        return Text_FromUTF8 ("JoyAxisMotion");
     case SDL_JOYBALLMOTION:
-        return PyString_FromString ("JoyBallMotion");
+        return Text_FromUTF8 ("JoyBallMotion");
     case SDL_JOYHATMOTION:
-        return PyString_FromString ("JoyHatMotion");
+        return Text_FromUTF8 ("JoyHatMotion");
     case SDL_JOYBUTTONUP:
-        return PyString_FromString ("JoyButtonUp");
+        return Text_FromUTF8 ("JoyButtonUp");
     case SDL_JOYBUTTONDOWN:
-        return PyString_FromString ("JoyButtonDown");
+        return Text_FromUTF8 ("JoyButtonDown");
     case SDL_QUIT:
-        return PyString_FromString ("Quit");
+        return Text_FromUTF8 ("Quit");
     case SDL_SYSWMEVENT:
-        return PyString_FromString ("SysWMEvent");
+        return Text_FromUTF8 ("SysWMEvent");
     case SDL_VIDEORESIZE:
-        return PyString_FromString ("VideoResize");
+        return Text_FromUTF8 ("VideoResize");
     case SDL_VIDEOEXPOSE:
-        return PyString_FromString ("VideoExpose");
+        return Text_FromUTF8 ("VideoExpose");
     case SDL_NOEVENT:
-        return PyString_FromString ("NoEvent");
+        return Text_FromUTF8 ("NoEvent");
     }
     if (((PyEvent*)self)->type >= SDL_USEREVENT &&
         ((PyEvent*)self)->type < SDL_NUMEVENTS)
-        return PyString_FromString ("UserEvent");
-    return PyString_FromString ("Unknown");
+        return Text_FromUTF8 ("UserEvent");
+    return Text_FromUTF8 ("Unknown");
 }
 
 /* C API */

File src/sdl/imagemod.c

View file
     SDL_Surface *surface;
     PyObject *sf, *file;
     char *filename;
-    
+
     if (!PyArg_ParseTuple (args, "O:load_bmp", &file))
         return NULL;
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        filename = PyString_AsString (file);
+        PyObject *tmp;
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
+
         Py_BEGIN_ALLOW_THREADS;
         surface = SDL_LoadBMP ((const char*)filename);
         Py_END_ALLOW_THREADS;
+
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops *rw = RWopsFromPython (file);
         if (!rw)
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by the PyObject_AsFileDescriptor() call */
+#endif
         PyErr_SetString (PyExc_TypeError, "file must be a string or file");
         return NULL;
     }
     char *filename;
     int _stat;
     
-    if (!PyArg_ParseTuple (args, "Os:save_bmp", &surface, &filename))
+    if (!PyArg_ParseTuple (args, "OO:save_bmp", &surface, &filename))
         return NULL;
     if (!PySurface_Check (surface))
     {

File src/sdl/joystick.c

View file
 _joystick_getname (PyObject *self, void *closure)
 {
     ASSERT_JOYSTICK_INIT(NULL);
-    return PyString_FromString (SDL_JoystickName (((PyJoystick*)self)->index));
+    return Text_FromUTF8 (SDL_JoystickName (((PyJoystick*)self)->index));
 }
 
 static PyObject*

File src/sdl/joystickmod.c

View file
         PyErr_SetString (PyExc_ValueError, "invalid joystick index");
         return NULL;
     }
-    return PyString_FromString (SDL_JoystickName (joy));
+    return Text_FromUTF8 (SDL_JoystickName (joy));
 }
 
 static PyObject*

File src/sdl/keyboardmod.c

View file
         PyErr_SetString (PyExc_ValueError, "invalid key value");
         return 0;
     }
-    return PyString_FromString (SDL_GetKeyName ((SDLKey)key));
+    return Text_FromUTF8 (SDL_GetKeyName ((SDLKey)key));
 }
 
 static PyObject*

File src/sdl/rwopsmod.c

View file
 static SDL_RWops*
 get_standard_rwop (PyObject* obj)
 {
-    if (PyString_Check (obj) || PyUnicode_Check (obj))
+#ifdef IS_PYTHON_3
+    int fd;
+#endif
+    if (IsTextObj (obj))
     {
         int result;
         char* name;
         /* TODO: allow wb ops! */
         return SDL_RWFromFile (name, "rb");
     }
+#ifdef IS_PYTHON_3
+    else if ((fd = PyObject_AsFileDescriptor (obj)) != -1)
+        return SDL_RWFromFP (fd, 0);
+    PyErr_Clear ();
+#else
     else if (PyFile_Check(obj))
         return SDL_RWFromFP (PyFile_AsFile (obj), 0);
+#endif
     return NULL;
 }
 
     if (!result)
         return -1;
 
-    if (!PyString_Check (result))
+    if (!Bytes_Check (result))
     {
         Py_DECREF (result);
         return -1;
     }
 
-    retval = PyString_GET_SIZE (result);
-    memcpy (ptr, PyString_AsString (result), (size_t) retval);
+    retval = Bytes_GET_SIZE (result);
+    memcpy (ptr, Bytes_AS_STRING (result), (size_t) retval);
     retval /= size;
 
     Py_DECREF (result);
         goto end;
     }
 
-    if (!PyString_Check (result))
+    if (!Bytes_Check (result))
     {
         Py_DECREF (result);
         PyErr_Print();
         goto end;
     }
 
-    retval = PyString_GET_SIZE (result);
-    memcpy (ptr, PyString_AsString (result), (size_t) retval);
+    retval = Bytes_GET_SIZE (result);
+    memcpy (ptr, Bytes_AS_STRING (result), (size_t) retval);
     retval /= size;
 
     Py_DECREF (result);

File src/sdl/sdlmod.c

View file
     char *err = SDL_GetError ();
     if (!err)
         Py_RETURN_NONE;
-    return PyString_FromString (err);
+    return Text_FromUTF8 (err);
 }
 
 static PyObject*

File src/sdl/surface.c

View file
 
     surface = ((PySurface*)self)->surface;
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        filename = PyString_AsString (file);
+        PyObject *tmp;
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
+        
         Py_BEGIN_ALLOW_THREADS;
         retval = pyg_surface_save (surface, filename, type);
         Py_END_ALLOW_THREADS;
+        
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops* rw;
         
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by PyObject_AsFileDescriptor() */
+#endif
         PyErr_SetString (PyExc_TypeError,
             "file must be file-like object or a string");
         return NULL;

File src/sdl/videomod.c

View file
     char buf[256];
     if (!SDL_VideoDriverName (buf, sizeof (buf)))
         Py_RETURN_NONE;
-    return PyString_FromString (buf);
+    return Text_FromUTF8 (buf);
 }
 
 static PyObject*

File src/sdlext/pixelarray.c

View file
     Sint32 absystep;
     Uint32 posx = 0;
     Uint32 posy = 0;
+#ifdef IS_PYTHON_3
+    PyObject *tmp1, *tmp2;
+#endif
 
     surface = ((PySurface*)array->surface)->surface;
     bpp = surface->format->BytesPerPixel;
     pixels = (Uint8 *) surface->pixels;
 
-    string = PyString_FromString ("PixelArray(");
+    string = Text_FromUTF8 ("PixelArray(");
 
     absxstep = ABS (array->xstep);
     absystep = ABS (array->ystep);
         while (posy < array->ylen)
         {
             /* Construct the rows */
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromUTF8 ("\n  [");
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
+#endif
             posx = 0;
             x = array->xstart;
             while (posx < (Uint32)xlen)
             {
                 /* Construct the columns */
                 pixel = (Uint32) *((Uint8 *) pixels + x + y * array->padding);
+#ifdef IS_PYTHON_3
+                tmp1 = string;
+                tmp2 = Text_FromFormat ("%ld, ", (long)pixel);
+                string = PyUnicode_Concat (tmp1, tmp2);
+                Py_XDECREF (tmp1);
+                Py_XDECREF (tmp2);
+                if (!string)
+                    return NULL;
+#else
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%ld, ", (long)pixel));
+#endif
                 x += array->xstep;
                 posx += absxstep;
             }
             pixel = (Uint32) *((Uint8 *) pixels + x + y * array->padding);
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromFormat ("%ld]", (long)pixel);
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%ld]", (long)pixel));
+#endif
             y += array->ystep;
             posy += absystep;
         }
         while (posy < array->ylen)
         {
             /* Construct the rows */
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromUTF8 ("\n  [");
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
+#endif
             posx = 0;
             x = array->xstart;
             while (posx < (Uint32)xlen)
                 /* Construct the columns */
                 pixel = (Uint32)
                     *((Uint16 *) (pixels + y * array->padding) + x);
+#ifdef IS_PYTHON_3
+                tmp1 = string;
+                tmp2 = Text_FromFormat ("%ld, ", (long)pixel);
+                string = PyUnicode_Concat (tmp1, tmp2);
+                Py_XDECREF (tmp1);
+                Py_XDECREF (tmp2);
+                if (!string)
+                    return NULL;
+#else
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%ld, ", (long)pixel));
+#endif
                 x += array->xstep;
                 posx += absxstep;
             }
             pixel = (Uint32) *((Uint16 *) (pixels + y * array->padding) + x);
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromFormat ("%ld]", (long)pixel);
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%ld]", (long)pixel));
+#endif
             y += array->ystep;
             posy += absystep;
         }
         while (posy < array->ylen)
         {
             /* Construct the rows */
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromUTF8 ("\n  [");
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
+#endif
             posx = 0;
             x = array->xstart;
             while (posx < (Uint32)xlen)
 #else
                 pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
 #endif
+#ifdef IS_PYTHON_3
+                tmp1 = string;
+                tmp2 = Text_FromFormat ("%ld, ", (long)pixel);
+                string = PyUnicode_Concat (tmp1, tmp2);
+                Py_XDECREF (tmp1);
+                Py_XDECREF (tmp2);
+                if (!string)
+                    return NULL;
+#else
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%ld, ", (long)pixel));
+#endif
                 x += array->xstep;
                 posx += absxstep;
             }
 #else
             pixel = (px24[2]) + (px24[1] << 8) + (px24[0] << 16);
 #endif
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromFormat ("%ld]", (long)pixel);
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%ld]", (long)pixel));
+#endif
             y += array->ystep;
             posy += absystep;
         }
         while (posy < array->ylen)
         {
             /* Construct the rows */
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromUTF8 ("\n  [");
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string, PyString_FromString ("\n  ["));
+#endif
             posx = 0;
             x = array->xstart;
             while (posx < (Uint32)xlen)
             {
                 /* Construct the columns */
                 pixel = *((Uint32 *) (pixels + y * array->padding) + x);
+#ifdef IS_PYTHON_3
+                tmp1 = string;
+                tmp2 = Text_FromFormat ("%ld, ", (long)pixel);
+                string = PyUnicode_Concat (tmp1, tmp2);
+                Py_XDECREF (tmp1);
+                Py_XDECREF (tmp2);
+                if (!string)
+                    return NULL;
+#else
                 PyString_ConcatAndDel (&string, PyString_FromFormat
                     ("%ld, ", (long)pixel));
+#endif
                 x += array->xstep;
                 posx += absxstep;
             }
             pixel = *((Uint32 *) (pixels + y * array->padding) + x);
+#ifdef IS_PYTHON_3
+            tmp1 = string;
+            tmp2 = Text_FromFormat ("%ld]", (long)pixel);
+            string = PyUnicode_Concat (tmp1, tmp2);
+            Py_XDECREF (tmp1);
+            Py_XDECREF (tmp2);
+            if (!string)
+                return NULL;
+#else
             PyString_ConcatAndDel (&string,
                 PyString_FromFormat ("%ld]", (long)pixel));
+#endif
             y += array->ystep;
             posy += absystep;
         }
         break;
     }
+#ifdef IS_PYTHON_3
+    tmp1 = string;
+    tmp2 = Text_FromUTF8 ("\n)");
+    string = PyUnicode_Concat (tmp1, tmp2);
+    Py_XDECREF (tmp1);
+    Py_XDECREF (tmp2);
+    if (!string)
+        return NULL;
+#else
     PyString_ConcatAndDel (&string, PyString_FromString ("\n)"));
+#endif
     return string;
 }
 

File src/sdlext/scrapmod.c

View file
     }
     if (!result)
         Py_RETURN_NONE;
-    return PyString_FromStringAndSize (data, size);
+    return Text_FromUTF8AndSize (data, size);
 }
 
 static PyObject*
     {
     case SCRAP_SELECTION:
     {
-        tmp = PyString_FromStringAndSize (data, length);
+        tmp = Text_FromUTF8AndSize (data, length);
         PyDict_SetItemString (_scrapselection, type, tmp);
         Py_DECREF (tmp);
         break;
     case SCRAP_CLIPBOARD:
     default:
     {
-        tmp = PyString_FromStringAndSize (data, length);
+        tmp = Text_FromUTF8AndSize (data, length);
         PyDict_SetItemString (_scrapclipboard, type, tmp);
         Py_DECREF (tmp);
         break;
 
     while (types[i])
     {
-        item = PyString_FromString (types[i]);
+        item = Text_FromUTF8 (types[i]);
         PyList_Append (list, item);
         Py_DECREF (item);
         free (types[i]);

File src/sdlimage/imagemod.c

View file
     char *err = IMG_GetError ();
     if (!err)
         Py_RETURN_NONE;
-    return PyString_FromString (err);
+    return Text_FromUTF8 (err);
 }
 
 static PyObject*
     if (!PyArg_ParseTuple (args, "O|s:load", &file, &type))
         return NULL;
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        filename = PyString_AsString (file);
+        PyObject *tmp;
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
 
         if (type)
         {
             surface = IMG_Load (filename);
             Py_END_ALLOW_THREADS;
         }
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops *rw = RWopsFromPython (file);
         if (!rw)
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by PyObject_AsFileDescriptor() */
+#endif
         PyErr_SetString (PyExc_TypeError, "file must be a string or file");
         return NULL;
     }

File src/sdlmixer/chunk.c

View file
     char *filename;
     PyObject *file;
     Mix_Chunk *chunk;
-    
+
     ASSERT_MIXER_OPEN (-1);
     
     if (!PyArg_ParseTuple (args, "O", &file))
         return -1;
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        filename = PyString_AsString (file);
+        PyObject *tmp;
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
 
         Py_BEGIN_ALLOW_THREADS;
         chunk = Mix_LoadWAV (filename);
         Py_END_ALLOW_THREADS;
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops *rw = RWopsFromPython (file);
         if (!rw)
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by PyObject_AsFileDescriptor () */
+#endif
         PyErr_SetString (PyExc_TypeError, "file must be string for file");
         return -1;
     }

File src/sdlmixer/mixermod.c

View file
     char *err = Mix_GetError ();
     if (!err)
         Py_RETURN_NONE;
-    return PyString_FromString (err);
+    return Text_FromUTF8 (err);
 }
 
 static PyObject*

File src/sdlmixer/music.c

View file
     if (!PyArg_ParseTuple (args, "O", &file))
         return -1;
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        filename = PyString_AsString (file);
+        PyObject *tmp;
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
 
         Py_BEGIN_ALLOW_THREADS;
         music = Mix_LoadMUS (filename);
         Py_END_ALLOW_THREADS;
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops *rw = RWopsFromPython (file);
         if (!rw)
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by PyObject_AsFileDescriptor(). */
+#endif
         PyErr_SetString (PyExc_TypeError, "file must be a string or file");
         return -1;
     }

File src/sdlttf/font.c

View file
         return -1;
     }
 
-    if (PyString_Check (file) || PyUnicode_Check (file))
+    if (IsTextObj (file))
     {
-        char *filename = PyString_AsString (file);
+        char *filename;
+        PyObject *tmp;
+
+        if (!UTF8FromObject (file, &filename, &tmp))
+            return NULL;
+
         Py_BEGIN_ALLOW_THREADS;
         if (_index == 0)
             font = TTF_OpenFont (filename, ptsize);
         else
             font = TTF_OpenFontIndex (filename, ptsize, _index);
         Py_END_ALLOW_THREADS;
+
+        Py_XDECREF (tmp);
     }
+#ifdef IS_PYTHON_3
+    else if (PyObject_AsFileDescriptor (file) != -1)
+#else
     else if (PyFile_Check (file))
+#endif
     {
         SDL_RWops *rw = RWopsFromPython (file);
         if (!rw)
     }
     else
     {
+#ifdef IS_PYTHON_3
+        PyErr_Clear (); /* Set by PyObject_AsFileDescriptor() */
+#endif
         PyErr_SetString (PyExc_TypeError, "file must be a string or file");
         return -1;
     }
 _font_getfamilyname (PyObject *self, void *closure)
 {
     ASSERT_TTF_INIT (NULL);
-    return PyString_FromString (TTF_FontFaceFamilyName (((PyFont*)self)->font));
+    return Text_FromUTF8 (TTF_FontFaceFamilyName (((PyFont*)self)->font));
 }
 
 static PyObject*
 _font_getstylename (PyObject *self, void *closure)
 {
     ASSERT_TTF_INIT (NULL);
-    return PyString_FromString (TTF_FontFaceStyleName (((PyFont*)self)->font));
+    return Text_FromUTF8 (TTF_FontFaceStyleName (((PyFont*)self)->font));
 }
 
 /* Methods */
         buf = PyUnicode_AsUnicode (textobj);
         isunicode = 1;
     }
-    else if (PyString_Check (textobj))
-        buf = PyString_AsString (textobj);
+    else if (Bytes_Check (textobj))
+        buf = Bytes_AS_STRING (textobj);
     else
     {
         PyErr_SetString (PyExc_TypeError,
     if (isunicode)
         length = PyUnicode_GetSize (textobj);
     else
-        length = PyString_Size (textobj);
+        length = Bytes_Size (textobj);
     if (length == 0)
         Py_RETURN_NONE;
 
     if (PyUnicode_Check (text))
     {
         PyObject* strob = PyUnicode_AsEncodedString (text, "utf-8", "replace");
-        char *string = PyString_AsString (strob);
+        char *string = Bytes_AS_STRING (strob);
         if (TTF_SizeUTF8 (font->font, string, &w, &h) == -1)
         {
             PyErr_SetString (PyExc_PyGameError, TTF_GetError ());
         return Py_BuildValue ("(ii)", w, h);
     }
     
-    if (PyString_Check (text))
+    if (Bytes_Check (text))
     {
-        char* string = PyString_AsString (text);
+        char* string = Bytes_AS_STRING (text);
         if (TTF_SizeText (font->font, string, &w, &h) == -1)
         {
             PyErr_SetString (PyExc_PyGameError, TTF_GetError ());
     if (PyUnicode_Check (text))
     {
         PyObject* strob = PyUnicode_AsEncodedString (text, "utf-8", "replace");
-        char *string = PyString_AsString (strob);
+        char *string = Bytes_AS_STRING (strob);
 
         switch (render)
         {
         }
         Py_DECREF (strob);
     }
-    else if (PyString_Check (text))
+    else if (Bytes_Check (text))
     {
-        char* string = PyString_AsString (text);
+        char* string = Bytes_AS_STRING (text);
 
         switch (render)
         {

File src/sdlttf/ttfmod.c

View file
     char *err = TTF_GetError ();
     if (!err)
         Py_RETURN_NONE;
-    return PyString_FromString (err);
+    return Text_FromUTF8 (err);
 }
 
 static PyObject*