Skip to content

Commit 4d21dc4

Browse files
committed
Optimize TracebackEntry.ishidden
The expected behavior is that there is no "__tracebackhide__" attribute, so use `getattr` instead of multiple try/except.
1 parent 9059722 commit 4d21dc4

File tree

1 file changed

+7
-12
lines changed

1 file changed

+7
-12
lines changed

src/_pytest/_code/code.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,25 +241,20 @@ def getsource(self, astcache=None):
241241

242242
def ishidden(self):
243243
""" return True if the current frame has a var __tracebackhide__
244-
resolving to True
244+
resolving to True.
245245
246246
If __tracebackhide__ is a callable, it gets called with the
247247
ExceptionInfo instance and can decide whether to hide the traceback.
248248
249249
mostly for internal use
250250
"""
251-
try:
252-
tbh = self.frame.f_locals["__tracebackhide__"]
253-
except KeyError:
254-
try:
255-
tbh = self.frame.f_globals["__tracebackhide__"]
256-
except KeyError:
257-
return False
258-
259-
if callable(tbh):
251+
f = self.frame
252+
tbh = f.f_locals.get(
253+
"__tracebackhide__", f.f_globals.get("__tracebackhide__", False)
254+
)
255+
if tbh and callable(tbh):
260256
return tbh(None if self._excinfo is None else self._excinfo())
261-
else:
262-
return tbh
257+
return tbh
263258

264259
def __str__(self):
265260
try:

0 commit comments

Comments
 (0)