Allow pathlib.Path in Image.open on Python 2.7

Although the pathlib backport for Python 2.7 may be deprecated:
https://pypi.python.org/pypi/pathlib/
It is still used by many projects. Therefore, changing to a
Try/Except pattern for checking for pathlib is not any more
obtrusive that the current >= Python 3.4 check and allows users
to use the backport without issue.
This commit is contained in:
Patrick Snape 2016-09-14 19:51:10 +01:00
parent 6e7553fb0f
commit e981c41b47

View File

@ -2279,10 +2279,14 @@ def open(fp, mode="r"):
filename = ""
if isPath(fp):
filename = fp
elif sys.version_info >= (3, 4):
from pathlib import Path
if isinstance(fp, Path):
filename = str(fp.resolve())
else:
try:
from pathlib import Path
if isinstance(fp, Path):
filename = str(fp.resolve())
except ImportError:
pass
if filename:
fp = builtins.open(filename, "rb")