Skip to content

Commit

Permalink
better support when binary data can be read as text on MacOS
Browse files Browse the repository at this point in the history
  • Loading branch information
spyoungtech committed Jan 27, 2021
1 parent d8dc444 commit b455315
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ If there is a platform or utility not currently listed, please request it by cre

### MacOS

There is a known issue that `pbcopy`/`pbpaste` do not support arbitrary binary data.
MacOS has support for multiple backends.

For full support on MacOS, the `pasteboard` package needs to be installed.
This is the default backend when available. You can install this as an extra:

```
pip install pyperclip3[pasteboard] --no-use-pep517
```

As a fallback, `pbcopy`/`pbpaste` can be used as a backend, but do not support arbitrary binary data, which may lead to
data being lost on copy/paste.

### Linux

Expand Down
10 changes: 5 additions & 5 deletions pyperclip3/macos_clip.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,11 @@ def __init__(self):

def copy(self, data: Union[str, bytes], encoding=None):
if isinstance(data, bytes):
if encoding is not None:
warnings.warn("encoding specified with a bytes argument. "
"Encoding option will be ignored. "
"To remove this warning, omit the encoding parameter or specify it as None", stacklevel=2)
self.pb.set_contents(data, self._bytes_type)
try:
data = data.decode()
self.pb.set_contents(data)
except UnicodeDecodeError:
self.pb.set_contents(data, self._bytes_type)
elif isinstance(data, str):
self.pb.set_contents(data)
else:
Expand Down

0 comments on commit b455315

Please sign in to comment.