pygame  committed 59379be

smarter missing modules

  • Participants
  • Parent commits c4c18e4
  • Branches default

Comments (0)

Files changed (2)

 # BREAK = change breaks existing code
 # BUG	= fixed a bug that was (or could have been) crashing
+May 27, 2002
+	added smart class to handle missing modules
 May 17, 2002
 	added display.set_gamma_ramp(). thx andrew

File lib/

         #import pygame.macosx
+class MissingModule:
+    def __init__(self, name, info='', urgent=0):
+ = name
+ = str(info)
+        self.urgent = urgent
+        self.ver = sys.version_info
+        if urgent:
+            self.warn()
+    def __getattr__(self, var):
+        if not self.urgent:
+            self.warn()
+        MissingModule = "%s module not available"
+        raise NotImplementedError, MissingModule
+    def __nonzero__(self):
+        return 0
+    def warn(self):
+        if self.urgent: type = 'import'
+        else: type = 'use'
+        message = '%s %s: %s' % (type,,
+        if self.ver[0]>=2 and self.ver[1]>=1:
+            import warnings
+            if self.urgent: level = 4
+            else: level = 3
+            warnings.warn(message, RuntimeWarning, level)
+        else:
+            print message
 #we need to import like this, each at a time. the cleanest way to import
 #our modules is with the import command (not the __import__ function)
 #next, the "standard" modules
 #we still allow them to be missing for stripped down pygame distributions
 try: import pygame.cdrom
-except (ImportError,IOError):cdrom=None
+except (ImportError,IOError), msg:cdrom=MissingModule("cdrom", msg, 1)
 try: import pygame.cursors
-except (ImportError,IOError):cursors=None
+except (ImportError,IOError), msg:cursors=MissingModule("cursors", msg, 1)
 try: import pygame.display
-except (ImportError,IOError):display=None
+except (ImportError,IOError), msg:display=MissingModule("display", msg, 1)
 try: import pygame.draw
-except (ImportError,IOError):draw=None
+except (ImportError,IOError), msg:draw=MissingModule("draw", msg, 1)
 try: import pygame.event
-except (ImportError,IOError):event=None
+except (ImportError,IOError), msg:event=MissingModule("event", msg, 1)
 try: import pygame.image
-except (ImportError,IOError):image=None
+except (ImportError,IOError), msg:image=MissingModule("image", msg, 1)
 try: import pygame.joystick
-except (ImportError,IOError):joystick=None
+except (ImportError,IOError), msg:joystick=MissingModule("joystick", msg, 1)
 try: import pygame.key
-except (ImportError,IOError):key=None
+except (ImportError,IOError), msg:key=MissingModule("key", msg, 1)
 try: import pygame.mouse
-except (ImportError,IOError):mouse=None
+except (ImportError,IOError), msg:mouse=MissingModule("mouse", msg, 1)
 try: import pygame.sprite
-except (ImportError,IOError):sprite=None
+except (ImportError,IOError), msg:sprite=MissingModule("sprite", msg, 1)
 try: from pygame.surface import *
 except (ImportError,IOError):Surface = lambda:Missing_Function
 try: import pygame.time
-except (ImportError,IOError):time=None
+except (ImportError,IOError), msg:time=MissingModule("time", msg, 1)
 try: import pygame.transform
-except (ImportError,IOError):transform=None
+except (ImportError,IOError), msg:transform=MissingModule("transform", msg, 1)
 #lastly, the "optional" pygame modules
 try: import pygame.font
-except (ImportError,IOError):font=None
+except (ImportError,IOError), msg:font=MissingModule("font", msg, 0)
 try: import pygame.mixer
-except (ImportError,IOError):mixer=None
+except (ImportError,IOError), msg:mixer=MissingModule("mixer", msg, 0)
 try: import
-except (ImportError,IOError):movie=None
+except (ImportError,IOError), msg:movie=MissingModule("movie", msg, 0)
 try: import pygame.surfarray
-except (ImportError,IOError):surfarray=None
+except (ImportError,IOError), msg:surfarray=MissingModule("surfarray", msg, 0)
 try: import pygame.sndarray
-except (ImportError,IOError):sndarray=None
+except (ImportError,IOError), msg:sndarray=MissingModule("sndarray", msg, 0)
 #there's also a couple "internal" modules not needed
 #by users, but putting them here helps "dependency finder"
 #cleanup namespace
-del pygame, os, sys, rwobject, surflock
+del pygame, os, sys, rwobject, surflock, MissingModule