Various Flake8 fixes

This commit is contained in:
Andrew Murray 2015-04-24 09:26:52 +10:00
parent 40165d6860
commit d1c182cadc
40 changed files with 201 additions and 230 deletions

View File

@ -18,6 +18,7 @@ _handler = None
#
# @param handler Handler object.
def register_handler(handler):
global _handler
_handler = handler
@ -25,9 +26,11 @@ def register_handler(handler):
# --------------------------------------------------------------------
# Image adapter
def _accept(prefix):
return prefix[:6] == b"SIMPLE"
class FITSStubImageFile(ImageFile.StubImageFile):
format = "FITS"

View File

@ -136,9 +136,9 @@ class FpxImageFile(ImageFile.ImageFile):
s = fp.read(36)
size = i32(s, 4), i32(s, 8)
#tilecount = i32(s, 12)
# tilecount = i32(s, 12)
tilesize = i32(s, 16), i32(s, 20)
#channels = i32(s, 24)
# channels = i32(s, 24)
offset = i32(s, 28)
length = i32(s, 32)

View File

@ -387,7 +387,7 @@ def init():
for plugin in _plugins:
try:
if DEBUG:
print ("Importing %s" % plugin)
print("Importing %s" % plugin)
__import__("PIL.%s" % plugin, globals(), locals(), [])
except ImportError:
if DEBUG:
@ -546,7 +546,7 @@ class Image:
self.fp.close()
except Exception as msg:
if DEBUG:
print ("Error closing: %s" % msg)
print("Error closing: %s" % msg)
# Instead of simply setting to None, we're setting up a
# deferred error that will better explain that the core image

View File

