Flake8 fixes

This commit is contained in:
Andrew Murray 2017-04-20 21:14:23 +10:00
parent 17a111cba7
commit 37b293f593
38 changed files with 177 additions and 170 deletions

View File

@ -325,6 +325,7 @@ def _normalize_mode(im, initial_call=False):
return im.convert("P")
return im.convert("L")
def _normalize_palette(im, palette, info):
"""
Normalizes the palette for image.
@ -364,6 +365,7 @@ def _normalize_palette(im, palette, info):
im.palette.palette = source_palette
return im
def _write_single_frame(im, fp, palette):
im_out = _normalize_mode(im, True)
im_out = _normalize_palette(im_out, palette, im.encoderinfo)
@ -383,6 +385,7 @@ def _write_single_frame(im, fp, palette):
fp.write(b"\0") # end of image data
def _write_multiple_frames(im, fp, palette):
duration = im.encoderinfo.get("duration", None)
@ -441,6 +444,7 @@ def _write_multiple_frames(im, fp, palette):
_write_frame_data(fp, im_frame, offset, frame_data['encoderinfo'])
return True
def _save_all(im, fp, filename):
_save(im, fp, filename, save_all=True)
@ -593,6 +597,7 @@ def _save_netpbm(im, fp, filename):
# cases where it took lots of memory and time previously.
_FORCE_OPTIMIZE = False
def _get_optimize(im, info):
"""
Palette optimization is a potentially expensive operation.
@ -627,6 +632,7 @@ def _get_optimize(im, info):
max(used_palette_colors) > len(used_palette_colors)):
return used_palette_colors
def _get_color_table_size(palette_bytes):
# calculate the palette size for the header
import math
@ -635,6 +641,7 @@ def _get_color_table_size(palette_bytes):
color_table_size = 0
return color_table_size
def _get_header_palette(palette_bytes):
"""
Returns the palette, null padded to the next power of 2 (*3) bytes
@ -652,6 +659,7 @@ def _get_header_palette(palette_bytes):
palette_bytes += o8(0) * 3 * actual_target_size_diff
return palette_bytes
def _get_palette_bytes(im):
"""
Gets the palette for inclusion in the gif header
@ -661,6 +669,7 @@ def _get_palette_bytes(im):
"""
return im.palette.palette
def _get_global_header(im, info):
"""Return a list of strings representing a GIF header"""
@ -699,6 +708,7 @@ def _get_global_header(im, info):
_get_header_palette(palette_bytes)
]
def _write_frame_data(fp, im_frame, offset, params):
try:
im_frame.encoderinfo = params
@ -716,6 +726,7 @@ def _write_frame_data(fp, im_frame, offset, params):
# --------------------------------------------------------------------
# Legacy GIF utilities
def getheader(im, palette=None, info=None):
"""
Legacy Method to get Gif data from image.
@ -733,7 +744,7 @@ def getheader(im, palette=None, info=None):
if info is None:
info = {}
if not "background" in info and "background" in im.info:
if "background" not in info and "background" in im.info:
info["background"] = im.info["background"]
im_mod = _normalize_palette(im, palette, info)
@ -743,6 +754,7 @@ def getheader(im, palette=None, info=None):
return header, used_palette_colors
# To specify duration, add the time in milliseconds to getdata(),
# e.g. getdata(im_frame, duration=1000)
def getdata(im, offset=(0, 0), **params):

View File

