##// 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 353 start = lineno - 1 - context//2
354 354 lines = linecache.getlines(filename)
355 encoding = io.guess_encoding(lines)
355 356 start = max(start, 0)
356 357 start = min(start, len(lines) - context)
357 358 lines = lines[start : start + context]
@@ -362,9 +363,8 b' class Pdb(OldPdb):'
362 363 and tpl_line_em \
363 364 or tpl_line
364 365 ret.append(self.__format_line(linetpl, filename,
365 start + 1 + i, line,
366 start + 1 + i, line.decode(encoding),
366 367 arrow = show_arrow) )
367
368 368 return ''.join(ret)
369 369
370 370 def __format_line(self, tpl_line, filename, lineno, line, arrow = False):
@@ -422,8 +422,10 b' class Pdb(OldPdb):'
422 422 tpl_line = '%%s%s%%s %s%%s' % (Colors.lineno, ColorsNormal)
423 423 tpl_line_em = '%%s%s%%s %s%%s%s' % (Colors.linenoEm, Colors.line, ColorsNormal)
424 424 src = []
425 lines = linecache.getlines(filename)
426 encoding = io.guess_encoding(lines)
425 427 for lineno in range(first, last+1):
426 line = linecache.getline(filename, lineno)
428 line = lines[lineno].decode(encoding)
427 429 if not line:
428 430 break
429 431
@@ -155,20 +155,27 b' class Tee(object):'
155 155 self.close()
156 156
157 157
158 def source_to_unicode(txt):
159 """Converts string with python source code to unicode
160 """
161 if isinstance(txt, unicode):
162 return txt
158 def guess_encoding(lines):
159 """check list of lines for line matching the source code encoding pattern
163 160
161 Only check first two lines
162 """
164 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 165 result = reg.match(row)
167 166 if result:
168 167 coding = result.groups()[0]
169 168 break
170 169 else:
171 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 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