1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
|
From: Corey Bryant <corey.bryant@canonical.com>
Date: Sat, 28 Dec 2019 17:31:33 +0100
Subject: Switch from GTK+2 to GTK+3 to avoid import of mutually exclusive
Bug-Ubuntu: https://bugs.launchpad.net/bugs/1722553
Origin: https://github.com/asweigart/pyperclip/pull/111
Forwarded: yes
modules that resulted in the following error:
AttributeError: When using gi.repository you must not import static modules like
"gobject". Please change all occurrences of "import gobject" to
"from gi.repository import GObject". See:
https://bugzilla.gnome.org/show_bug.cgi?id=709183
---
src/pyperclip/__init__.py | 40 +++++++++++++++++++++++++++++++++++-----
1 file changed, 35 insertions(+), 5 deletions(-)
diff --git a/src/pyperclip/__init__.py b/src/pyperclip/__init__.py
index 7f47c27..f5fea8e 100644
--- a/src/pyperclip/__init__.py
+++ b/src/pyperclip/__init__.py
@@ -20,7 +20,8 @@ On Linux, install xclip or xsel via package manager. For example, in Debian:
sudo apt-get install xclip
sudo apt-get install xsel
-Otherwise on Linux, you will need the gtk or PyQt5/PyQt4 modules installed.
+Otherwise on Linux, you will need the gi (GTK+ 3) or PyQt5/PyQt4 modules installed.
+gtk (GTK +2) is still supported as an older alternative to gi.
gtk and PyQt4 modules are not available for Python 3,
and this module does not work with PyGObject yet.
@@ -145,7 +146,6 @@ def init_gtk_clipboard():
import gtk
def copy_gtk(text):
- global cb
text = _stringifyText(text) # Converts non-str values to str.
cb = gtk.Clipboard()
cb.set_text(text)
@@ -162,6 +162,28 @@ def init_gtk_clipboard():
return copy_gtk, paste_gtk
+def init_gi_clipboard():
+ import gi
+ gi.require_version('Gtk', '3.0')
+ from gi.repository import Gtk, Gdk
+ cb = Gtk.Clipboard.get(Gdk.SELECTION_CLIPBOARD)
+
+ def copy_gi(text):
+ text = _stringifyText(text) # Converts non-str values to str.
+ cb.set_text(text, -1)
+ cb.store()
+
+ def paste_gi():
+ clipboardContents = cb.wait_for_text()
+ # for python 2, returns None if the clipboard is blank.
+ if clipboardContents is None:
+ return ''
+ else:
+ return clipboardContents
+
+ return copy_gi, paste_gi
+
+
def init_qt_clipboard():
global QApplication
# $DISPLAY should exist
@@ -523,11 +545,18 @@ def determine_clipboard():
# Setup for the LINUX platform:
if HAS_DISPLAY:
try:
- import gtk # check if gtk is installed
+ import gi # check if gi is installed (for GTK+ 3)
except ImportError:
- pass # We want to fail fast for all non-ImportError exceptions.
+ try:
+ import gtk # check if gtk is installed (fallback to GTK+ 2)
+ except ImportError:
+ pass # We want to fail fast for all non-ImportError exceptions.
+ else:
+ return init_gtk_clipboard()
else:
- return init_gtk_clipboard()
+ if gi.version_info[0] >= 3:
+ return init_gi_clipboard()
+ pass
if _executable_exists("xsel"):
return init_xsel_clipboard()
@@ -580,6 +609,7 @@ def set_clipboard(clipboard):
clipboard_types = {'pbcopy': init_osx_pbcopy_clipboard,
'pyobjc': init_osx_pyobjc_clipboard,
'gtk': init_gtk_clipboard,
+ 'gi': init_gi_clipboard,
'qt': init_qt_clipboard, # TODO - split this into 'qtpy', 'pyqt4', and 'pyqt5'
'xclip': init_xclip_clipboard,
'xsel': init_xsel_clipboard,
|