mirror of
https://github.com/python-pillow/Pillow.git
synced 2025-02-09 16:10:48 +03:00
In show_file, use os.remove to remove temporary images
This commit is contained in:
parent
eccd853d14
commit
8da80130db
|
@ -85,11 +85,13 @@ def test_ipythonviewer():
|
||||||
not on_ci() or is_win32(),
|
not on_ci() or is_win32(),
|
||||||
reason="Only run on CIs; hangs on Windows CIs",
|
reason="Only run on CIs; hangs on Windows CIs",
|
||||||
)
|
)
|
||||||
def test_file_deprecated():
|
def test_file_deprecated(tmp_path):
|
||||||
|
f = str(tmp_path / "temp.jpg")
|
||||||
for viewer in ImageShow._viewers:
|
for viewer in ImageShow._viewers:
|
||||||
|
hopper().save(f)
|
||||||
with pytest.warns(DeprecationWarning):
|
with pytest.warns(DeprecationWarning):
|
||||||
try:
|
try:
|
||||||
viewer.show_file(file="test.jpg")
|
viewer.show_file(file=f)
|
||||||
except NotImplementedError:
|
except NotImplementedError:
|
||||||
pass
|
pass
|
||||||
with pytest.raises(TypeError):
|
with pytest.raises(TypeError):
|
||||||
|
|
|
@ -15,7 +15,6 @@ import os
|
||||||
import shutil
|
import shutil
|
||||||
import subprocess
|
import subprocess
|
||||||
import sys
|
import sys
|
||||||
import tempfile
|
|
||||||
import warnings
|
import warnings
|
||||||
from shlex import quote
|
from shlex import quote
|
||||||
|
|
||||||
|
@ -180,16 +179,15 @@ class MacViewer(Viewer):
|
||||||
path = options.pop("file")
|
path = options.pop("file")
|
||||||
else:
|
else:
|
||||||
raise TypeError("Missing required argument: 'path'")
|
raise TypeError("Missing required argument: 'path'")
|
||||||
fd, temp_path = tempfile.mkstemp()
|
subprocess.call(["open", "-a", "Preview.app", path])
|
||||||
with os.fdopen(fd, "w") as f:
|
subprocess.Popen(
|
||||||
f.write(path)
|
[
|
||||||
with open(temp_path) as f:
|
sys.executable,
|
||||||
subprocess.Popen(
|
"-c",
|
||||||
["im=$(cat); open -a Preview.app $im; sleep 20; rm -f $im"],
|
"import os, sys, time;time.sleep(20);os.remove(sys.argv[1])",
|
||||||
shell=True,
|
path,
|
||||||
stdin=f,
|
]
|
||||||
)
|
)
|
||||||
os.remove(temp_path)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
|
@ -205,6 +203,16 @@ class UnixViewer(Viewer):
|
||||||
command = self.get_command_ex(file, **options)[0]
|
command = self.get_command_ex(file, **options)[0]
|
||||||
return f"({command} {quote(file)}; rm -f {quote(file)})&"
|
return f"({command} {quote(file)}; rm -f {quote(file)})&"
|
||||||
|
|
||||||
|
|
||||||
|
class XDGViewer(UnixViewer):
|
||||||
|
"""
|
||||||
|
The freedesktop.org ``xdg-open`` command.
|
||||||
|
"""
|
||||||
|
|
||||||
|
def get_command_ex(self, file, **options):
|
||||||
|
command = executable = "xdg-open"
|
||||||
|
return command, executable
|
||||||
|
|
||||||
def show_file(self, path=None, **options):
|
def show_file(self, path=None, **options):
|
||||||
"""
|
"""
|
||||||
Display given file.
|
Display given file.
|
||||||
|
@ -223,28 +231,11 @@ class UnixViewer(Viewer):
|
||||||
path = options.pop("file")
|
path = options.pop("file")
|
||||||
else:
|
else:
|
||||||
raise TypeError("Missing required argument: 'path'")
|
raise TypeError("Missing required argument: 'path'")
|
||||||
fd, temp_path = tempfile.mkstemp()
|
subprocess.Popen(["xdg-open", path])
|
||||||
with os.fdopen(fd, "w") as f:
|
os.remove(path)
|
||||||
f.write(path)
|
|
||||||
with open(temp_path) as f:
|
|
||||||
command = self.get_command_ex(path, **options)[0]
|
|
||||||
subprocess.Popen(
|
|
||||||
["im=$(cat);" + command + " $im; rm -f $im"], shell=True, stdin=f
|
|
||||||
)
|
|
||||||
os.remove(temp_path)
|
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class XDGViewer(UnixViewer):
|
|
||||||
"""
|
|
||||||
The freedesktop.org ``xdg-open`` command.
|
|
||||||
"""
|
|
||||||
|
|
||||||
def get_command_ex(self, file, **options):
|
|
||||||
command = executable = "xdg-open"
|
|
||||||
return command, executable
|
|
||||||
|
|
||||||
|
|
||||||
class DisplayViewer(UnixViewer):
|
class DisplayViewer(UnixViewer):
|
||||||
"""
|
"""
|
||||||
The ImageMagick ``display`` command.
|
The ImageMagick ``display`` command.
|
||||||
|
@ -257,6 +248,32 @@ class DisplayViewer(UnixViewer):
|
||||||
command += f" -name {quote(title)}"
|
command += f" -name {quote(title)}"
|
||||||
return command, executable
|
return command, executable
|
||||||
|
|
||||||
|
def show_file(self, path=None, **options):
|
||||||
|
"""
|
||||||
|
Display given file.
|
||||||
|
|
||||||
|
Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
|
||||||
|
and ``path`` should be used instead.
|
||||||
|
"""
|
||||||
|
if path is None:
|
||||||
|
if "file" in options:
|
||||||
|
warnings.warn(
|
||||||
|
"The 'file' argument is deprecated and will be removed in Pillow "
|
||||||
|
"10 (2023-07-01). Use 'path' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
path = options.pop("file")
|
||||||
|
else:
|
||||||
|
raise TypeError("Missing required argument: 'path'")
|
||||||
|
args = ["display"]
|
||||||
|
if "title" in options:
|
||||||
|
args += ["-name", options["title"]]
|
||||||
|
args.append(path)
|
||||||
|
|
||||||
|
subprocess.Popen(args)
|
||||||
|
os.remove(path)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class GmDisplayViewer(UnixViewer):
|
class GmDisplayViewer(UnixViewer):
|
||||||
"""The GraphicsMagick ``gm display`` command."""
|
"""The GraphicsMagick ``gm display`` command."""
|
||||||
|
@ -266,6 +283,27 @@ class GmDisplayViewer(UnixViewer):
|
||||||
command = "gm display"
|
command = "gm display"
|
||||||
return command, executable
|
return command, executable
|
||||||
|
|
||||||
|
def show_file(self, path=None, **options):
|
||||||
|
"""
|
||||||
|
Display given file.
|
||||||
|
|
||||||
|
Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
|
||||||
|
and ``path`` should be used instead.
|
||||||
|
"""
|
||||||
|
if path is None:
|
||||||
|
if "file" in options:
|
||||||
|
warnings.warn(
|
||||||
|
"The 'file' argument is deprecated and will be removed in Pillow "
|
||||||
|
"10 (2023-07-01). Use 'path' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
path = options.pop("file")
|
||||||
|
else:
|
||||||
|
raise TypeError("Missing required argument: 'path'")
|
||||||
|
subprocess.Popen(["gm", "display", path])
|
||||||
|
os.remove(path)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class EogViewer(UnixViewer):
|
class EogViewer(UnixViewer):
|
||||||
"""The GNOME Image Viewer ``eog`` command."""
|
"""The GNOME Image Viewer ``eog`` command."""
|
||||||
|
@ -275,6 +313,27 @@ class EogViewer(UnixViewer):
|
||||||
command = "eog -n"
|
command = "eog -n"
|
||||||
return command, executable
|
return command, executable
|
||||||
|
|
||||||
|
def show_file(self, path=None, **options):
|
||||||
|
"""
|
||||||
|
Display given file.
|
||||||
|
|
||||||
|
Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
|
||||||
|
and ``path`` should be used instead.
|
||||||
|
"""
|
||||||
|
if path is None:
|
||||||
|
if "file" in options:
|
||||||
|
warnings.warn(
|
||||||
|
"The 'file' argument is deprecated and will be removed in Pillow "
|
||||||
|
"10 (2023-07-01). Use 'path' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
path = options.pop("file")
|
||||||
|
else:
|
||||||
|
raise TypeError("Missing required argument: 'path'")
|
||||||
|
subprocess.Popen(["eog", "-n", path])
|
||||||
|
os.remove(path)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
class XVViewer(UnixViewer):
|
class XVViewer(UnixViewer):
|
||||||
"""
|
"""
|
||||||
|
@ -290,6 +349,32 @@ class XVViewer(UnixViewer):
|
||||||
command += f" -name {quote(title)}"
|
command += f" -name {quote(title)}"
|
||||||
return command, executable
|
return command, executable
|
||||||
|
|
||||||
|
def show_file(self, path=None, **options):
|
||||||
|
"""
|
||||||
|
Display given file.
|
||||||
|
|
||||||
|
Before Pillow 9.1.0, the first argument was ``file``. This is now deprecated,
|
||||||
|
and ``path`` should be used instead.
|
||||||
|
"""
|
||||||
|
if path is None:
|
||||||
|
if "file" in options:
|
||||||
|
warnings.warn(
|
||||||
|
"The 'file' argument is deprecated and will be removed in Pillow "
|
||||||
|
"10 (2023-07-01). Use 'path' instead.",
|
||||||
|
DeprecationWarning,
|
||||||
|
)
|
||||||
|
path = options.pop("file")
|
||||||
|
else:
|
||||||
|
raise TypeError("Missing required argument: 'path'")
|
||||||
|
args = ["xv"]
|
||||||
|
if "title" in options:
|
||||||
|
args += ["-name", options["title"]]
|
||||||
|
args.append(path)
|
||||||
|
|
||||||
|
subprocess.Popen(args)
|
||||||
|
os.remove(path)
|
||||||
|
return 1
|
||||||
|
|
||||||
|
|
||||||
if sys.platform not in ("win32", "darwin"): # unixoids
|
if sys.platform not in ("win32", "darwin"): # unixoids
|
||||||
if shutil.which("xdg-open"):
|
if shutil.which("xdg-open"):
|
||||||
|
|
Loading…
Reference in New Issue
Block a user