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

Do not automatically remove temporary ImageShow files on Unix #6045

Merged
merged 6 commits into from
Feb 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions Tests/test_imageshow.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ def test_show():
assert ImageShow.show(im)


def test_show_without_viewers():
viewers = ImageShow._viewers
ImageShow._viewers = []

im = hopper()
assert not ImageShow.show(im)

ImageShow._viewers = viewers


def test_viewer():
viewer = ImageShow.Viewer()

Expand Down
7 changes: 7 additions & 0 deletions docs/releasenotes/9.1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ breaking backwards compatibility.
Other Changes
=============

ImageShow temporary files on Unix
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

When calling :py:meth:`~PIL.Image.Image.show` or using :py:mod:`~PIL.ImageShow`,
a temporary file is created from the image. On Unix, Pillow will no longer delete these
files, and instead leave it to the operating system to do so.

Image._repr_pretty_
^^^^^^^^^^^^^^^^^^^

Expand Down
34 changes: 13 additions & 21 deletions src/PIL/ImageShow.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ def show(image, title=None, **options):
"""
for viewer in _viewers:
if viewer.show(image, title=title, **options):
return 1
return 0
return True
return False


class Viewer:
Expand Down Expand Up @@ -126,16 +126,6 @@ def show_file(self, path=None, **options):
os.system(self.get_command(path, **options))
return 1

def _remove_path_after_delay(self, path):
subprocess.Popen(
[
sys.executable,
"-c",
"import os, sys, time; time.sleep(20); os.remove(sys.argv[1])",
path,
]
)


# --------------------------------------------------------------------

Expand Down Expand Up @@ -190,7 +180,14 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.call(["open", "-a", "Preview.app", path])
self._remove_path_after_delay(path)
subprocess.Popen(
[
sys.executable,
"-c",
"import os, sys, time; time.sleep(20); os.remove(sys.argv[1])",
path,
]
)
return 1


Expand All @@ -204,7 +201,7 @@ class UnixViewer(Viewer):

def get_command(self, file, **options):
command = self.get_command_ex(file, **options)[0]
return f"({command} {quote(file)}; rm -f {quote(file)})&"
return f"({command} {quote(file)}"


class XDGViewer(UnixViewer):
Expand Down Expand Up @@ -235,7 +232,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["xdg-open", path])
self._remove_path_after_delay(path)
return 1


Expand All @@ -248,7 +244,7 @@ class DisplayViewer(UnixViewer):
def get_command_ex(self, file, title=None, **options):
command = executable = "display"
if title:
command += f" -name {quote(title)}"
command += f" -title {quote(title)}"
return command, executable

def show_file(self, path=None, **options):
Expand All @@ -270,11 +266,10 @@ def show_file(self, path=None, **options):
raise TypeError("Missing required argument: 'path'")
args = ["display"]
if "title" in options:
args += ["-name", options["title"]]
args += ["-title", options["title"]]
args.append(path)

subprocess.Popen(args)
os.remove(path)
return 1


Expand Down Expand Up @@ -304,7 +299,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["gm", "display", path])
os.remove(path)
return 1


Expand Down Expand Up @@ -334,7 +328,6 @@ def show_file(self, path=None, **options):
else:
raise TypeError("Missing required argument: 'path'")
subprocess.Popen(["eog", "-n", path])
os.remove(path)
return 1


Expand Down Expand Up @@ -375,7 +368,6 @@ def show_file(self, path=None, **options):
args.append(path)

subprocess.Popen(args)
os.remove(path)
return 1


Expand Down