##// END OF EJS Templates
determine encoding of source lines in %debug before colorizing
Jörgen Stenarson -
Show More
@@ -352,6 +352,7 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 start = max(start, 0)
356 start = max(start, 0)
356 start = min(start, len(lines) - context)
357 start = min(start, len(lines) - context)
357 lines = lines[start : start + context]
358 lines = lines[start : start + context]
@@ -362,9 +363,8 b' class Pdb(OldPdb):'
362 and tpl_line_em \
363 and tpl_line_em \
363 or tpl_line
364 or tpl_line
364 ret.append(self.__format_line(linetpl, filename,
365 ret.append(self.__format_line(linetpl, filename,
365 start + 1 + i, line,
366 start + 1 + i, line.decode(encoding),
366 arrow = show_arrow) )
367 arrow = show_arrow) )
367
368 return ''.join(ret)
368 return ''.join(ret)
369
369
370 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
370 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
@@ -422,8 +422,10 b' class Pdb(OldPdb):'
422 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
422 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
423 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
423 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
424 src = []
424 src = []
425 lines = linecache.getlines(filename)
426 encoding = io.guess_encoding(lines)
425 for lineno in range(first, last+1):
427 for lineno in range(first, last+1):
426 line = linecache.getline(filename, lineno)
428 line = lines[lineno].decode(encoding)
427 if not line:
429 if not line:
428 break
430 break
429
431
@@ -155,20 +155,27 b' class Tee(object):'
155 self.close()
155 self.close()
156
156
157
157
158 def source_to_unicode(txt):
158 def guess_encoding(lines):
159 """Converts string with python source code to unicode
159 """check list of lines for line matching the source code encoding pattern
160 """
161 if isinstance(txt, unicode):
162 return txt
163
160
161 Only check first two lines
162 """
164 reg = re.compile("#.*coding[:=]\s*([-\w.]+)")
163 reg = re.compile("#.*coding[:=]\s*([-\w.]+)")
165 for row in txt.split("\n", 2)[:2]: #We only need to check the first two lines
164 for row in lines[:2]: #We only need to check the first two lines
166 result = reg.match(row)
165 result = reg.match(row)
167 if result:
166 if result:
168 coding = result.groups()[0]
167 coding = result.groups()[0]
169 break
168 break
170 else:
169 else:
171 coding = "ascii"
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))
172 return txt.decode(coding, errors="replace")
179 return txt.decode(coding, errors="replace")
173
180
174
181
General Comments 0
You need to be logged in to leave comments. Login now