Commits

Lenard Lindstrom committed 65e001d

bug fix - required modules not raising error when failing to import.
Extension modules no longer absorb import errors.
When a required module fails to import, the extension module itself won't load.

Comments (0)

Files changed (30)

     def __init__(self, name, info='', urgent=0):
         self.name = name
         self.info = str(info)
+        try:
+            exc = sys.exc_info()
+            if exc[0] != None:
+                self.reason = "%s: %s" % (exc[0].__name__, str(exc[1]))
+            else:
+                self.reason = ""
+        finally:
+            del exc
         self.urgent = urgent
         if urgent:
             self.warn()
             self.warn()
             self.urgent = 1
         MissingPygameModule = "%s module not available" % self.name
+        if self.reason:
+            MissingPygameModule += "\n(%s)" % self.reason
         raise NotImplementedError, MissingPygameModule
 
     def __nonzero__(self):
         if self.urgent: type = 'import'
         else: type = 'use'
         message = '%s %s: %s' % (type, self.name, self.info)
+        if self.reason:
+            message += "\n(%s)" % self.reason
         try:
             import warnings
             if self.urgent: level = 4
 
 try: from pygame.surface import *
 except (ImportError,IOError):Surface = lambda:Missing_Function
-else:
-    # Required by pygame.surface.
-    import pygame.bufferproxy
 
 
 try:
 
 try: import pygame.mixer
 except (ImportError,IOError), msg:mixer=MissingModule("mixer", msg, 0)
-else:
-    # Required by pygame.mixer.
-    import pygame.bufferproxy
 
 try: import pygame.movie
 except (ImportError,IOError), msg:movie=MissingModule("movie", msg, 0)
 try: import pygame.scrap
 except (ImportError,IOError), msg:scrap=MissingModule("scrap", msg, 0)
 
-try: import pygame.numpyarray
-except (ImportError,IOError), msg:numpyarray=MissingModule("numpyarray", msg, 0)
-
 try: import pygame.surfarray
 except (ImportError,IOError), msg:surfarray=MissingModule("surfarray", msg, 0)
 
     """
     Some additional things that py2app/py2exe will want to see
     """
+    import bufferproxy
     import Numeric
     import numpy
     import OpenGL.GL

src/_arraysurfarray.c

 PYGAME_EXPORT
 void init_arraysurfarray(void)
 {
-    PyObject *module;
-    
-    module = Py_InitModule3("_arraysurfarray", surfarray_builtins,
-                            DOC_PYGAMESURFARRAY);
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
     import_pygame_base();
+    if (PyErr_Occurred ()) {
+	return;
+    }
     import_pygame_surface();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    Py_InitModule3("_arraysurfarray", surfarray_builtins, DOC_PYGAMESURFARRAY);
 }

src/_numericsndarray.c

 PYGAME_EXPORT
 void init_numericsndarray (void)
 {
-    PyObject *module, *dict;
-    PyObject *numeric_module;
-    
-    numeric_module = PyImport_ImportModule ("Numeric");
-    if (numeric_module != 0)
-    {
-	/* create the module */
-	module = Py_InitModule3 ("_numericsndarray", sndarray_builtins,
-				 DOC_PYGAMESNDARRAY);
-	dict = PyModule_GetDict (module);
-        
-	/*imported needed apis*/
-	import_pygame_base ();
-	import_pygame_mixer ();
-	import_array ();
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
     }
+    import_pygame_mixer ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_array ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* create the module */
+    Py_InitModule3 ("_numericsndarray", sndarray_builtins, DOC_PYGAMESNDARRAY);
 }