@ -53,6 +53,7 @@ class Color(_Enhance):
self.degenerate = image.convert(self.intermediate_mode).convert(image.mode)
class Contrast(_Enhance):
"""Adjust image contrast.

View File

@ -35,6 +35,7 @@ else: #PyQt4 is used
##
# (Internal) Turns an RGB color into a Qt compatible color integer.
def rgb(r, g, b, a=255):
# use qRgb to pack the colors, and then turn the resulting long
# into a negative integer with the same bitpattern.

View File

@ -234,7 +234,10 @@ __version__ = '0.30'
import io
import sys
import struct, array, os.path, datetime
import struct
import array
import os.path
import datetime
#[PL] Define explicitly the public API to avoid private objects in pydoc:
__all__ = ['OleFileIO', 'isOleFile', 'MAGIC']
@ -283,12 +286,17 @@ KEEP_UNICODE_NAMES = False
#[PL] DEBUG display mode: False by default, use set_debug_mode() or "-d" on
# command line to change it.
DEBUG_MODE = False
def debug_print(msg):
print(msg)
def debug_pass(msg):
pass
debug = debug_pass
def set_debug_mode(debug_mode):
"""
Set debug mode on or off, to control display of debugging messages.
@ -303,18 +311,18 @@ def set_debug_mode(debug_mode):
MAGIC = b'\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1'
#[PL]: added constants for Sector IDs (from AAF specifications)
# [PL]: added constants for Sector IDs (from AAF specifications)
MAXREGSECT = 0xFFFFFFFA; # maximum SECT
DIFSECT = 0xFFFFFFFC; # (-4) denotes a DIFAT sector in a FAT
FATSECT = 0xFFFFFFFD; # (-3) denotes a FAT sector in a FAT
ENDOFCHAIN = 0xFFFFFFFE; # (-2) end of a virtual stream chain
FREESECT = 0xFFFFFFFF; # (-1) unallocated sector
#[PL]: added constants for Directory Entry IDs (from AAF specifications)
# [PL]: added constants for Directory Entry IDs (from AAF specifications)
MAXREGSID = 0xFFFFFFFA; # maximum directory entry ID
NOSTREAM = 0xFFFFFFFF; # (-1) unallocated directory entry
#[PL] object types in storage (from AAF specifications)
# [PL] object types in storage (from AAF specifications)
STGTY_EMPTY = 0 # empty directory entry (according to OpenOffice.org doc)
STGTY_STORAGE = 1 # element is a storage object
STGTY_STREAM = 2 # element is a stream object
@ -433,7 +441,6 @@ def _clsid(clsid):
tuple(map(i8, clsid[8:16]))))
# UNICODE support:
# (necessary to handle storages/streams names which use Unicode)
@ -471,7 +478,6 @@ def filetime2datetime(filetime):
return _FILETIME_null_date + datetime.timedelta(microseconds=filetime//10)
#=== CLASSES ==================================================================
class OleMetadata:
@ -579,7 +585,6 @@ class OleMetadata:
self.language = None
self.doc_version = None
def parse_properties(self, olefile):
"""
Parse standard properties of an OLE file, from the streams
@ -663,7 +668,7 @@ class _OleStream(io.BytesIO):
"""
debug('_OleStream.__init__:')
debug(' sect=%d (%X), size=%d, offset=%d, sectorsize=%d, len(fat)=%d, fp=%s'
%(sect,sect,size,offset,sectorsize,len(fat), repr(fp)))
%(sect, sect, size, offset, sectorsize, len(fat), repr(fp)))
#[PL] To detect malformed documents with FAT loops, we compute the
# expected number of sectors in the stream:
unknown_size = False
@ -789,7 +794,6 @@ class _OleDirectoryEntry:
DIRENTRY_SIZE = 128
assert struct.calcsize(STRUCT_DIRENTRY) == DIRENTRY_SIZE
def __init__(self, entry, sid, olefile):
"""
Constructor for an _OleDirectoryEntry object.
@ -882,8 +886,6 @@ class _OleDirectoryEntry:
minifat = False
olefile._check_duplicate_stream(self.isectStart, minifat)
def build_storage_tree(self):
"""
Read and build the red-black tree attached to this _OleDirectoryEntry
@ -907,7 +909,6 @@ class _OleDirectoryEntry:
# (see rich comparison methods in this class)
self.kids.sort()
def append_kids(self, child_sid):
"""
Walk through red-black tree of children of this directory entry to add
@ -916,7 +917,7 @@ class _OleDirectoryEntry:
child_sid : index of child directory entry to use, or None when called
first time for the root. (only used during recursion)
"""
#[PL] this method was added to use simple recursion instead of a complex
# [PL] this method was added to use simple recursion instead of a complex
# algorithm.
# if this is not a storage or a leaf of the tree, nothing to do:
if child_sid == NOSTREAM:
@ -951,7 +952,6 @@ class _OleDirectoryEntry:
# Afterwards build kid's own tree if it's also a storage:
child.build_storage_tree()
def __eq__(self, other):
"Compare entries by name"
return self.name == other.name
@ -971,7 +971,6 @@ class _OleDirectoryEntry:
#TODO: replace by the same function as MS implementation ?
# (order by name length first, then case-insensitive order)
def dump(self, tab = 0):
"Dump this entry, and all its subentries (for debug purposes only)"
TYPES = ["(invalid)", "(storage)", "(stream)", "(lockbytes)",
@ -986,7 +985,6 @@ class _OleDirectoryEntry:
for kid in self.kids:
kid.dump(tab + 2)
def getmtime(self):
"""
Return modification time of a directory entry.
@ -1000,7 +998,6 @@ class _OleDirectoryEntry:
return None
return filetime2datetime(self.modifyTime)
def getctime(self):
"""
Return creation time of a directory entry.
@ -1064,7 +1061,6 @@ class OleFileIO:
if filename:
self.open(filename)
def _raise_defect(self, defect_level, message, exception_type=IOError):
"""
This method should be called for any defect found during file parsing.
@ -1086,7 +1082,6 @@ class OleFileIO:
# just record the issue, no exception raised:
self.parsing_issues.append((exception_type, message))
def open(self, filename):
"""
Open an OLE2 file.
@ -1189,7 +1184,7 @@ class OleFileIO:
self.sectDifStart,
self.csectDif
) = struct.unpack(fmt_header, header1)
debug( struct.unpack(fmt_header, header1))
debug(struct.unpack(fmt_header, header1))
if self.Sig != MAGIC:
# OLE signature should always be present
@ -1231,11 +1226,11 @@ class OleFileIO:
# rule => only a potential defect:
if self.signature != 0:
self._raise_defect(DEFECT_POTENTIAL, "incorrect OLE header (signature>0)")
debug( "MiniSectorCutoff = %d" % self.MiniSectorCutoff )
debug( "MiniFatStart = %X" % self.MiniFatStart )
debug( "csectMiniFat = %d" % self.csectMiniFat )
debug( "sectDifStart = %X" % self.sectDifStart )
debug( "csectDif = %d" % self.csectDif )
debug("MiniSectorCutoff = %d" % self.MiniSectorCutoff)
debug("MiniFatStart = %X" % self.MiniFatStart)
debug("csectMiniFat = %d" % self.csectMiniFat)
debug("sectDifStart = %X" % self.sectDifStart)
debug("csectDif = %d" % self.csectDif)
# calculate the number of sectors in the file
# (-1 because header doesn't count)
@ -1266,14 +1261,12 @@ class OleFileIO:
self.ministream = None
self.minifatsect = self.MiniFatStart #i32(header, 60)
def close(self):
"""
close the OLE file, to release the file object
"""
self.fp.close()
def _check_duplicate_stream(self, first_sect, minifat=False):
"""
Checks if a stream has not been already referenced elsewhere.
@ -1298,7 +1291,6 @@ class OleFileIO:
else:
used_streams.append(first_sect)
def dumpfat(self, fat, firstindex=0):
"Displays a part of FAT in human-readable form for debugging purpose"
# [PL] added only for debug
@ -1335,7 +1327,6 @@ class OleFileIO:
print(nom, end=" ")
print()
def dumpsect(self, sector, firstindex=0):
"Displays a sector in a human-readable form, for debugging purpose."
if not DEBUG_MODE:
@ -1370,7 +1361,6 @@ class OleFileIO:
a.byteswap()
return a
def loadfat_sect(self, sect):
"""
Adds the indexes of the given sector to the FAT
@ -1400,7 +1390,6 @@ class OleFileIO:
self.fat = self.fat + nextfat
return isect
def loadfat(self, header):
"""
Load the FAT table.
@ -1436,16 +1425,16 @@ class OleFileIO:
if self.sectDifStart >= self.nb_sect:
# initial DIFAT block index must be valid
self._raise_defect(DEFECT_FATAL, 'incorrect DIFAT, first index out of range')
debug( "DIFAT analysis..." )
debug("DIFAT analysis...")
# We compute the necessary number of DIFAT sectors :
# (each DIFAT sector = 127 pointers + 1 towards next DIFAT sector)
nb_difat = (self.csectFat-109 + 126)//127
debug( "nb_difat = %d" % nb_difat )
debug("nb_difat = %d" % nb_difat)
if self.csectDif != nb_difat:
raise IOError('incorrect DIFAT')
isect_difat = self.sectDifStart
for i in iterrange(nb_difat):
debug( "DIFAT block %d, sector %X" % (i, isect_difat) )
debug("DIFAT block %d, sector %X" % (i, isect_difat))
#TODO: check if corresponding FAT SID = DIFSECT
sector_difat = self.getsect(isect_difat)
difat = self.sect2array(sector_difat)
@ -1453,7 +1442,7 @@ class OleFileIO:
self.loadfat_sect(difat[:127])
# last DIFAT pointer is next DIFAT sector:
isect_difat = difat[127]
debug( "next DIFAT sector: %X" % isect_difat )
debug("next DIFAT sector: %X" % isect_difat)
# checks:
if isect_difat not in [ENDOFCHAIN, FREESECT]:
# last DIFAT pointer value must be ENDOFCHAIN or FREESECT
@ -1471,7 +1460,6 @@ class OleFileIO:
debug('\nFAT:')
self.dumpfat(self.fat)
def loadminifat(self):
"""
Load the MiniFAT table.
@ -1531,7 +1519,6 @@ class OleFileIO:
self._raise_defect(DEFECT_FATAL, 'incomplete OLE sector')
return sector
def loaddirectory(self, sect):
"""
Load the directory.
@ -1567,7 +1554,6 @@ class OleFileIO:
# read and build all storage trees, starting from the root:
self.root.build_storage_tree()
def _load_direntry (self, sid):
"""
Load a directory entry from the directory.
@ -1592,14 +1578,12 @@ class OleFileIO:
self.direntries[sid] = _OleDirectoryEntry(entry, sid, self)
return self.direntries[sid]
def dumpdirectory(self):
"""
Dump directory (for debugging only)
"""
self.root.dump()
def _open(self, start, size = 0x7FFFFFFF, force_FAT=False):
"""
Open a stream, either in FAT or MiniFAT according to its size.
@ -1633,7 +1617,6 @@ class OleFileIO:
return _OleStream(self.fp, start, size, 512,
self.sectorsize, self.fat, self._filesize)
def _list(self, files, prefix, node, streams=True, storages=False):
"""
(listdir helper)
@ -1659,7 +1642,6 @@ class OleFileIO:
# add it to the list
files.append(prefix[1:] + [entry.name])
def listdir(self, streams=True, storages=False):
"""
Return a list of streams stored in this file
@ -1672,7 +1654,6 @@ class OleFileIO:
self._list(files, [], self.root, streams, storages)
return files
def _find(self, filename):
"""
Returns directory entry of given filename. (openstream helper)
@ -1703,7 +1684,6 @@ class OleFileIO:
node = kid
return node.sid
def openstream(self, filename):
"""
Open a stream as a read-only file object (BytesIO).
@ -1724,7 +1704,6 @@ class OleFileIO:
raise IOError("this file is not a stream")
return self._open(entry.isectStart, entry.size)
def get_type(self, filename):
"""
Test if given filename exists as a stream or a storage in the OLE
@ -1744,7 +1723,6 @@ class OleFileIO:
except:
return False
def getmtime(self, filename):
"""
Return modification time of a stream/storage.
@ -1760,7 +1738,6 @@ class OleFileIO:
entry = self.direntries[sid]
return entry.getmtime()
def getctime(self, filename):
"""
Return creation time of a stream/storage.
@ -1776,7 +1753,6 @@ class OleFileIO:
entry = self.direntries[sid]
return entry.getctime()
def exists(self, filename):
"""
Test if given filename exists as a stream or a storage in the OLE
@ -1791,7 +1767,6 @@ class OleFileIO:
except:
return False
def get_size(self, filename):
"""
Return size of a stream in the OLE container, in bytes.
@ -1808,7 +1783,6 @@ class OleFileIO:
raise TypeError('object is not an OLE stream')
return entry.size
def get_rootentry_name(self):
"""
Return root entry name. Should usually be 'Root Entry' or 'R' in most
@ -1816,7 +1790,6 @@ class OleFileIO:
"""
return self.root.name
def getproperties(self, filename, convert_time=False, no_conversion=None):
"""
Return properties described in substream.
@ -1828,7 +1801,7 @@ class OleFileIO:
:returns: a dictionary of values indexed by id (integer)
"""
# make sure no_conversion is a list, just to simplify code below:
if no_conversion == None:
if no_conversion is None:
no_conversion = []
# stream path as a string to report exceptions:
streampath = filename
@ -1842,11 +1815,11 @@ class OleFileIO:
try:
# header
s = fp.read(28)
#clsid = _clsid(s[8:24])
# clsid = _clsid(s[8:24])
# format id
s = fp.read(20)
#fmtid = _clsid(s[:16])
# fmtid = _clsid(s[:16])
fp.seek(i32(s, 16))
# get section
@ -1869,7 +1842,7 @@ class OleFileIO:
offset = i32(s, 12+i*8)
type = i32(s, offset)
debug ('property id=%d: type=%d offset=%X' % (id, type, offset))
debug('property id=%d: type=%d offset=%X' % (id, type, offset))
# test for common types first (should perhaps use
# a dictionary instead?)
@ -1891,7 +1864,7 @@ class OleFileIO:
# CodePageString, see http://msdn.microsoft.com/en-us/library/dd942354.aspx
# size is a 32 bits integer, including the null terminator, and
# possibly trailing or embedded null chars
#TODO: if codepage is unicode, the string should be converted as such
# TODO: if codepage is unicode, the string should be converted as such
count = i32(s, offset+4)
value = s[offset+8:offset+8+count-1]
# remove all null chars:
@ -1909,7 +1882,7 @@ class OleFileIO:
count = i32(s, offset+4)
value = _unicode(s[offset+8:offset+8+count*2])
elif type == VT_FILETIME:
value = long(i32(s, offset+4)) + (long(i32(s, offset+8))<<32)
value = long(i32(s, offset+4)) + (long(i32(s, offset+8)) << 32)
# FILETIME is a 64-bit int: "number of 100ns periods
# since Jan 1,1601".
if convert_time and id not in no_conversion:
@ -1939,7 +1912,7 @@ class OleFileIO:
value = bool(i16(s, offset+4))
else:
value = None # everything else yields "None"
debug ('property id=%d: type=%d not implemented in parser yet' % (id, type))
debug('property id=%d: type=%d not implemented in parser yet' % (id, type))
# missing: VT_EMPTY, VT_NULL, VT_R4, VT_R8, VT_CY, VT_DATE,
# VT_DECIMAL, VT_I1, VT_I8, VT_UI8,
@ -1951,8 +1924,8 @@ class OleFileIO:
# type of items, e.g. VT_VECTOR|VT_BSTR
# see http://msdn.microsoft.com/en-us/library/dd942011.aspx
#print("%08x" % id, repr(value), end=" ")
#print("(%s)" % VT[i32(s, offset) & 0xFFF])
# print("%08x" % id, repr(value), end=" ")
# print("(%s)" % VT[i32(s, offset) & 0xFFF])
data[id] = value
except BaseException as exc:
@ -1999,7 +1972,7 @@ Options:
check_streams = False
for filename in sys.argv[1:]:
#try:
# try:
# OPTIONS:
if filename == '-d':
# option to switch debug mode on:
@ -2010,7 +1983,7 @@ Options:
check_streams = True
continue
ole = OleFileIO(filename)#, raise_defects=DEFECT_INCORRECT)
ole = OleFileIO(filename) #, raise_defects=DEFECT_INCORRECT)
print("-" * 68)
print(filename)
print("-" * 68)
@ -2027,8 +2000,8 @@ Options:
v = v[:50]
if isinstance(v, bytes):
# quick and dirty binary check:
for c in (1,2,3,4,5,6,7,11,12,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,31):
for c in (1, 2, 3, 4, 5, 6, 7, 11, 12, 14, 15, 16, 17, 18, 19, 20,
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31):
if c in bytearray(v):
v = '(binary data)'
break
@ -2039,7 +2012,7 @@ Options:
print('\nChecking streams...')
for streamname in ole.listdir():
# print name using repr() to convert binary chars to \xNN:
print('-', repr('/'.join(streamname)),'-', end=' ')
print('-', repr('/'.join(streamname)), '-', end=' ')
st_type = ole.get_type(streamname)
if st_type == STGTY_STREAM:
print('size %d' % ole.get_size(streamname))
@ -2066,7 +2039,7 @@ Options:
meta = ole.get_metadata()
meta.dump()
print()
#[PL] Test a few new methods:
# [PL] Test a few new methods:
root = ole.get_rootentry_name()
print('Root entry name: "%s"' % root)
if ole.exists('worddocument'):

