Show More
@@ -31,7 +31,7 from IPython.core.magic import ( | |||
|
31 | 31 | Magics, compress_dhist, magics_class, line_magic, cell_magic, line_cell_magic |
|
32 | 32 | ) |
|
33 | 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 | 35 | from IPython.utils.path import get_py_filename, unquote_filename |
|
36 | 36 | from IPython.utils.process import abbrev_cwd |
|
37 | 37 | from IPython.utils.terminal import set_term_title |
@@ -691,7 +691,7 class OSMagics(Magics): | |||
|
691 | 691 | print "Error: no such file, variable, URL, history range or macro" |
|
692 | 692 | return |
|
693 | 693 | |
|
694 | page.page(self.shell.pycolorize(cont)) | |
|
694 | page.page(self.shell.pycolorize(source_to_unicode(cont))) | |
|
695 | 695 | |
|
696 | 696 | @magic_arguments.magic_arguments() |
|
697 | 697 | @magic_arguments.argument( |
@@ -14,6 +14,7 from __future__ import print_function | |||
|
14 | 14 | #----------------------------------------------------------------------------- |
|
15 | 15 | # Imports |
|
16 | 16 | #----------------------------------------------------------------------------- |
|
17 | import re | |
|
17 | 18 | import os |
|
18 | 19 | import sys |
|
19 | 20 | import tempfile |
@@ -154,6 +155,23 class Tee(object): | |||
|
154 | 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 | 175 | def file_read(filename): |
|
158 | 176 | """Read a file and close it. Returns the file source.""" |
|
159 | 177 | fobj = open(filename,'r'); |
General Comments 0
You need to be logged in to leave comments.
Login now