src/_numericsurfarray.c

 PYGAME_EXPORT
 void init_numericsurfarray (void)
 {
-    PyObject *module;
-    PyObject* numeric_module;
-    
-    numeric_module = PyImport_ImportModule ("Numeric");
-    if (numeric_module != 0)
-    {
-        /* create the module */
-        module = Py_InitModule3 ("_numericsurfarray", surfarray_builtins,
-                                 DOC_PYGAMESURFARRAY);
-	    
-        import_pygame_base ();
-        import_pygame_surface ();
-        import_array ();
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
     }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_array ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* create the module */
+    Py_InitModule3 ("_numericsurfarray", surfarray_builtins, DOC_PYGAMESURFARRAY);
 }
 void initcamera(void)
 {
   PyObject *module, *dict;
+  /* imported needed apis; Do this first so if there is an error
+     the module is not loaded.
+  */
+  import_pygame_base ();
+  if (PyErr_Occurred ()) {
+    return;
+  }
+  import_pygame_surface ();
+  if (PyErr_Occurred ()) {
+    return;
+  }
+
+  /* type preparation */
   PyType_Init(PyCamera_Type);
   
   /* create the module */
   module = Py_InitModule3("camera", camera_builtins, DOC_PYGAMECAMERA);
   dict = PyModule_GetDict(module);
   PyDict_SetItemString(dict, "CameraType", (PyObject *)&PyCamera_Type);
-  import_pygame_base ();
-  import_pygame_surface ();
 }
     PyObject *module, *dict, *apiobj;
     static void* c_api[PYGAMEAPI_CDROM_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyCD_Type);
 
     /* create the module */
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
     PyObject *apiobj;
     static void* c_api[PYGAMEAPI_COLOR_NUMSLOTS];
 
-    if (PyType_Ready (&PyColor_Type) < 0)
-        return;
-    
-    /* create the module */
-    module = Py_InitModule3 ("color", NULL, "color module for pygame");
-    PyColor_Type.tp_getattro = PyObject_GenericGetAttr;
-    Py_INCREF (&PyColor_Type);
-    PyModule_AddObject (module, "Color", (PyObject *) &PyColor_Type);
-    dict = PyModule_GetDict (module);
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
 
     colordict = PyImport_ImportModule ("pygame.colordict");
     if (colordict)
         PyObject *_dict = PyModule_GetDict (colordict);
         PyObject *colors = PyDict_GetItemString (_dict, "THECOLORS");
         Py_INCREF (colors);
-        Py_INCREF (colors); /* Needed for the _AddObject call beneath */
         _COLORDICT = colors;
-        PyModule_AddObject (module, "THECOLORS", colors);
         Py_DECREF (colordict);
     }
+    else
+    {
+	return;
+    }
 
-    import_pygame_base ();
+    /* type preparation */
+    if (PyType_Ready (&PyColor_Type) < 0)
+    {
+	Py_DECREF (_COLORDICT);
+        return;
+    }
+    
+    /* create the module */
+    module = Py_InitModule3 ("color", NULL, "color module for pygame");
+    PyColor_Type.tp_getattro = PyObject_GenericGetAttr;
+    Py_INCREF (&PyColor_Type);
+    PyModule_AddObject (module, "Color", (PyObject *) &PyColor_Type);
+    Py_INCREF (_COLORDICT);
+    PyModule_AddObject (module, "THECOLORS", _COLORDICT);
+    dict = PyModule_GetDict (module);
 
     c_api[0] = &PyColor_Type;
     c_api[1] = PyColor_New;
     PyObject *module, *dict, *apiobj;
     static void* c_api[PYGAMEAPI_DISPLAY_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rect ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyVidInfo_Type);
 
     /* create the module */
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_rect ();
-    import_pygame_surface ();
 }
 PYGAME_EXPORT
 void initdraw(void)
 {
-	PyObject *module, *dict;
+        PyObject *module;
 
-    /* create the module */
+	/* imported needed apis; Do this first so if there is an error
+	   the module is not loaded.
+	*/
+	import_pygame_base();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+	import_pygame_color();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+	import_pygame_rect();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+	import_pygame_surface();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+
+	/* create the module */
 	module = Py_InitModule3("draw", draw_builtins, DOC_PYGAMEDRAW);
-	dict = PyModule_GetDict(module);
-
-	/*imported needed apis*/
-	import_pygame_base();
-        import_pygame_color();
-	import_pygame_rect();
-	import_pygame_surface();
 }
 
 
     PyObject *module, *dict, *apiobj;
     static void* c_api[PYGAMEAPI_EVENT_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyEvent_Type);
 
     /* create the module */
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
 