View File

@ -58,7 +58,7 @@ class PcxImageFile(ImageFile.ImageFile):
if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
raise SyntaxError("bad PCX image size")
if Image.DEBUG:
print ("BBox: %s %s %s %s" % bbox)
print("BBox: %s %s %s %s" % bbox)
# format
version = i8(s[1])
@ -66,7 +66,7 @@ class PcxImageFile(ImageFile.ImageFile):
planes = i8(s[65])
stride = i16(s, 66)
if Image.DEBUG:
print ("PCX version %s, bits %s, planes %s, stride %s" %
print("PCX version %s, bits %s, planes %s, stride %s" %
(version, bits, planes, stride))
self.info["dpi"] = i16(s, 12), i16(s, 14)
@ -106,7 +106,7 @@ class PcxImageFile(ImageFile.ImageFile):
bbox = (0, 0) + self.size
if Image.DEBUG:
print ("size: %sx%s" % self.size)
print("size: %sx%s" % self.size)
self.tile = [("pcx", bbox, self.fp.tell(), (rawmode, planes * stride))]
@ -143,7 +143,7 @@ def _save(im, fp, filename, check=0):
# gets overwritten.
if Image.DEBUG:
print ("PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d" % (
print("PcxImagePlugin._save: xwidth: %d, bits: %d, stride: %d" % (
im.size[0], bits, stride))
# under windows, we could determine the current screen size with

View File

@ -78,6 +78,7 @@ MAX_TEXT_CHUNK = ImageFile.SAFEBLOCK
# Set the maximum total text chunk size.
MAX_TEXT_MEMORY = 64 * MAX_TEXT_CHUNK
def _safe_zlib_decompress(s):
dobj = zlib.decompressobj()
plaintext = dobj.decompress(s, MAX_TEXT_CHUNK)

View File

@ -51,7 +51,7 @@ class PyAccess(object):
self.ysize = vals['ysize']
if DEBUG:
print (vals)
print(vals)
self._post_init()
def _post_init(self):

View File

@ -506,7 +506,7 @@ class ImageFileDirectory(collections.MutableMapping):
typ = self.tagtype[tag]
if Image.DEBUG:
print ("Tag %s, Type: %s, Value: %s" % (tag, typ, value))
print("Tag %s, Type: %s, Value: %s" % (tag, typ, value))
if typ == 1:
# byte data
@ -638,9 +638,9 @@ class TiffImageFile(ImageFile.ImageFile):
self.__fp = self.fp
if Image.DEBUG:
print ("*** TiffImageFile._open ***")
print ("- __first:", self.__first)
print ("- ifh: ", ifh)
print("*** TiffImageFile._open ***")
print("- __first:", self.__first)
print("- ifh: ", ifh)
# and load the first frame
self._seek(0)
@ -751,19 +751,19 @@ class TiffImageFile(ImageFile.ImageFile):
# that returns an IOError if there's no underlying fp. Easier to
# dea. with here by reordering.
if Image.DEBUG:
print ("have getvalue. just sending in a string from getvalue")
print("have getvalue. just sending in a string from getvalue")
n, err = decoder.decode(self.fp.getvalue())
elif hasattr(self.fp, "fileno"):
# we've got a actual file on disk, pass in the fp.
if Image.DEBUG:
print ("have fileno, calling fileno version of the decoder.")
print("have fileno, calling fileno version of the decoder.")
self.fp.seek(0)
# 4 bytes, otherwise the trace might error out
n, err = decoder.decode(b"fpfp")
else:
# we have something else.
if Image.DEBUG:
print ("don't have fileno or getvalue. just reading")
print("don't have fileno or getvalue. just reading")
# UNDONE -- so much for that buffer size thing.
n, err = decoder.decode(self.fp.read())
@ -943,7 +943,7 @@ class TiffImageFile(ImageFile.ImageFile):
(0, min(y, ysize), w, min(y+h, ysize)),
offsets[i], a))
if Image.DEBUG:
print ("tiles: ", self.tile)
print("tiles: ", self.tile)
y = y + h
if y >= self.size[1]:
x = y = 0
@ -1128,8 +1128,8 @@ def _save(im, fp, filename):
if libtiff:
if Image.DEBUG:
print ("Saving using libtiff encoder")
print (ifd.items())
print("Saving using libtiff encoder")
print(ifd.items())
_fp = 0
if hasattr(fp, "fileno"):
try:
@ -1186,7 +1186,7 @@ def _save(im, fp, filename):
atts[k] = v
if Image.DEBUG:
print (atts)
print(atts)
# libtiff always expects the bytes in native order.
# we're storing image byte order. So, if the rawmode

View File

@ -47,7 +47,7 @@ class BenchCffiAccess(PillowTestCase):
self.assertEqual(caccess[(0, 0)], access[(0, 0)])
print ("Size: %sx%s" % im.size)
print("Size: %sx%s" % im.size)
timer(iterate_get, 'PyAccess - get', im.size, access)
timer(iterate_set, 'PyAccess - set', im.size, access)
timer(iterate_get, 'C-api - get', im.size, caccess)

View File

@ -8,6 +8,7 @@ from PIL import Image, ImageFilter
min_iterations = 100
max_iterations = 10000
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
class TestImagingLeaks(PillowTestCase):
@ -40,4 +41,3 @@ class TestImagingLeaks(PillowTestCase):
if __name__ == '__main__':
unittest.main()

View File

@ -74,7 +74,6 @@ post-patch:
"""
def test_qtables_leak(self):
im = hopper('RGB')
@ -103,7 +102,6 @@ post-patch:
qtables = [standard_l_qtable,
standard_chrominance_qtable]
for count in range(iterations):
test_output = BytesIO()
im.save(test_output, "JPEG", qtables=qtables)
@ -172,7 +170,6 @@ post patch:
test_output = BytesIO()
im.save(test_output, "JPEG", exif=exif)
"""
base case:
MB

View File

@ -5,6 +5,7 @@ import zlib
TEST_FILE = "Tests/images/png_decompression_dos.png"
class TestPngDos(PillowTestCase):
def test_dos_text(self):
@ -19,14 +20,14 @@ class TestPngDos(PillowTestCase):
self.assertLess(len(s), 1024*1024, "Text chunk larger than 1M")
def test_dos_total_memory(self):
im = Image.new('L',(1,1))
im = Image.new('L', (1, 1))
compressed_data = zlib.compress('a'*1024*1023)
info = PngImagePlugin.PngInfo()
for x in range(64):
info.add_text('t%s'%x, compressed_data, 1)
info.add_itxt('i%s'%x, compressed_data, zip=True)
info.add_text('t%s' % x, compressed_data, 1)
info.add_itxt('i%s' % x, compressed_data, zip=True)
b = BytesIO()
im.save(b, 'PNG', pnginfo=info)

View File

@ -130,7 +130,7 @@ class PillowTestCase(unittest.TestCase):
# Skip if platform/travis matches, and
# PILLOW_RUN_KNOWN_BAD is not true in the environment.
if bool(os.environ.get('PILLOW_RUN_KNOWN_BAD', False)):
print (os.environ.get('PILLOW_RUN_KNOWN_BAD', False))
print(os.environ.get('PILLOW_RUN_KNOWN_BAD', False))
return
skip = True

View File

@ -2,6 +2,7 @@ from helper import unittest, PillowTestCase
from PIL import _binary
class TestBinary(PillowTestCase):
def test_standard(self):

View File

@ -148,8 +148,8 @@ class TestFileEps(PillowTestCase):
# open image with binary preview
Image.open(file3)
def _test_readline(self,t, ending):
ending = "Failure with line ending: %s" %("".join("%s" %ord(s) for s in ending))
def _test_readline(self, t, ending):
ending = "Failure with line ending: %s" % ("".join("%s" % ord(s) for s in ending))
self.assertEqual(t.readline().strip('\r\n'), 'something', ending)
self.assertEqual(t.readline().strip('\r\n'), 'else', ending)
self.assertEqual(t.readline().strip('\r\n'), 'baz', ending)
@ -174,30 +174,30 @@ class TestFileEps(PillowTestCase):
def _test_readline_file_universal(self, test_string, ending):
f = self.tempfile('temp.txt')
with open(f,'wb') as w:
with open(f, 'wb') as w:
if str is bytes:
w.write(test_string)
else:
w.write(test_string.encode('UTF-8'))
with open(f,'rU') as t:
with open(f, 'rU') as t:
self._test_readline(t, ending)
def _test_readline_file_psfile(self, test_string, ending):
f = self.tempfile('temp.txt')
with open(f,'wb') as w:
with open(f, 'wb') as w:
if str is bytes:
w.write(test_string)
else:
w.write(test_string.encode('UTF-8'))
with open(f,'rb') as r:
with open(f, 'rb') as r:
t = EpsImagePlugin.PSFile(r)
self._test_readline(t, ending)
def test_readline(self):
# check all the freaking line endings possible from the spec
#test_string = u'something\r\nelse\n\rbaz\rbif\n'
# test_string = u'something\r\nelse\n\rbaz\rbif\n'
line_endings = ['\r\n', '\n']
not_working_endings = ['\n\r', '\r']
strings = ['something', 'else', 'baz', 'bif']
@ -205,9 +205,9 @@ class TestFileEps(PillowTestCase):
for ending in line_endings:
s = ending.join(strings)
# Native Python versions will pass these endings.
#self._test_readline_stringio(s, ending)
#self._test_readline_io(s, ending)
#self._test_readline_file_universal(s, ending)
# self._test_readline_stringio(s, ending)
# self._test_readline_io(s, ending)
# self._test_readline_file_universal(s, ending)
self._test_readline_file_psfile(s, ending)
@ -217,9 +217,9 @@ class TestFileEps(PillowTestCase):
s = ending.join(strings)
# Native Python versions may fail on these endings.
#self._test_readline_stringio(s, ending)
#self._test_readline_io(s, ending)
#self._test_readline_file_universal(s, ending)
# self._test_readline_stringio(s, ending)
# self._test_readline_io(s, ending)
# self._test_readline_file_universal(s, ending)
self._test_readline_file_psfile(s, ending)

View File

@ -25,23 +25,22 @@ class TestFileIco(PillowTestCase):
# the default image
output.seek(0)
reloaded = Image.open(output)
self.assertEqual(reloaded.info['sizes'],set([(32, 32), (64, 64)]))
self.assertEqual(reloaded.info['sizes'], set([(32, 32), (64, 64)]))
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual((64, 64), reloaded.size)
self.assertEqual(reloaded.format, "ICO")
self.assert_image_equal(reloaded, hopper().resize((64,64), Image.LANCZOS))
self.assert_image_equal(reloaded, hopper().resize((64, 64), Image.LANCZOS))
# the other one
output.seek(0)
reloaded = Image.open(output)
reloaded.size = (32,32)
reloaded.size = (32, 32)
self.assertEqual(im.mode, reloaded.mode)
self.assertEqual((32, 32), reloaded.size)
self.assertEqual(reloaded.format, "ICO")
self.assert_image_equal(reloaded, hopper().resize((32,32), Image.LANCZOS))
self.assert_image_equal(reloaded, hopper().resize((32, 32), Image.LANCZOS))
if __name__ == '__main__':

View File

@ -301,15 +301,12 @@ class TestFileJpeg(PillowTestCase):
# sequence wrong length
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[]))
# sequence wrong length
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1,2,3,4,5]))
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1, 2, 3, 4, 5]))
# qtable entry not a sequence
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[1]))
# qtable entry has wrong number of items
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[[1,2,3,4]]))
self.assertRaises(Exception, lambda: self.roundtrip(im, qtables=[[1, 2, 3, 4]]))
@unittest.skipUnless(djpeg_available(), "djpeg not available")
def test_load_djpeg(self):
@ -340,9 +337,9 @@ class TestFileJpeg(PillowTestCase):
""" Generates a very hard to compress file
:param size: tuple
"""
return Image.frombytes('RGB',size, os.urandom(size[0]*size[1] *3))
return Image.frombytes('RGB', size, os.urandom(size[0]*size[1] * 3))
im = gen_random_image((512,512))
im = gen_random_image((512, 512))
f = self.tempfile("temp.jpeg")
im.save(f, quality=100, optimize=True)

