Commits

Jon Nordby  committed 9e95d8e

Image: Don't fail silently on load errors

If opening the image failed for any reason (permissions, wrong path, ...)
the error would be silently hidden by the catch-all except clause.
Instead only catch AttributeError to detect whether self.file_name is
a file-like object or a path string.

  • Participants
  • Parent commits 2f97be6

Comments (0)

Files changed (1)

         
     def _loadSurface_(self):
         """load image from file or file_obj"""
+
         try:
-            path = os.path.join(PROJECT_DIR, self.file_name)
-            surface = pygame.image.load(path).convert_alpha()
-        except:
-            path = self.file_name
-            buf = io.BytesIO(path.read())
-            surface = pygame.image.load(buf).convert_alpha()
+            imagelike = io.BytesIO(self.file_name.read())
+        except AttributeError:
+            imagelike = os.path.join(PROJECT_DIR, self.file_name)
+        surface = pygame.image.load(imagelike).convert_alpha()
         
         self.width = surface.get_width()
         self.height = surface.get_height()    
 
     def _loadSurface_(self):
         """load image from file or file_obj"""
+
         try:
-            path = os.path.join(PROJECT_DIR, self.file_name)
-            self.surface = pygame.image.load(path).convert_alpha()
-        except:
-            path = self.file_name
-            buf = io.BytesIO(path.read())
-            self.surface = pygame.image.load(buf).convert_alpha()
+            imagelike = io.BytesIO(self.file_name.read())
+        except AttributeError:
+            imagelike = os.path.join(PROJECT_DIR, self.file_name)
+        self.surface = pygame.image.load(imagelike).convert_alpha()
+
         self.original_surface = self.surface.copy()
         self.loaded = True