-    /*imported needed apis*/
-    import_pygame_base ();
     PyGame_RegisterQuit (user_event_cleanup);
 }
 {
     PyObject *module, *eventmodule, *dict;
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_event ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     module = Py_InitModule3 ("fastevent", fastevent_builtins,
                              doc_fastevent_MODULE);
     dict = PyModule_GetDict (module);
 
-    /* imported needed apis */
-    import_pygame_base ();
-    import_pygame_event ();
+    /* add the event module functions if available */
     eventmodule = PyImport_ImportModule ("pygame.event");
     if (eventmodule)
     {
 
     PyFONT_C_API[0] = PyFONT_C_API[0]; /*clean an unused warning*/
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_color ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     if (PyType_Ready (&PyFont_Type) < 0)
         return;
 
 
     /*imported needed apis*/
     import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
     import_pygame_color ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
     import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
     import_pygame_rwobject ();
 }
     PyObject *module, *dict;
     PyObject *extmodule;
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     module = Py_InitModule3 ("image", image_builtins, DOC_PYGAMEIMAGE);
     dict = PyModule_GetDict (module);
         */
         is_extended = 0;
     }
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_surface ();
-    import_pygame_rwobject ();
 }
 PYGAME_EXPORT
 void initimageext (void)
 {
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     Py_InitModule3 ("imageext", image_builtins, NULL);
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_surface ();
-    import_pygame_rwobject ();
 }
     PyObject *module, *dict, *apiobj;
     static void* c_api[PYGAMEAPI_JOYSTICK_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyJoystick_Type);
 
     /* create the module */
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
 {
     PyObject *module, *dict;
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     module = Py_InitModule3 ("key", key_builtins, DOC_PYGAMEKEY);
     dict = PyModule_GetDict (module);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
   PyObject *module, *dict, *apiobj;
   static void* c_api[PYGAMEAPI_MASK_NUMSLOTS];
 
+  /* imported needed apis; Do this first so if there is an error
+     the module is not loaded.
+  */
+  import_pygame_base ();
+  if (PyErr_Occurred ()) {
+    return;
+  } 
+  import_pygame_color ();
+  if (PyErr_Occurred ()) {
+    return;
+  }
+  import_pygame_surface ();
+  if (PyErr_Occurred ()) {
+    return;
+  }
+  import_pygame_rect ();
+  if (PyErr_Occurred ()) {
+    return;
+  }
+
   /* create the mask type */
   PyType_Init(PyMask_Type);
 
   c_api[0] = &PyMask_Type;
   apiobj   = PyCObject_FromVoidPtr (c_api, NULL);
   PyModule_AddObject (module, PYGAMEAPI_LOCAL_ENTRY, apiobj);
-
-  /* import other modules */
-  import_pygame_base ();
-  import_pygame_color ();
-  import_pygame_surface ();
-  import_pygame_rect ();
 }
 
 
     PyMIXER_C_API[0] = PyMIXER_C_API[0]; /*this cleans an unused warning*/
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+
+    /*imported needed apis*/
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_bufferproxy ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     if (PyType_Ready (&PySound_Type) < 0)
         return;
     PyType_Init (PyChannel_Type);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
 
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_rwobject ();
-    import_pygame_bufferproxy ();
-
     music = PyImport_ImportModule("pygame.mixer_music");
     if (!music) {
         /* try loading it under this name...
 PYGAME_EXPORT
 void initmouse (void)
 {
-    PyObject *module, *dict;
+    PyObject *module;
+
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
 
     /* create the module */
     module = Py_InitModule3 ("mouse", mouse_builtins, DOC_PYGAMEMOUSE);
-    dict = PyModule_GetDict (module);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
 {
     PyObject *module, *dict;
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rect ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyMovie_Type);
 
     /* create the module */
     dict = PyModule_GetDict (module);
 
     PyDict_SetItemString (dict, "MovieType", (PyObject *)&PyMovie_Type);
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_surface ();
-    import_pygame_rwobject ();
-    import_pygame_rect ();
 }
 PYGAME_EXPORT
 void initmovieext(void)
 {
-	PyObject *module, *dict;
+        PyObject *module, *dict;
 
+	/* imported needed apis; Do this first so if there is an error
+	   the module is not loaded.
+	*/
+	import_pygame_base();
+        if (PyErr_Occurred ()) {
+	    return;
+        }
+	import_pygame_surface();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+	import_pygame_rwobject();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+	import_pygame_rect();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+    
+	/* type preparation */
 	PyType_Init(PyMovie_Type);
 
 	/* create the module */
 
         /*imported needed apis*/
 	import_pygame_base();
+        if (PyErr_Occurred ()) {
+	    return;
+        }
 	import_pygame_surface();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
 	import_pygame_rwobject();
+	if (PyErr_Occurred ()) {
+	    return;
+	}
 	import_pygame_rect();
-
+	if (PyErr_Occurred ()) {
+	    return;
+	}
+    
     PyGame_RegisterQuit(autoquit);
 }
 
     PyObject *module;
 
     PyMIXER_C_API[0] = PyMIXER_C_API[0]; /*clean an unused warning*/
