Skip to content

Commit

Permalink
py3k: Backport Gohlke's tests to run on 2.6/2.7
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Brian Crowell authored and Brian Crowell committed Jan 10, 2013
1 parent ad784eb commit 1978851
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 47 deletions.
3 changes: 2 additions & 1 deletion PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import Image
import traceback, os
import io

MAXBLOCK = 65536

Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions Tests/run.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import print_function

# minimal test runner

import glob, os, sys
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_000_sanity.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from __future__ import print_function
from tester import *

import PIL
import PIL.Image

Expand All @@ -9,7 +12,7 @@
# 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))
Expand Down
31 changes: 0 additions & 31 deletions Tests/test_contents.py

This file was deleted.

2 changes: 1 addition & 1 deletion Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
10 changes: 8 additions & 2 deletions Tests/test_file_png.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down Expand Up @@ -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))

Expand Down
7 changes: 6 additions & 1 deletion Tests/test_image_fromstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@
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)

5 changes: 3 additions & 2 deletions Tests/test_image_getim.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
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))

3 changes: 1 addition & 2 deletions Tests/test_image_tostring.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@
from PIL import Image

def test_sanity():

data = lena().tobytes()
data = lena().tobytes() if py3 else lena().tostring()
assert_true(isinstance(data, bytes))
25 changes: 21 additions & 4 deletions Tests/test_lib_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ def pack(mode, rawmode):
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])
Expand Down Expand Up @@ -45,13 +49,26 @@ def pack(mode, rawmode):
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
Expand Down
5 changes: 4 additions & 1 deletion Tests/test_mode_i16.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,10 @@ def basic(mode):
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")
Expand Down
10 changes: 9 additions & 1 deletion Tests/tester.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
from __future__ import print_function

import sys
py3 = (sys.version_info >= (3,0))

# some test helpers

_target = None
Expand Down Expand Up @@ -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()

Expand Down

0 comments on commit 1978851

Please sign in to comment.