Repository: python-pillow/Pillow

Issue:
More tests for XpmImagePlugin.py
- Test `load_read()`.
- Helps towards: Test coverage >= 80% #722.
- flake8.

More tests for XpmImagePlugin.py
- Test `load_read()`.
- Helps towards: Test coverage >= 80% #722.
- flake8.



Discussion:

Resolving patch:
diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py
--- a/PIL/ImageFile.py
+++ b/PIL/ImageFile.py
@@ -133,11 +133,27 @@ def load(self):
             return pixel
 
         self.map = None
-
+        use_mmap = self.filename and len(self.tile) == 1
+        # As of pypy 2.1.0, memory mapping was failing here.
+        use_mmap = use_mmap and not hasattr(sys, 'pypy_version_info') 
+        
         readonly = 0
 
-        if self.filename and len(self.tile) == 1 and not hasattr(sys, 'pypy_version_info'):
-            # As of pypy 2.1.0, memory mapping was failing here.
+        # look for read/seek overrides
+        try:
+            read = self.load_read
+            # don't use mmap if there are custom read/seek functions
+            use_mmap = False
+        except AttributeError:
+            read = self.fp.read
+
+        try:
+            seek = self.load_seek
+            use_mmap = False
+        except AttributeError:
+            seek = self.fp.seek
+
+        if use_mmap:
             # try memory mapping
             d, e, o, a = self.tile[0]
             if d == "raw" and a[0] == self.mode and a[0] in Image._MAPMODES:
@@ -165,19 +181,7 @@ def load(self):
 
         self.load_prepare()
 
-        # look for read/seek overrides
-        try:
-            read = self.load_read
-        except AttributeError:
-            read = self.fp.read
-
-        try:
-            seek = self.load_seek
-        except AttributeError:
-            seek = self.fp.seek
-
         if not self.map:
-
             # sort tiles in file order
             self.tile.sort(key=_tilesort)
 
diff --git a/PIL/XpmImagePlugin.py b/PIL/XpmImagePlugin.py
--- a/PIL/XpmImagePlugin.py
+++ b/PIL/XpmImagePlugin.py
@@ -29,6 +29,7 @@
 def _accept(prefix):
     return prefix[:9] == b"/* XPM */"
 
+
 ##
 # Image plugin for X11 pixel maps.
 
@@ -86,9 +87,9 @@ def _open(self):
                     elif rgb[0:1] == b"#":
                         # FIXME: handle colour names (see ImagePalette.py)
                         rgb = int(rgb[1:], 16)
-                        palette[c] = o8((rgb >> 16) & 255) +\
-                                     o8((rgb >> 8) & 255) +\
-                                     o8(rgb & 255)
+                        palette[c] = (o8((rgb >> 16) & 255) +
+                                      o8((rgb >> 8) & 255) +
+                                      o8(rgb & 255))
                     else:
                         # unknown colour
                         raise ValueError("cannot read this XPM file")


Test patch:
diff --git a/Tests/test_file_xpm.py b/Tests/test_file_xpm.py
--- a/Tests/test_file_xpm.py
+++ b/Tests/test_file_xpm.py
@@ -1,21 +1,34 @@
-from helper import unittest, PillowTestCase
+from helper import unittest, PillowTestCase, lena
 
 from PIL import Image
 
 # sample ppm stream
-file = "Tests/images/lena.xpm"
-data = open(file, "rb").read()
+TEST_FILE = "Tests/images/lena.xpm"
 
 
 class TestFileXpm(PillowTestCase):
 
     def test_sanity(self):
-        im = Image.open(file)
+        im = Image.open(TEST_FILE)
         im.load()
         self.assertEqual(im.mode, "P")
         self.assertEqual(im.size, (128, 128))
         self.assertEqual(im.format, "XPM")
 
+        #large error due to quantization->44 colors.
+        self.assert_image_similar(im.convert('RGB'), lena('RGB'), 60) 
+
+    def test_load_read(self):
+        # Arrange
+        im = Image.open(TEST_FILE)
+        dummy_bytes = 1
+
+        # Act
+        data = im.load_read(dummy_bytes)
+
+        # Assert
+        self.assertEqual(len(data), 16384)
+
 
 if __name__ == '__main__':
     unittest.main()