View File

@ -27,7 +27,7 @@ class LibTiffTestCase(PillowTestCase):
self.assertEqual(im._compression, 'group4')
except:
print("No _compression")
print (dir(im))
print(dir(im))
# can we write it back out, in a different form.
out = self.tempfile("temp.png")
@ -243,13 +243,13 @@ class TestFileLibTiff(LibTiffTestCase):
im2 = Image.open('Tests/images/12in16bit.tif')
if Image.DEBUG:
print (im.getpixel((0, 0)))
print (im.getpixel((0, 1)))
print (im.getpixel((0, 2)))
print(im.getpixel((0, 0)))
print(im.getpixel((0, 1)))
print(im.getpixel((0, 2)))
print (im2.getpixel((0, 0)))
print (im2.getpixel((0, 1)))
print (im2.getpixel((0, 2)))
print(im2.getpixel((0, 0)))
print(im2.getpixel((0, 1)))
print(im2.getpixel((0, 2)))
self.assert_image_equal(im, im2)

View File

@ -153,7 +153,7 @@ class TestFilePng(PillowTestCase):
im = load(HEAD + chunk(b'iTXt', b'spam\0\1\0en\0Spam\0' +
zlib.compress(b"egg")[:1]) + TAIL)
self.assertEqual(im.info, {'spam':''})
self.assertEqual(im.info, {'spam': ''})
im = load(HEAD + chunk(b'iTXt', b'spam\0\1\1en\0Spam\0' +
zlib.compress(b"egg")) + TAIL)
@ -382,7 +382,6 @@ class TestFilePng(PillowTestCase):
self.assert_image_equal(im, repr_png)
if __name__ == '__main__':
unittest.main()

