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