Changed Python version checks in tests to use helper

This commit is contained in:
Andrew Murray 2018-04-19 19:40:56 +10:00
parent 48c8f1f219
commit b560f5b417
8 changed files with 40 additions and 36 deletions

View File

@ -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 helper 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')))

View File

@ -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 helper 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')))

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper, py3
from PIL import Image, EpsImagePlugin from PIL import Image, EpsImagePlugin
import io import io
@ -206,19 +206,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 +226,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)

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, PillowLeakTestCase, hopper from helper import unittest, PillowTestCase, PillowLeakTestCase, hopper, py3
from PIL import Image, ImageFile, PngImagePlugin from PIL import Image, ImageFile, PngImagePlugin
from io import BytesIO from io import BytesIO
@ -419,7 +419,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

View File

@ -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 helper 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'))

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper, py3
from PIL import Image from PIL import Image
@ -57,10 +57,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 +72,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)

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, hopper from helper import unittest, PillowTestCase, hopper, py3
from PIL import Image from PIL import Image
import os import os
@ -60,12 +60,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):

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase from helper import unittest, PillowTestCase, py3
from PIL import ImagePath, Image from PIL import ImagePath, Image
@ -77,10 +77,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: