From 5e63097f3473677e05fa25eb1a11e765b41b572f Mon Sep 17 00:00:00 2001 From: Andrew Murray Date: Thu, 11 Feb 2021 22:40:46 +1100 Subject: [PATCH] Added context managers --- Tests/test_file_ppm.py | 44 ++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/Tests/test_file_ppm.py b/Tests/test_file_ppm.py index 7b73580156a..d6bc451b5f4 100644 --- a/Tests/test_file_ppm.py +++ b/Tests/test_file_ppm.py @@ -114,8 +114,9 @@ def test_plain_pbm_truncated_data(tmp_path): with open(path, "wb") as f: f.write(b"P1\n128 128\n") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_pbm_invalid_data(tmp_path): @@ -123,8 +124,9 @@ def test_plain_pbm_invalid_data(tmp_path): with open(path, "wb") as f: f.write(b"P1\n128 128\n1009") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_ppm_data_with_comments(tmp_path): @@ -146,8 +148,9 @@ def test_plain_ppm_truncated_data(tmp_path): with open(path, "wb") as f: f.write(b"P3\n128 128\n255\n") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_ppm_invalid_data(tmp_path): @@ -155,8 +158,9 @@ def test_plain_ppm_invalid_data(tmp_path): with open(path, "wb") as f: f.write(b"P3\n128 128\n255\n100A") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_ppm_half_token_too_long(tmp_path): @@ -164,8 +168,9 @@ def test_plain_ppm_half_token_too_long(tmp_path): with open(path, "wb") as f: f.write(b"P3\n128 128\n255\n012345678910") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_ppm_token_too_long(tmp_path): @@ -173,8 +178,9 @@ def test_plain_ppm_token_too_long(tmp_path): with open(path, "wb") as f: f.write(b"P3\n128 128\n255\n012345678910 0") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_plain_ppm_value_too_large(tmp_path): @@ -182,8 +188,9 @@ def test_plain_ppm_value_too_large(tmp_path): with open(path, "wb") as f: f.write(b"P3\n128 128\n255\n256") - with pytest.raises(ValueError): - Image.open(path).load() + with Image.open(path) as im: + with pytest.raises(ValueError): + im.load() def test_not_ppm(tmp_path): @@ -206,7 +213,8 @@ def test_nondecimal_header(tmp_path): f.write(b"P6\n128\x00") with pytest.raises(ValueError): - Image.open(path) + with Image.open(path): + pass def test_header_token_too_long(tmp_path): @@ -215,7 +223,8 @@ def test_header_token_too_long(tmp_path): f.write(b"P6\n 012345678910") with pytest.raises(ValueError): - Image.open(path) + with Image.open(path): + pass def test_too_many_colors(tmp_path): @@ -224,7 +233,8 @@ def test_too_many_colors(tmp_path): f.write(b"P6\n1 1\n1000\n") with pytest.raises(ValueError): - Image.open(path) + with Image.open(path): + pass def test_truncated_header(tmp_path):