View File

@ -121,13 +121,13 @@ class TestFileTiff(PillowTestCase):
im2 = Image.open('Tests/images/12in16bit.tif')
if Image.DEBUG:
print (im.getpixel((0, 0)))
print (im.getpixel((0, 1)))
print (im.getpixel((0, 2)))
print(im.getpixel((0, 0)))
print(im.getpixel((0, 1)))
print(im.getpixel((0, 2)))
print (im2.getpixel((0, 0)))
print (im2.getpixel((0, 1)))
print (im2.getpixel((0, 2)))
print(im2.getpixel((0, 0)))
print(im2.getpixel((0, 1)))
print(im2.getpixel((0, 2)))
self.assert_image_equal(im, im2)

View File

@ -15,7 +15,6 @@ class TestFileWebpMetadata(PillowTestCase):
if not _webp.HAVE_WEBPMUX:
self.skipTest('WebPMux support not installed')
def test_read_exif_metadata(self):
file_path = "Tests/images/flower.webp"

View File

@ -15,7 +15,7 @@ class TestFileXpm(PillowTestCase):
self.assertEqual(im.size, (128, 128))
self.assertEqual(im.format, "XPM")
#large error due to quantization->44 colors.
# large error due to quantization->44 colors.
self.assert_image_similar(im.convert('RGB'), hopper('RGB'), 60)
def test_load_read(self):

