mirror of
				https://github.com/python-pillow/Pillow.git
				synced 2025-10-30 23:47:27 +03:00 
			
		
		
		
	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:
		
							parent
							
								
									ad784eb808
								
							
						
					
					
						commit
						197885164b
					
				|  | @ -29,6 +29,7 @@ | ||||||
| 
 | 
 | ||||||
| import Image | import Image | ||||||
| import traceback, os | import traceback, os | ||||||
|  | import io | ||||||
| 
 | 
 | ||||||
| MAXBLOCK = 65536 | MAXBLOCK = 65536 | ||||||
| 
 | 
 | ||||||
|  | @ -475,7 +476,7 @@ def _save(im, fp, tile): | ||||||
|     try: |     try: | ||||||
|         fh = fp.fileno() |         fh = fp.fileno() | ||||||
|         fp.flush() |         fp.flush() | ||||||
|     except AttributeError: |     except (AttributeError, io.UnsupportedOperation): | ||||||
|         # compress to Python file-compatible object |         # compress to Python file-compatible object | ||||||
|         for e, b, o, a in tile: |         for e, b, o, a in tile: | ||||||
|             e = Image._getencoder(im.mode, e, a, im.encoderconfig) |             e = Image._getencoder(im.mode, e, a, im.encoderconfig) | ||||||
|  |  | ||||||
|  | @ -1,3 +1,5 @@ | ||||||
|  | from __future__ import print_function | ||||||
|  | 
 | ||||||
| # minimal test runner | # minimal test runner | ||||||
| 
 | 
 | ||||||
| import glob, os, sys | import glob, os, sys | ||||||
|  |  | ||||||
|  | @ -1,3 +1,6 @@ | ||||||
|  | from __future__ import print_function | ||||||
|  | from tester import * | ||||||
|  | 
 | ||||||
| import PIL | import PIL | ||||||
| import PIL.Image | import PIL.Image | ||||||
| 
 | 
 | ||||||
|  | @ -9,7 +12,7 @@ assert PIL.Image.VERSION[:3] == '1.1' | ||||||
| # Create an image and do stuff with it. | # Create an image and do stuff with it. | ||||||
| im = PIL.Image.new("1", (100, 100)) | im = PIL.Image.new("1", (100, 100)) | ||||||
| assert (im.mode, im.size) == ('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. | # Create images in all remaining major modes. | ||||||
| im = PIL.Image.new("L", (100, 100)) | im = PIL.Image.new("L", (100, 100)) | ||||||
|  |  | ||||||
|  | @ -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) |  | ||||||
|  | @ -166,7 +166,7 @@ def test_truncated_jpeg(): | ||||||
|     def test(junk): |     def test(junk): | ||||||
|         if junk: |         if junk: | ||||||
|             # replace "junk" bytes at the end with junk |             # replace "junk" bytes at the end with junk | ||||||
|             file = BytesIO(data[:-junk] + bytes(junk*[0])) |             file = BytesIO(data[:-junk] + b'\0'*junk) | ||||||
|         else: |         else: | ||||||
|             file = BytesIO(data) |             file = BytesIO(data) | ||||||
|         im = Image.open(file) |         im = Image.open(file) | ||||||
|  |  | ||||||
|  | @ -24,7 +24,7 @@ def chunk(cid, *data): | ||||||
| 
 | 
 | ||||||
| o32 = PngImagePlugin.o32 | 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") | IDAT = chunk(b"IDAT") | ||||||
| IEND = chunk(b"IEND") | IEND = chunk(b"IEND") | ||||||
| 
 | 
 | ||||||
|  | @ -154,7 +154,13 @@ def test_scary(): | ||||||
| 
 | 
 | ||||||
|     import base64 |     import base64 | ||||||
|     file = "Tests/images/pngtest_bad.png.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) |     file = BytesIO(data) | ||||||
|     assert_exception(IOError, lambda: Image.open(file)) |     assert_exception(IOError, lambda: Image.open(file)) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,7 +5,12 @@ from PIL import Image | ||||||
| def test_sanity(): | def test_sanity(): | ||||||
| 
 | 
 | ||||||
