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