Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close OleFileIO instance when closing or exiting FPX or MIC #7005

Merged
merged 2 commits into from
Mar 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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