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.
This commit is contained in:
Brian Crowell 2012-10-15 00:55:39 -05:00 committed by Brian Crowell
parent ad784eb808
commit 197885164b
12 changed files with 61 additions and 47 deletions

View File

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

View File

@ -1,3 +1,5 @@
from __future__ import print_function
# minimal test runner
import glob, os, sys

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -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

View File

@ -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")

View File

@ -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()