From 99887a546763c950233f691cf5dd0107f38cfc81 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 16 Sep 2014 16:05:22 +0300 Subject: [PATCH 1/4] Improve the error message when importing ImageGrab on non-Windows (#901) --- PIL/ImageGrab.py | 5 ++++- Tests/test_imagegrab.py | 25 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/PIL/ImageGrab.py b/PIL/ImageGrab.py index 9bb190934..ef0135334 100644 --- a/PIL/ImageGrab.py +++ b/PIL/ImageGrab.py @@ -17,6 +17,9 @@ from PIL import Image +import sys +if sys.platform != "win32": + raise ImportError("ImageGrab is Windows only") try: # built-in driver (1.1.3 and later) @@ -40,7 +43,7 @@ def grab(bbox=None): def grabclipboard(): - debug = 0 # temporary interface + debug = 0 # temporary interface data = Image.core.grabclipboard(debug) if isinstance(data, bytes): from PIL import BmpImagePlugin diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index 13affe6b9..dd6f50fb9 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -1,5 +1,8 @@ from helper import unittest, PillowTestCase +import exceptions +import sys + try: from PIL import ImageGrab @@ -19,6 +22,28 @@ except ImportError: self.skipTest("ImportError") +class TestImageGrabImport(PillowTestCase): + + def test_import(self): + # Arrange + exception = None + + # Act + try: + from PIL import ImageGrab + ImageGrab.__name__ # dummy to prevent Pyflakes warning + except Exception as exception: + pass + + # Assert + if sys.platform == 'win32': + self.assertIsNone(exception, None) + else: + self.assertIsInstance(exception, exceptions.ImportError) + self.assertEqual(exception.message, + "ImageGrab is Windows only") + + if __name__ == '__main__': unittest.main() From 300a3f0e70c5399e8401f1770cc5c35d99e18789 Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 16 Sep 2014 16:44:51 +0300 Subject: [PATCH 2/4] Fix for Python 3 --- Tests/test_imagegrab.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index dd6f50fb9..e1c35ac4e 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -1,6 +1,5 @@ from helper import unittest, PillowTestCase -import exceptions import sys try: @@ -39,7 +38,7 @@ class TestImageGrabImport(PillowTestCase): if sys.platform == 'win32': self.assertIsNone(exception, None) else: - self.assertIsInstance(exception, exceptions.ImportError) + self.assertIsInstance(exception, ImportError) self.assertEqual(exception.message, "ImageGrab is Windows only") From 311a0c6f68f2600bcd5ce7e35c66339d5400ce4a Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 16 Sep 2014 17:19:15 +0300 Subject: [PATCH 3/4] Another Python 3 fix --- Tests/test_imagegrab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index e1c35ac4e..ec4572adb 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -31,8 +31,8 @@ class TestImageGrabImport(PillowTestCase): try: from PIL import ImageGrab ImageGrab.__name__ # dummy to prevent Pyflakes warning - except Exception as exception: - pass + except Exception as e: + exception = e # Assert if sys.platform == 'win32': From e5a068de53c6b44aa8be6c5145da1a727b38202d Mon Sep 17 00:00:00 2001 From: Hugo Date: Tue, 16 Sep 2014 17:41:03 +0300 Subject: [PATCH 4/4] Yet another Python 3 fix --- Tests/test_imagegrab.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Tests/test_imagegrab.py b/Tests/test_imagegrab.py index ec4572adb..ea6b499b2 100644 --- a/Tests/test_imagegrab.py +++ b/Tests/test_imagegrab.py @@ -39,8 +39,7 @@ class TestImageGrabImport(PillowTestCase): self.assertIsNone(exception, None) else: self.assertIsInstance(exception, ImportError) - self.assertEqual(exception.message, - "ImageGrab is Windows only") + self.assertEqual(str(exception), "ImageGrab is Windows only") if __name__ == '__main__':