Anonymous avatar Anonymous committed 39a061b

Fixed module initialization and memory leakage.

Comments (0)

Files changed (18)

src/base/basemod.c

 */
 #define PYGAME_BASE_INTERNAL
 
+#include "pymacros.h"
 #include "internals.h"
 #include "pgbase.h"
 #include "base_doc.h"
     _BaseState *state;
     PyObject *mod, *c_api_obj;
     
-    /* Complete types */
-    PyColor_Type.tp_new = PyType_GenericNew;
-    if (PyType_Ready (&PyColor_Type) < 0)
-        MODINIT_RETURN(NULL);
-    PyFRect_Type.tp_new = PyType_GenericNew;
-    if (PyType_Ready (&PyFRect_Type) < 0)
-        MODINIT_RETURN(NULL);
-    PyRect_Type.tp_new = PyType_GenericNew;
-    if (PyType_Ready (&PyRect_Type) < 0)
-        MODINIT_RETURN(NULL);
-    if (PyType_Ready (&PyBufferProxy_Type) < 0)
-        MODINIT_RETURN(NULL);
-    if (PyType_Ready (&PySurface_Type) < 0)
-        MODINIT_RETURN(NULL);
-    if (PyType_Ready(&PyFont_Type) < 0)
-        MODINIT_RETURN(NULL);
-
-    Py_INCREF (&PyColor_Type);
-    Py_INCREF (&PyFRect_Type);
-    Py_INCREF (&PyRect_Type);
-    Py_INCREF (&PyBufferProxy_Type);
-    Py_INCREF (&PySurface_Type);
-    Py_INCREF (&PyFont_Type);
-
 #ifdef IS_PYTHON_3
     mod = PyModule_Create (&_basemodule);
 #else
         MODINIT_RETURN(NULL);
     }
 
+    /* Complete types */
+    PyColor_Type.tp_new = PyType_GenericNew;
+    if (PyType_Ready (&PyColor_Type) < 0)
+        goto failed;
+    PyFRect_Type.tp_new = PyType_GenericNew;
+    if (PyType_Ready (&PyFRect_Type) < 0)
+        goto failed;
+    PyRect_Type.tp_new = PyType_GenericNew;
+    if (PyType_Ready (&PyRect_Type) < 0)
+        goto failed;
+    if (PyType_Ready (&PyBufferProxy_Type) < 0)
+        goto failed;
+    if (PyType_Ready (&PySurface_Type) < 0)
+        goto failed;
+    if (PyType_Ready(&PyFont_Type) < 0)
+        goto failed;
+
     Py_INCREF(state->error);
-    PyModule_AddObject (mod, "Error", state->error);
+    if (PyModule_AddObject (mod, "Error", state->error) == -1)
+    {
+        Py_DECREF (state->error);
+        goto failed;
+    }
 
-    PyModule_AddObject (mod, "Color", (PyObject *) &PyColor_Type);
-    PyModule_AddObject (mod, "Rect", (PyObject *) &PyRect_Type);
-    PyModule_AddObject (mod, "FRect", (PyObject *) &PyFRect_Type);
-    PyModule_AddObject (mod, "BufferProxy", (PyObject *) &PyBufferProxy_Type);
-    PyModule_AddObject (mod, "Surface", (PyObject *) &PySurface_Type);
-    PyModule_AddObject (mod, "Font", (PyObject *) &PyFont_Type);
+    ADD_OBJ_OR_FAIL (mod, "Color", PyColor_Type, failed);
+    ADD_OBJ_OR_FAIL (mod, "Rect", PyRect_Type, failed);
+    ADD_OBJ_OR_FAIL (mod, "FRect", PyFRect_Type, failed);
+    ADD_OBJ_OR_FAIL (mod, "BufferProxy", PyBufferProxy_Type, failed);
+    ADD_OBJ_OR_FAIL (mod, "Surface", PySurface_Type, failed);
+    ADD_OBJ_OR_FAIL (mod, "Font", PyFont_Type, failed);
     
     /* Export C API */
     c_api[PYGAME_BASE_FIRSTSLOT] = state->error;
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_BASE_ENTRY, c_api_obj);
+    {
+        if (PyModule_AddObject (mod, PYGAME_BASE_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto failed;
+        }
+    }
     MODINIT_RETURN(mod);
+
+failed:
+    Py_DECREF (mod);
+    MODINIT_RETURN (NULL);
 }

