##// END OF EJS Templates
Merge pull request #4460 from takluyver/clipboard-improvements...
Matthias Bussonnier -
r13516:e9d90d02 merge
parent child Browse files
Show More
@@ -0,0 +1,20 b''
1 import nose.tools as nt
2
3 from IPython.core.error import TryNext
4 from IPython.lib.clipboard import ClipboardEmpty
5 from IPython.utils.py3compat import unicode_type
6
7 def test_clipboard_get():
8 # Smoketest for clipboard access - we can't easily guarantee that the
9 # clipboard is accessible and has something on it, but this tries to
10 # exercise the relevant code anyway.
11 try:
12 a = get_ipython().hooks.clipboard_get()
13 except ClipboardEmpty:
14 # Nothing in clipboard to get
15 pass
16 except TryNext:
17 # No clipboard access API available
18 pass
19 else:
20 nt.assert_is_instance(a, unicode_type)
@@ -6,6 +6,9 b' import subprocess'
6 6 from IPython.core.error import TryNext
7 7 import IPython.utils.py3compat as py3compat
8 8
9 class ClipboardEmpty(ValueError):
10 pass
11
9 12 def win32_clipboard_get():
10 13 """ Get the current clipboard's text on Windows.
11 14
@@ -17,9 +20,16 b' def win32_clipboard_get():'
17 20 raise TryNext("Getting text from the clipboard requires the pywin32 "
18 21 "extensions: http://sourceforge.net/projects/pywin32/")
19 22 win32clipboard.OpenClipboard()
20 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
21 # FIXME: convert \r\n to \n?
22 win32clipboard.CloseClipboard()
23 try:
24 text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
25 except TypeError:
26 try:
27 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
28 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
29 except TypeError:
30 raise ClipboardEmpty
31 finally:
32 win32clipboard.CloseClipboard()
23 33 return text
24 34
25 35 def osx_clipboard_get():
@@ -41,17 +51,21 b' def tkinter_clipboard_get():'
41 51 implementation that uses that toolkit.
42 52 """
43 53 try:
44 from tkinter import Tk # Py 3
54 from tkinter import Tk, TclError # Py 3
45 55 except ImportError:
46 56 try:
47 from Tkinter import Tk # Py 2
57 from Tkinter import Tk, TclError # Py 2
48 58 except ImportError:
49 59 raise TryNext("Getting text from the clipboard on this platform "
50 60 "requires Tkinter.")
51 61 root = Tk()
52 62 root.withdraw()
53 text = root.clipboard_get()
54 root.destroy()
63 try:
64 text = root.clipboard_get()
65 except TclError:
66 raise ClipboardEmpty
67 finally:
68 root.destroy()
55 69 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
56 70 return text
57 71
@@ -24,6 +24,7 b' from IPython.core.usage import interactive_usage, default_banner'
24 24 from IPython.core.inputsplitter import IPythonInputSplitter
25 25 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
26 26 from IPython.core.magic import Magics, magics_class, line_magic
27 from IPython.lib.clipboard import ClipboardEmpty
27 28 from IPython.testing.skipdoctest import skip_doctest
28 29 from IPython.utils.encoding import get_stream_enc
29 30 from IPython.utils import py3compat
@@ -216,6 +217,8 b' class TerminalMagics(Magics):'
216 217 else:
217 218 error('Could not get text from the clipboard.')
218 219 return
220 except ClipboardEmpty:
221 raise UsageError("The clipboard appears to be empty")
219 222
220 223 # By default, echo back to terminal unless quiet mode is requested
221 224 if 'q' not in opts:
General Comments 0
You need to be logged in to leave comments. Login now