##// 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:

r7276:bf42bf0f
r9853:7f9a133e
Show More
test_rmagic.py
62 lines | 2.1 KiB | text/x-python | PythonLexer
Jonathan Taylor
added tests of R line and cell magics
r7215 import numpy as np
from IPython.core.interactiveshell import InteractiveShell
from IPython.extensions import rmagic
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 import nose.tools as nt
Jonathan Taylor
added tests of R line and cell magics
r7215
Jonathan Taylor
fixing InteractiveShell instantiation in test
r7222 ip = get_ipython()
ip.magic('load_ext rmagic')
Jonathan Taylor
added tests of R line and cell magics
r7215
def test_push():
rm = rmagic.RMagics(ip)
ip.push({'X':np.arange(5), 'Y':np.array([3,5,4,6,7])})
ip.run_line_magic('Rpush', 'X Y')
np.testing.assert_almost_equal(np.asarray(rm.r('X')), ip.user_ns['X'])
np.testing.assert_almost_equal(np.asarray(rm.r('Y')), ip.user_ns['Y'])
def test_pull():
rm = rmagic.RMagics(ip)
rm.r('Z=c(11:20)')
ip.run_line_magic('Rpull', 'Z')
np.testing.assert_almost_equal(np.asarray(rm.r('Z')), ip.user_ns['Z'])
np.testing.assert_almost_equal(ip.user_ns['Z'], np.arange(11,21))
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 def test_Rconverter():
datapy= np.array([(1, 2.9, 'a'), (2, 3.5, 'b'), (3, 2.1, 'c')],
dtype=[('x', '<i4'), ('y', '<f8'), ('z', '|S1')])
ip.user_ns['datapy'] = datapy
ip.run_line_magic('Rpush', 'datapy')
# test to see if a copy is being made
Jonathan Taylor
added Rget, and options to try to return strucutred array
r7276 v = ip.run_line_magic('Rget', '-d datapy')
w = ip.run_line_magic('Rget', '-d datapy')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 np.testing.assert_almost_equal(w['x'], v['x'])
np.testing.assert_almost_equal(w['y'], v['y'])
nt.assert_true(np.all(w['z'] == v['z']))
np.testing.assert_equal(id(w.data), id(v.data))
nt.assert_equal(w.dtype, v.dtype)
Jonathan Taylor
added Rget, and options to try to return strucutred array
r7276 ip.run_cell_magic('R', ' -d datar datar=datapy', '')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268
Jonathan Taylor
added Rget, and options to try to return strucutred array
r7276 u = ip.run_line_magic('Rget', ' -d datar')
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 np.testing.assert_almost_equal(u['x'], v['x'])
np.testing.assert_almost_equal(u['y'], v['y'])
nt.assert_true(np.all(u['z'] == v['z']))
np.testing.assert_equal(id(u.data), id(v.data))
nt.assert_equal(u.dtype, v.dtype)
Jonathan Taylor
added tests of R line and cell magics
r7215
def test_cell_magic():
ip.push({'x':np.arange(5), 'y':np.array([3,5,4,6,7])})
snippet = '''
print(summary(a))
Jonathan Taylor
return vectors instead of structured arrays when 1d and no names
r7268 plot(x, y, pch=23, bg='orange', cex=2)
plot(x, x)
print(summary(x))
Jonathan Taylor
added tests of R line and cell magics
r7215 r = resid(a)
xc = coef(a)
'''
Jonathan Taylor
removed %Rinline, %R has been converted to line_cell_magic
r7217 ip.run_cell_magic('R', '-i x,y -o r,xc a=lm(y~x)', snippet)
Jonathan Taylor
added tests of R line and cell magics
r7215 np.testing.assert_almost_equal(ip.user_ns['xc'], [3.2, 0.9])
np.testing.assert_almost_equal(ip.user_ns['r'], np.array([-0.2, 0.9, -1. , 0.1, 0.2]))