##// END OF EJS Templates
Better clipboard handling, esp. with pywin32...
Thomas Kluyver -
Show More
@@ -6,6 +6,9 b' import subprocess'
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):
10 pass
11
9 def win32_clipboard_get():
12 def win32_clipboard_get():
10 """ Get the current clipboard's text on Windows.
13 """ Get the current clipboard's text on Windows.
11
14
@@ -17,9 +20,16 b' def win32_clipboard_get():'
17 raise TryNext("Getting text from the clipboard requires the pywin32 "
20 raise TryNext("Getting text from the clipboard requires the pywin32 "
18 "extensions: http://sourceforge.net/projects/pywin32/")
21 "extensions: http://sourceforge.net/projects/pywin32/")
19 win32clipboard.OpenClipboard()
22 win32clipboard.OpenClipboard()
20 text = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT)
23 try:
21 # FIXME: convert \r\n to \n?
24 text = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT)
22 win32clipboard.CloseClipboard()
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 return text
33 return text
24
34
25 def osx_clipboard_get():
35 def osx_clipboard_get():
@@ -41,17 +51,21 b' def tkinter_clipboard_get():'
41 implementation that uses that toolkit.
51 implementation that uses that toolkit.
42 """
52 """
43 try:
53 try:
44 from tkinter import Tk # Py 3
54 from tkinter import Tk, TclError # Py 3
45 except ImportError:
55 except ImportError:
46 try:
56 try:
47 from Tkinter import Tk # Py 2
57 from Tkinter import Tk, TclError # Py 2
48 except ImportError:
58 except ImportError:
49 raise TryNext("Getting text from the clipboard on this platform "
59 raise TryNext("Getting text from the clipboard on this platform "
50 "requires Tkinter.")
60 "requires Tkinter.")
51 root = Tk()
61 root = Tk()
52 root.withdraw()
62 root.withdraw()
53 text = root.clipboard_get()
63 try:
54 root.destroy()
64 text = root.clipboard_get()
65 except TclError:
66 raise ClipboardEmpty
67 finally:
68 root.destroy()
55 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
69 text = py3compat.cast_unicode(text, py3compat.DEFAULT_ENCODING)
56 return text
70 return text
57
71
@@ -24,6 +24,7 b' from IPython.core.usage import interactive_usage, default_banner'
24 from IPython.core.inputsplitter import IPythonInputSplitter
24 from IPython.core.inputsplitter import IPythonInputSplitter
25 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
25 from IPython.core.interactiveshell import InteractiveShell, InteractiveShellABC
26 from IPython.core.magic import Magics, magics_class, line_magic
26 from IPython.core.magic import Magics, magics_class, line_magic
27 from IPython.lib.clipboard import ClipboardEmpty
27 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.utils.encoding import get_stream_enc
29 from IPython.utils.encoding import get_stream_enc
29 from IPython.utils import py3compat
30 from IPython.utils import py3compat
@@ -216,6 +217,8 b' class TerminalMagics(Magics):'
216 else:
217 else:
217 error('Could not get text from the clipboard.')
218 error('Could not get text from the clipboard.')
218 return
219 return
220 except ClipboardEmpty:
221 raise UsageError("The clipboard appears to be empty")
219
222
220 # By default, echo back to terminal unless quiet mode is requested
223 # By default, echo back to terminal unless quiet mode is requested
221 if 'q' not in opts:
224 if 'q' not in opts:
General Comments 0
You need to be logged in to leave comments. Login now