Skip to content

Commit

Permalink
Added broader exception handling when enabling clipboard functionalit…
Browse files Browse the repository at this point in the history
…y via pyperclip.
  • Loading branch information
kmvanbrunt committed Dec 15, 2021
1 parent bef3c75 commit f5af7d3
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.4 (TBD, 2021)
* Enhancements
* Added broader exception handling when enabling clipboard functionality via `pyperclip`.

## 2.3.3 (November 29, 2021)
* Enhancements
* Added clearer exception handling to `BorderedTable` and `SimpleTable`.
Expand Down
14 changes: 8 additions & 6 deletions cmd2/clipboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@
import pyperclip # type: ignore[import]

# noinspection PyProtectedMember
from pyperclip import (
PyperclipException,
)

# Can we access the clipboard? Should always be true on Windows and Mac, but only sometimes on Linux
# noinspection PyBroadException
try:
# Try getting the contents of the clipboard
_ = pyperclip.paste()
except (PyperclipException, FileNotFoundError, ValueError):
# NOTE: FileNotFoundError is for Windows Subsystem for Linux (WSL) when Windows paths are removed from $PATH
# NOTE: ValueError is for headless Linux systems without Gtk installed

# pyperclip raises at least the following types of exceptions. To be safe, just catch all Exceptions.
# FileNotFoundError on Windows Subsystem for Linux (WSL) when Windows paths are removed from $PATH
# ValueError for headless Linux systems without Gtk installed
# AssertionError can be raised by paste_klipper().
# PyperclipException for pyperclip-specific exceptions
except Exception:
can_clip = False
else:
can_clip = True
Expand Down
2 changes: 1 addition & 1 deletion cmd2/cmd2.py
Original file line number Diff line number Diff line change
Expand Up @@ -4086,7 +4086,7 @@ def do_shell(self, args: argparse.Namespace) -> None:
self.last_result = proc.returncode

# If the process was stopped by Ctrl-C, then inform the caller by raising a KeyboardInterrupt.
# This is to support things like stop_on_keyboard_interrupt in run_cmds_plus_hooks().
# This is to support things like stop_on_keyboard_interrupt in runcmds_plus_hooks().
if proc.returncode == ctrl_c_ret_code:
self._raise_keyboard_interrupt()

Expand Down

0 comments on commit f5af7d3

Please sign in to comment.