##// END OF EJS Templates
convert sourcecode to unicode before colorizing in %pycat
Jörgen Stenarson -
Show More
@@ -31,7 +31,7 b' from IPython.core.magic import ('
31 Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic
31 Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic
32 )
32 )
33 from IPython.testing.skipdoctest import skip_doctest
33 from IPython.testing.skipdoctest import skip_doctest
34 from IPython.utils.io import file_read, nlprint
34 from IPython.utils.io import file_read, nlprint, source_to_unicode
35 from IPython.utils.path import get_py_filename, unquote_filename
35 from IPython.utils.path import get_py_filename, unquote_filename
36 from IPython.utils.process import abbrev_cwd
36 from IPython.utils.process import abbrev_cwd
37 from IPython.utils.terminal import set_term_title
37 from IPython.utils.terminal import set_term_title
@@ -691,7 +691,7 b' class OSMagics(Magics):'
691 print "Error: no such file, variable, URL, history range or macro"
691 print "Error: no such file, variable, URL, history range or macro"
692 return
692 return
693
693
694 page.page(self.shell.pycolorize(cont))
694 page.page(self.shell.pycolorize(source_to_unicode(cont)))
695
695
696 @magic_arguments.magic_arguments()
696 @magic_arguments.magic_arguments()
697 @magic_arguments.argument(
697 @magic_arguments.argument(
@@ -14,6 +14,7 b' from __future__ import print_function'
14 #-----------------------------------------------------------------------------
14 #-----------------------------------------------------------------------------
15 # Imports
15 # Imports
16 #-----------------------------------------------------------------------------
16 #-----------------------------------------------------------------------------
17 import re
17 import os
18 import os
18 import sys
19 import sys
19 import tempfile
20 import tempfile
@@ -154,6 +155,23 b' class Tee(object):'
154 self.close()
155 self.close()
155
156
156
157
158 def source_to_unicode(txt):
159 """Converts string with python source code to unicode
160 """
161 if isinstance(txt, unicode):
162 return txt
163
164 reg = re.compile("#.*coding[:=]\s*([-\w.]+)")
165 for row in txt.split("\n", 2)[:2]: #We only need to check the first two lines
166 result = reg.match(row)
167 if result:
168 coding = result.groups()[0]
169 break
170 else:
171 coding = "ascii"
172 return txt.decode(coding, errors="replace")
173
174
157 def file_read(filename):
175 def file_read(filename):
158 """Read a file and close it. Returns the file source."""
176 """Read a file and close it. Returns the file source."""
159 fobj = open(filename,'r');
177 fobj = open(filename,'r');
General Comments 0
You need to be logged in to leave comments. Login now