[IPython-dev] Unicode bug in %whos, with patch
Paul Mueller
gakusei at dakotacom.net
Thu May 3 16:22:43 EDT 2007
Hi,
If you have a unicode variable with a character whose ordinal is >= 128, you
get a UnicodeEncodeError on using %whos (this is using r2304):
In [1]:u = u'\x80'
In [2]:%whos
Variable Type Data/Info
-------------------------------
u unicode ---------------------------------------------------------------------------
<type 'exceptions.UnicodeEncodeError'> Traceback (most recent call last)
/home/yuurei/src/mods/ipython/<ipython console> in <module>()
/home/yuurei/src/mods/ipython/IPython/iplib.py in ipmagic(self, arg_s)
962 else:
963 magic_args = self.var_expand(magic_args,1)
--> 964 return fn(magic_args)
965
966 def ipalias(self,arg_s):
/home/yuurei/src/mods/ipython/IPython/Magic.py in magic_whos(self, parameter_s)
1057 print '(%s Mb)' % (vbytes/Mb,)
1058 else:
-> 1059 vstr = str(var).replace('\n','\\n')
1060 if len(vstr) < 50:
1061 print vstr
<type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\x80' in position 0: ordinal not in range(128)
I've attached a patch that fixes this.
Paul Mueller
-------------- next part --------------
Index: IPython/Magic.py
===================================================================
--- IPython/Magic.py (revision 2304)
+++ IPython/Magic.py (working copy)
@@ -1056,7 +1056,12 @@
else:
print '(%s Mb)' % (vbytes/Mb,)
else:
- vstr = str(var).replace('\n','\\n')
+ try:
+ vstr = str(var)
+ except UnicodeEncodeError:
+ vstr = unicode(var).encode(sys.getdefaultencoding(),
+ 'backslashreplace')
+ vstr = vstr.replace('\n','\\n')
if len(vstr) < 50:
print vstr
else:
More information about the IPython-dev
mailing list