From b560f5b4173c102952dbd3b8ba518385da16a49d Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 19 Apr 2018 19:40:56 +1000 Subject: [PATCH] Changed Python version checks in tests to use helper --- Tests/check_icns_dos.py | 7 ++++--- Tests/check_j2k_dos.py | 10 ++++++---- Tests/test_file_eps.py | 20 ++++++++++---------- Tests/test_file_png.py | 4 ++-- Tests/test_font_pcf.py | 3 ++- Tests/test_format_hsv.py | 14 +++++++------- Tests/test_image.py | 10 +++++----- Tests/test_imagepath.py | 8 ++++---- 8 files changed, 40 insertions(+), 36 deletions(-) diff --git a/Tests/check_icns_dos.py b/Tests/check_icns_dos.py index e56709bbb..0a7734ddb 100644 --- a/Tests/check_icns_dos.py +++ b/Tests/check_icns_dos.py @@ -2,10 +2,11 @@ # Run from anywhere that PIL is importable. from PIL import Image +from helper import py3 from io import BytesIO -if bytes is str: - Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00'))) -else: +if py3: Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00', 'latin-1'))) +else: + Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00'))) diff --git a/Tests/check_j2k_dos.py b/Tests/check_j2k_dos.py index 9f06888a3..b8f6a9962 100644 --- a/Tests/check_j2k_dos.py +++ b/Tests/check_j2k_dos.py @@ -2,12 +2,14 @@ # Run from anywhere that PIL is importable. from PIL import Image +from helper import py3 from io import BytesIO -if bytes is str: - Image.open(BytesIO(bytes( - '\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang'))) -else: +if py3: Image.open(BytesIO(bytes( '\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang', 'latin-1'))) + +else: + Image.open(BytesIO(bytes( + '\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang'))) diff --git a/Tests/test_file_eps.py b/Tests/test_file_eps.py index 2313b292c..48b134983 100644 --- a/Tests/test_file_eps.py +++ b/Tests/test_file_eps.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase, hopper +from helper import unittest, PillowTestCase, hopper, py3 from PIL import Image, EpsImagePlugin import io @@ -206,19 +206,19 @@ class TestFileEps(PillowTestCase): self._test_readline(t, ending) def _test_readline_io(self, test_string, ending): - if str is bytes: - t = io.StringIO(unicode(test_string)) - else: + if py3: t = io.StringIO(test_string) + else: + t = io.StringIO(unicode(test_string)) self._test_readline(t, ending) def _test_readline_file_universal(self, test_string, ending): f = self.tempfile('temp.txt') with open(f, 'wb') as w: - if str is bytes: - w.write(test_string) - else: + if py3: w.write(test_string.encode('UTF-8')) + else: + w.write(test_string) with open(f, 'rU') as t: self._test_readline(t, ending) @@ -226,10 +226,10 @@ class TestFileEps(PillowTestCase): def _test_readline_file_psfile(self, test_string, ending): f = self.tempfile('temp.txt') with open(f, 'wb') as w: - if str is bytes: - w.write(test_string) - else: + if py3: w.write(test_string.encode('UTF-8')) + else: + w.write(test_string) with open(f, 'rb') as r: t = EpsImagePlugin.PSFile(r) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index abf3f2953..d45b4aacf 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -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 io import BytesIO @@ -419,7 +419,7 @@ class TestFilePng(PillowTestCase): im = roundtrip(im, pnginfo=info) 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(chr(0x400) + chr(0x472) + chr(0x4ff)) # Cyrillic rt_text(chr(0x4e00) + chr(0x66f0) + # CJK diff --git a/Tests/test_font_pcf.py b/Tests/test_font_pcf.py index 2b344c6c2..952c975d3 100644 --- a/Tests/test_font_pcf.py +++ b/Tests/test_font_pcf.py @@ -2,6 +2,7 @@ from helper import unittest, PillowTestCase from PIL import Image, FontFile, PcfFontFile from PIL import ImageFont, ImageDraw +from helper import py3 codecs = dir(Image.core) @@ -76,7 +77,7 @@ class TestFontPcf(PillowTestCase): message = "".join(chr(i+1) for i in range(140, 232)) self._test_high_characters(message) # accept bytes instances in Py3. - if bytes is not str: + if py3: self._test_high_characters(message.encode('latin1')) diff --git a/Tests/test_format_hsv.py b/Tests/test_format_hsv.py index 2cc54c910..8b9a12286 100644 --- a/Tests/test_format_hsv.py +++ b/Tests/test_format_hsv.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase, hopper +from helper import unittest, PillowTestCase, hopper, py3 from PIL import Image @@ -57,10 +57,10 @@ class TestFormatHSV(PillowTestCase): (r, g, b) = im.split() - if bytes is str: - conv_func = self.str_to_float - else: + if py3: conv_func = self.int_to_float + else: + conv_func = self.str_to_float if hasattr(itertools, 'izip'): iter_helper = itertools.izip @@ -72,11 +72,11 @@ class TestFormatHSV(PillowTestCase): for (_r, _g, _b) in iter_helper(r.tobytes(), g.tobytes(), b.tobytes())] - if str is bytes: - new_bytes = b''.join(chr(h)+chr(s)+chr(v) for ( + if py3: + new_bytes = b''.join(bytes(chr(h)+chr(s)+chr(v), 'latin-1') for ( h, s, v) in converted) 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) hsv = Image.frombytes(mode, r.size, new_bytes) diff --git a/Tests/test_image.py b/Tests/test_image.py index f64ea241b..305d5da1d 100644 --- a/Tests/test_image.py +++ b/Tests/test_image.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase, hopper +from helper import unittest, PillowTestCase, hopper, py3 from PIL import Image import os @@ -60,12 +60,12 @@ class TestImage(PillowTestCase): self.assertEqual(im.height, 4) def test_invalid_image(self): - if str is bytes: - import StringIO - im = StringIO.StringIO('') - else: + if py3: import io im = io.BytesIO(b'') + else: + import StringIO + im = StringIO.StringIO('') self.assertRaises(IOError, Image.open, im) def test_bad_mode(self): diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index 98a6d3416..05d24b89b 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -1,4 +1,4 @@ -from helper import unittest, PillowTestCase +from helper import unittest, PillowTestCase, py3 from PIL import ImagePath, Image @@ -77,10 +77,10 @@ class TestImagePath(PillowTestCase): # This fails due to the invalid malloc above, # and segfaults for i in range(200000): - if str is bytes: - x[i] = "0"*16 - else: + if py3: x[i] = b'0'*16 + else: + x[i] = "0"*16 class evil: