Better unity with Image.py file (on string type testing and warnings)

This commit is contained in:
Nicolas Pieuchot 2013-04-25 22:32:43 +02:00
parent 282562ec19
commit 4d136d94ce

View File

@ -29,7 +29,11 @@ from __future__ import print_function
from PIL import Image from PIL import Image
import os, sys import os, sys
import warnings
try:
import warnings
except ImportError:
warnings = None
class _imagingft_not_installed: class _imagingft_not_installed:
# module placeholder # module placeholder
@ -41,14 +45,12 @@ try:
except ImportError: except ImportError:
core = _imagingft_not_installed() core = _imagingft_not_installed()
# Python3 compatibility for basestring if bytes is str:
try: def isStringType(t):
basestring # attempt to evaluate basestring return isinstance(t, basestring)
def isstr(s): else:
return isinstance(s, basestring) # Python2.x def isStringType(t):
except NameError: return isinstance(t, str)
def isstr(s):
return isinstance(s, str) # Python3.x
# FIXME: add support for pilfont2 format (see FontFile.py) # FIXME: add support for pilfont2 format (see FontFile.py)
@ -142,10 +144,11 @@ class FreeTypeFont:
def __init__(self, font=None, size=10, index=0, encoding="", file=None): def __init__(self, font=None, size=10, index=0, encoding="", file=None):
# FIXME: use service provider instead # FIXME: use service provider instead
if file: if file:
if warnings:
warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning) warnings.warn('file parameter deprecated, please use font parameter instead.', DeprecationWarning)
font = file font = file
if isstr(font): if isStringType(font):
self.font = core.getfont(font, size, index, encoding) self.font = core.getfont(font, size, index, encoding)
else: else:
bytes = font.read() bytes = font.read()
@ -234,6 +237,7 @@ def truetype(font=None, size=10, index=0, encoding="", filename=None):
"Load a truetype font file." "Load a truetype font file."
if filename: if filename:
if warnings:
warnings.warn('filename parameter deprecated, please use font parameter instead.', DeprecationWarning) warnings.warn('filename parameter deprecated, please use font parameter instead.', DeprecationWarning)
font = filename font = filename