View File

@ -72,7 +72,7 @@ class TestImagePutData(PillowTestCase):
im = Image.new('L', (150, 100))
im.putdata(arr)
self.assertEqual(len(im.getdata()),len(arr))
self.assertEqual(len(im.getdata()), len(arr))
def test_array_F(self):
# shouldn't segfault
@ -82,7 +82,7 @@ class TestImagePutData(PillowTestCase):
arr = array('f', [0.0])*15000
im.putdata(arr)
self.assertEqual(len(im.getdata()),len(arr))
self.assertEqual(len(im.getdata()), len(arr))
if __name__ == '__main__':
unittest.main()

View File

@ -21,7 +21,7 @@ class TestImagingCoreResize(PillowTestCase):
im = hopper(mode)
r = self.resize(im, (15, 12), Image.NEAREST)
self.assertEqual(r.mode, mode)
self.assertEqual(r.size, (15, 12) )
self.assertEqual(r.size, (15, 12))
self.assertEqual(r.im.bands, im.im.bands)
def test_convolution_modes(self):
@ -35,7 +35,7 @@ class TestImagingCoreResize(PillowTestCase):
im = hopper(mode)
r = self.resize(im, (15, 12), Image.BILINEAR)
self.assertEqual(r.mode, mode)
self.assertEqual(r.size, (15, 12) )
self.assertEqual(r.size, (15, 12))
self.assertEqual(r.im.bands, im.im.bands)
def test_reduce_filters(self):

