From d24a419299a11fce3d8a30655a55a9c7b9c8f3c8 Mon Sep 17 00:00:00 2001 From: Freek Dijkstra Date: Tue, 23 Jul 2013 10:44:27 +0200 Subject: [PATCH 1/2] Warnings if imaging library has wrong UCS support. Raise exceptions for unknown errors. --- PIL/Image.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index ca3d472c9..621efecb9 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -61,7 +61,9 @@ try: except ImportError as v: core = _imaging_not_installed() - if str(v)[:20] == "Module use of python" and warnings: + if not warnings: # the warnings module is available since Python 2.1 + raise # raise the original ImportError. + elif str(v).startswith("Module use of python"): # The _imaging C module is present, but not compiled for # the right version (windows only). Print a warning, if # possible. @@ -70,8 +72,25 @@ except ImportError as v: "of Python; most PIL functions will be disabled", RuntimeWarning ) - if str(v).startswith("The _imaging extension") and warnings: + elif str(v).startswith("The _imaging extension"): warnings.warn(str(v), RuntimeWarning) + elif "Symbol not found: _PyUnicodeUCS2_FromString" in str(v): + warnings.warn( + "The _imaging extension was build for Python with UCS2 support; " + "recompile PIL or build Python --without-wide-unicode. " + "Most PIL functions will be disabled", + RuntimeWarning + ) + elif "Symbol not found: _PyUnicodeUCS4_FromString" in str(v): + warnings.warn( + "The _imaging extension was build for Python with UCS4 support; " + "recompile PIL or build Python --with-wide-unicode. " + "Most PIL functions will be disabled", + RuntimeWarning + ) + else: + # unknown problem. Raise the original exception. + raise try: import builtins From e3e0638a5f7ee1a425d70ac92e127b84455d21b7 Mon Sep 17 00:00:00 2001 From: wiredfool Date: Tue, 23 Jul 2013 10:17:15 -0700 Subject: [PATCH 2/2] Fail fast if there's an import error on _imaging --- PIL/Image.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/PIL/Image.py b/PIL/Image.py index 621efecb9..bb15aaa95 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -28,10 +28,7 @@ from __future__ import print_function from PIL import VERSION, PILLOW_VERSION, _plugins -try: - import warnings -except ImportError: - warnings = None +import warnings class _imaging_not_installed: # module placeholder @@ -56,41 +53,36 @@ try: from PIL import _imaging as core if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): raise ImportError("The _imaging extension was built for another " - " version of Pillow or PIL. Most PIL functions " - " will be disabled ") + " version of Pillow or PIL") except ImportError as v: core = _imaging_not_installed() - if not warnings: # the warnings module is available since Python 2.1 - raise # raise the original ImportError. - elif str(v).startswith("Module use of python"): + # Explanations for ways that we know we might have an import error + if str(v).startswith("Module use of python"): # The _imaging C module is present, but not compiled for # the right version (windows only). Print a warning, if # possible. warnings.warn( "The _imaging extension was built for another version " - "of Python; most PIL functions will be disabled", + "of Python.", RuntimeWarning ) elif str(v).startswith("The _imaging extension"): warnings.warn(str(v), RuntimeWarning) elif "Symbol not found: _PyUnicodeUCS2_FromString" in str(v): warnings.warn( - "The _imaging extension was build for Python with UCS2 support; " - "recompile PIL or build Python --without-wide-unicode. " - "Most PIL functions will be disabled", + "The _imaging extension was built for Python with UCS2 support; " + "recompile PIL or build Python --without-wide-unicode. ", RuntimeWarning ) elif "Symbol not found: _PyUnicodeUCS4_FromString" in str(v): warnings.warn( - "The _imaging extension was build for Python with UCS4 support; " - "recompile PIL or build Python --with-wide-unicode. " - "Most PIL functions will be disabled", + "The _imaging extension was built for Python with UCS4 support; " + "recompile PIL or build Python --with-wide-unicode. ", RuntimeWarning ) - else: - # unknown problem. Raise the original exception. - raise + # Fail here anyway. Don't let people run with a mostly broken Pillow. + raise try: import builtins