jaraco / jaraco.clipboard

MIT License
19 stars 1 forks source link

Clipboard Access Denied Error #9

Closed Sn3akyP3t3 closed 3 years ago

Sn3akyP3t3 commented 4 years ago

I was attempting to switch over from pyperclip to jaraco.clipboard and I'm immediately running into problems when running unit tests that were passing before the conversion. Nothing fancy, just text in the Win 10 clipboard, but its throwing an exception error and I'm thinking if jaraco.clipboard is mostly just a wrapper for the underlying libraries then this shouldn't be happening.

Here's the relevant tail end of the Traceback. Prior to this is the unit test details which is not relevant.

File "C:\Program Files\Python38\lib\site-packages\jaraco\windows\clipboard.py", line 168, in set_unicode_text
    with context():
  File "C:\Program Files\Python38\lib\contextlib.py", line 113, in __enter__
    return next(self.gen)
  File "C:\Program Files\Python38\lib\site-packages\jaraco\windows\clipboard.py", line 207, in context
    OpenClipboard()
  File "C:\Program Files\Python38\lib\site-packages\jaraco\windows\clipboard.py", line 30, in OpenClipboard
    handle_nonzero_success(windll.user32.OpenClipboard(owner))
  File "C:\Program Files\Python38\lib\site-packages\jaraco\windows\error.py", line 85, in handle_nonzero_success
    raise WindowsError()
jaraco.windows.error.WindowsError: Access is denied.

As you can see the installation is to Program Files which is for all users of this PC which does require use of elevated admin rights to perform installations and such, but shouldn't pose this particular problem unless there is some sort of delegation that is or is not happening.

jaraco commented 3 years ago

Thanks for the report. You're right that jaraco.clipboard is mainly a thin wrapper around jaraco.windows when running on Windows. You probably would get the same error if you were to run jaraco.windows.clipboard.set_unicode_text("foo").

The error message Access is denied. comes from Windows, so for some reason, Windows thinks that the user running the Python executable does not have privilege to set text on the clipboard. That seems like a pretty basic functionality, but honestly, I don't know enough about Windows or its user access controls to say for certain.

What I can say with confidence is that the tests that set unicode text do in fact pass on the Windows test runners (example), so there's something else peculiar to your environment that's causing the errors.

You mentioned that you're not running with elevated admin rights - have you tried running with them? Can you think of anything else in your environment that may be restricting your access to the clipboard? I don't see anything in the api docs for OpenClipboard that explain why access would be denied.

Probably your best bet here is to take a simple Python script that replicates underlying error and work with support avenues for Windows on the issue. Probably this command should replicate the issue:

python -c "import ctypes; assert ctypes.windll.user32.OpenClipboard(None) > 0"

It runs to completion on my Windows machine without error. I suspect the assertion will fail in your environment.

Feel free to report back if I can provide more details and especially if you've found some defect with jaraco.clipboard. If you find something that should be improved in jaraco.windows instead, feel free to file a bug there (or comment here and I'll transfer the issue).

In the meantime, I'm going to close this as it's unlikely to be actionable for this project.