From bc658e17917ff8959e4db752c8d12082a5cbd8d1 Mon Sep 17 00:00:00 2001 From: TheNooB Date: Fri, 11 Aug 2023 17:47:36 +0800 Subject: [PATCH 1/7] Add session type check in grabclipboard for Linux --- src/PIL/ImageGrab.py | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 927033c60..c23b40295 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -140,7 +140,24 @@ def grabclipboard(): return BmpImagePlugin.DibImageFile(data) return None else: - if shutil.which("wl-paste"): + if shutil.which("loginctl"): + try: + loginctl = subprocess.check_output("loginctl").decode().split("\n") + except subprocess.CalledProcessError: + loginctl = None + else: + loginctl = None + + if loginctl is not None: + username = subprocess.check_output("whoami").decode().strip("\n") + sessionid = [line.split()[0] for line in loginctl if username in line.split()][0] + sessiontype = subprocess.check_output( + ["loginctl", "show-session", sessionid, "-p", "Type"] + ).decode().strip("\n").split("=")[1] + else: # Session type check failed + sessiontype = None + + if shutil.which("wl-paste") and ((sessiontype == "wayland") or (sessiontype is None)): output = subprocess.check_output(["wl-paste", "-l"]).decode() mimetypes = output.splitlines() if "image/png" in mimetypes: @@ -153,11 +170,12 @@ def grabclipboard(): args = ["wl-paste"] if mimetype: args.extend(["-t", mimetype]) - elif shutil.which("xclip"): + elif shutil.which("xclip") and ((sessiontype == "x11") or (sessiontype is None)): args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] else: msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" raise NotImplementedError(msg) + p = subprocess.run(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) err = p.stderr if err: From 164ea2df6f60c6071088c683b48f1a0c686a30a3 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:42:09 +0000 Subject: [PATCH 2/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- src/PIL/ImageGrab.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index c23b40295..44bc6d38f 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -146,18 +146,27 @@ def grabclipboard(): except subprocess.CalledProcessError: loginctl = None else: - loginctl = None + loginctl = None if loginctl is not None: username = subprocess.check_output("whoami").decode().strip("\n") - sessionid = [line.split()[0] for line in loginctl if username in line.split()][0] - sessiontype = subprocess.check_output( - ["loginctl", "show-session", sessionid, "-p", "Type"] - ).decode().strip("\n").split("=")[1] - else: # Session type check failed + sessionid = [ + line.split()[0] for line in loginctl if username in line.split() + ][0] + sessiontype = ( + subprocess.check_output( + ["loginctl", "show-session", sessionid, "-p", "Type"] + ) + .decode() + .strip("\n") + .split("=")[1] + ) + else: # Session type check failed sessiontype = None - if shutil.which("wl-paste") and ((sessiontype == "wayland") or (sessiontype is None)): + if shutil.which("wl-paste") and ( + (sessiontype == "wayland") or (sessiontype is None) + ): output = subprocess.check_output(["wl-paste", "-l"]).decode() mimetypes = output.splitlines() if "image/png" in mimetypes: @@ -170,7 +179,9 @@ def grabclipboard(): args = ["wl-paste"] if mimetype: args.extend(["-t", mimetype]) - elif shutil.which("xclip") and ((sessiontype == "x11") or (sessiontype is None)): + elif shutil.which("xclip") and ( + (sessiontype == "x11") or (sessiontype is None) + ): args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] else: msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" From a8b3feac86c0800968a52db1ff48477076f106c4 Mon Sep 17 00:00:00 2001 From: TheNooB <73348767+TheNooB2706@users.noreply.github.com> Date: Fri, 11 Aug 2023 21:01:05 +0800 Subject: [PATCH 3/7] Apply suggestions from code review Simplify conditional expressions Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/ImageGrab.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 44bc6d38f..c510c835f 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -164,9 +164,7 @@ def grabclipboard(): else: # Session type check failed sessiontype = None - if shutil.which("wl-paste") and ( - (sessiontype == "wayland") or (sessiontype is None) - ): + if shutil.which("wl-paste") and sessiontype in ["wayland", None]: output = subprocess.check_output(["wl-paste", "-l"]).decode() mimetypes = output.splitlines() if "image/png" in mimetypes: @@ -179,9 +177,7 @@ def grabclipboard(): args = ["wl-paste"] if mimetype: args.extend(["-t", mimetype]) - elif shutil.which("xclip") and ( - (sessiontype == "x11") or (sessiontype is None) - ): + elif shutil.which("xclip") and sessiontype in ["x11", None]: args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] else: msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" From 7b157b045aa3f7b5ca61d94b25ed03479759325e Mon Sep 17 00:00:00 2001 From: TheNooB Date: Fri, 11 Aug 2023 21:14:34 +0800 Subject: [PATCH 4/7] Use os.getlogin() instead of whoami command for getting username --- src/PIL/ImageGrab.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index c510c835f..07540a1db 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -149,7 +149,7 @@ def grabclipboard(): loginctl = None if loginctl is not None: - username = subprocess.check_output("whoami").decode().strip("\n") + username = os.getlogin() sessionid = [ line.split()[0] for line in loginctl if username in line.split() ][0] From 0b6ab7914562b764dad45c620a8c36e9cabfe070 Mon Sep 17 00:00:00 2001 From: TheNooB Date: Sat, 12 Aug 2023 12:51:09 +0800 Subject: [PATCH 5/7] Check session type using environment variable instead of loginctl --- src/PIL/ImageGrab.py | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 07540a1db..07b617cc3 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -140,27 +140,10 @@ def grabclipboard(): return BmpImagePlugin.DibImageFile(data) return None else: - if shutil.which("loginctl"): - try: - loginctl = subprocess.check_output("loginctl").decode().split("\n") - except subprocess.CalledProcessError: - loginctl = None - else: - loginctl = None - - if loginctl is not None: - username = os.getlogin() - sessionid = [ - line.split()[0] for line in loginctl if username in line.split() - ][0] - sessiontype = ( - subprocess.check_output( - ["loginctl", "show-session", sessionid, "-p", "Type"] - ) - .decode() - .strip("\n") - .split("=")[1] - ) + if os.getenv("WAYLAND_DISPLAY"): + sessiontype = "wayland" + elif os.getenv("DISPLAY"): + sessiontype = "x11" else: # Session type check failed sessiontype = None From e06edcb527a94c7095d3ea008a6f77ef793f7adc Mon Sep 17 00:00:00 2001 From: TheNooB <73348767+TheNooB2706@users.noreply.github.com> Date: Sat, 12 Aug 2023 18:33:36 +0800 Subject: [PATCH 6/7] Reformat variable name following PEP8 Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com> --- src/PIL/ImageGrab.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 07b617cc3..6dbdd0abc 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -141,13 +141,13 @@ def grabclipboard(): return None else: if os.getenv("WAYLAND_DISPLAY"): - sessiontype = "wayland" + session_type = "wayland" elif os.getenv("DISPLAY"): - sessiontype = "x11" + session_type = "x11" else: # Session type check failed - sessiontype = None + session_type = None - if shutil.which("wl-paste") and sessiontype in ["wayland", None]: + if shutil.which("wl-paste") and session_type in ["wayland", None]: output = subprocess.check_output(["wl-paste", "-l"]).decode() mimetypes = output.splitlines() if "image/png" in mimetypes: @@ -160,7 +160,7 @@ def grabclipboard(): args = ["wl-paste"] if mimetype: args.extend(["-t", mimetype]) - elif shutil.which("xclip") and sessiontype in ["x11", None]: + elif shutil.which("xclip") and session_type in ["x11", None]: args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] else: msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux" From 9c3bc70f667e105c8bce5f34d8895ac5fcfbd02f Mon Sep 17 00:00:00 2001 From: Andrew Murray <3112309+radarhere@users.noreply.github.com> Date: Tue, 22 Aug 2023 10:31:28 +1000 Subject: [PATCH 7/7] Use tuples Co-authored-by: Hugo van Kemenade --- src/PIL/ImageGrab.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/PIL/ImageGrab.py b/src/PIL/ImageGrab.py index 6dbdd0abc..43019f74a 100644 --- a/src/PIL/ImageGrab.py +++ b/src/PIL/ImageGrab.py @@ -147,7 +147,7 @@ def grabclipboard(): else: # Session type check failed session_type = None - if shutil.which("wl-paste") and session_type in ["wayland", None]: + if shutil.which("wl-paste") and session_type in ("wayland", None): output = subprocess.check_output(["wl-paste", "-l"]).decode() mimetypes = output.splitlines() if "image/png" in mimetypes: @@ -160,7 +160,7 @@ def grabclipboard(): args = ["wl-paste"] if mimetype: args.extend(["-t", mimetype]) - elif shutil.which("xclip") and session_type in ["x11", None]: + elif shutil.which("xclip") and session_type in ("x11", None): args = ["xclip", "-selection", "clipboard", "-t", "image/png", "-o"] else: msg = "wl-paste or xclip is required for ImageGrab.grabclipboard() on Linux"