Show More
@@ -1,7 +1,5 b'' | |||
|
1 | """Wrapper around linecache which decodes files to unicode according to PEP 263. | |
|
2 | ||
|
3 | This is only needed for Python 2 - linecache in Python 3 does the same thing | |
|
4 | itself. | |
|
1 | """ | |
|
2 | Wrapper around linecache which decodes files to unicode according to PEP 263. | |
|
5 | 3 | """ |
|
6 | 4 | import functools |
|
7 | 5 | import linecache |
@@ -10,35 +8,9 b' import sys' | |||
|
10 | 8 | from IPython.utils import py3compat |
|
11 | 9 | from IPython.utils import openpy |
|
12 | 10 | |
|
13 | if py3compat.PY3: | |
|
14 | getline = linecache.getline | |
|
15 | ||
|
16 | # getlines has to be looked up at runtime, because doctests monkeypatch it. | |
|
17 | @functools.wraps(linecache.getlines) | |
|
18 | def getlines(filename, module_globals=None): | |
|
19 | return linecache.getlines(filename, module_globals=module_globals) | |
|
20 | ||
|
21 | else: | |
|
22 | def getlines(filename, module_globals=None): | |
|
23 | """Get the lines (as unicode) for a file from the cache. | |
|
24 | Update the cache if it doesn't contain an entry for this file already.""" | |
|
25 | filename = py3compat.cast_bytes(filename, sys.getfilesystemencoding()) | |
|
26 | lines = linecache.getlines(filename, module_globals=module_globals) | |
|
27 | ||
|
28 | if (not lines) or isinstance(lines[0], str): | |
|
29 | return lines | |
|
30 | ||
|
31 | readline = openpy._list_readline(lines) | |
|
32 | try: | |
|
33 | encoding, _ = openpy.detect_encoding(readline) | |
|
34 | except SyntaxError: | |
|
35 | encoding = 'ascii' | |
|
36 | return [l.decode(encoding, 'replace') for l in lines] | |
|
11 | getline = linecache.getline | |
|
37 | 12 | |
|
38 | # This is a straight copy of linecache.getline | |
|
39 | def getline(filename, lineno, module_globals=None): | |
|
40 |
|
|
|
41 | if 1 <= lineno <= len(lines): | |
|
42 | return lines[lineno-1] | |
|
43 | else: | |
|
44 | return '' | |
|
13 | # getlines has to be looked up at runtime, because doctests monkeypatch it. | |
|
14 | @functools.wraps(linecache.getlines) | |
|
15 | def getlines(filename, module_globals=None): | |
|
16 | return linecache.getlines(filename, module_globals=module_globals) |
General Comments 0
You need to be logged in to leave comments.
Login now