comparison mercurial/statprof.py @ 49578:aab3d4c05720 stable

profile: prevent a crash when line number is unknown For some unknown reason, `self.lineno` can be None. The previous code crashed in such case, we now ignore the case, as we do for other error in this function. We also fallback to using `-1` in the output when this lack of line number makes it to the display code. The reason of unknown line-numbers is… unknown.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Sat, 12 Nov 2022 02:30:41 +0100
parents 642e31cb55f0
children 9eb69fa5a783
comparison
equal deleted inserted replaced
49576:d12446766a35 49578:aab3d4c05720
234 cls.cache[k] = v 234 cls.cache[k] = v
235 return v 235 return v
236 236
237 def getsource(self, length): 237 def getsource(self, length):
238 if self.source is None: 238 if self.source is None:
239 lineno = self.lineno - 1
240 try: 239 try:
240 lineno = self.lineno - 1 # lineno can be None
241 with open(self.path, b'rb') as fp: 241 with open(self.path, b'rb') as fp:
242 for i, line in enumerate(fp): 242 for i, line in enumerate(fp):
243 if i == lineno: 243 if i == lineno:
244 self.source = line.strip() 244 self.source = line.strip()
245 break 245 break
771 771
772 codepattern = b'%s%s %d: %s%s' 772 codepattern = b'%s%s %d: %s%s'
773 codestring = codepattern % ( 773 codestring = codepattern % (
774 prefix, 774 prefix,
775 b'line'.rjust(spacing_len), 775 b'line'.rjust(spacing_len),
776 site.lineno, 776 site.lineno if site.lineno is not None else -1,
777 b''.ljust(max(0, 4 - len(str(site.lineno)))), 777 b''.ljust(max(0, 4 - len(str(site.lineno)))),
778 site.getsource(30), 778 site.getsource(30),
779 ) 779 )
780 780
781 finalstring = liststring + codestring 781 finalstring = liststring + codestring