Fail fast if there's an import error on _imaging

This commit is contained in:
wiredfool 2013-07-23 10:17:15 -07:00
parent d24a419299
commit e3e0638a5f

View File

@ -28,10 +28,7 @@ from __future__ import print_function
from PIL import VERSION, PILLOW_VERSION, _plugins from PIL import VERSION, PILLOW_VERSION, _plugins
try: import warnings
import warnings
except ImportError:
warnings = None
class _imaging_not_installed: class _imaging_not_installed:
# module placeholder # module placeholder
@ -56,41 +53,36 @@ try:
from PIL import _imaging as core from PIL import _imaging as core
if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None): if PILLOW_VERSION != getattr(core, 'PILLOW_VERSION', None):
raise ImportError("The _imaging extension was built for another " raise ImportError("The _imaging extension was built for another "
" version of Pillow or PIL. Most PIL functions " " version of Pillow or PIL")
" will be disabled ")
except ImportError as v: except ImportError as v:
core = _imaging_not_installed() core = _imaging_not_installed()
if not warnings: # the warnings module is available since Python 2.1 # Explanations for ways that we know we might have an import error
raise # raise the original ImportError. if str(v).startswith("Module use of python"):
elif str(v).startswith("Module use of python"):
# The _imaging C module is present, but not compiled for # The _imaging C module is present, but not compiled for
# the right version (windows only). Print a warning, if # the right version (windows only). Print a warning, if
# possible. # possible.
warnings.warn( warnings.warn(
"The _imaging extension was built for another version " "The _imaging extension was built for another version "
"of Python; most PIL functions will be disabled", "of Python.",
RuntimeWarning RuntimeWarning
) )
elif str(v).startswith("The _imaging extension"): elif str(v).startswith("The _imaging extension"):
warnings.warn(str(v), RuntimeWarning) warnings.warn(str(v), RuntimeWarning)
elif "Symbol not found: _PyUnicodeUCS2_FromString" in str(v): elif "Symbol not found: _PyUnicodeUCS2_FromString" in str(v):
warnings.warn( warnings.warn(
"The _imaging extension was build for Python with UCS2 support; " "The _imaging extension was built for Python with UCS2 support; "
"recompile PIL or build Python --without-wide-unicode. " "recompile PIL or build Python --without-wide-unicode. ",
"Most PIL functions will be disabled",
RuntimeWarning RuntimeWarning
) )
elif "Symbol not found: _PyUnicodeUCS4_FromString" in str(v): elif "Symbol not found: _PyUnicodeUCS4_FromString" in str(v):
warnings.warn( warnings.warn(
"The _imaging extension was build for Python with UCS4 support; " "The _imaging extension was built for Python with UCS4 support; "
"recompile PIL or build Python --with-wide-unicode. " "recompile PIL or build Python --with-wide-unicode. ",
"Most PIL functions will be disabled",
RuntimeWarning RuntimeWarning
) )
else: # Fail here anyway. Don't let people run with a mostly broken Pillow.
# unknown problem. Raise the original exception. raise
raise
try: try:
import builtins import builtins