Skip to content

Commit

Permalink
Merge pull request #3909 from hugovk/run-black
Browse files Browse the repository at this point in the history
Format code with Black
  • Loading branch information
hugovk committed Jun 22, 2019
2 parents 9d5a50a + 488a017 commit f5cd8b4
Show file tree
Hide file tree
Showing 175 changed files with 5,186 additions and 4,532 deletions.
4 changes: 2 additions & 2 deletions Tests/32bit_segfault_check.py
Expand Up @@ -4,5 +4,5 @@
import sys


if sys.maxsize < 2**32:
im = Image.new('L', (999999, 999999), 0)
if sys.maxsize < 2 ** 32:
im = Image.new("L", (999999, 999999), 0)
27 changes: 15 additions & 12 deletions Tests/bench_cffi_access.py
Expand Up @@ -26,18 +26,21 @@ def timer(func, label, *args):
starttime = time.time()
for x in range(iterations):
func(*args)
if time.time()-starttime > 10:
print("%s: breaking at %s iterations, %.6f per iteration" % (
label, x+1, (time.time()-starttime)/(x+1.0)))
if time.time() - starttime > 10:
print(
"%s: breaking at %s iterations, %.6f per iteration"
% (label, x + 1, (time.time() - starttime) / (x + 1.0))
)
break
if x == iterations-1:
if x == iterations - 1:
endtime = time.time()
print("%s: %.4f s %.6f per iteration" % (
label, endtime-starttime, (endtime-starttime)/(x+1.0)))
print(
"%s: %.4f s %.6f per iteration"
% (label, endtime - starttime, (endtime - starttime) / (x + 1.0))
)


class BenchCffiAccess(PillowTestCase):

def test_direct(self):
im = hopper()
im.load()
Expand All @@ -48,11 +51,11 @@ def test_direct(self):
self.assertEqual(caccess[(0, 0)], access[(0, 0)])

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)
timer(iterate_set, 'C-api - set', im.size, caccess)
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)
timer(iterate_set, "C-api - set", im.size, caccess)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
1 change: 1 addition & 0 deletions Tests/bench_get.py
Expand Up @@ -2,6 +2,7 @@
import timeit

import sys

sys.path.insert(0, ".")


Expand Down
2 changes: 1 addition & 1 deletion Tests/check_fli_overflow.py
Expand Up @@ -12,5 +12,5 @@ def test_fli_overflow(self):
im.load()


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
5 changes: 2 additions & 3 deletions Tests/check_icns_dos.py
Expand Up @@ -6,7 +6,6 @@
from io import BytesIO

if py3:
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00',
'latin-1')))
Image.open(BytesIO(bytes("icns\x00\x00\x00\x10hang\x00\x00\x00\x00", "latin-1")))
else:
Image.open(BytesIO(bytes('icns\x00\x00\x00\x10hang\x00\x00\x00\x00')))
Image.open(BytesIO(bytes("icns\x00\x00\x00\x10hang\x00\x00\x00\x00")))
24 changes: 13 additions & 11 deletions Tests/check_imaging_leaks.py
Expand Up @@ -9,11 +9,11 @@
max_iterations = 10000


@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS")
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
class TestImagingLeaks(PillowTestCase):

def _get_mem_usage(self):
from resource import getpagesize, getrusage, RUSAGE_SELF

mem = getrusage(RUSAGE_SELF).ru_maxrss
return mem * getpagesize() / 1024 / 1024

Expand All @@ -25,20 +25,22 @@ def _test_leak(self, min_iterations, max_iterations, fn, *args, **kwargs):
if i < min_iterations:
mem_limit = mem + 1
continue
msg = 'memory usage limit exceeded after %d iterations' % (i + 1)
msg = "memory usage limit exceeded after %d iterations" % (i + 1)
self.assertLessEqual(mem, mem_limit, msg)

def test_leak_putdata(self):
im = Image.new('RGB', (25, 25))
self._test_leak(min_iterations, max_iterations,
im.putdata, im.getdata())
im = Image.new("RGB", (25, 25))
self._test_leak(min_iterations, max_iterations, im.putdata, im.getdata())

def test_leak_getlist(self):
im = Image.new('P', (25, 25))
self._test_leak(min_iterations, max_iterations,
# Pass a new list at each iteration.
lambda: im.point(range(256)))
im = Image.new("P", (25, 25))
self._test_leak(
min_iterations,
max_iterations,
# Pass a new list at each iteration.
lambda: im.point(range(256)),
)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
16 changes: 11 additions & 5 deletions Tests/check_j2k_dos.py
Expand Up @@ -6,10 +6,16 @@
from io import BytesIO

if py3:
Image.open(BytesIO(bytes(
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang',
'latin-1')))
Image.open(
BytesIO(
bytes(
"\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang",
"latin-1",
)
)
)

else:
Image.open(BytesIO(bytes(
'\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang')))
Image.open(
BytesIO(bytes("\x00\x00\x00\x0cjP\x20\x20\x0d\x0a\x87\x0a\x00\x00\x00\x00hang"))
)
14 changes: 8 additions & 6 deletions Tests/check_j2k_leaks.py
Expand Up @@ -4,21 +4,22 @@
from io import BytesIO

# Limits for testing the leak
mem_limit = 1024*1048576
stack_size = 8*1048576
iterations = int((mem_limit/stack_size)*2)
mem_limit = 1024 * 1048576
stack_size = 8 * 1048576
iterations = int((mem_limit / stack_size) * 2)
codecs = dir(Image.core)
test_file = "Tests/images/rgb_trns_ycbc.jp2"


