##// END OF EJS Templates
Catch more errors from clipboard access on Windows
Thomas Kluyver -
Show More
@@ -1,72 +1,72 b''
1 """ Utilities for accessing the platform's clipboard.
1 """ Utilities for accessing the platform's clipboard.
2 """
2 """
3
3
4 import subprocess
4 import subprocess
5
5
6 from IPython.core.error import TryNext
6 from IPython.core.error import TryNext
7 import IPython.utils.py3compat as py3compat
7 import IPython.utils.py3compat as py3compat
8
8
9 class ClipboardEmpty(ValueError):
9 class ClipboardEmpty(ValueError):
10 pass
10 pass
11
11
12 def win32_clipboard_get():
12 def win32_clipboard_get():
13 """ Get the current clipboard's text on Windows.
13 """ Get the current clipboard's text on Windows.
14
14
15 Requires Mark Hammond's pywin32 extensions.
15 Requires Mark Hammond's pywin32 extensions.
16 """
16 """
17 try:
17 try:
18 import win32clipboard
18 import win32clipboard
19 except ImportError:
19 except ImportError:
20 raise TryNext("Getting text from the clipboard requires the pywin32 "
20 raise TryNext("Getting text from the clipboard requires the pywin32 "
21 "extensions: http://sourceforge.net/projects/pywin32/")
21 "extensions: http://sourceforge.net/projects/pywin32/")
22 win32clipboard.OpenClipboard()
22 win32clipboard.OpenClipboard()
23 try:
23 try:
24 text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
24 text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
25 except TypeError:
25 except (TypeError, win32clipboard.error):
26 try:
26 try:
27 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
27 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
28 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
28 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
29 except TypeError:
29 except (TypeError, win32clipboard.error):
30 raise ClipboardEmpty
30 raise ClipboardEmpty
31 finally:
31 finally:
32 win32clipboard.CloseClipboard()
32 win32clipboard.CloseClipboard()
33 return text
33 return text
34
34
35 def osx_clipboard_get():
35 def osx_clipboard_get():
36 """ Get the clipboard's text on OS X.
36 """ Get the clipboard's text on OS X.
37 """
37 """
38 p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'],
38 p = subprocess.Popen(['pbpaste', '-Prefer', 'ascii'],
39 stdout=subprocess.PIPE)
39 stdout=subprocess.PIPE)
40 text, stderr = p.communicate()
40 text, stderr = p.communicate()
41 # Text comes in with old Mac \r line endings. Change them to \n.
41 # Text comes in with old Mac \r line endings. Change them to \n.
42 text = text.replace(b'\r', b'\n')
42 text = text.replace(b'\r', b'\n')
43 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
43 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
44 return text
44 return text
45
45
46 def tkinter_clipboard_get():
46 def tkinter_clipboard_get():
47 """ Get the clipboard's text using Tkinter.
47 """ Get the clipboard's text using Tkinter.
48
48
49 This is the default on systems that are not Windows or OS X. It may
49 This is the default on systems that are not Windows or OS X. It may
50 interfere with other UI toolkits and should be replaced with an
50 interfere with other UI toolkits and should be replaced with an
51 implementation that uses that toolkit.
51 implementation that uses that toolkit.
52 """
52 """
53 try:
53 try:
54 from tkinter import Tk, TclError # Py 3
54 from tkinter import Tk, TclError # Py 3
55 except ImportError:
55 except ImportError:
56 try:
56 try:
57 from Tkinter import Tk, TclError # Py 2
57 from Tkinter import Tk, TclError # Py 2
58 except ImportError:
58 except ImportError:
59 raise TryNext("Getting text from the clipboard on this platform "
59 raise TryNext("Getting text from the clipboard on this platform "
60 "requires Tkinter.")
60 "requires Tkinter.")
61 root = Tk()
61 root = Tk()
62 root.withdraw()
62 root.withdraw()
63 try:
63 try:
64 text = root.clipboard_get()
64 text = root.clipboard_get()
65 except TclError:
65 except TclError:
66 raise ClipboardEmpty
66 raise ClipboardEmpty
67 finally:
67 finally:
68 root.destroy()
68 root.destroy()
69 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
69 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
70 return text
70 return text
71
71
72
72
General Comments 0
You need to be logged in to leave comments. Login now