##// END OF EJS Templates
Backport PR #2738: Unicode content crashes the pager (console)...
Backport PR #2738: Unicode content crashes the pager (console) We've run into an interesting bug in the astropy project. https://github.com/astropy/astropy/issues/600 When displaying a docstring that contains Unicode and is also long enough that it gets sent to the pager it fails since the docstring can't be sent to the pager as ascii. This crashes in the middle of sending content to the pager, so the shell ends up in an inconsistent state and stops echoing the keyboard etc. The fix (attached) is merely to encode the content sent to the pager in the same encoding as the terminal (`sys.stdout.encoding`). Strictly speaking, this isn't always the right thing to do, since the pager may be configured to expect a different encoding than the terminal, but that is sort of an irrational way to configure a machine... ;) For example, `less`, in the absence of any special environment variables to tell it otherwise, uses the standard `LC*` environment variables to determine what to do, which should be the same mechanism the terminal also uses by default. If anyone can suggest a better fix, I'm all for it. Perhaps it should be configurable, defaulting to `sys.stdout.encoding`?

File last commit:

r6624:9d90e074
r9853:7f9a133e
Show More
ipy_render.py
67 lines | 1.6 KiB | text/x-python | PythonLexer
vivainio
crlf cleanup
r680 """ IPython extension: Render templates from variables and paste to clipbard """
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 from IPython.core import ipapi
vivainio
crlf cleanup
r680
Brian Granger
ipapi.py => core/ipapi.py and imports updated.
r2027 ip = ipapi.get()
vivainio
crlf cleanup
r680
from string import Template
import sys,os
Thomas Kluyver
Remove bundled Itpl module.
r6624 # We no longer bundle Itpl. If you update this module, you should use advanced
# string formatting instead.
Brian Granger
Moved Itpl.py to deathrow as we already have a copy in external.
r2031 from IPython.external.Itpl import itplns
vivainio
crlf cleanup
r680
def toclip_w32(s):
""" Places contents of s to clipboard
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 Needs pyvin32 to work:
http://sourceforge.net/projects/pywin32/
"""
import win32clipboard as cl
import win32con
cl.OpenClipboard()
cl.EmptyClipboard()
cl.SetClipboardText( s.replace('\n','\r\n' ))
cl.CloseClipboard()
try:
Bernardo B. Marques
remove all trailling spaces
r4872 import win32clipboard
vivainio
crlf cleanup
r680 toclip = toclip_w32
except ImportError:
def toclip(s): pass
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680
def render(tmpl):
""" Render a template (Itpl format) from ipython variables
Example:
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 $ import ipy_render
$ my_name = 'Bob' # %store this for convenience
$ t_submission_form = "Submission report, author: $my_name" # %store also
$ render t_submission_form
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 => returns "Submission report, author: Bob" and copies to clipboard on win32
# if template exist as a file, read it. Note: ;f hei vaan => f("hei vaan")
Bernardo B. Marques
remove all trailling spaces
r4872 $ ;render c:/templates/greeting.txt
vivainio
crlf cleanup
r680 Template examples (Ka-Ping Yee's Itpl library):
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 Here is a $string.
Here is a $module.member.
Here is an $object.member.
Here is a $functioncall(with, arguments).
Here is an ${arbitrary + expression}.
Here is an $array[3] member.
Here is a $dictionary['member'].
"""
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 if os.path.isfile(tmpl):
tmpl = open(tmpl).read()
Bernardo B. Marques
remove all trailling spaces
r4872
vivainio
crlf cleanup
r680 res = itplns(tmpl, ip.user_ns)
toclip(res)
return res
Brian Granger
Continuing a massive refactor of everything.
r2205 ip.push('render')