|     im1 = lena() |     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) |     assert_image_equal(im1, im2) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -5,9 +5,10 @@ from PIL import Image | ||||||
| def test_sanity(): | def test_sanity(): | ||||||
| 
 | 
 | ||||||
|     im = lena() |     im = lena() | ||||||
| 
 |  | ||||||
|     type_repr = repr(type(im.getim())) |     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)) |     assert_true(isinstance(im.im.id, int)) | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -3,6 +3,5 @@ from tester import * | ||||||
| from PIL import Image | from PIL import Image | ||||||
| 
 | 
 | ||||||
| def test_sanity(): | def test_sanity(): | ||||||
| 
 |     data = lena().tobytes() if py3 else lena().tostring() | ||||||
|     data = lena().tobytes() |  | ||||||
|     assert_true(isinstance(data, bytes)) |     assert_true(isinstance(data, bytes)) | ||||||
|  |  | ||||||
|  | @ -12,7 +12,11 @@ def test_pack(): | ||||||
|             im = Image.new(mode, (1, 1), 1) |             im = Image.new(mode, (1, 1), 1) | ||||||
|         else: |         else: | ||||||
|             im = Image.new(mode, (1, 1), (1, 2, 3, 4)[:len(mode)]) |             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"), [128]) | ||||||
|     assert_equal(pack("1", "1;I"), [0]) |     assert_equal(pack("1", "1;I"), [0]) | ||||||
|  | @ -45,13 +49,26 @@ def test_pack(): | ||||||
| def test_unpack(): | def test_unpack(): | ||||||
| 
 | 
 | ||||||
|     def unpack(mode, rawmode, bytes_): |     def unpack(mode, rawmode, bytes_): | ||||||
|         data = bytes(range(1,bytes_+1)) |         im = None | ||||||
|         im = Image.frombytes(mode, (1, 1), data, "raw", rawmode, 0, 1) | 
 | ||||||
|  |         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)) |         return im.getpixel((0, 0)) | ||||||
| 
 | 
 | ||||||
|     def unpack_1(mode, rawmode, value): |     def unpack_1(mode, rawmode, value): | ||||||
|         assert mode == "1" |         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()) |         return tuple(im.getdata()) | ||||||
| 
 | 
 | ||||||
|     X = 255 |     X = 255 | ||||||
|  |  | ||||||
|  | @ -82,7 +82,10 @@ def test_basic(): | ||||||
| def test_tostring(): | def test_tostring(): | ||||||
| 
 | 
 | ||||||
|     def tostring(mode): |     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("L"), b"\x01") | ||||||
|     assert_equal(tostring("I;16"), b"\x01\x00") |     assert_equal(tostring("I;16"), b"\x01\x00") | ||||||
|  |  | ||||||
|  | @ -1,3 +1,8 @@ | ||||||
|  | from __future__ import print_function | ||||||
|  | 
 | ||||||
|  | import sys | ||||||
|  | py3 = (sys.version_info >= (3,0)) | ||||||
|  | 
 | ||||||
| # some test helpers | # some test helpers | ||||||
| 
 | 
 | ||||||
| _target = None | _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)) |         failure(msg or "got mode %r, expected %r" % (a.mode, b.mode)) | ||||||
|     elif a.size != b.size: |     elif a.size != b.size: | ||||||
|         failure(msg or "got size %r, expected %r" % (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") |         failure(msg or "got different content") | ||||||
|         # generate better diff? |         # generate better diff? | ||||||
|  |     elif not py3 and a.tostring() != b.tostring(): | ||||||
|  |         failure(msg or "got different content") | ||||||
|  |         # same complaint? | ||||||
|     else: |     else: | ||||||
|         success() |         success() | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue
	
	Block a user