View File

@ -17,6 +17,7 @@ except ImportError as v:
SRGB = "Tests/icc/sRGB_IEC61966-2-1_black_scaled.icc"
HAVE_PROFILE = os.path.exists(SRGB)
class TestImageCms(PillowTestCase):
def setUp(self):

View File

@ -64,7 +64,7 @@ class TestImageDraw(PillowTestCase):
# Assert
self.assert_image_similar(
im, Image.open("Tests/images/imagedraw_arc.png"),1)
im, Image.open("Tests/images/imagedraw_arc.png"), 1)
def test_arc1(self):
self.helper_arc(BBOX1)
@ -97,7 +97,7 @@ class TestImageDraw(PillowTestCase):
# Assert
self.assert_image_similar(
im, Image.open("Tests/images/imagedraw_chord.png"),1)
im, Image.open("Tests/images/imagedraw_chord.png"), 1)
def test_chord1(self):
self.helper_chord(BBOX1)
@ -116,7 +116,7 @@ class TestImageDraw(PillowTestCase):
# Assert
self.assert_image_similar(
im, Image.open("Tests/images/imagedraw_ellipse.png"),1)
im, Image.open("Tests/images/imagedraw_ellipse.png"), 1)
def test_ellipse1(self):
self.helper_ellipse(BBOX1)
@ -154,7 +154,7 @@ class TestImageDraw(PillowTestCase):
# Assert
self.assert_image_similar(
im, Image.open("Tests/images/imagedraw_pieslice.png"),1)
im, Image.open("Tests/images/imagedraw_pieslice.png"), 1)
def test_pieslice1(self):
self.helper_pieslice(BBOX1)

