code.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274
  1. import ast
  2. import inspect
  3. import os
  4. import re
  5. import sys
  6. import traceback
  7. from inspect import CO_VARARGS
  8. from inspect import CO_VARKEYWORDS
  9. from io import StringIO
  10. from pathlib import Path
  11. from traceback import format_exception_only
  12. from types import CodeType
  13. from types import FrameType
  14. from types import TracebackType
  15. from typing import Any
  16. from typing import Callable
  17. from typing import ClassVar
  18. from typing import Dict
  19. from typing import Generic
  20. from typing import Iterable
  21. from typing import List
  22. from typing import Mapping
  23. from typing import Optional
  24. from typing import overload
  25. from typing import Pattern
  26. from typing import Sequence
  27. from typing import Set
  28. from typing import Tuple
  29. from typing import Type
  30. from typing import TYPE_CHECKING
  31. from typing import TypeVar
  32. from typing import Union
  33. from weakref import ref
  34. import attr
  35. import pluggy
  36. import _pytest
  37. from _pytest._code.source import findsource
  38. from _pytest._code.source import getrawcode
  39. from _pytest._code.source import getstatementrange_ast
  40. from _pytest._code.source import Source
  41. from _pytest._io import TerminalWriter
  42. from _pytest._io.saferepr import safeformat
  43. from _pytest._io.saferepr import saferepr
  44. from _pytest.compat import final
  45. from _pytest.compat import get_real_func
  46. from _pytest.deprecated import check_ispytest
  47. from _pytest.pathlib import absolutepath
  48. from _pytest.pathlib import bestrelpath
  49. if TYPE_CHECKING:
  50. from typing_extensions import Literal
  51. from typing_extensions import SupportsIndex
  52. from weakref import ReferenceType
  53. _TracebackStyle = Literal["long", "short", "line", "no", "native", "value", "auto"]
  54. class Code:
  55. """Wrapper around Python code objects."""
  56. __slots__ = ("raw",)
  57. def __init__(self, obj: CodeType) -> None:
  58. self.raw = obj
  59. @classmethod
  60. def from_function(cls, obj: object) -> "Code":
  61. return cls(getrawcode(obj))
  62. def __eq__(self, other):
  63. return self.raw == other.raw
  64. # Ignore type because of https://github.com/python/mypy/issues/4266.
  65. __hash__ = None # type: ignore
  66. @property
  67. def firstlineno(self) -> int:
  68. return self.raw.co_firstlineno - 1
  69. @property
  70. def name(self) -> str:
  71. return self.raw.co_name
  72. @property
  73. def path(self) -> Union[Path, str]:
  74. """Return a path object pointing to source code, or an ``str`` in
  75. case of ``OSError`` / non-existing file."""
  76. if not self.raw.co_filename:
  77. return ""
  78. try:
  79. p = absolutepath(self.raw.co_filename)
  80. # maybe don't try this checking
  81. if not p.exists():
  82. raise OSError("path check failed.")
  83. return p
  84. except OSError:
  85. # XXX maybe try harder like the weird logic
  86. # in the standard lib [linecache.updatecache] does?
  87. return self.raw.co_filename
  88. @property
  89. def fullsource(self) -> Optional["Source"]:
  90. """Return a _pytest._code.Source object for the full source file of the code."""
  91. full, _ = findsource(self.raw)
  92. return full
  93. def source(self) -> "Source":
  94. """Return a _pytest._code.Source object for the code object's source only."""
  95. # return source only for that part of code
  96. return Source(self.raw)
  97. def getargs(self, var: bool = False) -> Tuple[str, ...]:
  98. """Return a tuple with the argument names for the code object.
  99. If 'var' is set True also return the names of the variable and
  100. keyword arguments when present.
  101. """
  102. # Handy shortcut for getting args.
  103. raw = self.raw
  104. argcount = raw.co_argcount
  105. if var:
  106. argcount += raw.co_flags & CO_VARARGS
  107. argcount += raw.co_flags & CO_VARKEYWORDS
  108. return raw.co_varnames[:argcount]
  109. class Frame:
  110. """Wrapper around a Python frame holding f_locals and f_globals
  111. in which expressions can be evaluated."""
  112. __slots__ = ("raw",)
  113. def __init__(self, frame: FrameType) -> None:
  114. self.raw = frame
  115. @property
  116. def lineno(self) -> int:
  117. return self.raw.f_lineno - 1
  118. @property
  119. def f_globals(self) -> Dict[str, Any]:
  120. return self.raw.f_globals
  121. @property
  122. def f_locals(self) -> Dict[str, Any]:
  123. return self.raw.f_locals
  124. @property
  125. def code(self) -> Code:
  126. return Code(self.raw.f_code)
  127. @property
  128. def statement(self) -> "Source":
  129. """Statement this frame is at."""
  130. if self.code.fullsource is None:
  131. return Source("")
  132. return self.code.fullsource.getstatement(self.lineno)
  133. def eval(self, code, **vars):
  134. """Evaluate 'code' in the frame.
  135. 'vars' are optional additional local variables.
  136. Returns the result of the evaluation.
  137. """
  138. f_locals = self.f_locals.copy()
  139. f_locals.update(vars)
  140. return eval(code, self.f_globals, f_locals)
  141. def repr(self, object: object) -> str:
  142. """Return a 'safe' (non-recursive, one-line) string repr for 'object'."""
  143. return saferepr(object)
  144. def getargs(self, var: bool = False):
  145. """Return a list of tuples (name, value) for all arguments.
  146. If 'var' is set True, also include the variable and keyword arguments
  147. when present.
  148. """
  149. retval = []
  150. for arg in self.code.getargs(var):
  151. try:
  152. retval.append((arg, self.f_locals[arg]))
  153. except KeyError:
  154. pass # this can occur when using Psyco
  155. return retval
  156. class TracebackEntry:
  157. """A single entry in a Traceback."""
  158. __slots__ = ("_rawentry", "_excinfo", "_repr_style")
  159. def __init__(
  160. self,
  161. rawentry: TracebackType,
  162. excinfo: Optional["ReferenceType[ExceptionInfo[BaseException]]"] = None,
  163. ) -> None:
  164. self._rawentry = rawentry
  165. self._excinfo = excinfo
  166. self._repr_style: Optional['Literal["short", "long"]'] = None
  167. @property
  168. def lineno(self) -> int:
  169. return self._rawentry.tb_lineno - 1
  170. def set_repr_style(self, mode: "Literal['short', 'long']") -> None:
  171. assert mode in ("short", "long")
  172. self._repr_style = mode
  173. @property
  174. def frame(self) -> Frame:
  175. return Frame(self._rawentry.tb_frame)
  176. @property
  177. def relline(self) -> int:
  178. return self.lineno - self.frame.code.firstlineno
  179. def __repr__(self) -> str:
  180. return "<TracebackEntry %s:%d>" % (self.frame.code.path, self.lineno + 1)
  181. @property
  182. def statement(self) -> "Source":
  183. """_pytest._code.Source object for the current statement."""
  184. source = self.frame.code.fullsource
  185. assert source is not None
  186. return source.getstatement(self.lineno)
  187. @property
  188. def path(self) -> Union[Path, str]:
  189. """Path to the source code."""
  190. return self.frame.code.path
  191. @property
  192. def locals(self) -> Dict[str, Any]:
  193. """Locals of underlying frame."""
  194. return self.frame.f_locals
  195. def getfirstlinesource(self) -> int:
  196. return self.frame.code.firstlineno
  197. def getsource(
  198. self, astcache: Optional[Dict[Union[str, Path], ast.AST]] = None
  199. ) -> Optional["Source"]:
  200. """Return failing source code."""
  201. # we use the passed in astcache to not reparse asttrees
  202. # within exception info printing
  203. source = self.frame.code.fullsource
  204. if source is None:
  205. return None
  206. key = astnode = None
  207. if astcache is not None:
  208. key = self.frame.code.path
  209. if key is not None:
  210. astnode = astcache.get(key, None)
  211. start = self.getfirstlinesource()
  212. try:
  213. astnode, _, end = getstatementrange_ast(
  214. self.lineno, source, astnode=astnode
  215. )
  216. except SyntaxError:
  217. end = self.lineno + 1
  218. else:
  219. if key is not None and astcache is not None:
  220. astcache[key] = astnode
  221. return source[start:end]
  222. source = property(getsource)
  223. def ishidden(self) -> bool:
  224. """Return True if the current frame has a var __tracebackhide__
  225. resolving to True.
  226. If __tracebackhide__ is a callable, it gets called with the
  227. ExceptionInfo instance and can decide whether to hide the traceback.
  228. Mostly for internal use.
  229. """
  230. tbh: Union[
  231. bool, Callable[[Optional[ExceptionInfo[BaseException]]], bool]
  232. ] = False
  233. for maybe_ns_dct in (self.frame.f_locals, self.frame.f_globals):
  234. # in normal cases, f_locals and f_globals are dictionaries
  235. # however via `exec(...)` / `eval(...)` they can be other types
  236. # (even incorrect types!).
  237. # as such, we suppress all exceptions while accessing __tracebackhide__
  238. try:
  239. tbh = maybe_ns_dct["__tracebackhide__"]
  240. except Exception:
  241. pass
  242. else:
  243. break
  244. if tbh and callable(tbh):
  245. return tbh(None if self._excinfo is None else self._excinfo())
  246. return tbh
  247. def __str__(self) -> str:
  248. name = self.frame.code.name
  249. try:
  250. line = str(self.statement).lstrip()
  251. except KeyboardInterrupt:
  252. raise
  253. except BaseException:
  254. line = "???"
  255. # This output does not quite match Python's repr for traceback entries,
  256. # but changing it to do so would break certain plugins. See
  257. # https://github.com/pytest-dev/pytest/pull/7535/ for details.
  258. return " File %r:%d in %s\n %s\n" % (
  259. str(self.path),
  260. self.lineno + 1,
  261. name,
  262. line,
  263. )
  264. @property
  265. def name(self) -> str:
  266. """co_name of underlying code."""
  267. return self.frame.code.raw.co_name
  268. class Traceback(List[TracebackEntry]):
  269. """Traceback objects encapsulate and offer higher level access to Traceback entries."""
  270. def __init__(
  271. self,
  272. tb: Union[TracebackType, Iterable[TracebackEntry]],
  273. excinfo: Optional["ReferenceType[ExceptionInfo[BaseException]]"] = None,
  274. ) -> None:
  275. """Initialize from given python traceback object and ExceptionInfo."""
  276. self._excinfo = excinfo
  277. if isinstance(tb, TracebackType):
  278. def f(cur: TracebackType) -> Iterable[TracebackEntry]:
  279. cur_: Optional[TracebackType] = cur
  280. while cur_ is not None:
  281. yield TracebackEntry(cur_, excinfo=excinfo)
  282. cur_ = cur_.tb_next
  283. super().__init__(f(tb))
  284. else:
  285. super().__init__(tb)
  286. def cut(
  287. self,
  288. path: Optional[Union["os.PathLike[str]", str]] = None,
  289. lineno: Optional[int] = None,
  290. firstlineno: Optional[int] = None,
  291. excludepath: Optional["os.PathLike[str]"] = None,
  292. ) -> "Traceback":
  293. """Return a Traceback instance wrapping part of this Traceback.
  294. By providing any combination of path, lineno and firstlineno, the
  295. first frame to start the to-be-returned traceback is determined.
  296. This allows cutting the first part of a Traceback instance e.g.
  297. for formatting reasons (removing some uninteresting bits that deal
  298. with handling of the exception/traceback).
  299. """
  300. path_ = None if path is None else os.fspath(path)
  301. excludepath_ = None if excludepath is None else os.fspath(excludepath)
  302. for x in self:
  303. code = x.frame.code
  304. codepath = code.path
  305. if path is not None and str(codepath) != path_:
  306. continue
  307. if (
  308. excludepath is not None
  309. and isinstance(codepath, Path)
  310. and excludepath_ in (str(p) for p in codepath.parents) # type: ignore[operator]
  311. ):
  312. continue
  313. if lineno is not None and x.lineno != lineno:
  314. continue
  315. if firstlineno is not None and x.frame.code.firstlineno != firstlineno:
  316. continue
  317. return Traceback(x._rawentry, self._excinfo)
  318. return self
  319. @overload
  320. def __getitem__(self, key: "SupportsIndex") -> TracebackEntry:
  321. ...
  322. @overload
  323. def __getitem__(self, key: slice) -> "Traceback":
  324. ...
  325. def __getitem__(
  326. self, key: Union["SupportsIndex", slice]
  327. ) -> Union[TracebackEntry, "Traceback"]:
  328. if isinstance(key, slice):
  329. return self.__class__(super().__getitem__(key))
  330. else:
  331. return super().__getitem__(key)
  332. def filter(
  333. self, fn: Callable[[TracebackEntry], bool] = lambda x: not x.ishidden()
  334. ) -> "Traceback":
  335. """Return a Traceback instance with certain items removed
  336. fn is a function that gets a single argument, a TracebackEntry
  337. instance, and should return True when the item should be added
  338. to the Traceback, False when not.
  339. By default this removes all the TracebackEntries which are hidden
  340. (see ishidden() above).
  341. """
  342. return Traceback(filter(fn, self), self._excinfo)
  343. def getcrashentry(self) -> TracebackEntry:
  344. """Return last non-hidden traceback entry that lead to the exception of a traceback."""
  345. for i in range(-1, -len(self) - 1, -1):
  346. entry = self[i]
  347. if not entry.ishidden():
  348. return entry
  349. return self[-1]
  350. def recursionindex(self) -> Optional[int]:
  351. """Return the index of the frame/TracebackEntry where recursion originates if
  352. appropriate, None if no recursion occurred."""
  353. cache: Dict[Tuple[Any, int, int], List[Dict[str, Any]]] = {}
  354. for i, entry in enumerate(self):
  355. # id for the code.raw is needed to work around
  356. # the strange metaprogramming in the decorator lib from pypi
  357. # which generates code objects that have hash/value equality
  358. # XXX needs a test
  359. key = entry.frame.code.path, id(entry.frame.code.raw), entry.lineno
  360. # print "checking for recursion at", key
  361. values = cache.setdefault(key, [])
  362. if values:
  363. f = entry.frame
  364. loc = f.f_locals
  365. for otherloc in values:
  366. if otherloc == loc:
  367. return i
  368. values.append(entry.frame.f_locals)
  369. return None
  370. E = TypeVar("E", bound=BaseException, covariant=True)
  371. @final
  372. @attr.s(repr=False, init=False, auto_attribs=True)
  373. class ExceptionInfo(Generic[E]):
  374. """Wraps sys.exc_info() objects and offers help for navigating the traceback."""
  375. _assert_start_repr: ClassVar = "AssertionError('assert "
  376. _excinfo: Optional[Tuple[Type["E"], "E", TracebackType]]
  377. _striptext: str
  378. _traceback: Optional[Traceback]
  379. def __init__(
  380. self,
  381. excinfo: Optional[Tuple[Type["E"], "E", TracebackType]],
  382. striptext: str = "",
  383. traceback: Optional[Traceback] = None,
  384. *,
  385. _ispytest: bool = False,
  386. ) -> None:
  387. check_ispytest(_ispytest)
  388. self._excinfo = excinfo
  389. self._striptext = striptext
  390. self._traceback = traceback
  391. @classmethod
  392. def from_exc_info(
  393. cls,
  394. exc_info: Tuple[Type[E], E, TracebackType],
  395. exprinfo: Optional[str] = None,
  396. ) -> "ExceptionInfo[E]":
  397. """Return an ExceptionInfo for an existing exc_info tuple.
  398. .. warning::
  399. Experimental API
  400. :param exprinfo:
  401. A text string helping to determine if we should strip
  402. ``AssertionError`` from the output. Defaults to the exception
  403. message/``__str__()``.
  404. """
  405. _striptext = ""
  406. if exprinfo is None and isinstance(exc_info[1], AssertionError):
  407. exprinfo = getattr(exc_info[1], "msg", None)
  408. if exprinfo is None:
  409. exprinfo = saferepr(exc_info[1])
  410. if exprinfo and exprinfo.startswith(cls._assert_start_repr):
  411. _striptext = "AssertionError: "
  412. return cls(exc_info, _striptext, _ispytest=True)
  413. @classmethod
  414. def from_current(
  415. cls, exprinfo: Optional[str] = None
  416. ) -> "ExceptionInfo[BaseException]":
  417. """Return an ExceptionInfo matching the current traceback.
  418. .. warning::
  419. Experimental API
  420. :param exprinfo:
  421. A text string helping to determine if we should strip
  422. ``AssertionError`` from the output. Defaults to the exception
  423. message/``__str__()``.
  424. """
  425. tup = sys.exc_info()
  426. assert tup[0] is not None, "no current exception"
  427. assert tup[1] is not None, "no current exception"
  428. assert tup[2] is not None, "no current exception"
  429. exc_info = (tup[0], tup[1], tup[2])
  430. return ExceptionInfo.from_exc_info(exc_info, exprinfo)
  431. @classmethod
  432. def for_later(cls) -> "ExceptionInfo[E]":
  433. """Return an unfilled ExceptionInfo."""
  434. return cls(None, _ispytest=True)
  435. def fill_unfilled(self, exc_info: Tuple[Type[E], E, TracebackType]) -> None:
  436. """Fill an unfilled ExceptionInfo created with ``for_later()``."""
  437. assert self._excinfo is None, "ExceptionInfo was already filled"
  438. self._excinfo = exc_info
  439. @property
  440. def type(self) -> Type[E]:
  441. """The exception class."""
  442. assert (
  443. self._excinfo is not None
  444. ), ".type can only be used after the context manager exits"
  445. return self._excinfo[0]
  446. @property
  447. def value(self) -> E:
  448. """The exception value."""
  449. assert (
  450. self._excinfo is not None
  451. ), ".value can only be used after the context manager exits"
  452. return self._excinfo[1]
  453. @property
  454. def tb(self) -> TracebackType:
  455. """The exception raw traceback."""
  456. assert (
  457. self._excinfo is not None
  458. ), ".tb can only be used after the context manager exits"
  459. return self._excinfo[2]
  460. @property
  461. def typename(self) -> str:
  462. """The type name of the exception."""
  463. assert (
  464. self._excinfo is not None
  465. ), ".typename can only be used after the context manager exits"
  466. return self.type.__name__
  467. @property
  468. def traceback(self) -> Traceback:
  469. """The traceback."""
  470. if self._traceback is None:
  471. self._traceback = Traceback(self.tb, excinfo=ref(self))
  472. return self._traceback
  473. @traceback.setter
  474. def traceback(self, value: Traceback) -> None:
  475. self._traceback = value
  476. def __repr__(self) -> str:
  477. if self._excinfo is None:
  478. return "<ExceptionInfo for raises contextmanager>"
  479. return "<{} {} tblen={}>".format(
  480. self.__class__.__name__, saferepr(self._excinfo[1]), len(self.traceback)
  481. )
  482. def exconly(self, tryshort: bool = False) -> str:
  483. """Return the exception as a string.
  484. When 'tryshort' resolves to True, and the exception is an
  485. AssertionError, only the actual exception part of the exception
  486. representation is returned (so 'AssertionError: ' is removed from
  487. the beginning).
  488. """
  489. lines = format_exception_only(self.type, self.value)
  490. text = "".join(lines)
  491. text = text.rstrip()
  492. if tryshort:
  493. if text.startswith(self._striptext):
  494. text = text[len(self._striptext) :]
  495. return text
  496. def errisinstance(
  497. self, exc: Union[Type[BaseException], Tuple[Type[BaseException], ...]]
  498. ) -> bool:
  499. """Return True if the exception is an instance of exc.
  500. Consider using ``isinstance(excinfo.value, exc)`` instead.
  501. """
  502. return isinstance(self.value, exc)
  503. def _getreprcrash(self) -> "ReprFileLocation":
  504. exconly = self.exconly(tryshort=True)
  505. entry = self.traceback.getcrashentry()
  506. path, lineno = entry.frame.code.raw.co_filename, entry.lineno
  507. return ReprFileLocation(path, lineno + 1, exconly)
  508. def getrepr(
  509. self,
  510. showlocals: bool = False,
  511. style: "_TracebackStyle" = "long",
  512. abspath: bool = False,
  513. tbfilter: bool = True,
  514. funcargs: bool = False,
  515. truncate_locals: bool = True,
  516. chain: bool = True,
  517. ) -> Union["ReprExceptionInfo", "ExceptionChainRepr"]:
  518. """Return str()able representation of this exception info.
  519. :param bool showlocals:
  520. Show locals per traceback entry.
  521. Ignored if ``style=="native"``.
  522. :param str style:
  523. long|short|no|native|value traceback style.
  524. :param bool abspath:
  525. If paths should be changed to absolute or left unchanged.
  526. :param bool tbfilter:
  527. Hide entries that contain a local variable ``__tracebackhide__==True``.
  528. Ignored if ``style=="native"``.
  529. :param bool funcargs:
  530. Show fixtures ("funcargs" for legacy purposes) per traceback entry.
  531. :param bool truncate_locals:
  532. With ``showlocals==True``, make sure locals can be safely represented as strings.
  533. :param bool chain:
  534. If chained exceptions in Python 3 should be shown.
  535. .. versionchanged:: 3.9
  536. Added the ``chain`` parameter.
  537. """
  538. if style == "native":
  539. return ReprExceptionInfo(
  540. ReprTracebackNative(
  541. traceback.format_exception(
  542. self.type, self.value, self.traceback[0]._rawentry
  543. )
  544. ),
  545. self._getreprcrash(),
  546. )
  547. fmt = FormattedExcinfo(
  548. showlocals=showlocals,
  549. style=style,
  550. abspath=abspath,
  551. tbfilter=tbfilter,
  552. funcargs=funcargs,
  553. truncate_locals=truncate_locals,
  554. chain=chain,
  555. )
  556. return fmt.repr_excinfo(self)
  557. def match(self, regexp: Union[str, Pattern[str]]) -> "Literal[True]":
  558. """Check whether the regular expression `regexp` matches the string
  559. representation of the exception using :func:`python:re.search`.
  560. If it matches `True` is returned, otherwise an `AssertionError` is raised.
  561. """
  562. __tracebackhide__ = True
  563. msg = "Regex pattern {!r} does not match {!r}."
  564. if regexp == str(self.value):
  565. msg += " Did you mean to `re.escape()` the regex?"
  566. assert re.search(regexp, str(self.value)), msg.format(regexp, str(self.value))
  567. # Return True to allow for "assert excinfo.match()".
  568. return True
  569. @attr.s(auto_attribs=True)
  570. class FormattedExcinfo:
  571. """Presenting information about failing Functions and Generators."""
  572. # for traceback entries
  573. flow_marker: ClassVar = ">"
  574. fail_marker: ClassVar = "E"
  575. showlocals: bool = False
  576. style: "_TracebackStyle" = "long"
  577. abspath: bool = True
  578. tbfilter: bool = True
  579. funcargs: bool = False
  580. truncate_locals: bool = True
  581. chain: bool = True
  582. astcache: Dict[Union[str, Path], ast.AST] = attr.ib(
  583. factory=dict, init=False, repr=False
  584. )
  585. def _getindent(self, source: "Source") -> int:
  586. # Figure out indent for the given source.
  587. try:
  588. s = str(source.getstatement(len(source) - 1))
  589. except KeyboardInterrupt:
  590. raise
  591. except BaseException:
  592. try:
  593. s = str(source[-1])
  594. except KeyboardInterrupt:
  595. raise
  596. except BaseException:
  597. return 0
  598. return 4 + (len(s) - len(s.lstrip()))
  599. def _getentrysource(self, entry: TracebackEntry) -> Optional["Source"]:
  600. source = entry.getsource(self.astcache)
  601. if source is not None:
  602. source = source.deindent()
  603. return source
  604. def repr_args(self, entry: TracebackEntry) -> Optional["ReprFuncArgs"]:
  605. if self.funcargs:
  606. args = []
  607. for argname, argvalue in entry.frame.getargs(var=True):
  608. args.append((argname, saferepr(argvalue)))
  609. return ReprFuncArgs(args)
  610. return None
  611. def get_source(
  612. self,
  613. source: Optional["Source"],
  614. line_index: int = -1,
  615. excinfo: Optional[ExceptionInfo[BaseException]] = None,
  616. short: bool = False,
  617. ) -> List[str]:
  618. """Return formatted and marked up source lines."""
  619. lines = []
  620. if source is None or line_index >= len(source.lines):
  621. source = Source("???")
  622. line_index = 0
  623. if line_index < 0:
  624. line_index += len(source)
  625. space_prefix = " "
  626. if short:
  627. lines.append(space_prefix + source.lines[line_index].strip())
  628. else:
  629. for line in source.lines[:line_index]:
  630. lines.append(space_prefix + line)
  631. lines.append(self.flow_marker + " " + source.lines[line_index])
  632. for line in source.lines[line_index + 1 :]:
  633. lines.append(space_prefix + line)
  634. if excinfo is not None:
  635. indent = 4 if short else self._getindent(source)
  636. lines.extend(self.get_exconly(excinfo, indent=indent, markall=True))
  637. return lines
  638. def get_exconly(
  639. self,
  640. excinfo: ExceptionInfo[BaseException],
  641. indent: int = 4,
  642. markall: bool = False,
  643. ) -> List[str]:
  644. lines = []
  645. indentstr = " " * indent
  646. # Get the real exception information out.
  647. exlines = excinfo.exconly(tryshort=True).split("\n")
  648. failindent = self.fail_marker + indentstr[1:]
  649. for line in exlines:
  650. lines.append(failindent + line)
  651. if not markall:
  652. failindent = indentstr
  653. return lines
  654. def repr_locals(self, locals: Mapping[str, object]) -> Optional["ReprLocals"]:
  655. if self.showlocals:
  656. lines = []
  657. keys = [loc for loc in locals if loc[0] != "@"]
  658. keys.sort()
  659. for name in keys:
  660. value = locals[name]
  661. if name == "__builtins__":
  662. lines.append("__builtins__ = <builtins>")
  663. else:
  664. # This formatting could all be handled by the
  665. # _repr() function, which is only reprlib.Repr in
  666. # disguise, so is very configurable.
  667. if self.truncate_locals:
  668. str_repr = saferepr(value)
  669. else:
  670. str_repr = safeformat(value)
  671. # if len(str_repr) < 70 or not isinstance(value, (list, tuple, dict)):
  672. lines.append(f"{name:<10} = {str_repr}")
  673. # else:
  674. # self._line("%-10s =\\" % (name,))
  675. # # XXX
  676. # pprint.pprint(value, stream=self.excinfowriter)
  677. return ReprLocals(lines)
  678. return None
  679. def repr_traceback_entry(
  680. self,
  681. entry: TracebackEntry,
  682. excinfo: Optional[ExceptionInfo[BaseException]] = None,
  683. ) -> "ReprEntry":
  684. lines: List[str] = []
  685. style = entry._repr_style if entry._repr_style is not None else self.style
  686. if style in ("short", "long"):
  687. source = self._getentrysource(entry)
  688. if source is None:
  689. source = Source("???")
  690. line_index = 0
  691. else:
  692. line_index = entry.lineno - entry.getfirstlinesource()
  693. short = style == "short"
  694. reprargs = self.repr_args(entry) if not short else None
  695. s = self.get_source(source, line_index, excinfo, short=short)
  696. lines.extend(s)
  697. if short:
  698. message = "in %s" % (entry.name)
  699. else:
  700. message = excinfo and excinfo.typename or ""
  701. entry_path = entry.path
  702. path = self._makepath(entry_path)
  703. reprfileloc = ReprFileLocation(path, entry.lineno + 1, message)
  704. localsrepr = self.repr_locals(entry.locals)
  705. return ReprEntry(lines, reprargs, localsrepr, reprfileloc, style)
  706. elif style == "value":
  707. if excinfo:
  708. lines.extend(str(excinfo.value).split("\n"))
  709. return ReprEntry(lines, None, None, None, style)
  710. else:
  711. if excinfo:
  712. lines.extend(self.get_exconly(excinfo, indent=4))
  713. return ReprEntry(lines, None, None, None, style)
  714. def _makepath(self, path: Union[Path, str]) -> str:
  715. if not self.abspath and isinstance(path, Path):
  716. try:
  717. np = bestrelpath(Path.cwd(), path)
  718. except OSError:
  719. return str(path)
  720. if len(np) < len(str(path)):
  721. return np
  722. return str(path)
  723. def repr_traceback(self, excinfo: ExceptionInfo[BaseException]) -> "ReprTraceback":
  724. traceback = excinfo.traceback
  725. if self.tbfilter:
  726. traceback = traceback.filter()
  727. if isinstance(excinfo.value, RecursionError):
  728. traceback, extraline = self._truncate_recursive_traceback(traceback)
  729. else:
  730. extraline = None
  731. last = traceback[-1]
  732. entries = []
  733. if self.style == "value":
  734. reprentry = self.repr_traceback_entry(last, excinfo)
  735. entries.append(reprentry)
  736. return ReprTraceback(entries, None, style=self.style)
  737. for index, entry in enumerate(traceback):
  738. einfo = (last == entry) and excinfo or None
  739. reprentry = self.repr_traceback_entry(entry, einfo)
  740. entries.append(reprentry)
  741. return ReprTraceback(entries, extraline, style=self.style)
  742. def _truncate_recursive_traceback(
  743. self, traceback: Traceback
  744. ) -> Tuple[Traceback, Optional[str]]:
  745. """Truncate the given recursive traceback trying to find the starting
  746. point of the recursion.
  747. The detection is done by going through each traceback entry and
  748. finding the point in which the locals of the frame are equal to the
  749. locals of a previous frame (see ``recursionindex()``).
  750. Handle the situation where the recursion process might raise an
  751. exception (for example comparing numpy arrays using equality raises a
  752. TypeError), in which case we do our best to warn the user of the
  753. error and show a limited traceback.
  754. """
  755. try:
  756. recursionindex = traceback.recursionindex()
  757. except Exception as e:
  758. max_frames = 10
  759. extraline: Optional[str] = (
  760. "!!! Recursion error detected, but an error occurred locating the origin of recursion.\n"
  761. " The following exception happened when comparing locals in the stack frame:\n"
  762. " {exc_type}: {exc_msg}\n"
  763. " Displaying first and last {max_frames} stack frames out of {total}."
  764. ).format(
  765. exc_type=type(e).__name__,
  766. exc_msg=str(e),
  767. max_frames=max_frames,
  768. total=len(traceback),
  769. )
  770. # Type ignored because adding two instances of a List subtype
  771. # currently incorrectly has type List instead of the subtype.
  772. traceback = traceback[:max_frames] + traceback[-max_frames:] # type: ignore
  773. else:
  774. if recursionindex is not None:
  775. extraline = "!!! Recursion detected (same locals & position)"
  776. traceback = traceback[: recursionindex + 1]
  777. else:
  778. extraline = None
  779. return traceback, extraline
  780. def repr_excinfo(
  781. self, excinfo: ExceptionInfo[BaseException]
  782. ) -> "ExceptionChainRepr":
  783. repr_chain: List[
  784. Tuple[ReprTraceback, Optional[ReprFileLocation], Optional[str]]
  785. ] = []
  786. e: Optional[BaseException] = excinfo.value
  787. excinfo_: Optional[ExceptionInfo[BaseException]] = excinfo
  788. descr = None
  789. seen: Set[int] = set()
  790. while e is not None and id(e) not in seen:
  791. seen.add(id(e))
  792. if excinfo_:
  793. reprtraceback = self.repr_traceback(excinfo_)
  794. reprcrash: Optional[ReprFileLocation] = (
  795. excinfo_._getreprcrash() if self.style != "value" else None
  796. )
  797. else:
  798. # Fallback to native repr if the exception doesn't have a traceback:
  799. # ExceptionInfo objects require a full traceback to work.
  800. reprtraceback = ReprTracebackNative(
  801. traceback.format_exception(type(e), e, None)
  802. )
  803. reprcrash = None
  804. repr_chain += [(reprtraceback, reprcrash, descr)]
  805. if e.__cause__ is not None and self.chain:
  806. e = e.__cause__
  807. excinfo_ = (
  808. ExceptionInfo.from_exc_info((type(e), e, e.__traceback__))
  809. if e.__traceback__
  810. else None
  811. )
  812. descr = "The above exception was the direct cause of the following exception:"
  813. elif (
  814. e.__context__ is not None and not e.__suppress_context__ and self.chain
  815. ):
  816. e = e.__context__
  817. excinfo_ = (
  818. ExceptionInfo.from_exc_info((type(e), e, e.__traceback__))
  819. if e.__traceback__
  820. else None
  821. )
  822. descr = "During handling of the above exception, another exception occurred:"
  823. else:
  824. e = None
  825. repr_chain.reverse()
  826. return ExceptionChainRepr(repr_chain)
  827. @attr.s(eq=False, auto_attribs=True)
  828. class TerminalRepr:
  829. def __str__(self) -> str:
  830. # FYI this is called from pytest-xdist's serialization of exception
  831. # information.
  832. io = StringIO()
  833. tw = TerminalWriter(file=io)
  834. self.toterminal(tw)
  835. return io.getvalue().strip()
  836. def __repr__(self) -> str:
  837. return f"<{self.__class__} instance at {id(self):0x}>"
  838. def toterminal(self, tw: TerminalWriter) -> None:
  839. raise NotImplementedError()
  840. # This class is abstract -- only subclasses are instantiated.
  841. @attr.s(eq=False)
  842. class ExceptionRepr(TerminalRepr):
  843. # Provided by subclasses.
  844. reprcrash: Optional["ReprFileLocation"]
  845. reprtraceback: "ReprTraceback"
  846. def __attrs_post_init__(self) -> None:
  847. self.sections: List[Tuple[str, str, str]] = []
  848. def addsection(self, name: str, content: str, sep: str = "-") -> None:
  849. self.sections.append((name, content, sep))
  850. def toterminal(self, tw: TerminalWriter) -> None:
  851. for name, content, sep in self.sections:
  852. tw.sep(sep, name)
  853. tw.line(content)
  854. @attr.s(eq=False, auto_attribs=True)
  855. class ExceptionChainRepr(ExceptionRepr):
  856. chain: Sequence[Tuple["ReprTraceback", Optional["ReprFileLocation"], Optional[str]]]
  857. def __attrs_post_init__(self) -> None:
  858. super().__attrs_post_init__()
  859. # reprcrash and reprtraceback of the outermost (the newest) exception
  860. # in the chain.
  861. self.reprtraceback = self.chain[-1][0]
  862. self.reprcrash = self.chain[-1][1]
  863. def toterminal(self, tw: TerminalWriter) -> None:
  864. for element in self.chain:
  865. element[0].toterminal(tw)
  866. if element[2] is not None:
  867. tw.line("")
  868. tw.line(element[2], yellow=True)
  869. super().toterminal(tw)
  870. @attr.s(eq=False, auto_attribs=True)
  871. class ReprExceptionInfo(ExceptionRepr):
  872. reprtraceback: "ReprTraceback"
  873. reprcrash: "ReprFileLocation"
  874. def toterminal(self, tw: TerminalWriter) -> None:
  875. self.reprtraceback.toterminal(tw)
  876. super().toterminal(tw)
  877. @attr.s(eq=False, auto_attribs=True)
  878. class ReprTraceback(TerminalRepr):
  879. reprentries: Sequence[Union["ReprEntry", "ReprEntryNative"]]
  880. extraline: Optional[str]
  881. style: "_TracebackStyle"
  882. entrysep: ClassVar = "_ "
  883. def toterminal(self, tw: TerminalWriter) -> None:
  884. # The entries might have different styles.
  885. for i, entry in enumerate(self.reprentries):
  886. if entry.style == "long":
  887. tw.line("")
  888. entry.toterminal(tw)
  889. if i < len(self.reprentries) - 1:
  890. next_entry = self.reprentries[i + 1]
  891. if (
  892. entry.style == "long"
  893. or entry.style == "short"
  894. and next_entry.style == "long"
  895. ):
  896. tw.sep(self.entrysep)
  897. if self.extraline:
  898. tw.line(self.extraline)
  899. class ReprTracebackNative(ReprTraceback):
  900. def __init__(self, tblines: Sequence[str]) -> None:
  901. self.style = "native"
  902. self.reprentries = [ReprEntryNative(tblines)]
  903. self.extraline = None
  904. @attr.s(eq=False, auto_attribs=True)
  905. class ReprEntryNative(TerminalRepr):
  906. lines: Sequence[str]
  907. style: ClassVar["_TracebackStyle"] = "native"
  908. def toterminal(self, tw: TerminalWriter) -> None:
  909. tw.write("".join(self.lines))
  910. @attr.s(eq=False, auto_attribs=True)
  911. class ReprEntry(TerminalRepr):
  912. lines: Sequence[str]
  913. reprfuncargs: Optional["ReprFuncArgs"]
  914. reprlocals: Optional["ReprLocals"]
  915. reprfileloc: Optional["ReprFileLocation"]
  916. style: "_TracebackStyle"
  917. def _write_entry_lines(self, tw: TerminalWriter) -> None:
  918. """Write the source code portions of a list of traceback entries with syntax highlighting.
  919. Usually entries are lines like these:
  920. " x = 1"
  921. "> assert x == 2"
  922. "E assert 1 == 2"
  923. This function takes care of rendering the "source" portions of it (the lines without
  924. the "E" prefix) using syntax highlighting, taking care to not highlighting the ">"
  925. character, as doing so might break line continuations.
  926. """
  927. if not self.lines:
  928. return
  929. # separate indents and source lines that are not failures: we want to
  930. # highlight the code but not the indentation, which may contain markers
  931. # such as "> assert 0"
  932. fail_marker = f"{FormattedExcinfo.fail_marker} "
  933. indent_size = len(fail_marker)
  934. indents: List[str] = []
  935. source_lines: List[str] = []
  936. failure_lines: List[str] = []
  937. for index, line in enumerate(self.lines):
  938. is_failure_line = line.startswith(fail_marker)
  939. if is_failure_line:
  940. # from this point on all lines are considered part of the failure
  941. failure_lines.extend(self.lines[index:])
  942. break
  943. else:
  944. if self.style == "value":
  945. source_lines.append(line)
  946. else:
  947. indents.append(line[:indent_size])
  948. source_lines.append(line[indent_size:])
  949. tw._write_source(source_lines, indents)
  950. # failure lines are always completely red and bold
  951. for line in failure_lines:
  952. tw.line(line, bold=True, red=True)
  953. def toterminal(self, tw: TerminalWriter) -> None:
  954. if self.style == "short":
  955. assert self.reprfileloc is not None
  956. self.reprfileloc.toterminal(tw)
  957. self._write_entry_lines(tw)
  958. if self.reprlocals:
  959. self.reprlocals.toterminal(tw, indent=" " * 8)
  960. return
  961. if self.reprfuncargs:
  962. self.reprfuncargs.toterminal(tw)
  963. self._write_entry_lines(tw)
  964. if self.reprlocals:
  965. tw.line("")
  966. self.reprlocals.toterminal(tw)
  967. if self.reprfileloc:
  968. if self.lines:
  969. tw.line("")
  970. self.reprfileloc.toterminal(tw)
  971. def __str__(self) -> str:
  972. return "{}\n{}\n{}".format(
  973. "\n".join(self.lines), self.reprlocals, self.reprfileloc
  974. )
  975. @attr.s(eq=False, auto_attribs=True)
  976. class ReprFileLocation(TerminalRepr):
  977. path: str = attr.ib(converter=str)
  978. lineno: int
  979. message: str
  980. def toterminal(self, tw: TerminalWriter) -> None:
  981. # Filename and lineno output for each entry, using an output format
  982. # that most editors understand.
  983. msg = self.message
  984. i = msg.find("\n")
  985. if i != -1:
  986. msg = msg[:i]
  987. tw.write(self.path, bold=True, red=True)
  988. tw.line(f":{self.lineno}: {msg}")
  989. @attr.s(eq=False, auto_attribs=True)
  990. class ReprLocals(TerminalRepr):
  991. lines: Sequence[str]
  992. def toterminal(self, tw: TerminalWriter, indent="") -> None:
  993. for line in self.lines:
  994. tw.line(indent + line)
  995. @attr.s(eq=False, auto_attribs=True)
  996. class ReprFuncArgs(TerminalRepr):
  997. args: Sequence[Tuple[str, object]]
  998. def toterminal(self, tw: TerminalWriter) -> None:
  999. if self.args:
  1000. linesofar = ""
  1001. for name, value in self.args:
  1002. ns = f"{name} = {value}"
  1003. if len(ns) + len(linesofar) + 2 > tw.fullwidth:
  1004. if linesofar:
  1005. tw.line(linesofar)
  1006. linesofar = ns
  1007. else:
  1008. if linesofar:
  1009. linesofar += ", " + ns
  1010. else:
  1011. linesofar = ns
  1012. if linesofar:
  1013. tw.line(linesofar)
  1014. tw.line("")
  1015. def getfslineno(obj: object) -> Tuple[Union[str, Path], int]:
  1016. """Return source location (path, lineno) for the given object.
  1017. If the source cannot be determined return ("", -1).
  1018. The line number is 0-based.
  1019. """
  1020. # xxx let decorators etc specify a sane ordering
  1021. # NOTE: this used to be done in _pytest.compat.getfslineno, initially added
  1022. # in 6ec13a2b9. It ("place_as") appears to be something very custom.
  1023. obj = get_real_func(obj)
  1024. if hasattr(obj, "place_as"):
  1025. obj = obj.place_as # type: ignore[attr-defined]
  1026. try:
  1027. code = Code.from_function(obj)
  1028. except TypeError:
  1029. try:
  1030. fn = inspect.getsourcefile(obj) or inspect.getfile(obj) # type: ignore[arg-type]
  1031. except TypeError:
  1032. return "", -1
  1033. fspath = fn and absolutepath(fn) or ""
  1034. lineno = -1
  1035. if fspath:
  1036. try:
  1037. _, lineno = findsource(obj)
  1038. except OSError:
  1039. pass
  1040. return fspath, lineno
  1041. return code.path, code.firstlineno
  1042. # Relative paths that we use to filter traceback entries from appearing to the user;
  1043. # see filter_traceback.
  1044. # note: if we need to add more paths than what we have now we should probably use a list
  1045. # for better maintenance.
  1046. _PLUGGY_DIR = Path(pluggy.__file__.rstrip("oc"))
  1047. # pluggy is either a package or a single module depending on the version
  1048. if _PLUGGY_DIR.name == "__init__.py":
  1049. _PLUGGY_DIR = _PLUGGY_DIR.parent
  1050. _PYTEST_DIR = Path(_pytest.__file__).parent
  1051. def filter_traceback(entry: TracebackEntry) -> bool:
  1052. """Return True if a TracebackEntry instance should be included in tracebacks.
  1053. We hide traceback entries of:
  1054. * dynamically generated code (no code to show up for it);
  1055. * internal traceback from pytest or its internal libraries, py and pluggy.
  1056. """
  1057. # entry.path might sometimes return a str object when the entry
  1058. # points to dynamically generated code.
  1059. # See https://bitbucket.org/pytest-dev/py/issues/71.
  1060. raw_filename = entry.frame.code.raw.co_filename
  1061. is_generated = "<" in raw_filename and ">" in raw_filename
  1062. if is_generated:
  1063. return False
  1064. # entry.path might point to a non-existing file, in which case it will
  1065. # also return a str object. See #1133.
  1066. p = Path(entry.path)
  1067. parents = p.parents
  1068. if _PLUGGY_DIR in parents:
  1069. return False
  1070. if _PYTEST_DIR in parents:
  1071. return False
  1072. return True