##// END OF EJS Templates
merge functionality in io and openpy relating to encoding...
Jörgen Stenarson -
Show More
@@ -32,7 +32,7 b' import sys'
32
32
33 from IPython.utils import PyColorize
33 from IPython.utils import PyColorize
34 from IPython.core import ipapi
34 from IPython.core import ipapi
35 from IPython.utils import coloransi, io
35 from IPython.utils import coloransi, io, openpy
36 from IPython.core.excolors import exception_colors
36 from IPython.core.excolors import exception_colors
37
37
38 # See if we can use pydb.
38 # See if we can use pydb.
@@ -352,7 +352,10 b' class Pdb(OldPdb):'
352
352
353 start = lineno - 1 - context//2
353 start = lineno - 1 - context//2
354 lines = linecache.getlines(filename)
354 lines = linecache.getlines(filename)
355 encoding = io.guess_encoding(lines)
355 try:
356 encoding, _ = openpy.detect_encoding(lambda :lines[:2].pop(0))
357 except SyntaxError:
358 encoding = "ascii"
356 start = max(start, 0)
359 start = max(start, 0)
357 start = min(start, len(lines) - context)
360 start = min(start, len(lines) - context)
358 lines = lines[start : start + context]
361 lines = lines[start : start + context]
@@ -363,7 +366,7 b' class Pdb(OldPdb):'
363 and tpl_line_em \
366 and tpl_line_em \
364 or tpl_line
367 or tpl_line
365 ret.append(self.__format_line(linetpl, filename,
368 ret.append(self.__format_line(linetpl, filename,
366 start + 1 + i, line.decode(encoding),
369 start + 1 + i, line.decode(encoding, errors="replace"),
367 arrow = show_arrow) )
370 arrow = show_arrow) )
368 return ''.join(ret)
371 return ''.join(ret)
369
372
@@ -423,9 +426,12 b' class Pdb(OldPdb):'
423 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
426 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
424 src = []
427 src = []
425 lines = linecache.getlines(filename)
428 lines = linecache.getlines(filename)
426 encoding = io.guess_encoding(lines)
429 try:
430 encoding, _ = openpy.detect_encoding(lambda :lines[:2].pop(0))
431 except SyntaxError:
432 encoding = "ascii"
427 for lineno in range(first, last+1):
433 for lineno in range(first, last+1):
428 line = lines[lineno].decode(encoding)
434 line = lines[lineno].decode(encoding, errors="replace")
429 if not line:
435 if not line:
430 break
436 break
431
437
@@ -35,6 +35,7 b' from IPython.core import page'
35 from IPython.testing.skipdoctest import skip_doctest_py3
35 from IPython.testing.skipdoctest import skip_doctest_py3
36 from IPython.utils import PyColorize
36 from IPython.utils import PyColorize
37 from IPython.utils import io
37 from IPython.utils import io
38 from IPython.utils import openpy
38 from IPython.utils import py3compat
39 from IPython.utils import py3compat
39 from IPython.utils.text import indent
40 from IPython.utils.text import indent
40 from IPython.utils.wildcard import list_namespace
41 from IPython.utils.wildcard import list_namespace
@@ -457,7 +458,7 b' class Inspector:'
457 # Print only text files, not extension binaries. Note that
458 # Print only text files, not extension binaries. Note that
458 # getsourcelines returns lineno with 1-offset and page() uses
459 # getsourcelines returns lineno with 1-offset and page() uses
459 # 0-offset, so we must adjust.
460 # 0-offset, so we must adjust.
460 page.page(self.format(io.source_to_unicode(open(ofile).read())), lineno-1)
461 page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1)
461
462
462 def _format_fields(self, fields, title_width=12):
463 def _format_fields(self, fields, title_width=12):
463 """Formats a list of fields for display.
464 """Formats a list of fields for display.
@@ -155,30 +155,6 b' class Tee(object):'
155 self.close()
155 self.close()
156
156
157
157
158 def guess_encoding(lines):
159 """check list of lines for line matching the source code encoding pattern
160
161 Only check first two lines
162 """
163 reg = re.compile("#.*coding[:=]\s*([-\w.]+)")
164 for row in lines[:2]: #We only need to check the first two lines
165 result = reg.match(row)
166 if result:
167 coding = result.groups()[0]
168 break
169 else:
170 coding = "ascii"
171 return coding
172
173 def source_to_unicode(txt):
174 """Converts string with python source code to unicode
175 """
176 if isinstance(txt, unicode):
177 return txt
178 coding = guess_encoding(txt.split("\n", 2))
179 return txt.decode(coding, errors="replace")
180
181
182 def file_read(filename):
158 def file_read(filename):
183 """Read a file and close it. Returns the file source."""
159 """Read a file and close it. Returns the file source."""
184 fobj = open(filename,'r');
160 fobj = open(filename,'r');
@@ -9,6 +9,7 b' from __future__ import absolute_import'
9 import io
9 import io
10 from io import TextIOWrapper
10 from io import TextIOWrapper
11 import re
11 import re
12 from StringIO import StringIO
12 import urllib
13 import urllib
13
14
14 cookie_re = re.compile(ur"coding[:=]\s*([-\w.]+)", re.UNICODE)
15 cookie_re = re.compile(ur"coding[:=]\s*([-\w.]+)", re.UNICODE)
@@ -120,6 +121,17 b' except ImportError:'
120 text.mode = 'r'
121 text.mode = 'r'
121 return text
122 return text
122
123
124 def source_to_unicode(txt):
125 """Converts string with python source code to unicode
126 """
127 if isinstance(txt, unicode):
128 return txt
129 try:
130 coding, _ = detect_encoding(StringIO(txt).readline)
131 except SyntaxError:
132 coding = "ascii"
133 return txt.decode(coding, errors="replace")
134
123 def strip_encoding_cookie(filelike):
135 def strip_encoding_cookie(filelike):
124 """Generator to pull lines from a text-mode file, skipping the encoding
136 """Generator to pull lines from a text-mode file, skipping the encoding
125 cookie if it is found in the first two lines.
137 cookie if it is found in the first two lines.
@@ -38,7 +38,7 b' from IPython.lib.kernel import ('
38 get_connection_file, get_connection_info, connect_qtconsole
38 get_connection_file, get_connection_info, connect_qtconsole
39 )
39 )
40 from IPython.testing.skipdoctest import skip_doctest
40 from IPython.testing.skipdoctest import skip_doctest
41 from IPython.utils import io
41 from IPython.utils import io, openpy
42 from IPython.utils.jsonutil import json_clean, encode_images
42 from IPython.utils.jsonutil import json_clean, encode_images
43 from IPython.utils.process import arg_split
43 from IPython.utils.process import arg_split
44 from IPython.utils import py3compat
44 from IPython.utils import py3compat
@@ -355,7 +355,9 b' class KernelMagics(Magics):'
355
355
356 cont = open(arg_s).read()
356 cont = open(arg_s).read()
357 if arg_s.endswith('.py'):
357 if arg_s.endswith('.py'):
358 cont = self.shell.pycolorize(io.source_to_unicode(cont))
358 cont = self.shell.pycolorize(openpy.read_py_file(arg_s, skip_encoding_cookie=False))
359 else:
360 cont = open(arg_s).read()
359 page.page(cont)
361 page.page(cont)
360
362
361 more = line_magic('more')(less)
363 more = line_magic('more')(less)
General Comments 0
You need to be logged in to leave comments. Login now