mirror of
https://github.com/python-pillow/Pillow.git
synced 2024-12-26 01:46:18 +03:00
Merge pull request #3105 from radarhere/py3
Change Python version checks to use util
This commit is contained in:
commit
04a1f71b4c
|
@ -2,10 +2,11 @@
|
||||||
# Run from anywhere that PIL is importable.
|
# Run from anywhere that PIL is importable.
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from PIL._util import py3
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
if bytes is str:
|
if py3:
|
||||||
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00')))
|
|
||||||
else:
|
|
||||||
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00',
|
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00',
|
||||||
'latin-1')))
|
'latin-1')))
|
||||||
|
else:
|
||||||
|
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00')))
|
||||||
|
|
|
@ -2,12 +2,14 @@
|
||||||
# Run from anywhere that PIL is importable.
|
# Run from anywhere that PIL is importable.
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from PIL._util import py3
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
if bytes is str:
|
if py3:
|
||||||
Image.open(BytesIO(bytes(
|
|
||||||
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang')))
|
|
||||||
else:
|
|
||||||
Image.open(BytesIO(bytes(
|
Image.open(BytesIO(bytes(
|
||||||
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang',
|
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang',
|
||||||
'latin-1')))
|
'latin-1')))
|
||||||
|
|
||||||
|
else:
|
||||||
|
Image.open(BytesIO(bytes(
|
||||||
|
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang')))
|
||||||
|
|
|
@ -8,6 +8,7 @@ import os
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
from PIL import Image, ImageMath
|
from PIL import Image, ImageMath
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
@ -265,8 +266,6 @@ class PillowLeakTestCase(PillowTestCase):
|
||||||
|
|
||||||
# helpers
|
# helpers
|
||||||
|
|
||||||
py3 = sys.version_info.major >= 3
|
|
||||||
|
|
||||||
if not py3:
|
if not py3:
|
||||||
# Remove DeprecationWarning in Python 3
|
# Remove DeprecationWarning in Python 3
|
||||||
PillowTestCase.assertRaisesRegex = PillowTestCase.assertRaisesRegexp
|
PillowTestCase.assertRaisesRegex = PillowTestCase.assertRaisesRegexp
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from helper import unittest, PillowTestCase, hopper
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image, EpsImagePlugin
|
from PIL import Image, EpsImagePlugin
|
||||||
|
from PIL._util import py3
|
||||||
import io
|
import io
|
||||||
|
|
||||||
# Our two EPS test files (they are identical except for their bounding boxes)
|
# Our two EPS test files (they are identical except for their bounding boxes)
|
||||||
|
@ -206,19 +207,19 @@ class TestFileEps(PillowTestCase):
|
||||||
self._test_readline(t, ending)
|
self._test_readline(t, ending)
|
||||||
|
|
||||||
def _test_readline_io(self, test_string, ending):
|
def _test_readline_io(self, test_string, ending):
|
||||||
if str is bytes:
|
if py3:
|
||||||
t = io.StringIO(unicode(test_string))
|
|
||||||
else:
|
|
||||||
t = io.StringIO(test_string)
|
t = io.StringIO(test_string)
|
||||||
|
else:
|
||||||
|
t = io.StringIO(unicode(test_string))
|
||||||
self._test_readline(t, ending)
|
self._test_readline(t, ending)
|
||||||
|
|
||||||
def _test_readline_file_universal(self, test_string, ending):
|
def _test_readline_file_universal(self, test_string, ending):
|
||||||
f = self.tempfile('temp.txt')
|
f = self.tempfile('temp.txt')
|
||||||
with open(f, 'wb') as w:
|
with open(f, 'wb') as w:
|
||||||
if str is bytes:
|
if py3:
|
||||||
w.write(test_string)
|
|
||||||
else:
|
|
||||||
w.write(test_string.encode('UTF-8'))
|
w.write(test_string.encode('UTF-8'))
|
||||||
|
else:
|
||||||
|
w.write(test_string)
|
||||||
|
|
||||||
with open(f, 'rU') as t:
|
with open(f, 'rU') as t:
|
||||||
self._test_readline(t, ending)
|
self._test_readline(t, ending)
|
||||||
|
@ -226,10 +227,10 @@ class TestFileEps(PillowTestCase):
|
||||||
def _test_readline_file_psfile(self, test_string, ending):
|
def _test_readline_file_psfile(self, test_string, ending):
|
||||||
f = self.tempfile('temp.txt')
|
f = self.tempfile('temp.txt')
|
||||||
with open(f, 'wb') as w:
|
with open(f, 'wb') as w:
|
||||||
if str is bytes:
|
if py3:
|
||||||
w.write(test_string)
|
|
||||||
else:
|
|
||||||
w.write(test_string.encode('UTF-8'))
|
w.write(test_string.encode('UTF-8'))
|
||||||
|
else:
|
||||||
|
w.write(test_string)
|
||||||
|
|
||||||
with open(f, 'rb') as r:
|
with open(f, 'rb') as r:
|
||||||
t = EpsImagePlugin.PSFile(r)
|
t = EpsImagePlugin.PSFile(r)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from __future__ import print_function
|
from __future__ import print_function
|
||||||
from helper import unittest, PillowTestCase, hopper, py3
|
from helper import unittest, PillowTestCase, hopper
|
||||||
from PIL import features
|
from PIL import features
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
from ctypes import c_float
|
from ctypes import c_float
|
||||||
import io
|
import io
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from helper import unittest, PillowTestCase, PillowLeakTestCase, hopper
|
from helper import unittest, PillowTestCase, PillowLeakTestCase, hopper
|
||||||
from PIL import Image, ImageFile, PngImagePlugin
|
from PIL import Image, ImageFile, PngImagePlugin
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import zlib
|
import zlib
|
||||||
|
@ -419,7 +420,7 @@ class TestFilePng(PillowTestCase):
|
||||||
im = roundtrip(im, pnginfo=info)
|
im = roundtrip(im, pnginfo=info)
|
||||||
self.assertEqual(im.info, {"Text": value})
|
self.assertEqual(im.info, {"Text": value})
|
||||||
|
|
||||||
if str is not bytes:
|
if py3:
|
||||||
rt_text(" Aa" + chr(0xa0) + chr(0xc4) + chr(0xff)) # Latin1
|
rt_text(" Aa" + chr(0xa0) + chr(0xc4) + chr(0xff)) # Latin1
|
||||||
rt_text(chr(0x400) + chr(0x472) + chr(0x4ff)) # Cyrillic
|
rt_text(chr(0x400) + chr(0x472) + chr(0x4ff)) # Cyrillic
|
||||||
rt_text(chr(0x4e00) + chr(0x66f0) + # CJK
|
rt_text(chr(0x4e00) + chr(0x66f0) + # CJK
|
||||||
|
|
|
@ -3,9 +3,10 @@ from io import BytesIO
|
||||||
import struct
|
import struct
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from helper import unittest, PillowTestCase, hopper, py3
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image, TiffImagePlugin
|
from PIL import Image, TiffImagePlugin
|
||||||
|
from PIL._util import py3
|
||||||
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION, RESOLUTION_UNIT
|
from PIL.TiffImagePlugin import X_RESOLUTION, Y_RESOLUTION, RESOLUTION_UNIT
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
logger = logging.getLogger(__name__)
|
||||||
|
|
|
@ -2,6 +2,7 @@ from helper import unittest, PillowTestCase
|
||||||
|
|
||||||
from PIL import Image, FontFile, PcfFontFile
|
from PIL import Image, FontFile, PcfFontFile
|
||||||
from PIL import ImageFont, ImageDraw
|
from PIL import ImageFont, ImageDraw
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
codecs = dir(Image.core)
|
codecs = dir(Image.core)
|
||||||
|
|
||||||
|
@ -76,7 +77,7 @@ class TestFontPcf(PillowTestCase):
|
||||||
message = "".join(chr(i+1) for i in range(140, 232))
|
message = "".join(chr(i+1) for i in range(140, 232))
|
||||||
self._test_high_characters(message)
|
self._test_high_characters(message)
|
||||||
# accept bytes instances in Py3.
|
# accept bytes instances in Py3.
|
||||||
if bytes is not str:
|
if py3:
|
||||||
self._test_high_characters(message.encode('latin1'))
|
self._test_high_characters(message.encode('latin1'))
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from helper import unittest, PillowTestCase, hopper
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
import colorsys
|
import colorsys
|
||||||
import itertools
|
import itertools
|
||||||
|
@ -57,10 +58,10 @@ class TestFormatHSV(PillowTestCase):
|
||||||
|
|
||||||
(r, g, b) = im.split()
|
(r, g, b) = im.split()
|
||||||
|
|
||||||
if bytes is str:
|
if py3:
|
||||||
conv_func = self.str_to_float
|
|
||||||
else:
|
|
||||||
conv_func = self.int_to_float
|
conv_func = self.int_to_float
|
||||||
|
else:
|
||||||
|
conv_func = self.str_to_float
|
||||||
|
|
||||||
if hasattr(itertools, 'izip'):
|
if hasattr(itertools, 'izip'):
|
||||||
iter_helper = itertools.izip
|
iter_helper = itertools.izip
|
||||||
|
@ -72,11 +73,11 @@ class TestFormatHSV(PillowTestCase):
|
||||||
for (_r, _g, _b) in iter_helper(r.tobytes(), g.tobytes(),
|
for (_r, _g, _b) in iter_helper(r.tobytes(), g.tobytes(),
|
||||||
b.tobytes())]
|
b.tobytes())]
|
||||||
|
|
||||||
if str is bytes:
|
if py3:
|
||||||
new_bytes = b''.join(chr(h)+chr(s)+chr(v) for (
|
new_bytes = b''.join(bytes(chr(h)+chr(s)+chr(v), 'latin-1') for (
|
||||||
h, s, v) in converted)
|
h, s, v) in converted)
|
||||||
else:
|
else:
|
||||||
new_bytes = b''.join(bytes(chr(h)+chr(s)+chr(v), 'latin-1') for (
|
new_bytes = b''.join(chr(h)+chr(s)+chr(v) for (
|
||||||
h, s, v) in converted)
|
h, s, v) in converted)
|
||||||
|
|
||||||
hsv = Image.frombytes(mode, r.size, new_bytes)
|
hsv = Image.frombytes(mode, r.size, new_bytes)
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from helper import unittest, PillowTestCase, hopper
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from PIL._util import py3
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
@ -60,12 +61,12 @@ class TestImage(PillowTestCase):
|
||||||
self.assertEqual(im.height, 4)
|
self.assertEqual(im.height, 4)
|
||||||
|
|
||||||
def test_invalid_image(self):
|
def test_invalid_image(self):
|
||||||
if str is bytes:
|
if py3:
|
||||||
import StringIO
|
|
||||||
im = StringIO.StringIO('')
|
|
||||||
else:
|
|
||||||
import io
|
import io
|
||||||
im = io.BytesIO(b'')
|
im = io.BytesIO(b'')
|
||||||
|
else:
|
||||||
|
import StringIO
|
||||||
|
im = StringIO.StringIO('')
|
||||||
self.assertRaises(IOError, Image.open, im)
|
self.assertRaises(IOError, Image.open, im)
|
||||||
|
|
||||||
def test_bad_mode(self):
|
def test_bad_mode(self):
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
from helper import unittest, PillowTestCase, hopper, py3
|
from helper import unittest, PillowTestCase, hopper
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
|
|
||||||
class TestImageGetIm(PillowTestCase):
|
class TestImageGetIm(PillowTestCase):
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
from helper import unittest, PillowTestCase
|
from helper import unittest, PillowTestCase
|
||||||
|
|
||||||
from PIL import ImagePath, Image
|
from PIL import ImagePath, Image
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
import array
|
import array
|
||||||
import struct
|
import struct
|
||||||
|
@ -77,10 +78,10 @@ class TestImagePath(PillowTestCase):
|
||||||
# This fails due to the invalid malloc above,
|
# This fails due to the invalid malloc above,
|
||||||
# and segfaults
|
# and segfaults
|
||||||
for i in range(200000):
|
for i in range(200000):
|
||||||
if str is bytes:
|
if py3:
|
||||||
x[i] = "0"*16
|
|
||||||
else:
|
|
||||||
x[i] = b'0'*16
|
x[i] = b'0'*16
|
||||||
|
else:
|
||||||
|
x[i] = "0"*16
|
||||||
|
|
||||||
|
|
||||||
class evil:
|
class evil:
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
from helper import unittest, PillowTestCase, hopper, py3
|
from helper import unittest, PillowTestCase, hopper
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
|
from PIL._util import py3
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|
|
@ -26,6 +26,7 @@ import os
|
||||||
import sys
|
import sys
|
||||||
from . import Image, ImageFile
|
from . import Image, ImageFile
|
||||||
from ._binary import i32le as i32
|
from ._binary import i32le as i32
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
__version__ = "0.5"
|
__version__ = "0.5"
|
||||||
|
|
||||||
|
@ -206,12 +207,12 @@ class EpsImageFile(ImageFile.ImageFile):
|
||||||
# Rewrap the open file pointer in something that will
|
# Rewrap the open file pointer in something that will
|
||||||
# convert line endings and decode to latin-1.
|
# convert line endings and decode to latin-1.
|
||||||
try:
|
try:
|
||||||
if bytes is str:
|
if py3:
|
||||||
# Python2, no encoding conversion necessary
|
|
||||||
fp = open(self.fp.name, "Ur")
|
|
||||||
else:
|
|
||||||
# Python3, can use bare open command.
|
# Python3, can use bare open command.
|
||||||
fp = open(self.fp.name, "Ur", encoding='latin-1')
|
fp = open(self.fp.name, "Ur", encoding='latin-1')
|
||||||
|
else:
|
||||||
|
# Python2, no encoding conversion necessary
|
||||||
|
fp = open(self.fp.name, "Ur")
|
||||||
except:
|
except:
|
||||||
# Expect this for bytesio/stringio
|
# Expect this for bytesio/stringio
|
||||||
fp = PSFile(self.fp)
|
fp = PSFile(self.fp)
|
||||||
|
|
|
@ -25,6 +25,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from . import VERSION, PILLOW_VERSION, _plugins
|
from . import VERSION, PILLOW_VERSION, _plugins
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
import warnings
|
import warnings
|
||||||
|
@ -1260,10 +1261,10 @@ class Image(object):
|
||||||
|
|
||||||
self.load()
|
self.load()
|
||||||
try:
|
try:
|
||||||
if bytes is str:
|
if py3:
|
||||||
return [i8(c) for c in self.im.getpalette()]
|
|
||||||
else:
|
|
||||||
return list(self.im.getpalette())
|
return list(self.im.getpalette())
|
||||||
|
else:
|
||||||
|
return [i8(c) for c in self.im.getpalette()]
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return None # no palette
|
return None # no palette
|
||||||
|
|
||||||
|
@ -1586,10 +1587,10 @@ class Image(object):
|
||||||
palette = ImagePalette.raw(data.rawmode, data.palette)
|
palette = ImagePalette.raw(data.rawmode, data.palette)
|
||||||
else:
|
else:
|
||||||
if not isinstance(data, bytes):
|
if not isinstance(data, bytes):
|
||||||
if bytes is str:
|
if py3:
|
||||||
data = "".join(chr(x) for x in data)
|
|
||||||
else:
|
|
||||||
data = bytes(data)
|
data = bytes(data)
|
||||||
|
else:
|
||||||
|
data = "".join(chr(x) for x in data)
|
||||||
palette = ImagePalette.raw(rawmode, data)
|
palette = ImagePalette.raw(rawmode, data)
|
||||||
self.mode = "P"
|
self.mode = "P"
|
||||||
self.palette = palette
|
self.palette = palette
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from . import Image
|
from . import Image
|
||||||
from ._util import isDirectory, isPath
|
from ._util import isDirectory, isPath, py3
|
||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
|
@ -314,10 +314,10 @@ def load_path(filename):
|
||||||
for directory in sys.path:
|
for directory in sys.path:
|
||||||
if isDirectory(directory):
|
if isDirectory(directory):
|
||||||
if not isinstance(filename, str):
|
if not isinstance(filename, str):
|
||||||
if bytes is str:
|
if py3:
|
||||||
filename = filename.encode("utf-8")
|
|
||||||
else:
|
|
||||||
filename = filename.decode("utf-8")
|
filename = filename.decode("utf-8")
|
||||||
|
else:
|
||||||
|
filename = filename.encode("utf-8")
|
||||||
try:
|
try:
|
||||||
return load(os.path.join(directory, filename))
|
return load(os.path.join(directory, filename))
|
||||||
except IOError:
|
except IOError:
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from . import Image, _imagingmath
|
from . import Image, _imagingmath
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import builtins
|
import builtins
|
||||||
|
@ -100,7 +101,7 @@ class _Operand(object):
|
||||||
# an image is "true" if it contains at least one non-zero pixel
|
# an image is "true" if it contains at least one non-zero pixel
|
||||||
return self.im.getbbox() is not None
|
return self.im.getbbox() is not None
|
||||||
|
|
||||||
if bytes is str:
|
if not py3:
|
||||||
# Provide __nonzero__ for pre-Py3k
|
# Provide __nonzero__ for pre-Py3k
|
||||||
__nonzero__ = __bool__
|
__nonzero__ = __bool__
|
||||||
del __bool__
|
del __bool__
|
||||||
|
@ -151,7 +152,7 @@ class _Operand(object):
|
||||||
def __rpow__(self, other):
|
def __rpow__(self, other):
|
||||||
return self.apply("pow", other, self)
|
return self.apply("pow", other, self)
|
||||||
|
|
||||||
if bytes is str:
|
if not py3:
|
||||||
# Provide __div__ and __rdiv__ for pre-Py3k
|
# Provide __div__ and __rdiv__ for pre-Py3k
|
||||||
__div__ = __truediv__
|
__div__ = __truediv__
|
||||||
__rdiv__ = __rtruediv__
|
__rdiv__ = __rtruediv__
|
||||||
|
|
|
@ -17,7 +17,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from . import Image
|
from . import Image
|
||||||
from ._util import isPath
|
from ._util import isPath, py3
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
qt_is_installed = True
|
qt_is_installed = True
|
||||||
|
@ -123,10 +123,10 @@ def _toqclass_helper(im):
|
||||||
# handle filename, if given instead of image name
|
# handle filename, if given instead of image name
|
||||||
if hasattr(im, "toUtf8"):
|
if hasattr(im, "toUtf8"):
|
||||||
# FIXME - is this really the best way to do this?
|
# FIXME - is this really the best way to do this?
|
||||||
if str is bytes:
|
if py3:
|
||||||
im = unicode(im.toUtf8(), "utf-8")
|
|
||||||
else:
|
|
||||||
im = str(im.toUtf8(), "utf-8")
|
im = str(im.toUtf8(), "utf-8")
|
||||||
|
else:
|
||||||
|
im = unicode(im.toUtf8(), "utf-8")
|
||||||
if isPath(im):
|
if isPath(im):
|
||||||
im = Image.open(im)
|
im = Image.open(im)
|
||||||
|
|
||||||
|
|
|
@ -16,6 +16,7 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from . import EpsImagePlugin
|
from . import EpsImagePlugin
|
||||||
|
from ._util import py3
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -34,7 +35,7 @@ class PSDraw(object):
|
||||||
self.fp = fp
|
self.fp = fp
|
||||||
|
|
||||||
def _fp_write(self, to_write):
|
def _fp_write(self, to_write):
|
||||||
if bytes is str or self.fp == sys.stdout:
|
if not py3 or self.fp == sys.stdout:
|
||||||
self.fp.write(to_write)
|
self.fp.write(to_write)
|
||||||
else:
|
else:
|
||||||
self.fp.write(bytes(to_write, 'UTF-8'))
|
self.fp.write(bytes(to_write, 'UTF-8'))
|
||||||
|
|
|
@ -4,6 +4,7 @@ import mmap
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import zlib
|
import zlib
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from UserDict import UserDict # Python 2.x
|
from UserDict import UserDict # Python 2.x
|
||||||
|
@ -11,12 +12,12 @@ except ImportError:
|
||||||
UserDict = collections.UserDict # Python 3.x
|
UserDict = collections.UserDict # Python 3.x
|
||||||
|
|
||||||
|
|
||||||
if str == bytes: # Python 2.x
|
if py3: # Python 3.x
|
||||||
def make_bytes(s): # pragma: no cover
|
|
||||||
return s # pragma: no cover
|
|
||||||
else: # Python 3.x
|
|
||||||
def make_bytes(s):
|
def make_bytes(s):
|
||||||
return s.encode("us-ascii")
|
return s.encode("us-ascii")
|
||||||
|
else: # Python 2.x
|
||||||
|
def make_bytes(s): # pragma: no cover
|
||||||
|
return s # pragma: no cover
|
||||||
|
|
||||||
|
|
||||||
# see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set on page 656
|
# see 7.9.2.2 Text String Type on page 86 and D.3 PDFDocEncoding Character Set on page 656
|
||||||
|
@ -72,10 +73,10 @@ PDFDocEncoding = {
|
||||||
def decode_text(b):
|
def decode_text(b):
|
||||||
if b[:len(codecs.BOM_UTF16_BE)] == codecs.BOM_UTF16_BE:
|
if b[:len(codecs.BOM_UTF16_BE)] == codecs.BOM_UTF16_BE:
|
||||||
return b[len(codecs.BOM_UTF16_BE):].decode("utf_16_be")
|
return b[len(codecs.BOM_UTF16_BE):].decode("utf_16_be")
|
||||||
elif str == bytes: # Python 2.x
|
elif py3: # Python 3.x
|
||||||
return u"".join(PDFDocEncoding.get(ord(byte), byte) for byte in b)
|
|
||||||
else:
|
|
||||||
return "".join(PDFDocEncoding.get(byte, chr(byte)) for byte in b)
|
return "".join(PDFDocEncoding.get(byte, chr(byte)) for byte in b)
|
||||||
|
else: # Python 2.x
|
||||||
|
return u"".join(PDFDocEncoding.get(ord(byte), byte) for byte in b)
|
||||||
|
|
||||||
|
|
||||||
class PdfFormatError(RuntimeError):
|
class PdfFormatError(RuntimeError):
|
||||||
|
@ -214,20 +215,18 @@ class PdfName:
|
||||||
allowed_chars = set(range(33, 127)) - set(ord(c) for c in "#%/()<>[]{}")
|
allowed_chars = set(range(33, 127)) - set(ord(c) for c in "#%/()<>[]{}")
|
||||||
|
|
||||||
def __bytes__(self):
|
def __bytes__(self):
|
||||||
if str == bytes: # Python 2.x
|
result = bytearray(b"/")
|
||||||
result = bytearray(b"/")
|
for b in self.name:
|
||||||
for b in self.name:
|
if py3: # Python 3.x
|
||||||
if ord(b) in self.allowed_chars:
|
|
||||||
result.append(b)
|
|
||||||
else:
|
|
||||||
result.extend(b"#%02X" % ord(b))
|
|
||||||
else: # Python 3.x
|
|
||||||
result = bytearray(b"/")
|
|
||||||
for b in self.name:
|
|
||||||
if b in self.allowed_chars:
|
if b in self.allowed_chars:
|
||||||
result.append(b)
|
result.append(b)
|
||||||
else:
|
else:
|
||||||
result.extend(make_bytes("#%02X" % b))
|
result.extend(make_bytes("#%02X" % b))
|
||||||
|
else: # Python 2.x
|
||||||
|
if ord(b) in self.allowed_chars:
|
||||||
|
result.append(b)
|
||||||
|
else:
|
||||||
|
result.extend(b"#%02X" % ord(b))
|
||||||
return bytes(result)
|
return bytes(result)
|
||||||
|
|
||||||
__str__ = __bytes__
|
__str__ = __bytes__
|
||||||
|
@ -281,7 +280,7 @@ class PdfDict(UserDict):
|
||||||
out.extend(b"\n>>")
|
out.extend(b"\n>>")
|
||||||
return bytes(out)
|
return bytes(out)
|
||||||
|
|
||||||
if str == bytes:
|
if not py3:
|
||||||
__str__ = __bytes__
|
__str__ = __bytes__
|
||||||
|
|
||||||
|
|
||||||
|
@ -289,13 +288,13 @@ class PdfBinary:
|
||||||
def __init__(self, data):
|
def __init__(self, data):
|
||||||
self.data = data
|
self.data = data
|
||||||
|
|
||||||
if str == bytes: # Python 2.x
|
if py3: # Python 3.x
|
||||||
|
def __bytes__(self):
|
||||||
|
return make_bytes("<%s>" % "".join("%02X" % b for b in self.data))
|
||||||
|
else: # Python 2.x
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "<%s>" % "".join("%02X" % ord(b) for b in self.data)
|
return "<%s>" % "".join("%02X" % ord(b) for b in self.data)
|
||||||
|
|
||||||
else: # Python 3.x
|
|
||||||
def __bytes__(self):
|
|
||||||
return make_bytes("<%s>" % "".join("%02X" % b for b in self.data))
|
|
||||||
|
|
||||||
|
|
||||||
class PdfStream:
|
class PdfStream:
|
||||||
|
@ -333,7 +332,7 @@ def pdf_repr(x):
|
||||||
return bytes(PdfDict(x))
|
return bytes(PdfDict(x))
|
||||||
elif isinstance(x, list):
|
elif isinstance(x, list):
|
||||||
return bytes(PdfArray(x))
|
return bytes(PdfArray(x))
|
||||||
elif (str == bytes and isinstance(x, unicode)) or (str != bytes and isinstance(x, str)):
|
elif (py3 and isinstance(x, str)) or (not py3 and isinstance(x, unicode)):
|
||||||
return pdf_repr(encode_text(x))
|
return pdf_repr(encode_text(x))
|
||||||
elif isinstance(x, bytes):
|
elif isinstance(x, bytes):
|
||||||
return b"(" + x.replace(b"\\", b"\\\\").replace(b"(", b"\\(").replace(b")", b"\\)") + b")" # XXX escape more chars? handle binary garbage
|
return b"(" + x.replace(b"\\", b"\\\\").replace(b"(", b"\\(").replace(b")", b"\\)") + b")" # XXX escape more chars? handle binary garbage
|
||||||
|
|
|
@ -38,6 +38,7 @@ import struct
|
||||||
|
|
||||||
from . import Image, ImageFile, ImagePalette
|
from . import Image, ImageFile, ImagePalette
|
||||||
from ._binary import i8, i16be as i16, i32be as i32, o16be as o16, o32be as o32
|
from ._binary import i8, i16be as i16, i32be as i32, o16be as o16, o32be as o32
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
__version__ = "0.9"
|
__version__ = "0.9"
|
||||||
|
|
||||||
|
@ -437,7 +438,7 @@ class PngStream(ChunkStream):
|
||||||
k = s
|
k = s
|
||||||
v = b""
|
v = b""
|
||||||
if k:
|
if k:
|
||||||
if bytes is not str:
|
if py3:
|
||||||
k = k.decode('latin-1', 'strict')
|
k = k.decode('latin-1', 'strict')
|
||||||
v = v.decode('latin-1', 'replace')
|
v = v.decode('latin-1', 'replace')
|
||||||
|
|
||||||
|
@ -473,7 +474,7 @@ class PngStream(ChunkStream):
|
||||||
v = b""
|
v = b""
|
||||||
|
|
||||||
if k:
|
if k:
|
||||||
if bytes is not str:
|
if py3:
|
||||||
k = k.decode('latin-1', 'strict')
|
k = k.decode('latin-1', 'strict')
|
||||||
v = v.decode('latin-1', 'replace')
|
v = v.decode('latin-1', 'replace')
|
||||||
|
|
||||||
|
@ -510,7 +511,7 @@ class PngStream(ChunkStream):
|
||||||
return s
|
return s
|
||||||
else:
|
else:
|
||||||
return s
|
return s
|
||||||
if bytes is not str:
|
if py3:
|
||||||
try:
|
try:
|
||||||
k = k.decode("latin-1", "strict")
|
k = k.decode("latin-1", "strict")
|
||||||
lang = lang.decode("utf-8", "strict")
|
lang = lang.decode("utf-8", "strict")
|
||||||
|
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
from . import Image, ImageFile
|
from . import Image, ImageFile
|
||||||
from ._binary import i8, o8, i16be as i16
|
from ._binary import i8, o8, i16be as i16
|
||||||
|
from ._util import py3
|
||||||
import struct
|
import struct
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
@ -165,7 +166,7 @@ def _save(im, fp, filename):
|
||||||
pinmax = 255
|
pinmax = 255
|
||||||
# Image name (79 characters max, truncated below in write)
|
# Image name (79 characters max, truncated below in write)
|
||||||
imgName = os.path.splitext(os.path.basename(filename))[0]
|
imgName = os.path.splitext(os.path.basename(filename))[0]
|
||||||
if str is not bytes:
|
if py3:
|
||||||
imgName = imgName.encode('ascii', 'ignore')
|
imgName = imgName.encode('ascii', 'ignore')
|
||||||
# Standard representation of pixel in the file
|
# Standard representation of pixel in the file
|
||||||
colormap = 0
|
colormap = 0
|
||||||
|
|
|
@ -43,6 +43,7 @@ from __future__ import division, print_function
|
||||||
|
|
||||||
from . import Image, ImageFile, ImagePalette, TiffTags
|
from . import Image, ImageFile, ImagePalette, TiffTags
|
||||||
from ._binary import i8, o8
|
from ._binary import i8, o8
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
import collections
|
import collections
|
||||||
from fractions import Fraction
|
from fractions import Fraction
|
||||||
|
@ -519,7 +520,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
def __contains__(self, tag):
|
def __contains__(self, tag):
|
||||||
return tag in self._tags_v2 or tag in self._tagdata
|
return tag in self._tags_v2 or tag in self._tagdata
|
||||||
|
|
||||||
if bytes is str:
|
if not py3:
|
||||||
def has_key(self, tag):
|
def has_key(self, tag):
|
||||||
return tag in self
|
return tag in self
|
||||||
|
|
||||||
|
@ -528,7 +529,7 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
|
|
||||||
def _setitem(self, tag, value, legacy_api):
|
def _setitem(self, tag, value, legacy_api):
|
||||||
basetypes = (Number, bytes, str)
|
basetypes = (Number, bytes, str)
|
||||||
if bytes is str:
|
if not py3:
|
||||||
basetypes += unicode,
|
basetypes += unicode,
|
||||||
|
|
||||||
info = TiffTags.lookup(tag)
|
info = TiffTags.lookup(tag)
|
||||||
|
@ -549,14 +550,14 @@ class ImageFileDirectory_v2(collections.MutableMapping):
|
||||||
elif all(isinstance(v, float) for v in values):
|
elif all(isinstance(v, float) for v in values):
|
||||||
self.tagtype[tag] = 12
|
self.tagtype[tag] = 12
|
||||||
else:
|
else:
|
||||||
if bytes is str:
|
if py3:
|
||||||
# Never treat data as binary by default on Python 2.
|
|
||||||
self.tagtype[tag] = 2
|
|
||||||
else:
|
|
||||||
if all(isinstance(v, str) for v in values):
|
if all(isinstance(v, str) for v in values):
|
||||||
self.tagtype[tag] = 2
|
self.tagtype[tag] = 2
|
||||||
|
else:
|
||||||
|
# Never treat data as binary by default on Python 2.
|
||||||
|
self.tagtype[tag] = 2
|
||||||
|
|
||||||
if self.tagtype[tag] == 7 and bytes is not str:
|
if self.tagtype[tag] == 7 and py3:
|
||||||
values = [value.encode("ascii", 'replace') if isinstance(
|
values = [value.encode("ascii", 'replace') if isinstance(
|
||||||
value, str) else value]
|
value, str) else value]
|
||||||
|
|
||||||
|
@ -1503,7 +1504,7 @@ def _save(im, fp, filename):
|
||||||
if tag not in TiffTags.LIBTIFF_CORE:
|
if tag not in TiffTags.LIBTIFF_CORE:
|
||||||
continue
|
continue
|
||||||
if tag not in atts and tag not in blocklist:
|
if tag not in atts and tag not in blocklist:
|
||||||
if isinstance(value, unicode if bytes is str else str):
|
if isinstance(value, str if py3 else unicode):
|
||||||
atts[tag] = value.encode('ascii', 'replace') + b"\0"
|
atts[tag] = value.encode('ascii', 'replace') + b"\0"
|
||||||
elif isinstance(value, IFDRational):
|
elif isinstance(value, IFDRational):
|
||||||
atts[tag] = float(value)
|
atts[tag] = float(value)
|
||||||
|
|
|
@ -23,13 +23,14 @@ from __future__ import print_function
|
||||||
|
|
||||||
from . import Image, ImageFile
|
from . import Image, ImageFile
|
||||||
from ._binary import i16le as word, si16le as short, i32le as dword, si32le as _long
|
from ._binary import i16le as word, si16le as short, i32le as dword, si32le as _long
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
|
|
||||||
__version__ = "0.2"
|
__version__ = "0.2"
|
||||||
|
|
||||||
_handler = None
|
_handler = None
|
||||||
|
|
||||||
if str != bytes:
|
if py3:
|
||||||
long = int
|
long = int
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,19 +12,20 @@
|
||||||
#
|
#
|
||||||
|
|
||||||
from struct import unpack, pack
|
from struct import unpack, pack
|
||||||
|
from ._util import py3
|
||||||
|
|
||||||
if bytes is str:
|
if py3:
|
||||||
def i8(c):
|
|
||||||
return ord(c)
|
|
||||||
|
|
||||||
def o8(i):
|
|
||||||
return chr(i & 255)
|
|
||||||
else:
|
|
||||||
def i8(c):
|
def i8(c):
|
||||||
return c if c.__class__ is int else c[0]
|
return c if c.__class__ is int else c[0]
|
||||||
|
|
||||||
def o8(i):
|
def o8(i):
|
||||||
return bytes((i & 255,))
|
return bytes((i & 255,))
|
||||||
|
else:
|
||||||
|
def i8(c):
|
||||||
|
return ord(c)
|
||||||
|
|
||||||
|
def o8(i):
|
||||||
|
return chr(i & 255)
|
||||||
|
|
||||||
|
|
||||||
# Input, le = little endian, be = big endian
|
# Input, le = little endian, be = big endian
|
||||||
|
|
|
@ -1,17 +1,19 @@
|
||||||
import os
|
import os, sys
|
||||||
|
|
||||||
if bytes is str:
|
py3 = sys.version_info.major >= 3
|
||||||
def isStringType(t):
|
|
||||||
return isinstance(t, basestring)
|
|
||||||
|
|
||||||
def isPath(f):
|
if py3:
|
||||||
return isinstance(f, basestring)
|
|
||||||
else:
|
|
||||||
def isStringType(t):
|
def isStringType(t):
|
||||||
return isinstance(t, str)
|
return isinstance(t, str)
|
||||||
|
|
||||||
def isPath(f):
|
def isPath(f):
|
||||||
return isinstance(f, (bytes, str))
|
return isinstance(f, (bytes, str))
|
||||||
|
else:
|
||||||
|
def isStringType(t):
|
||||||
|
return isinstance(t, basestring)
|
||||||
|
|
||||||
|
def isPath(f):
|
||||||
|
return isinstance(f, basestring)
|
||||||
|
|
||||||
|
|
||||||
# Checks if an object is a string, and that it points to a directory.
|
# Checks if an object is a string, and that it points to a directory.
|
||||||
|
|
Loading…
Reference in New Issue
Block a user