Skip to content

Commit

Permalink
Merge pull request #7005 from radarhere/unclosed_file
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk committed Mar 12, 2023
2 parents 258cb8d + f9cbc2e commit be244b5
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 2 deletions.
1 change: 1 addition & 0 deletions Tests/test_file_bufrstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def open(self, im):

def load(self, im):
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
Expand Down
1 change: 1 addition & 0 deletions Tests/test_file_fits.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ def open(self, im):

def load(self, im):
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

handler = Handler()
Expand Down
10 changes: 10 additions & 0 deletions Tests/test_file_fpx.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ def test_sanity():
assert_image_equal_tofile(im, "Tests/images/input_bw_one_band.png")


def test_close():
with Image.open("Tests/images/input_bw_one_band.fpx") as im:
pass
assert im.ole.fp.closed

im = Image.open("Tests/images/input_bw_one_band.fpx")
im.close()
assert im.ole.fp.closed


def test_invalid_file():
# Test an invalid OLE file
invalid_file = "Tests/images/flower.jpg"
Expand Down
1 change: 1 addition & 0 deletions Tests/test_file_gribstub.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ def open(self, im):

def load(self, im):
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
Expand Down
1 change: 1 addition & 0 deletions Tests/test_file_hdf5stub.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def open(self, im):

def load(self, im):
self.loaded = True
im.fp.close()
return Image.new("RGB", (1, 1))

def save(self, im, fp, filename):
Expand Down
10 changes: 10 additions & 0 deletions Tests/test_file_mic.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_seek():
assert im.tell() == 0


def test_close():
with Image.open(TEST_FILE) as im:
pass
assert im.ole.fp.closed

im = Image.open(TEST_FILE)
im.close()
assert im.ole.fp.closed


def test_invalid_file():
# Test an invalid OLE file
invalid_file = "Tests/images/flower.jpg"
Expand Down
4 changes: 2 additions & 2 deletions Tests/test_imageshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_show_without_viewers():
viewers = ImageShow._viewers
ImageShow._viewers = []

im = hopper()
assert not ImageShow.show(im)
with hopper() as im:
assert not ImageShow.show(im)

ImageShow._viewers = viewers

Expand Down
8 changes: 8 additions & 0 deletions src/PIL/FpxImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ def load(self):

return ImageFile.ImageFile.load(self)

def close(self):
self.ole.close()
super().close()

def __exit__(self, *args):
self.ole.close()
super().__exit__()


#
# --------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions src/PIL/MicImagePlugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ def seek(self, frame):
def tell(self):
return self.frame

def close(self):
self.ole.close()
super().close()

def __exit__(self, *args):
self.ole.close()
super().__exit__()


#
# --------------------------------------------------------------------
Expand Down

0 comments on commit be244b5

Please sign in to comment.