profile: prevent a crash when line number is unknown stable
authorPierre-Yves David <pierre-yves.david@octobus.net>
Sat, 12 Nov 2022 02:30:41 +0100
branchstable
changeset 49578 aab3d4c05720
parent 49576 d12446766a35
child 49579 15a89b722937
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.
mercurial/statprof.py
--- a/mercurial/statprof.py	Sun Nov 06 12:15:35 2022 -0500
+++ b/mercurial/statprof.py	Sat Nov 12 02:30:41 2022 +0100
@@ -236,8 +236,8 @@
 
     def getsource(self, length):
         if self.source is None:
-            lineno = self.lineno - 1
             try:
+                lineno = self.lineno - 1  # lineno can be None
                 with open(self.path, b'rb') as fp:
                     for i, line in enumerate(fp):
                         if i == lineno:
@@ -773,7 +773,7 @@
             codestring = codepattern % (
                 prefix,
                 b'line'.rjust(spacing_len),
-                site.lineno,
+                site.lineno if site.lineno is not None else -1,
                 b''.ljust(max(0, 4 - len(str(site.lineno)))),
                 site.getsource(30),
             )