From c35207cbc15bee2a729d5f2b5b3f4df9df4558e5 Mon Sep 17 00:00:00 2001 From: Brian Crowell Date: Mon, 15 Oct 2012 00:55:39 -0500 Subject: [PATCH] py3k: Backport Gohlke's tests to run on 2.6/2.7 Most of the differences are in tobytes/tostring naming and expected behavior of the bytes() constructor. The latter was usually easy to fix with the right bytes literal. This is a good preview of what will have to happen in the Python 3 code. --- PIL/ImageFile.py | 3 ++- Tests/run.py | 2 ++ Tests/test_000_sanity.py | 5 ++++- Tests/test_contents.py | 31 ------------------------------- Tests/test_file_jpeg.py | 2 +- Tests/test_file_png.py | 10 ++++++++-- Tests/test_image_fromstring.py | 7 ++++++- Tests/test_image_getim.py | 5 +++-- Tests/test_image_tostring.py | 3 +-- Tests/test_lib_pack.py | 25 +++++++++++++++++++++---- Tests/test_mode_i16.py | 5 ++++- Tests/tester.py | 10 +++++++++- 12 files changed, 61 insertions(+), 47 deletions(-) delete mode 100644 Tests/test_contents.py diff --git a/PIL/ImageFile.py b/PIL/ImageFile.py index 7800d2a4b..ef13e5b00 100644 --- a/PIL/ImageFile.py +++ b/PIL/ImageFile.py @@ -29,6 +29,7 @@ import Image import traceback, os +import io MAXBLOCK = 65536 @@ -475,7 +476,7 @@ def _save(im, fp, tile): try: fh = fp.fileno() fp.flush() - except AttributeError: + except (AttributeError, io.UnsupportedOperation): # compress to Python file-compatible object for e, b, o, a in tile: e = Image._getencoder(im.mode, e, a, im.encoderconfig) diff --git a/Tests/run.py b/Tests/run.py index 27a7afa9f..1ce31448c 100644 --- a/Tests/run.py +++ b/Tests/run.py @@ -1,3 +1,5 @@ +from __future__ import print_function + # minimal test runner import glob, os, sys diff --git a/Tests/test_000_sanity.py b/Tests/test_000_sanity.py index cba99e4fe..f9c34527c 100644 --- a/Tests/test_000_sanity.py +++ b/Tests/test_000_sanity.py @@ -1,3 +1,6 @@ +from __future__ import print_function +from tester import * + import PIL import PIL.Image @@ -9,7 +12,7 @@ assert PIL.Image.VERSION[:3] == '1.1' # Create an image and do stuff with it. im = PIL.Image.new("1", (100, 100)) assert (im.mode, im.size) == ('1', (100, 100)) -assert len(im.tobytes()) == 1300 +assert len(im.tobytes() if py3 else im.tostring()) == 1300 # Create images in all remaining major modes. im = PIL.Image.new("L", (100, 100)) diff --git a/Tests/test_contents.py b/Tests/test_contents.py deleted file mode 100644 index 0455ad89c..000000000 --- a/Tests/test_contents.py +++ /dev/null @@ -1,31 +0,0 @@ -from tester import * - -import os, glob - -contents = {} -for file in open("CONTENTS"): - file = file.strip() - if file: - contents[os.path.normpath(file)] = None - -patterns = [ - "PIL/*.py", - "*.c", - "libImaging/*.c", - "libImaging/*.h", - ] - -def test_files(): - - for pattern in patterns: - for file in glob.glob(pattern): - file = os.path.normpath("Imaging/" + file) - assert_true(file in contents, "%r not in CONTENTS" % file) - -def test_contents(): - - for file in contents: - root, file = file.split(os.sep, 1) - if file == "MANIFEST": - continue # generated by distribution scripts - assert_true(os.path.isfile(file), "%r not found" % file) diff --git a/Tests/test_file_jpeg.py b/Tests/test_file_jpeg.py index 126c67237..5246930c8 100644 --- a/Tests/test_file_jpeg.py +++ b/Tests/test_file_jpeg.py @@ -166,7 +166,7 @@ def test_truncated_jpeg(): def test(junk): if junk: # replace "junk" bytes at the end with junk - file = BytesIO(data[:-junk] + bytes(junk*[0])) + file = BytesIO(data[:-junk] + b'\0'*junk) else: file = BytesIO(data) im = Image.open(file) diff --git a/Tests/test_file_png.py b/Tests/test_file_png.py index 329598c9d..6fb67fc02 100644 --- a/Tests/test_file_png.py +++ b/Tests/test_file_png.py @@ -24,7 +24,7 @@ def chunk(cid, *data): o32 = PngImagePlugin.o32 -IHDR = chunk(b"IHDR", o32(1), o32(1), bytes((8,2)), bytes((0,0,0))) +IHDR = chunk(b"IHDR", o32(1), o32(1), b'\x08\x02', b'\0\0\0') IDAT = chunk(b"IDAT") IEND = chunk(b"IEND") @@ -154,7 +154,13 @@ def test_scary(): import base64 file = "Tests/images/pngtest_bad.png.base64" - data = base64.decodebytes(open(file, 'rb').read()) + data = None + + if py3: + data = base64.decodebytes(open(file, 'rb').read()) + else: + data = base64.decodestring(open(file, 'rb').read()) + file = BytesIO(data) assert_exception(IOError, lambda: Image.open(file)) diff --git a/Tests/test_image_fromstring.py b/Tests/test_image_fromstring.py index f529993cd..ddc53ebcc 100644 --- a/Tests/test_image_fromstring.py +++ b/Tests/test_image_fromstring.py @@ -5,7 +5,12 @@ from PIL import Image def test_sanity(): im1 = lena() - im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes()) + im2 = None + + if py3: + im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes()) + else: + im2 = Image.fromstring(im1.mode, im1.size, im1.tostring()) assert_image_equal(im1, im2) diff --git a/Tests/test_image_getim.py b/Tests/test_image_getim.py index 14ca427d0..8d2f12fc2 100644 --- a/Tests/test_image_getim.py +++ b/Tests/test_image_getim.py @@ -5,9 +5,10 @@ from PIL import Image def test_sanity(): im = lena() - type_repr = repr(type(im.getim())) - assert_true("PyCapsule" in type_repr) + + if py3: + assert_true("PyCapsule" in type_repr) assert_true(isinstance(im.im.id, int)) diff --git a/Tests/test_image_tostring.py b/Tests/test_image_tostring.py index b992b63c6..c4d89df93 100644 --- a/Tests/test_image_tostring.py +++ b/Tests/test_image_tostring.py @@ -3,6 +3,5 @@ from tester import * from PIL import Image def test_sanity(): - - data = lena().tobytes() + data = lena().tobytes() if py3 else lena().tostring() assert_true(isinstance(data, bytes)) diff --git a/Tests/test_lib_pack.py b/Tests/test_lib_pack.py index cd26a54ea..c295ac652 100644 --- a/Tests/test_lib_pack.py +++ b/Tests/test_lib_pack.py @@ -12,7 +12,11 @@ def test_pack(): im = Image.new(mode, (1, 1), 1) else: im = Image.new(mode, (1, 1), (1, 2, 3, 4)[:len(mode)]) - return list(im.tobytes("raw", rawmode)) + + if py3: + return list(im.tobytes("raw", rawmode)) + else: + return [ord(c) for c in im.tostring("raw", rawmode)] assert_equal(pack("1", "1"), [128]) assert_equal(pack("1", "1;I"), [0]) @@ -45,13 +49,26 @@ def test_pack(): def test_unpack(): def unpack(mode, rawmode, bytes_): - data = bytes(range(1,bytes_+1)) - im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1) + im = None + + if py3: + data = bytes(range(1,bytes_+1)) + im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1) + else: + data = ''.join(chr(i) for i in range(1,bytes_+1)) + im = Image.fromstring(mode, (1, 1), data, "raw", rawmode, 0, 1) + return im.getpixel((0, 0)) def unpack_1(mode, rawmode, value): assert mode == "1" - im = Image.frombytes(mode, (8, 1), bytes([value]), "raw", rawmode, 0, 1) + im = None + + if py3: + im = Image.frombytes(mode, (8, 1), bytes([value]), "raw", rawmode, 0, 1) + else: + im = Image.fromstring(mode, (8, 1), chr(value), "raw", rawmode, 0, 1) + return tuple(im.getdata()) X = 255 diff --git a/Tests/test_mode_i16.py b/Tests/test_mode_i16.py index 91042e6f1..060a68bca 100644 --- a/Tests/test_mode_i16.py +++ b/Tests/test_mode_i16.py @@ -82,7 +82,10 @@ def test_basic(): def test_tostring(): def tostring(mode): - return Image.new(mode, (1, 1), 1).tobytes() + if py3: + return Image.new(mode, (1, 1), 1).tobytes() + else: + return Image.new(mode, (1, 1), 1).tostring() assert_equal(tostring("L"), b"\x01") assert_equal(tostring("I;16"), b"\x01\x00") diff --git a/Tests/tester.py b/Tests/tester.py index 3e9d9e689..14e4a2d2d 100644 --- a/Tests/tester.py +++ b/Tests/tester.py @@ -1,3 +1,8 @@ +from __future__ import print_function + +import sys +py3 = (sys.version_info >= (3,0)) + # some test helpers _target = None @@ -139,9 +144,12 @@ def assert_image_equal(a, b, msg=None): failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) elif a.size != b.size: failure(msg or "got size %r, expected %r" % (a.size, b.size)) - elif a.tobytes() != b.tobytes(): + elif py3 and a.tobytes() != b.tobytes(): failure(msg or "got different content") # generate better diff? + elif not py3 and a.tostring() != b.tostring(): + failure(msg or "got different content") + # same complaint? else: success()