src/base/internals.h

 int FSizeFromObject (PyObject* obj, double *w, double *h);
 int ASCIIFromObject (PyObject *obj, char **text, PyObject **freeme);
 int UTF8FromObject (PyObject *obj, char **text, PyObject **freeme);
+int LongFromObj (PyObject* obj, long* val);
+unsigned long UlongFromObj (PyObject* obj, long* val);
 
 extern PyTypeObject PyColor_Type;
 #define PyColor_Check(x) (PyObject_TypeCheck(x, &PyColor_Type))

src/freetype/ft_mod.c

 #define PYGAME_FREETYPE_INTERNAL
 #define PYGAME_FREETYPE_FONT_INTERNAL
 
+#include "pymacros.h"
 #include "pgfreetype.h"
 #include "ft_wrap.h"
 #include "ft_mod.h"
     PyFreeTypeFont_Type.tp_base = &PyFont_Type;
     if (PyType_Ready(&PyFreeTypeFont_Type) < 0)
         goto fail;
-
-    Py_INCREF(&PyFreeTypeFont_Type);
-    PyModule_AddObject(mod, "Font", (PyObject *)&PyFreeTypeFont_Type); 
+    ADD_OBJ_OR_FAIL (mod, "Font", PyFreeTypeFont_Type, fail);
 
     /* 
      * Export C API.
      */
-
     ftfont_export_capi(c_api);
 
     c_api_obj = PyCObject_FromVoidPtr((void *) c_api, NULL);
 
     if (c_api_obj)
-        PyModule_AddObject(mod, PYGAME_FREETYPE_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject(mod, PYGAME_FREETYPE_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
 #ifdef HAVE_PYGAME_SDL_VIDEO
     if (import_pygame2_sdl_base () < 0)

src/mask/maskmod.c

 #define PYGAME_MASKMOD_INTERNAL
 
 #include <stdlib.h>
+#include "pymacros.h"
 #include "pgbase.h"
 #include "maskmod.h"
 #include "pgmask.h"
 
     /* get the size from the surface, and create the mask. */
     mask = bitmask_create (surf->w, surf->h);
-
     if (!mask)
     {
         PyErr_SetString (PyExc_MemoryError, "memory allocation failed");
         return NULL;
     }
 
+    /* create the new python object from mask */
+    maskobj = (PyMask*)PyMask_Type.tp_new (&PyMask_Type, NULL, NULL);
+    if (!maskobj)
+    {
+        bitmask_free (mask);
+        return NULL;
+    }
+    maskobj->mask = mask;
+
     /* lock the surface, release the GIL. */
-    lock = PySDLSurface_AcquireLockObj (surfobj, (PyObject*)mask);
+    lock = PySDLSurface_AcquireLockObj (surfobj, (PyObject*) maskobj);
     if (!lock)
         return NULL;
 
         }
     }
 
+    /* unlock the surface, release the GIL. */
     Py_END_ALLOW_THREADS;
-
-    /* unlock the surface, release the GIL.
-     */
     Py_DECREF (lock);
-
-    /*create the new python object from mask*/        
-    maskobj = (PyMask*)PyMask_Type.tp_new (&PyMask_Type, NULL, NULL);
-    if (maskobj)
-        maskobj->mask = mask;
-    else
-        bitmask_free (mask);
-
     return (PyObject*)maskobj;
 }
 
         PyModuleDef_HEAD_INIT, "mask", DOC_MASK, -1, _mask_methods,
         NULL, NULL, NULL, NULL
     };
-#endif
-
-    /* Complete types */
-    if (PyType_Ready (&PyMask_Type) < 0)
-        goto fail;
-    
-    Py_INCREF (&PyMask_Type);
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_maskmodule);
 #else
     mod = Py_InitModule3 ("mask", _mask_methods, DOC_MASK);
     if (!mod)
         goto fail;
 
-    PyModule_AddObject (mod, "Mask", (PyObject *) &PyMask_Type);
+    /* Complete types */
+    if (PyType_Ready (&PyMask_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "Mask", PyMask_Type, fail);
     
     mask_export_capi (c_api);
-
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_MASK_ENTRY, c_api_obj);
+    {
+        if (PyModule_AddObject (mod, PYGAME_MASK_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;

src/math/mathmod.c

 */
 #define PYGAME_MATH_INTERNAL
 
+#include "pymacros.h"
 #include "pgbase.h"
 #include "mathmod.h"
 #include "pgmath.h"
         _math_methods,
         NULL, NULL, NULL, NULL
     };
-#endif
-    PyVectorIter_Type.tp_new = &PyType_GenericNew;
-    PyVectorIter_Type.tp_iter = &PyObject_SelfIter;
-    if (PyType_Ready (&PyVectorIter_Type) < 0)
-        goto fail;
-    if (PyType_Ready (&PyVector_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyVector_Type);
-    PyVector2_Type.tp_base = &PyVector_Type; 
-    if (PyType_Ready (&PyVector2_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyVector2_Type);
-    PyVector3_Type.tp_base = &PyVector_Type; 
-    if (PyType_Ready (&PyVector3_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyVector3_Type);    
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("base", _math_methods, DOC_BASE);
     if (!mod)
         goto fail;
 
-    PyModule_AddObject (mod, "Vector", (PyObject *) &PyVector_Type);
-    PyModule_AddObject (mod, "Vector2", (PyObject *) &PyVector2_Type);
-    PyModule_AddObject (mod, "Vector3", (PyObject *) &PyVector3_Type);
+    PyVectorIter_Type.tp_new = &PyType_GenericNew;
+    PyVectorIter_Type.tp_iter = &PyObject_SelfIter;
+    if (PyType_Ready (&PyVectorIter_Type) < 0)
+        goto fail;
+    if (PyType_Ready (&PyVector_Type) < 0)
+        goto fail;
+    PyVector2_Type.tp_base = &PyVector_Type; 
+    if (PyType_Ready (&PyVector2_Type) < 0)
+        goto fail;
+    PyVector3_Type.tp_base = &PyVector_Type; 
+    if (PyType_Ready (&PyVector3_Type) < 0)
+        goto fail;
+
+    ADD_OBJ_OR_FAIL (mod, "Vector", PyVector_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Vector2", PyVector2_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Vector3", PyVector3_Type, fail);
 
     /* Export C API */
     c_api[PYGAME_MATH_FIRSTSLOT] = &VectorCoordsFromObj;
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_MATH_ENTRY, c_api_obj);
-        
+    {
+        if (PyModule_AddObject (mod, PYGAME_MATH_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
+   
     if (import_pygame2_base () < 0)
         goto fail;
     MODINIT_RETURN(mod);

src/openal/openalmod.c

 */
 #define PYGAME_OPENALBASE_INTERNAL
 
+#include "pymacros.h"
 #include "openalmod.h"
 #include "pgbase.h"
 #include "pgopenal.h"
     PyDevice_Type.tp_new = PyType_GenericNew;
     if (PyType_Ready (&PyDevice_Type) < 0)
         goto fail;
-
-    PyModule_AddObject (mod, "Device", (PyObject *) &PyDevice_Type);
     
+    ADD_OBJ_OR_FAIL (mod, "Device", PyDevice_Type, fail);
     MODINIT_RETURN(mod);
 fail:
     Py_XDECREF (mod);
+/**
+ * Useful python macros.
+ */
+
+#include <Python.h>
+
+#define ADD_OBJ_OR_FAIL(mod,name,type,mark)                       \
+    Py_INCREF(&type);                                             \
+    if (PyModule_AddObject (mod, name, (PyObject *) &type) == -1) \
+    {                                                             \
+        Py_DECREF(&type);                                         \
+        goto mark;                                                \
+    }

src/sdl/cdrommod.c

 */
 #define PYGAME_SDLCDROM_INTERNAL
 
+#include "pymacros.h"
 #include "cdrommod.h"
 #include "pgsdl.h"
 #include "sdlcdrom_doc.h"
     _SDLCDromState *state;
 
     static void *c_api[PYGAME_SDLCDROM_SLOTS];
-    
-    /* Complete types */
-    if (PyType_Ready (&PyCD_Type) < 0)
-        goto fail;
-    if (PyType_Ready (&PyCDTrack_Type) < 0)
-        goto fail;
-
-    Py_INCREF (&PyCD_Type);
-    Py_INCREF (&PyCDTrack_Type);
 
 #ifdef IS_PYTHON_3
     mod = PyModule_Create (&_cdrommodule);
     for (i = 0; i < MAX_CDROMS; i++)
         state->cdrom_drives[i] = NULL;
 
-    PyModule_AddObject (mod, "CD", (PyObject *) &PyCD_Type);
-    PyModule_AddObject (mod, "CDTrack", (PyObject *) &PyCDTrack_Type);
+    
+    /* Complete types */
+    if (PyType_Ready (&PyCD_Type) < 0)
+        goto fail;
+    if (PyType_Ready (&PyCDTrack_Type) < 0)
+        goto fail;
+
+    ADD_OBJ_OR_FAIL (mod, "CD", PyCD_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "CDTrack", PyCDTrack_Type, fail);
 
     cdrom_export_capi (c_api);
     cdtrack_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLCDROM_ENTRY, c_api_obj);    
-
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLCDROM_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
     if (import_pygame2_base () < 0)
         goto fail;
     if (import_pygame2_sdl_base () < 0)

src/sdl/eventmod.c

 */
 #define PYGAME_SDLEVENT_INTERNAL
 
+#include "pymacros.h"
 #include "eventmod.h"
 #include "pgsdl.h"
 #include "sdlevent_doc.h"
     _SDLEventState *state;
     static void *c_api[PYGAME_SDLEVENT_SLOTS];
 
-    /* Complete types */
-    if (PyType_Ready (&PyEvent_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyEvent_Type);
-
 #ifdef IS_PYTHON_3
     mod = PyModule_Create (&_eventmodule);
 #else
     state = SDLEVENT_MOD_STATE(mod);
     state->filterhook = NULL;
 
-    PyModule_AddObject (mod, "Event", (PyObject *) &PyEvent_Type);
+    /* Complete types */
+    if (PyType_Ready (&PyEvent_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "Event", PyEvent_Type, fail);
 
     event_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLEVENT_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLEVENT_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;

src/sdl/joystickmod.c

 */
 #define PYGAME_SDLJOYSTICK_INTERNAL
 
+#include "pymacros.h"
 #include "joystickmod.h"
 #include "pgsdl.h"
 #include "sdljoystick_doc.h"
     _SDLJoystickState *state;
     static void *c_api[PYGAME_SDLJOYSTICK_SLOTS];
 
-    /* Complete types */
-    if (PyType_Ready (&PyJoystick_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyJoystick_Type);
-
-
 #ifdef IS_PYTHON_3
     mod = PyModule_Create (&_joystickmodule);
 #else
     for (i = 0; i < MAX_JOYSTICKS; i++)
         state->joysticks[i] = NULL;
 
-    PyModule_AddObject (mod, "Joystick", (PyObject *) &PyJoystick_Type);
+    /* Complete types */
+    if (PyType_Ready (&PyJoystick_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "Joystick", PyJoystick_Type, fail);
 
     joystick_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLJOYSTICK_ENTRY, c_api_obj);    
-
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLJOYSTICK_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
     if (import_pygame2_base () < 0)
         goto fail;
     if (import_pygame2_sdl_base () < 0)

src/sdl/mousemod.c

 */
 #define PYGAME_SDLMOUSE_INTERNAL
 
+#include "pymacros.h"
 #include "mousemod.h"
 #include "pgsdl.h"
 #include "sdlmouse_doc.h"
         _mouse_methods,
         NULL, NULL, NULL, NULL
     };
-#endif
-
-    /* Complete types */
-    if (PyType_Ready (&PyCursor_Type) < 0)
-        goto fail;
-    
-    Py_INCREF (&PyCursor_Type);
-    
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("mouse", _mouse_methods, DOC_MOUSE);
     if (!mod)
         goto fail;
 
-    PyModule_AddObject (mod, "Cursor", (PyObject *) &PyCursor_Type);
+    /* Complete types */
+    if (PyType_Ready (&PyCursor_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "Cursor", PyCursor_Type, fail);
     
     cursor_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLMOUSE_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLMOUSE_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     cursors = PyImport_ImportModule ("cursors");
     if (cursors)
-        PyModule_AddObject (mod, "cursors", cursors);
+    {
+        if (PyModule_AddObject (mod, "cursors", cursors) == -1)
+        {
+            Py_DECREF (cursors);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;

src/sdl/rwopsmod.c

         NULL,
         NULL, NULL, NULL, NULL
     };
-#endif
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("rwops", NULL, "");
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLRWOPS_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLRWOPS_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;
    
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLBASE_ENTRY, c_api_obj);    
-
-    Py_AtExit (_quit);
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLBASE_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;
 
+    Py_AtExit (_quit);
     MODINIT_RETURN (mod);
 fail:
     Py_XDECREF (mod);

src/sdl/videomod.c

 */
 #define PYGAME_SDLVIDEO_INTERNAL
 
+#include "pymacros.h"
 #include "videomod.h"
 #include "pgsdl.h"
 #include "sdlvideo_doc.h"
         _video_methods,
         NULL, NULL, NULL, NULL
     };
+    mod = PyModule_Create (&_module);
+#else
+    mod = Py_InitModule3 ("video", _video_methods, DOC_VIDEO);
 #endif
+    if (!mod)
+        goto fail;
 
     if (import_pygame2_base () < 0)
         goto fail;
     if (PyType_Ready (&PyPixelFormat_Type) < 0)
         goto fail;
 
-    Py_INCREF (&PySDLSurface_Type);
-    Py_INCREF (&PyOverlay_Type);
-    Py_INCREF (&PyPixelFormat_Type);
-
-#ifdef IS_PYTHON_3
-    mod = PyModule_Create (&_module);
-#else
-    mod = Py_InitModule3 ("video", _video_methods, DOC_VIDEO);
-#endif
-    if (!mod)
-        goto fail;
-    
-    PyModule_AddObject (mod, "PixelFormat", (PyObject *) &PyPixelFormat_Type);
-    PyModule_AddObject (mod, "Surface", (PyObject *) &PySDLSurface_Type);
-    PyModule_AddObject (mod, "Overlay", (PyObject *) &PyOverlay_Type);
+    ADD_OBJ_OR_FAIL (mod, "PixelFormat", PyPixelFormat_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Surface", PySDLSurface_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Overlay", PyOverlay_Type, fail);
     
     c_api[PYGAME_SDLVIDEO_FIRSTSLOT+0] = ColorFromObj;
 
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLVIDEO_ENTRY, c_api_obj);    
-
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLVIDEO_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
     MODINIT_RETURN(mod);
 
 fail:

src/sdlext/sdlextmod.c

 */
 #define PYGAME_SDLEXT_INTERNAL
 
+#include "pymacros.h"
 #include "sdlextmod.h"
 #include "pgsdlext.h"
 #include "pgsdl.h"
         NULL,
         NULL, NULL, NULL, NULL
     };
-#endif
-
-    if (PyType_Ready (&PyPixelArray_Type) < 0)
-        goto fail;
-    Py_INCREF (&PyPixelArray_Type);
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("base", NULL, DOC_BASE);
     if (!mod)
         goto fail;
 
-    PyModule_AddObject (mod, "PixelArray", (PyObject *) &PyPixelArray_Type);
+    if (PyType_Ready (&PyPixelArray_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "PixelArray", PyPixelArray_Type, fail);
     
     /* Export C API */
     pixelarray_export_capi (c_api);
    
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLEXT_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLEXT_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;

src/sdlgfx/gfxmod.c

 #define PYGAME_SDLGFX_INTERNAL
 
 #include <SDL_gfxPrimitives.h>
+#include "pymacros.h"
 #include "gfxmod.h"
 #include "pggfx.h"
 #include "pgsdl.h"
         _gfx_methods,
         NULL, NULL, NULL, NULL
     };
-#endif
-    if (PyType_Ready (&PyFPSmanager_Type) < 0)
-        goto fail;
-
-    Py_INCREF (&PyFPSmanager_Type);
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("base", _gfx_methods, DOC_BASE);
     if (!mod)
         goto fail;
 
-    PyModule_AddObject (mod, "FPSmanager", (PyObject *) &PyFPSmanager_Type);
+    if (PyType_Ready (&PyFPSmanager_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "FPSmanager", PyFPSmanager_Type, fail);
 
     /* Export C API */
     fps_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLGFX_ENTRY, c_api_obj);
-        
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLGFX_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }   
     if (import_pygame2_base () < 0)
         goto fail;
     if (import_pygame2_sdl_base () < 0)

src/sdlmixer/mixermod.c

 */
 #define PYGAME_SDLMIXER_INTERNAL
 
+#include "pymacros.h"
 #include "mixermod.h"
 #include "pgmixer.h"
 #include "pgsdl.h"
         _mixer_methods,
         NULL, NULL, NULL, NULL
     };
-#endif
-    if (PyType_Ready (&PyChunk_Type) < 0)
-        goto fail;
-    if (PyType_Ready (&PyChannel_Type) < 0)
-        goto fail;
-    if (PyType_Ready (&PyMusic_Type) < 0)
-        goto fail;
-
-    Py_INCREF (&PyChunk_Type);
-    Py_INCREF (&PyChannel_Type);
-    Py_INCREF (&PyMusic_Type);
-
-
-#ifdef IS_PYTHON_3
     mod = PyModule_Create (&_module);
 #else
     mod = Py_InitModule3 ("base", _mixer_methods, DOC_BASE);
     if (!mod)
         goto fail;
         
-    PyModule_AddObject (mod, "Chunk", (PyObject *) &PyChunk_Type);
-    PyModule_AddObject (mod, "Channel", (PyObject *) &PyChannel_Type);
-    PyModule_AddObject (mod, "Music", (PyObject *) &PyMusic_Type);
+    if (PyType_Ready (&PyChunk_Type) < 0)
+        goto fail;
+    if (PyType_Ready (&PyChannel_Type) < 0)
+        goto fail;
+    if (PyType_Ready (&PyMusic_Type) < 0)
+        goto fail;
+    ADD_OBJ_OR_FAIL (mod, "Chunk", PyChunk_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Channel", PyChannel_Type, fail);
+    ADD_OBJ_OR_FAIL (mod, "Music", PyMusic_Type, fail);
 
     /* Export C API */
     chunk_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLMIXER_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLMIXER_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_base () < 0)
         goto fail;

src/sdlttf/ttfmod.c

 
 */
 #define PYGAME_SDLTTF_INTERNAL
-//#define PYGAME_FONT_INTERNAL
 
+#include "pymacros.h"
 #include "ttfmod.h"
 #include "pgttf.h"
 #include "pgsdl.h"
         _ttf_methods,
         NULL, NULL, NULL, NULL
     };
+    mod = PyModule_Create (&_module);
+#else
+    mod = Py_InitModule3 ("base", _ttf_methods, DOC_BASE);
 #endif
-
+    if (!mod)
+        goto fail;
+    
     /* Import Pygame2 Base API to access PyFont_Type */
     if (import_pygame2_base () < 0)
         goto fail;
     if (PyType_Ready (&PySDLFont_TTF_Type) < 0)
         goto fail;
 
-    Py_INCREF (&PySDLFont_TTF_Type);
-
-#ifdef IS_PYTHON_3
-    mod = PyModule_Create (&_module);
-#else
-    mod = Py_InitModule3 ("base", _ttf_methods, DOC_BASE);
-#endif
-    if (!mod)
-        goto fail;
-    
-    PyModule_AddObject (mod, "Font", (PyObject *) &PySDLFont_TTF_Type);
+    ADD_OBJ_OR_FAIL (mod, "Font", PySDLFont_TTF_Type, fail);
 
     /* Export C API */
     font_export_capi (c_api);
 
     c_api_obj = PyCObject_FromVoidPtr ((void *) c_api, NULL);
     if (c_api_obj)
-        PyModule_AddObject (mod, PYGAME_SDLTTF_ENTRY, c_api_obj);    
+    {
+        if (PyModule_AddObject (mod, PYGAME_SDLTTF_ENTRY, c_api_obj) == -1)
+        {
+            Py_DECREF (c_api_obj);
+            goto fail;
+        }
+    }
 
     if (import_pygame2_sdl_base () < 0)
         goto fail;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.