+
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rwobject ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     module = Py_InitModule3 ("mixer_music", music_builtins,
                              DOC_PYGAMEMIXERMUSIC);
                        PyCObject_FromVoidPtr (&current_music, NULL));
     PyModule_AddObject(module, "_QUEUE_POINTER",
                        PyCObject_FromVoidPtr (&queue_music, NULL));
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_rwobject ();
 }
 void initoverlay (void)
 {
     PyObject *module;
+
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rect ();    
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* create the module */
     module = Py_InitModule ("overlay", overlay_methods );
 
     PyOverlay_Type.ob_type = &PyType_Type;
 
     /* create the module reference */
     PyModule_AddObject (module, "Overlay", (PyObject *)&PyOverlay_Type);
-
-    import_pygame_base ();
-    import_pygame_rect ();    
 }
     PyObject *apiobj;
     static void* c_api[PYGAMEAPI_PIXELARRAY_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_color();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     if (PyType_Ready (&PyPixelArray_Type) < 0)
         return;
     
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
-
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_color();
-    import_pygame_surface ();
 }
     (*(int(*)(PyObject*,PyObject*,SDL_Rect*,SDL_Rect*,int))             \
      PyGAME_C_API[PYGAMEAPI_SURFACE_FIRSTSLOT + 2])
 
-#define import_pygame_surface() {                                   \
+#define import_pygame_surface() do {                                   \
 	PyObject *_module = PyImport_ImportModule("pygame.surface"); \
 	if (_module != NULL) {                                       \
             PyObject *_dict = PyModule_GetDict(_module);                  \
             }                                                           \
             Py_DECREF(_module);                                          \
         }                                                               \
+	else                                                            \
+	{                                                               \
+	    break;                                                      \
+	}                                                               \
 	_module = PyImport_ImportModule("pygame.surflock");              \
 	if (_module != NULL) {                                           \
             PyObject *_dict = PyModule_GetDict(_module);                  \
             }                                                           \
             Py_DECREF(_module);                                          \
         }                                                               \
-    }
+    } while (0)
 #endif
 
 
     PyObject *module, *dict, *apiobj;
     static void* c_api[PYGAMEAPI_RECT_NUMSLOTS];
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* Create the module and add the functions */
     PyType_Init (PyRect_Type);
     if (PyType_Ready (&PyRect_Type) < 0)
     apiobj = PyCObject_FromVoidPtr (c_api, NULL);
     PyDict_SetItemString (dict, PYGAMEAPI_LOCAL_ENTRY, apiobj);
     Py_DECREF (apiobj);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
 {
     PyObject *mod;
 
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
     /* create the module */
     mod = Py_InitModule3 ("scrap", scrap_builtins, NULL);
-
-    /*imported needed apis*/
-    import_pygame_base ();
 }
     PyObject *module, *dict, *apiobj, *lockmodule;
     static void* c_api[PYGAMEAPI_SURFACE_NUMSLOTS];
     
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_color ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rect ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_bufferproxy();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* import the surflock module manually */
+    lockmodule = PyImport_ImportModule ("pygame.surflock");
+    if (lockmodule != NULL)
+    {
+        PyObject *_dict = PyModule_GetDict (lockmodule);
+        PyObject *_c_api = PyDict_GetItemString (_dict, PYGAMEAPI_LOCAL_ENTRY);
+
+        if (PyCObject_Check (_c_api))
+        {
+            int i;
+            void **localptr = (void *)PyCObject_AsVoidPtr (_c_api);
+
+            for (i = 0; i < PYGAMEAPI_SURFLOCK_NUMSLOTS; ++i)
+                PyGAME_C_API[i + PYGAMEAPI_SURFLOCK_FIRSTSLOT] = localptr[i];
+        }
+        Py_DECREF (lockmodule);
+    }
+    else
+    {
+	return;
+    }
+
+    /* type preparation */
     if (PyType_Ready(&PySurface_Type) < 0)
         return;
     
     Py_DECREF (apiobj);
     /* Py_INCREF (PySurface_Type.tp_dict); INCREF's done in SetItemString */
     PyDict_SetItemString (dict, "_dict", PySurface_Type.tp_dict);
-
-    /* imported needed apis */
-    import_pygame_base ();
-    import_pygame_color ();
-    import_pygame_rect ();
-    import_pygame_bufferproxy();
-
-    /* import the surflock module manually */
-    lockmodule = PyImport_ImportModule ("pygame.surflock");
-    if (lockmodule != NULL)
-    {
-        PyObject *_dict = PyModule_GetDict (lockmodule);
-        PyObject *_c_api = PyDict_GetItemString (_dict, PYGAMEAPI_LOCAL_ENTRY);
-
-        if (PyCObject_Check (_c_api))
-        {
-            int i;
-            void **localptr = (void *)PyCObject_AsVoidPtr (_c_api);
-
-            for (i = 0; i < PYGAMEAPI_SURFLOCK_NUMSLOTS; ++i)
-                PyGAME_C_API[i + PYGAMEAPI_SURFLOCK_FIRSTSLOT] = localptr[i];
-        }
-        Py_DECREF (lockmodule);
-    }
 }
 {
     PyObject *module;
     
+    /* need to import base module, just so SDL is happy. Do this first so if
+       the module is there is an error the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* type preparation */
     PyType_Init (PyClock_Type);
     
     /* create the module */
     module = Py_InitModule3 ("time", time_builtins, DOC_PYGAMETIME);
-    
-    /*need to import base module, just so SDL is happy*/
-    import_pygame_base ();
 }
 void inittransform (void)
 {
     PyObject *module;
+
+    /* imported needed apis; Do this first so if there is an error
+       the module is not loaded.
+    */
+    import_pygame_base ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_color ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_rect ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+    import_pygame_surface ();
+    if (PyErr_Occurred ()) {
+	return;
+    }
+
+    /* create the module */
     module = Py_InitModule3 ("transform", transform_builtins,
                              DOC_PYGAMETRANSFORM);
 
-    /*imported needed apis*/
-    import_pygame_base ();
-    import_pygame_color ();
-    import_pygame_rect ();
-    import_pygame_surface ();
-
     smoothscale_init();
 }