@ -1554,7 +1554,6 @@ class Image(object):
else: # L-mode
source_palette = bytearray(i//3 for i in range(768))
palette_bytes = b""
new_positions = [0]*256
@ -1607,8 +1606,6 @@ class Image(object):
return m_im
def resize(self, size, resample=NEAREST):
"""
Returns a resized copy of this image.
@ -2627,6 +2624,7 @@ def registered_extensions():
init()
return EXTENSION
def register_decoder(name, decoder):
"""
Registers an image decoder. This function should not be

View File

@ -167,7 +167,6 @@ class ImageCmsProfile(object):
else:
raise TypeError("Invalid type for Profile")
def _set(self, profile, filename=None):
self.profile = profile
self.filename = filename

View File

@ -540,6 +540,7 @@ class PyCodecState(object):
return (self.xoff, self.yoff,
self.xoff+self.xsize, self.yoff+self.ysize)
class PyDecoder(object):
"""
Python implementation of a format decoder. Override this class and
@ -616,7 +617,6 @@ class PyDecoder(object):
else:
(x0, y0, x1, y1) = (0, 0, 0, 0)
if x0 == 0 and x1 == 0:
self.state.xsize, self.state.ysize = self.im.size
else:

View File

@ -25,7 +25,8 @@
from . import Image, ImageFile
from ._binary import i16le as i16, o16le as o16, i8
import struct, io
import struct
import io
__version__ = "0.1"
@ -105,7 +106,6 @@ class MspDecoder(ImageFile.PyDecoder):
#
# which are then interpreted as a bit packed mode '1' image
_pulls_fd = True
def decode(self, buffer):

View File

@ -52,7 +52,6 @@ class SunImageFile(ImageFile.ImageFile):
# DWORD ColorMapLength; /* Size of the color map in bytes */
# } SUNRASTER;
# HEAD
s = self.fp.read(32)
if i32(s) != 0x59a66a95:

View File

@ -616,7 +616,8 @@ class ImageFileDirectory_v2(collections.MutableMapping):
@_register_loader(5, 8)
def load_rational(self, data, legacy_api=True):
vals = self._unpack("{}L".format(len(data) // 4), data)
combine = lambda a, b: (a, b) if legacy_api else IFDRational(a, b)
def combine(a, b): return (a, b) if legacy_api else IFDRational(a, b)
return tuple(combine(num, denom)
for num, denom in zip(vals[::2], vals[1::2]))
@ -636,7 +637,8 @@ class ImageFileDirectory_v2(collections.MutableMapping):
@_register_loader(10, 8)
def load_signed_rational(self, data, legacy_api=True):
vals = self._unpack("{}l".format(len(data) // 4), data)
combine = lambda a, b: (a, b) if legacy_api else IFDRational(a, b)
def combine(a, b): return (a, b) if legacy_api else IFDRational(a, b)
return tuple(combine(num, denom)
for num, denom in zip(vals[::2], vals[1::2]))
@ -1492,6 +1494,7 @@ def _save(im, fp, filename):
# just to access o32 and o16 (using correct byte order)
im._debug_multipage = ifd
class AppendingTiffWriter:
fieldSizes = [
0, # None
@ -1737,6 +1740,7 @@ class AppendingTiffWriter:
else:
self.rewriteLastLong(offset)
def _save_all(im, fp, filename):
if not hasattr(im, "n_frames"):
return _save(im, fp, filename)

View File

@ -25,7 +25,6 @@ from . import Image, ImageFile
from ._binary import i16le as word, si16le as short, i32le as dword, si32le as _long
__version__ = "0.2"
_handler = None
@ -66,6 +65,7 @@ if hasattr(Image.core, "drawwmf"):
# --------------------------------------------------------------------
# Read WMF file
def _accept(prefix):
return (
prefix[:6] == b"\xd7\xcd\xc6\x9a\x00\x00" or

View File

@ -37,6 +37,7 @@ def i16le(c, o=0):
"""
return unpack("<H", c[o:o+2])[0]
def si16le(c, o=0):
"""
Converts a 2-bytes (16 bits) string to a signed integer.
@ -56,6 +57,7 @@ def i32le(c, o=0):
"""
return unpack("<I", c[o:o+4])[0]
def si32le(c, o=0):
"""
Converts a 4-bytes (32 bits) string to a signed integer.

View File

@ -250,6 +250,7 @@ if sys.platform == 'win32':
else:
IMCONVERT = 'convert'
def distro():
if os.path.exists('/etc/os-release'):
with open('/etc/os-release', 'r') as f:
@ -257,6 +258,7 @@ def distro():
if 'ID=' in line:
return line.strip().split('=')[1]
class cached_property(object):
def __init__(self, func):
self.func = func

View File

@ -72,7 +72,6 @@ class TestFileGif(PillowTestCase):
self.assert_image_equal(im.convert('RGB'), reloaded.convert('RGB'))
# These do optimize the palette
check(128, 511, 128)
check(64, 511, 64)
@ -532,8 +531,5 @@ class TestFileGif(PillowTestCase):
GifImagePlugin._FORCE_OPTIMIZE = False
if __name__ == '__main__':
unittest.main()

View File

@ -42,6 +42,7 @@ class LibTiffTestCase(PillowTestCase):
out_bytes = io.BytesIO()
im.save(out_bytes, format='tiff', compression='group4')
class TestFileLibTiff(LibTiffTestCase):
def test_g4_tiff(self):

View File

@ -65,12 +65,12 @@ class TestFileMsp(PillowTestCase):
"Even More Extra image files not installed")
def test_msp_v2(self):
for f in os.listdir(YA_EXTRA_DIR):
if not '.MSP' in f: continue
if '.MSP' not in f:
continue
path = os.path.join(YA_EXTRA_DIR, f)
self._assert_file_image_equal(path,
path.replace('.MSP', '.png'))
def test_cannot_save_wrong_mode(self):
# Arrange
im = hopper()

View File

@ -41,7 +41,6 @@ class TestFilePpm(PillowTestCase):
self.assertRaises(ValueError, lambda: Image.open(path))
def test_neg_ppm(self):
# Storage.c accepted negative values for xsize, ysize. the
# internal open_ppm function didn't check for sanity but it

View File

@ -56,6 +56,5 @@ class TestFileSgi(PillowTestCase):
roundtrip(hopper(mode))
if __name__ == '__main__':
unittest.main()

View File

@ -6,6 +6,7 @@ import os
EXTRA_DIR = 'Tests/images/sunraster'
class TestFileSun(PillowTestCase):
def test_sanity(self):
@ -30,7 +31,6 @@ class TestFileSun(PillowTestCase):
target = Image.open('Tests/images/sunraster.im1.png')
self.assert_image_equal(im, target)
@unittest.skipIf(not os.path.exists(EXTRA_DIR),
"Extra image files not installed")
def test_others(self):

View File

@ -1,6 +1,7 @@
from helper import unittest, PillowTestCase, hopper
from PIL import Image
class TestFileWmf(PillowTestCase):
def test_load_raw(self):

View File

@ -375,13 +375,16 @@ class TestImage(PillowTestCase):
self.assert_image_equal(im, target)
class MockEncoder(object):pass
class MockEncoder(object):
pass
def mock_encode(*args):
encoder = MockEncoder()
encoder.args = args
return encoder
class TestRegistry(PillowTestCase):
def test_encode_registry(self):

View File

@ -97,7 +97,6 @@ class TestImageGetPixel(AccessTest):
with self.assertRaises(IndexError):
im.getpixel((0, 0))
def test_basic(self):
for mode in ("1", "L", "LA", "I", "I;16", "I;16B", "F",
"P", "PA", "RGB", "RGBA", "RGBX", "CMYK", "YCbCr"):

View File

@ -187,7 +187,6 @@ class TestImageConvert(PillowTestCase):
else:
self.assert_image_similar(converted_im, target.split()[0], 1)
matrix_convert('RGB')
matrix_convert('L')

View File

@ -104,6 +104,5 @@ class TestImageCrop(PillowTestCase):
self.assertEqual(cropped.getdata()[2], (0, 0, 0))
if __name__ == '__main__':
unittest.main()

View File

@ -1,8 +1,9 @@
from helper import unittest, PillowTestCase, hopper, distro
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase
from PIL import ImageQt
class TestFromQPixmap(PillowQPixmapTestCase, PillowTestCase):
def roundtrip(self, expected):

View File

@ -1,6 +1,6 @@
from __future__ import print_function
from helper import unittest, PillowTestCase, hopper
from PIL import Image, ImageDraw, ImageMode
from PIL import Image, ImageDraw
class TestImagingResampleVulnerability(PillowTestCase):

View File

@ -98,7 +98,5 @@ class TestImageRotate(PillowTestCase):
self.rotate(im, im.mode, 45, center=(0, 0), translate=(im.size[0]/2, 0))
if __name__ == '__main__':
unittest.main()

View File

@ -21,6 +21,7 @@ if ImageQt.qt_is_installed:
from PySide.QtGui import QWidget, QHBoxLayout, QLabel, QApplication
QT_VERSION = 4
class TestToQImage(PillowQtTestCase, PillowTestCase):
def test_sanity(self):
@ -58,7 +59,6 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
src = src.convert('P')
self.assert_image_equal(reloaded, src)
def test_segfault(self):
PillowQtTestCase.setUp(self)

View File

@ -1,4 +1,4 @@
from helper import unittest, PillowTestCase, hopper, distro
from helper import unittest, PillowTestCase, hopper
from test_imageqt import PillowQtTestCase, PillowQPixmapTestCase
from PIL import ImageQt

View File

@ -310,7 +310,6 @@ class TestImageDraw(PillowTestCase):
self.assert_image_equal(im, im_floodfill)
del draw
@unittest.skipIf(hasattr(sys, 'pypy_version_info'),
"Causes fatal RPython error on PyPy")
def test_floodfill_border(self):

View File

@ -142,6 +142,8 @@ class MockPyDecoder(ImageFile.PyDecoder):
return (-1, 0)
xoff, yoff, xsize, ysize = 10, 20, 100, 100
class MockImageFile(ImageFile.ImageFile):
def _open(self):
self.rawmode = 'RGBA'
@ -149,6 +151,7 @@ class MockImageFile(ImageFile.ImageFile):
self.size = (200, 200)
self.tile = [("MOCK", (xoff, yoff, xoff+xsize, yoff+ysize), 32, None)]
class TestPyDecoder(PillowTestCase):
def get_decoder(self):

View File

@ -62,7 +62,6 @@ class TestImageSequence(PillowTestCase):
self.assert_image_equal(frame, firstFrame)
break
def test_palette_mmap(self):
# Using mmap in ImageFile can require to reload the palette.
im = Image.open('Tests/images/multipage-mmap.tiff')

View File

@ -13,6 +13,7 @@ except (OSError, ImportError) as v:
TK_MODES = ('1', 'L', 'P', 'RGB', 'RGBA')
class TestImageTk(PillowTestCase):
def setUp(self):
@ -46,7 +47,6 @@ class TestImageTk(PillowTestCase):
im = ImageTk._get_image_from_kw(kw)
self.assertEqual(im, None)
def test_photoimage(self):
for mode in TK_MODES:
# test as image:
@ -62,8 +62,6 @@ class TestImageTk(PillowTestCase):
# reloaded = ImageTk.getimage(im_tk)
# self.assert_image_equal(reloaded, im)
def test_photoimage_blank(self):
# test a image using mode/size:
for mode in TK_MODES:
@ -88,6 +86,5 @@ class TestImageTk(PillowTestCase):
# self.assert_image_equal(reloaded, im)
if __name__ == '__main__':
unittest.main()

View File

@ -233,7 +233,6 @@ class DdsImageFile(ImageFile.ImageFile):
bitcount, rmask, gmask, bmask, amask = struct.unpack("<5I",
header.read(20))
if fourcc == b"DXT1":
self.decoder = "DXT1"
codec = _dxt1
@ -247,7 +246,6 @@ class DdsImageFile(ImageFile.ImageFile):
(self.decoder, (0, 0) + self.size, 0, (self.mode, 0, 1))
]
def load_seek(self, pos):
pass
@ -260,9 +258,9 @@ class DXT1Decoder(ImageFile.PyDecoder):
self.set_as_raw(_dxt1(self.fd, self.state.xsize, self.state.ysize))
except struct.error:
raise IOError("Truncated DDS file")
return 0, 0
class DXT5Decoder(ImageFile.PyDecoder):
_pulls_fd = True