Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9aa74ee

Browse files
committed
merge functionality in io and openpy relating to encoding
New functions were introduced in openpy to deal with encoding in python files. This commit removes redundant code from io and moves source_to_unicode to openpy.
1 parent 5f06484 commit 9aa74ee

5 files changed

Lines changed: 29 additions & 32 deletions

File tree

IPython/core/debugger.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333
from IPython.utils import PyColorize
3434
from IPython.core import ipapi
35-
from IPython.utils import coloransi, io
35+
from IPython.utils import coloransi, io, openpy
3636
from IPython.core.excolors import exception_colors
3737

3838
# See if we can use pydb.
@@ -352,7 +352,10 @@ def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
352352

353353
start = lineno - 1 - context//2
354354
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"
356359
start = max(start, 0)
357360
start = min(start, len(lines) - context)
358361
lines = lines[start : start + context]
@@ -363,7 +366,7 @@ def format_stack_entry(self, frame_lineno, lprefix=': ', context = 3):
363366
and tpl_line_em \
364367
or tpl_line
365368
ret.append(self.__format_line(linetpl, filename,
366-
start + 1 + i, line.decode(encoding),
369+
start + 1 + i, line.decode(encoding, errors="replace"),
367370
arrow = show_arrow) )
368371
return ''.join(ret)
369372

@@ -423,9 +426,12 @@ def print_list_lines(self, filename, first, last):
423426
tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
424427
src = []
425428
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"
427433
for lineno in range(first, last+1):
428-
line = lines[lineno].decode(encoding)
434+
line = lines[lineno].decode(encoding, errors="replace")
429435
if not line:
430436
break
431437

IPython/core/oinspect.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from IPython.testing.skipdoctest import skip_doctest_py3
3636
from IPython.utils import PyColorize
3737
from IPython.utils import io
38+
from IPython.utils import openpy
3839
from IPython.utils import py3compat
3940
from IPython.utils.text import indent
4041
from IPython.utils.wildcard import list_namespace
@@ -457,7 +458,7 @@ def pfile(self, obj, oname=''):
457458
# Print only text files, not extension binaries. Note that
458459
# getsourcelines returns lineno with 1-offset and page() uses
459460
# 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)
461462

462463
def _format_fields(self, fields, title_width=12):
463464
"""Formats a list of fields for display.

IPython/utils/io.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -155,30 +155,6 @@ def __del__(self):
155155
self.close()
156156

157157

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-
182158
def file_read(filename):
183159
"""Read a file and close it. Returns the file source."""
184160
fobj = open(filename,'r');

IPython/utils/openpy.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io
1010
from io import TextIOWrapper
1111
import re
12+
from StringIO import StringIO
1213
import urllib
1314

1415
cookie_re = re.compile(ur"coding[:=]\s*([-\w.]+)", re.UNICODE)
@@ -120,6 +121,17 @@ def open(filename):
120121
text.mode = 'r'
121122
return text
122123

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+
123135
def strip_encoding_cookie(filelike):
124136
"""Generator to pull lines from a text-mode file, skipping the encoding
125137
cookie if it is found in the first two lines.

IPython/zmq/zmqshell.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
get_connection_file, get_connection_info, connect_qtconsole
3939
)
4040
from IPython.testing.skipdoctest import skip_doctest
41-
from IPython.utils import io
41+
from IPython.utils import io, openpy
4242
from IPython.utils.jsonutil import json_clean, encode_images
4343
from IPython.utils.process import arg_split
4444
from IPython.utils import py3compat
@@ -355,7 +355,9 @@ def less(self, arg_s):
355355

356356
cont = open(arg_s).read()
357357
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()
359361
page.page(cont)
360362

361363
more = line_magic('more')(less)

0 commit comments

Comments
 (0)