View File

@ -21,19 +21,18 @@ class TestImageEnhance(PillowTestCase):
im = Image.new("RGB", (1, 1))
ImageEnhance.Sharpness(im).enhance(0.5)
def _half_transparent_image(self):
# returns an image, half transparent, half solid
im = hopper('RGB')
transparent = Image.new('L', im.size, 0)
solid = Image.new('L', (im.size[0]//2, im.size[1]), 255)
transparent.paste(solid, (0,0))
transparent.paste(solid, (0, 0))
im.putalpha(transparent)
return im
def _check_alpha(self,im, original, op, amount):
def _check_alpha(self, im, original, op, amount):
self.assertEqual(im.getbands(), original.getbands())
self.assert_image_equal(im.split()[-1], original.split()[-1],
"Diff on %s: %s" % (op, amount))
@ -45,8 +44,8 @@ class TestImageEnhance(PillowTestCase):
original = self._half_transparent_image()
for op in ['Color', 'Brightness', 'Contrast', 'Sharpness']:
for amount in [0,0.5,1.0]:
self._check_alpha(getattr(ImageEnhance,op)(original).enhance(amount),
for amount in [0, 0.5, 1.0]:
self._check_alpha(getattr(ImageEnhance, op)(original).enhance(amount),
original, op, amount)

View File

@ -1,6 +1,7 @@
from helper import unittest, PillowTestCase
from PIL import Image, ImageFont, ImageDraw
class TestImageFontBitmap(PillowTestCase):
def test_similar(self):
text = 'EmbeddedBitmap'

View File

@ -24,16 +24,16 @@ class TestImageSequence(PillowTestCase):
def _test_multipage_tiff(self, dbg=False):
# debug had side effect of calling fp.tell.
Image.DEBUG=dbg
Image.DEBUG = dbg
im = Image.open('Tests/images/multipage.tiff')
for index, frame in enumerate(ImageSequence.Iterator(im)):
frame.load()
self.assertEqual(index, im.tell())
frame.convert('RGB')
Image.DEBUG=False
Image.DEBUG = False
def test_tiff(self):
#self._test_multipage_tiff(True)
# self._test_multipage_tiff(True)
self._test_multipage_tiff(False)
def test_libtiff(self):
@ -43,7 +43,7 @@ class TestImageSequence(PillowTestCase):
self.skipTest("tiff support not available")
TiffImagePlugin.READ_LIBTIFF = True
#self._test_multipage_tiff(True)
# self._test_multipage_tiff(True)
self._test_multipage_tiff(False)
TiffImagePlugin.READ_LIBTIFF = False

View File

@ -129,7 +129,7 @@ class TestNumpy(PillowTestCase):
arr = numpy.zeros((15000,), numpy.float32)
im.putdata(arr)
self.assertEqual(len(im.getdata()),len(arr))
self.assertEqual(len(im.getdata()), len(arr))
if __name__ == '__main__':

View File

@ -21,25 +21,23 @@ class Test_scipy_resize(PillowTestCase):
self.skipTest("Scipy Required")
def test_imresize(self):
im = np.random.random((10,20))
im = np.random.random((10, 20))
for T in np.sctypes['float'] + [float]:
# 1.1 rounds to below 1.1 for float16, 1.101 works
im1 = misc.imresize(im,T(1.101))
self.assertEqual(im1.shape,(11,22))
im1 = misc.imresize(im, T(1.101))
self.assertEqual(im1.shape, (11, 22))
def test_imresize4(self):
im = np.array([[1,2],
[3,4]])
res = np.array([[ 1. , 1.25, 1.75, 2. ],
[ 1.5 , 1.75, 2.25, 2.5 ],
[ 2.5 , 2.75, 3.25, 3.5 ],
[ 3. , 3.25, 3.75, 4. ]], dtype=np.float32)
im = np.array([[1, 2],
[3, 4]])
res = np.array([[1. , 1.25, 1.75, 2. ],
[1.5, 1.75, 2.25, 2.5],
[2.5, 2.75, 3.25, 3.5],
[3. , 3.25, 3.75, 4. ]], dtype=np.float32)
# Check that resizing by target size, float and int are the same
im2 = misc.imresize(im, (4,4), mode='F') # output size
im2 = misc.imresize(im, (4, 4), mode='F') # output size
im3 = misc.imresize(im, 2., mode='F') # fraction
im4 = misc.imresize(im, 200, mode='F') # percentage
assert_equal(im2, res)
assert_equal(im3, res)
assert_equal(im4, res)