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 use temporary file when grabbing clipboard on Linux #7200

Merged
merged 1 commit into from
Jun 6, 2023
Merged
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
12 changes: 5 additions & 7 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# See the README file for information on usage and redistribution.
#

import io
import os
import shutil
import subprocess
Expand Down Expand Up @@ -128,8 +129,6 @@
files = data[o:].decode("mbcs").split("\0")
return files[: files.index("")]
if isinstance(data, bytes):
import io

data = io.BytesIO(data)
if fmt == "png":
from . import PngImagePlugin
Expand Down Expand Up @@ -159,13 +158,12 @@
else:
msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"
raise NotImplementedError(msg)
fh, filepath = tempfile.mkstemp()
err = subprocess.run(args, stdout=fh, stderr=subprocess.PIPE).stderr
os.close(fh)
p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
err = p.stderr

Check warning on line 162 in src/PIL/ImageGrab.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L161-L162

Added lines #L161 - L162 were not covered by tests
if err:
msg = f"{args[0]} error: {err.strip().decode()}"
raise ChildProcessError(msg)
im = Image.open(filepath)
data = io.BytesIO(p.stdout)
im = Image.open(data)

Check warning on line 167 in src/PIL/ImageGrab.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/ImageGrab.py#L166-L167

Added lines #L166 - L167 were not covered by tests
im.load()
os.unlink(filepath)
return im