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

allow pasting arbitrary binary content in xclip #21

Merged
merged 1 commit into from
Oct 31, 2022

Conversation

mgaitan
Copy link
Contributor

@mgaitan mgaitan commented Jul 22, 2022

This proposal fix a very basic use case in linux. When I capture a screenshot pyclip (CTRL+PrtScr or SHIFT+CTRL+PrtScr in gnome), the data is in the clipboard but pycliptry to paste it as STRING and fail. Instead I try to parse the available targets and select the best one if it couldn't be pasted as plain/text.

This is original issue

In [1]: import pyclip                                                                                                                                         

In [2]: pyclip.paste()                                                                                                                                        
---------------------------------------------------------------------------
ClipboardException                        Traceback (most recent call last)
<ipython-input-2-d18644d9efc4> in <module>
----> 1 pyclip.paste()

~/lab/pyclip/pyclip/__init__.py in paste(*args, **kwargs)
     40     if DEFAULT_CLIPBOARD is None:
     41         raise ClipboardSetupException("Could not setup clipboard").with_traceback(_CLIPBOARD_EXCEPTION_TB)
---> 42     return DEFAULT_CLIPBOARD.paste(*args, **kwargs)
     43 
     44 

~/lab/pyclip/pyclip/xclip_clip.py in paste(self, encoding, text, errors)
    100 
    101         if completed_proc.returncode != 0:
--> 102             raise ClipboardException(
    103                 f"Copy failed. xclip returned code: {completed_proc.returncode!r} "
    104                 f"Stderr: {completed_proc.stderr!r} "

ClipboardException: Copy failed. xclip returned code: 1 Stderr: b'Error: target STRING not available\n' Stdout: b''
In [3]:    

and after my patch

In [1]: import pyclip                                                                                                                                         

In [2]: pyclip.paste()                                                                                                                                        
Out[2]: b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00=\x00\x00\x002\x08\x06\x00\x00\x00\xef4\xd3<\x00\x00\x00\x04sBIT\x08\x08\x08\x08|\x08d\x88\x00\x00\x00\x19tEXtSoftware\x00gnome-screenshot\xef\x03\xbf>\x00\x00\x00&tEXtCreation Time\x00vie 22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xbf\xf7\xae\x82\xc6\xa4K\xef\xe8\xb3R\x1clL\x1e6\xbb\xf4\x7fl+\xe5\x17\xa6v8=.\xf8\xd21\xe0\xbdQ%\x98\x8f\x1f\x8c\\\x068\r\xa5\xcfb\x1a[.\xca\x054<\x8e]\xc8g\xa7w\ri\x07xS\xa6\x9a\x99\x9b#\xb2\xd2\xff\x8bC\xd4\xff\xc0\x0b\x1e-\xbf\x03\xebx\x17^\xb8\xe9\xb9?sz\xd8?\xde\xc2\xf3t\xf4\xeft\x12h5\xe2\x1c\xb0-+\x19}\xe9\x10P\xf9=m\xa5\xb5`\xa5\xb5`\xa5\xb5`\xa5\xb5`\xa5\xb5`\xa5\xb5`\xa5\xb5`\xa5\xb5p\x0e\xe7sY\xc5\xe7\xc6\x94]\x00\x00\x00\x00IEND\xaeB`\x82'

for tests, It's hard to set real clipboard "targets" programmatically, so I simply mocked the subprocess call and check the paste command suprocess' arguments.

@mgaitan
Copy link
Contributor Author

mgaitan commented Sep 12, 2022

@spyoungtech Could you consider this in the mid-term and eventually release a new version? Currently I have project depending on my forked version, which is being installed from sources, but it's not ideal.

@spyoungtech
Copy link
Owner

Sounds reasonable to me. I'd hope we can also get this use case functional in other platforms.

@spyoungtech spyoungtech merged commit 48f23b0 into spyoungtech:main Oct 31, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants