Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing improvements and better Python 3.x support #64

Merged
merged 6 commits into from
Mar 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,11 @@ def tobytes(self, encoder_name="raw", *args):
if bytes is str:
# Declare tostring as alias to tobytes
def tostring(self, *args, **kw):
warnings.warn('tostring() is deprecated. Please call tobytes() instead.', DeprecationWarning)
warnings.warn(
'tostring() is deprecated. Please call tobytes() instead.',
DeprecationWarning,
stacklevel=2,
)
return self.tobytes(*args, **kw)

##
Expand Down Expand Up @@ -1809,7 +1813,11 @@ def frombytes(mode, size, data, decoder_name="raw", *args):
if bytes is str:
# Declare fromstring as an alias for frombytes
def fromstring(*args, **kw):
warnings.warn('fromstring() is deprecated. Please call frombytes() instead.', DeprecationWarning)
warnings.warn(
'fromstring() is deprecated. Please call frombytes() instead.',
DeprecationWarning,
stacklevel=2
)
return frombytes(*args, **kw)

##
Expand Down
6 changes: 3 additions & 3 deletions PIL/ImageFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -205,10 +205,10 @@ def load(self):
break
else:
raise IndexError(ie)

if not s: # truncated jpeg
self.tile = []

if LOAD_TRUNCATED_IMAGES:
break
else:
Expand All @@ -226,7 +226,7 @@ def load(self):

self.fp = None # might be shared

if (t == 0 or not LOAD_TRUNCATED_IMAGES) and not self.map and e < 0:
if (not LOAD_TRUNCATED_IMAGES or t == 0) and not self.map and e < 0:
# still raised if decoder fails to return anything
raise_ioerror(e)

Expand Down
5 changes: 5 additions & 0 deletions PIL/TiffImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,11 @@ def __delitem__(self, tag):
def __iter__(self):
return itertools.chain(self.tags.__iter__(), self.tagdata.__iter__())

def items(self):
keys = list(self.__iter__())
values = [self[key] for key in keys]
return zip(keys, values)

# load primitives

load_dispatch = {}
Expand Down
1 change: 1 addition & 0 deletions Tests/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,5 +91,6 @@ def tests(n):
print(skipped)
if failure:
print("***", tests(failure), "of", (success + failure), "failed.")
sys.exit(1)
else:
print(tests(success), "passed.")
2 changes: 1 addition & 1 deletion Tests/test_000_sanity.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,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() if py3 else im.tostring()) == 1300
assert len(im.tobytes()) == 1300

# Create images in all remaining major modes.
im = PIL.Image.new("L", (100, 100))
Expand Down
5 changes: 5 additions & 0 deletions Tests/test_file_jpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,3 +177,8 @@ def test(junk):
assert_no_exception(lambda: test(4))
assert_no_exception(lambda: test(8))
assert_exception(IOError, lambda: test(10))

def test_exif():
im = Image.open("Tests/images/pil_sample_rgb.jpg")
info = im._getexif()
assert_equal(info[305], 'Adobe Photoshop CS Macintosh')
10 changes: 10 additions & 0 deletions Tests/test_image_frombytes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from tester import *

from PIL import Image

def test_sanity():
im1 = lena()
im2 = Image.frombytes(im1.mode, im1.size, im1.tobytes())

assert_image_equal(im1, im2)

16 changes: 0 additions & 16 deletions Tests/test_image_fromstring.py

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from PIL import Image

def test_sanity():
data = lena().tobytes() if py3 else lena().tostring()
data = lena().tobytes()
assert_true(isinstance(data, bytes))
8 changes: 4 additions & 4 deletions Tests/test_lib_pack.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def pack(mode, rawmode):
if py3:
return list(im.tobytes("raw", rawmode))
else:
return [ord(c) for c in im.tostring("raw", rawmode)]
return [ord(c) for c in im.tobytes("raw", rawmode)]

assert_equal(pack("1", "1"), [128])
assert_equal(pack("1", "1;I"), [0])
Expand Down Expand Up @@ -53,10 +53,10 @@ def unpack(mode, rawmode, bytes_):

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)

im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1)

return im.getpixel((0, 0))

Expand All @@ -67,7 +67,7 @@ def unpack_1(mode, rawmode, value):
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)
im = Image.frombytes(mode, (8, 1), chr(value), "raw", rawmode, 0, 1)

return tuple(im.getdata())

Expand Down
17 changes: 7 additions & 10 deletions Tests/test_mode_i16.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,15 @@ def basic(mode):
basic("I")


def test_tostring():
def test_tobytes():

def tostring(mode):
if py3:
return Image.new(mode, (1, 1), 1).tobytes()
else:
return Image.new(mode, (1, 1), 1).tostring()
def tobytes(mode):
return Image.new(mode, (1, 1), 1).tobytes()

assert_equal(tostring("L"), b"\x01")
assert_equal(tostring("I;16"), b"\x01\x00")
assert_equal(tostring("I;16B"), b"\x00\x01")
assert_equal(tostring("I"), b"\x01\x00\x00\x00")
assert_equal(tobytes("L"), b"\x01")
assert_equal(tobytes("I;16"), b"\x01\x00")
assert_equal(tobytes("I;16B"), b"\x00\x01")
assert_equal(tobytes("I"), b"\x01\x00\x00\x00")


def test_convert():
Expand Down
5 changes: 1 addition & 4 deletions Tests/tester.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,9 @@ 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 py3 and a.tobytes() != b.tobytes():
elif 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
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ envlist = py26, py27, py32, py33, pypy
[testenv]
commands =
{envpython} selftest.py
{envpython} Tests/run.py --installed