From fd29e707e967517180a89996e5581488c6c81f5b Mon Sep 17 00:00:00 2001 From: Alex Clark Date: Sun, 30 Jun 2013 07:04:42 -0400 Subject: [PATCH] Apply @Arfrever patch, fixes #258 --- PIL/GdImageFile.py | 3 ++- PIL/Image.py | 23 +++++------------------ PIL/ImageCms.py | 3 ++- PIL/ImageDraw.py | 7 ++++--- PIL/ImageFile.py | 3 ++- PIL/ImageFont.py | 17 ++++++++--------- PIL/ImageOps.py | 3 ++- PIL/ImageQt.py | 3 ++- PIL/JpegImagePlugin.py | 3 ++- PIL/OleFileIO.py | 3 ++- 10 files changed, 31 insertions(+), 37 deletions(-) diff --git a/PIL/GdImageFile.py b/PIL/GdImageFile.py index 3e18aff83..f1dbc7c62 100644 --- a/PIL/GdImageFile.py +++ b/PIL/GdImageFile.py @@ -26,6 +26,7 @@ __version__ = "0.1" from PIL import ImageFile, ImagePalette, _binary +from PIL._util import isPath try: import builtins @@ -77,7 +78,7 @@ def open(fp, mode = "r"): if mode != "r": raise ValueError("bad mode") - if isinstance(fp, str): + if isPath(fp): filename = fp fp = builtins.open(fp, "rb") else: diff --git a/PIL/Image.py b/PIL/Image.py index 49c48feb6..db66c6f84 100644 --- a/PIL/Image.py +++ b/PIL/Image.py @@ -81,6 +81,7 @@ except ImportError: from PIL import ImageMode from PIL._binary import i8, o8 +from PIL._util import isPath, isStringType import os, sys @@ -88,26 +89,12 @@ import os, sys import collections import numbers -if bytes is str: - def isStringType(t): - return isinstance(t, basestring) -else: - def isStringType(t): - return isinstance(t, str) - ## # (Internal) Checks if an object is an image object. def isImageType(t): return hasattr(t, "im") -## -# (Internal) Checks if an object is a string, and that it points to a -# directory. - -def isDirectory(f): - return isStringType(f) and os.path.isdir(f) - # # Debug level @@ -1421,10 +1408,10 @@ class Image: def save(self, fp, format=None, **params): "Save image to file or stream" - if isStringType(fp): + if isPath(fp): filename = fp else: - if hasattr(fp, "name") and isStringType(fp.name): + if hasattr(fp, "name") and isPath(fp.name): filename = fp.name else: filename = "" @@ -1455,7 +1442,7 @@ class Image: init() save_handler = SAVE[format.upper()] # unknown format - if isStringType(fp): + if isPath(fp): fp = builtins.open(fp, "wb") close = 1 else: @@ -1984,7 +1971,7 @@ def open(fp, mode="r"): if mode != "r": raise ValueError("bad mode") - if isStringType(fp): + if isPath(fp): filename = fp fp = builtins.open(fp, "rb") else: diff --git a/PIL/ImageCms.py b/PIL/ImageCms.py index c8745777b..e3cb11f36 100644 --- a/PIL/ImageCms.py +++ b/PIL/ImageCms.py @@ -83,6 +83,7 @@ VERSION = "0.1.0 pil" from PIL import Image from PIL import _imagingcms +from PIL._util import isStringType core = _imagingcms @@ -139,7 +140,7 @@ class ImageCmsProfile: def __init__(self, profile): # accepts a string (filename), a file-like object, or a low-level # profile object - if Image.isStringType(profile): + if isStringType(profile): self._set(core.profile_open(profile), profile) elif hasattr(profile, "read"): self._set(core.profile_frombytes(profile.read())) diff --git a/PIL/ImageDraw.py b/PIL/ImageDraw.py index e786ef5a7..a03d26016 100644 --- a/PIL/ImageDraw.py +++ b/PIL/ImageDraw.py @@ -33,6 +33,7 @@ import numbers from PIL import Image, ImageColor +from PIL._util import isStringType try: import warnings @@ -98,7 +99,7 @@ class ImageDraw: "'setink' is deprecated; use keyword arguments instead", DeprecationWarning, stacklevel=2 ) - if Image.isStringType(ink): + if isStringType(ink): ink = ImageColor.getcolor(ink, self.mode) if self.palette and not isinstance(ink, numbers.Number): ink = self.palette.getcolor(ink) @@ -141,13 +142,13 @@ class ImageDraw: ink = self.ink else: if ink is not None: - if Image.isStringType(ink): + if isStringType(ink): ink = ImageColor.getcolor(ink, self.mode) if self.palette and not isinstance(ink, numbers.Number): ink = self.palette.getcolor(ink) ink = self.draw.draw_ink(ink, self.mode) if fill is not None: - if Image.isStringType(fill): + if isStringType(fill): fill = ImageColor.getcolor(fill, self.mode) if self.palette and not isinstance(fill, numbers.Number): fill = self.palette.getcolor(fill) diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 28c60c8b3..e3fff2854 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -28,6 +28,7 @@ # from PIL import Image +from PIL._util import isPath import traceback, os import io @@ -81,7 +82,7 @@ class ImageFile(Image.Image): self.decoderconfig = () self.decodermaxblock = MAXBLOCK - if Image.isStringType(fp): + if isPath(fp): # filename self.fp = open(fp, "rb") self.filename = fp diff --git a/PIL/ImageFont.py b/PIL/ImageFont.py index ac76e7a83..f0575d7a8 100644 --- a/PIL/ImageFont.py +++ b/PIL/ImageFont.py @@ -28,6 +28,7 @@ from __future__ import print_function from PIL import Image +from PIL._util import isDirectory, isPath import os, sys try: @@ -45,13 +46,6 @@ try: except ImportError: core = _imagingft_not_installed() -if bytes is str: - def isStringType(t): - return isinstance(t, basestring) -else: - def isStringType(t): - return isinstance(t, str) - # FIXME: add support for pilfont2 format (see FontFile.py) # -------------------------------------------------------------------- @@ -148,7 +142,7 @@ class FreeTypeFont: warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning) font = file - if isStringType(font): + if isPath(font): self.font = core.getfont(font, size, index, encoding) else: self.font_bytes = font.read() @@ -266,7 +260,12 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None): def load_path(filename): "Load a font file, searching along the Python path." for dir in sys.path: - if Image.isDirectory(dir): + if isDirectory(dir): + if not isinstance(filename, "utf-8"): + if bytes is str: + filename = filename.encode("utf-8") + else: + filename = filename.decode("utf-8") try: return load(os.path.join(dir, filename)) except IOError: diff --git a/PIL/ImageOps.py b/PIL/ImageOps.py index e9badff37..a7f360259 100644 --- a/PIL/ImageOps.py +++ b/PIL/ImageOps.py @@ -18,6 +18,7 @@ # from PIL import Image +from PIL._util import isStringType import operator from functools import reduce @@ -43,7 +44,7 @@ def _border(border): return left, top, right, bottom def _color(color, mode): - if Image.isStringType(color): + if isStringType(color): from PIL import ImageColor color = ImageColor.getcolor(color, mode) return color diff --git a/PIL/ImageQt.py b/PIL/ImageQt.py index a69943e7b..d8b838d25 100644 --- a/PIL/ImageQt.py +++ b/PIL/ImageQt.py @@ -16,6 +16,7 @@ # from PIL import Image +from PIL._util import isPath from PyQt4.QtGui import QImage, qRgb @@ -45,7 +46,7 @@ class ImageQt(QImage): if hasattr(im, "toUtf8"): # FIXME - is this really the best way to do this? im = unicode(im.toUtf8(), "utf-8") - if Image.isStringType(im): + if isPath(im): im = Image.open(im) if im.mode == "1": diff --git a/PIL/JpegImagePlugin.py b/PIL/JpegImagePlugin.py index 4f36f5a1f..a403ce189 100644 --- a/PIL/JpegImagePlugin.py +++ b/PIL/JpegImagePlugin.py @@ -37,6 +37,7 @@ __version__ = "0.6" import array, struct from PIL import Image, ImageFile, _binary from PIL.JpegPresets import presets +from PIL._util import isStringType i8 = _binary.i8 o8 = _binary.o8 @@ -488,7 +489,7 @@ def _save(im, fp, filename): def validate_qtables(qtables): if qtables is None: return qtables - if isinstance(qtables, basestring): + if isStringType(qtables): try: lines = [int(num) for line in qtables.splitlines() for num in line.split('#', 1)[0].split()] diff --git a/PIL/OleFileIO.py b/PIL/OleFileIO.py index 4d26ceb87..b7c984169 100644 --- a/PIL/OleFileIO.py +++ b/PIL/OleFileIO.py @@ -41,6 +41,7 @@ from __future__ import print_function import io import sys from PIL import _binary +from PIL._util import isPath if str is not bytes: long = int @@ -269,7 +270,7 @@ class OleFileIO: def open(self, filename): """Open an OLE2 file""" - if isinstance(filename, str): + if isPath(filename): self.fp = open(filename, "rb") else: self.fp = filename