mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-06-25 15:33:09 +03:00
Merge pull request #2291 from asergi/pathlib2
Use pathlib2 for Path objects on Python < 3.4
This commit is contained in:
commit
c786213b09
27
PIL/Image.py
27
PIL/Image.py
|
@ -123,6 +123,16 @@ try:
|
||||||
except ImportError:
|
except ImportError:
|
||||||
HAS_CFFI = False
|
HAS_CFFI = False
|
||||||
|
|
||||||
|
try:
|
||||||
|
from pathlib import Path
|
||||||
|
HAS_PATHLIB = True
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
from pathlib2 import Path
|
||||||
|
HAS_PATHLIB = True
|
||||||
|
except ImportError:
|
||||||
|
HAS_PATHLIB = False
|
||||||
|
|
||||||
|
|
||||||
def isImageType(t):
|
def isImageType(t):
|
||||||
"""
|
"""
|
||||||
|
@ -1875,11 +1885,9 @@ class Image(object):
|
||||||
if isPath(fp):
|
if isPath(fp):
|
||||||
filename = fp
|
filename = fp
|
||||||
open_fp = True
|
open_fp = True
|
||||||
elif sys.version_info >= (3, 4):
|
elif HAS_PATHLIB and isinstance(fp, Path):
|
||||||
from pathlib import Path
|
filename = str(fp)
|
||||||
if isinstance(fp, Path):
|
open_fp = True
|
||||||
filename = str(fp)
|
|
||||||
open_fp = True
|
|
||||||
if not filename and hasattr(fp, "name") and isPath(fp.name):
|
if not filename and hasattr(fp, "name") and isPath(fp.name):
|
||||||
# only set the name for metadata purposes
|
# only set the name for metadata purposes
|
||||||
filename = fp.name
|
filename = fp.name
|
||||||
|
@ -2516,13 +2524,8 @@ def open(fp, mode="r"):
|
||||||
filename = ""
|
filename = ""
|
||||||
if isPath(fp):
|
if isPath(fp):
|
||||||
filename = fp
|
filename = fp
|
||||||
else:
|
elif HAS_PATHLIB and isinstance(fp, Path):
|
||||||
try:
|
filename = str(fp.resolve())
|
||||||
from pathlib import Path
|
|
||||||
if isinstance(fp, Path):
|
|
||||||
filename = str(fp.resolve())
|
|
||||||
except ImportError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
if filename:
|
if filename:
|
||||||
fp = builtins.open(filename, "rb")
|
fp = builtins.open(filename, "rb")
|
||||||
|
|
|
@ -2,7 +2,6 @@ from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
import os
|
import os
|
||||||
import sys
|
|
||||||
|
|
||||||
|
|
||||||
class TestImage(PillowTestCase):
|
class TestImage(PillowTestCase):
|
||||||
|
@ -72,10 +71,9 @@ class TestImage(PillowTestCase):
|
||||||
def test_bad_mode(self):
|
def test_bad_mode(self):
|
||||||
self.assertRaises(ValueError, Image.open, "filename", "bad mode")
|
self.assertRaises(ValueError, Image.open, "filename", "bad mode")
|
||||||
|
|
||||||
@unittest.skipIf(sys.version_info < (3, 4),
|
@unittest.skipUnless(Image.HAS_PATHLIB, "requires pathlib/pathlib2")
|
||||||
"pathlib only available in Python 3.4 or later")
|
|
||||||
def test_pathlib(self):
|
def test_pathlib(self):
|
||||||
from pathlib import Path
|
from PIL.Image import Path
|
||||||
im = Image.open(Path("Tests/images/hopper.jpg"))
|
im = Image.open(Path("Tests/images/hopper.jpg"))
|
||||||
self.assertEqual(im.mode, "RGB")
|
self.assertEqual(im.mode, "RGB")
|
||||||
self.assertEqual(im.size, (128, 128))
|
self.assertEqual(im.size, (128, 128))
|
||||||
|
|
Loading…
Reference in New Issue
Block a user