@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS")
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
class TestJpegLeaks(PillowTestCase):
def setUp(self):
if "jpeg2k_encoder" not in codecs or "jpeg2k_decoder" not in codecs:
self.skipTest('JPEG 2000 support not available')
self.skipTest("JPEG 2000 support not available")

def test_leak_load(self):
from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK

setrlimit(RLIMIT_STACK, (stack_size, stack_size))
setrlimit(RLIMIT_AS, (mem_limit, mem_limit))
for _ in range(iterations):
Expand All @@ -27,6 +28,7 @@ def test_leak_load(self):

def test_leak_save(self):
from resource import setrlimit, RLIMIT_AS, RLIMIT_STACK

setrlimit(RLIMIT_STACK, (stack_size, stack_size))
setrlimit(RLIMIT_AS, (mem_limit, mem_limit))
for _ in range(iterations):
Expand All @@ -38,5 +40,5 @@ def test_leak_save(self):
test_output.read()


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
6 changes: 3 additions & 3 deletions Tests/check_j2k_overflow.py
Expand Up @@ -5,11 +5,11 @@
class TestJ2kEncodeOverflow(PillowTestCase):
def test_j2k_overflow(self):

im = Image.new('RGBA', (1024, 131584))
target = self.tempfile('temp.jpc')
im = Image.new("RGBA", (1024, 131584))
target = self.tempfile("temp.jpc")
with self.assertRaises(IOError):
im.save(target)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
35 changes: 22 additions & 13 deletions Tests/check_jpeg_leaks.py
Expand Up @@ -14,7 +14,7 @@
"""


@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or macOS")
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
class TestJpegLeaks(PillowTestCase):

"""
Expand Down Expand Up @@ -74,9 +74,11 @@ class TestJpegLeaks(PillowTestCase):
"""

def test_qtables_leak(self):
im = hopper('RGB')
im = hopper("RGB")

standard_l_qtable = [int(s) for s in """
standard_l_qtable = [
int(s)
for s in """
16 11 10 16 24 40 51 61
12 12 14 19 26 58 60 55
14 13 16 24 40 57 69 56
Expand All @@ -85,9 +87,14 @@ def test_qtables_leak(self):
24 35 55 64 81 104 113 92
49 64 78 87 103 121 120 101
72 92 95 98 112 100 103 99
""".split(None)]

standard_chrominance_qtable = [int(s) for s in """
""".split(
None
)
]

standard_chrominance_qtable = [
int(s)
for s in """
17 18 24 47 99 99 99 99
18 21 26 66 99 99 99 99
24 26 56 99 99 99 99 99
Expand All @@ -96,10 +103,12 @@ def test_qtables_leak(self):
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
99 99 99 99 99 99 99 99
""".split(None)]
""".split(
None
)
]

qtables = [standard_l_qtable,
standard_chrominance_qtable]
qtables = [standard_l_qtable, standard_chrominance_qtable]

for _ in range(iterations):
test_output = BytesIO()
Expand Down Expand Up @@ -161,8 +170,8 @@ def test_exif_leak(self):
0 11.33
"""
im = hopper('RGB')
exif = b'12345678'*4096
im = hopper("RGB")
exif = b"12345678" * 4096

for _ in range(iterations):
test_output = BytesIO()
Expand Down Expand Up @@ -195,12 +204,12 @@ def test_base_save(self):
0 +----------------------------------------------------------------------->Gi
0 7.882
"""
im = hopper('RGB')
im = hopper("RGB")

for _ in range(iterations):
test_output = BytesIO()
im.save(test_output, "JPEG")


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
9 changes: 4 additions & 5 deletions Tests/check_large_memory.py
Expand Up @@ -22,12 +22,11 @@
XDIM = 48000


@unittest.skipIf(sys.maxsize <= 2**32, "requires 64-bit system")
@unittest.skipIf(sys.maxsize <= 2 ** 32, "requires 64-bit system")
class LargeMemoryTest(PillowTestCase):

def _write_png(self, xdim, ydim):
f = self.tempfile('temp.png')
im = Image.new('L', (xdim, ydim), 0)
f = self.tempfile("temp.png")
im = Image.new("L", (xdim, ydim), 0)
im.save(f)

def test_large(self):
Expand All @@ -44,5 +43,5 @@ def test_size_greater_than_int(self):
Image.fromarray(arr)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
10 changes: 5 additions & 5 deletions Tests/check_large_memory_numpy.py
Expand Up @@ -11,6 +11,7 @@
# Raspberry Pis).

from PIL import Image

try:
import numpy as np
except ImportError:
Expand All @@ -20,14 +21,13 @@
XDIM = 48000


@unittest.skipIf(sys.maxsize <= 2**32, "requires 64-bit system")
@unittest.skipIf(sys.maxsize <= 2 ** 32, "requires 64-bit system")
class LargeMemoryNumpyTest(PillowTestCase):

def _write_png(self, xdim, ydim):
dtype = np.uint8
a = np.zeros((xdim, ydim), dtype=dtype)
f = self.tempfile('temp.png')
im = Image.fromarray(a, 'L')
f = self.tempfile("temp.png")
im = Image.fromarray(a, "L")
im.save(f)

def test_large(self):
Expand All @@ -39,5 +39,5 @@ def test_2gpx(self):
self._write_png(XDIM, XDIM)


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion Tests/check_libtiff_segfault.py
Expand Up @@ -15,5 +15,5 @@ def test_segfault(self):
im.load()


if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit f5cd8b4

Please sign in to comment.