Details
Details
- Reviewers
pulkit - Group Reviewers
hg-reviewers - Commits
- rHGc085cb134b9e: statprof: use context manager when reading source from file
Diff Detail
Diff Detail
- Repository
- rHG Mercurial
- Lint
Lint Skipped - Unit
Unit Tests Skipped
pulkit |
hg-reviewers |
Lint Skipped |
Unit Tests Skipped |
Path | Packages | |||
---|---|---|---|---|
M | mercurial/statprof.py (14 lines) |
Commit | Parents | Author | Summary | Date |
---|---|---|---|---|
9a2302bbdd78 | 41b42f58c5f9 | Martin von Zweigbergk | Aug 30 2019, 7:43 PM |
Status | Author | Revision | |
---|---|---|---|
Closed | martinvonz | ||
Closed | martinvonz | ||
Closed | martinvonz | ||
Closed | martinvonz | ||
Closed | martinvonz |
except KeyError: | except KeyError: | ||||
v = cls(path, lineno, function) | v = cls(path, lineno, function) | ||||
cls.cache[k] = v | cls.cache[k] = v | ||||
return v | return v | ||||
def getsource(self, length): | def getsource(self, length): | ||||
if self.source is None: | if self.source is None: | ||||
lineno = self.lineno - 1 | lineno = self.lineno - 1 | ||||
fp = None | |||||
try: | try: | ||||
fp = open(self.path, 'rb') | with open(self.path, 'rb') as fp: | ||||
for i, line in enumerate(fp): | for i, line in enumerate(fp): | ||||
if i == lineno: | if i == lineno: | ||||
self.source = line.strip() | self.source = line.strip() | ||||
break | break | ||||
except: | except: | ||||
pass | pass | ||||
finally: | |||||
if fp: | |||||
fp.close() | |||||
if self.source is None: | if self.source is None: | ||||
self.source = '' | self.source = '' | ||||
source = self.source | source = self.source | ||||
if len(source) > length: | if len(source) > length: | ||||
source = source[:(length - 3)] + "..." | source = source[:(length - 3)] + "..." | ||||
return source | return source | ||||