_pytest._code.code module

class Code(rawcode)[source]

Bases: object

wrapper around Python code objects

path

return a path object pointing to source code (or a str in case of OSError / non-existing file).

fullsource

return a _pytest._code.Source object for the full source file of the code

source() → Source[source]

return a _pytest._code.Source object for the code object’s source only

getargs(var: bool = False) → Tuple[str, …][source]

return a tuple with the argument names for the code object

if ‘var’ is set True also return the names of the variable and keyword arguments when present

class Frame(frame: frame)[source]

Bases: object

Wrapper around a Python frame holding f_locals and f_globals in which expressions can be evaluated.

statement

statement this frame is at

eval(code, **vars)[source]

evaluate ‘code’ in the frame

‘vars’ are optional additional local variables

returns the result of the evaluation

exec_(code, **vars)None[source]

exec ‘code’ in the frame

‘vars’ are optional; additional local variables

repr(object: object)str[source]

return a ‘safe’ (non-recursive, one-line) string repr for ‘object’

is_true(object)[source]
getargs(var: bool = False)[source]

return a list of tuples (name, value) for all arguments

if ‘var’ is set True also include the variable and keyword arguments when present

class TracebackEntry(rawentry: traceback, excinfo=None)[source]

Bases: object

a single entry in a traceback

_repr_style: Optional[Literal[“short”, “long”]] = None
exprinfo = None
set_repr_style(mode: Literal[‘short’, ‘long’])None[source]
frame
relline
statement

_pytest._code.Source object for the current statement

path

path to the source code

locals

locals of underlying frame

getfirstlinesource()int[source]
getsource(astcache=None) → Optional[Source][source]

return failing source code.

source

return failing source code.

ishidden()[source]

return True if the current frame has a var __tracebackhide__ resolving to True.

If __tracebackhide__ is a callable, it gets called with the ExceptionInfo instance and can decide whether to hide the traceback.

mostly for internal use

name

co_name of underlying code

class Traceback(tb: Union[traceback, Iterable[_pytest._code.code.TracebackEntry]], excinfo: Optional[ReferenceType[ExceptionInfo]] = None)[source]

Bases: list, typing.Generic

Traceback objects encapsulate and offer higher level access to Traceback entries.

cut(path=None, lineno: Optional[int] = None, firstlineno: Optional[int] = None, excludepath=None)_pytest._code.code.Traceback[source]

return a Traceback instance wrapping part of this Traceback

by providing any combination of path, lineno and firstlineno, the first frame to start the to-be-returned traceback is determined

this allows cutting the first part of a Traceback instance e.g. for formatting reasons (removing some uninteresting bits that deal with handling of the exception/traceback)

filter(fn: Callable[[_pytest._code.code.TracebackEntry], bool] = <function Traceback.<lambda>>)_pytest._code.code.Traceback[source]

return a Traceback instance with certain items removed

fn is a function that gets a single argument, a TracebackEntry instance, and should return True when the item should be added to the Traceback, False when not

by default this removes all the TracebackEntries which are hidden (see ishidden() above)

getcrashentry()_pytest._code.code.TracebackEntry[source]

return last non-hidden traceback entry that lead to the exception of a traceback.

recursionindex() → Optional[int][source]

return the index of the frame/TracebackEntry where recursion originates if appropriate, None if no recursion occurred

class ExceptionInfo(*args, **kwds)[source]

Bases: typing.Generic

wraps sys.exc_info() objects and offers help for navigating the traceback.

_assert_start_repr = "AssertionError('assert "
classmethod from_exc_info(exc_info: Tuple[Type[_E], _E, traceback], exprinfo: Optional[str] = None) → ExceptionInfo[_E][source]

returns an ExceptionInfo for an existing exc_info tuple.

Warning

Experimental API

Parameters

exprinfo – a text string helping to determine if we should strip AssertionError from the output, defaults to the exception message/__str__()

classmethod from_current(exprinfo: Optional[str] = None)_pytest._code.code.ExceptionInfo[BaseException][source]

returns an ExceptionInfo matching the current traceback

Warning

Experimental API

Parameters

exprinfo – a text string helping to determine if we should strip AssertionError from the output, defaults to the exception message/__str__()

classmethod for_later()_pytest._code.code.ExceptionInfo[_E][source]

return an unfilled ExceptionInfo

fill_unfilled(exc_info: Tuple[Type[_E], _E, traceback])None[source]

fill an unfilled ExceptionInfo created with for_later()

type

the exception class

value

the exception value

tb

the exception raw traceback

typename

the type name of the exception

traceback

the traceback

exconly(tryshort: bool = False)str[source]

return the exception as a string

when ‘tryshort’ resolves to True, and the exception is a _pytest._code._AssertionError, only the actual exception part of the exception representation is returned (so ‘AssertionError: ‘ is removed from the beginning)

errisinstance(exc: Union[Type[BaseException], Tuple[Type[BaseException], …]])bool[source]

return True if the exception is an instance of exc

_getreprcrash()_pytest._code.code.ReprFileLocation[source]
getrepr(showlocals: bool = False, style: _TracebackStyle = 'long', abspath: bool = False, tbfilter: bool = True, funcargs: bool = False, truncate_locals: bool = True, chain: bool = True) → Union[ReprExceptionInfo, ExceptionChainRepr][source]

Return str()able representation of this exception info.

Parameters
  • showlocals (bool) – Show locals per traceback entry. Ignored if style=="native".

  • style (str) – long|short|no|native traceback style

  • abspath (bool) – If paths should be changed to absolute or left unchanged.

  • tbfilter (bool) – Hide entries that contain a local variable __tracebackhide__==True. Ignored if style=="native".

  • funcargs (bool) – Show fixtures (“funcargs” for legacy purposes) per traceback entry.

  • truncate_locals (bool) – With showlocals==True, make sure locals can be safely represented as strings.

  • chain (bool) – if chained exceptions in Python 3 should be shown.

Changed in version 3.9: Added the chain parameter.

match(regexp: Union[str, Pattern]) → Literal[True][source]

Check whether the regular expression regexp matches the string representation of the exception using re.search(). If it matches True is returned. If it doesn’t match an AssertionError is raised.

class FormattedExcinfo(showlocals: bool = False, style: _TracebackStyle = 'long', abspath: bool = True, tbfilter: bool = True, funcargs: bool = False, truncate_locals: bool = True, chain: bool = True)[source]

Bases: object

presenting information about failing Functions and Generators.

flow_marker = '>'
fail_marker = 'E'
_getindent(source: Source)int[source]
_getentrysource(entry: _pytest._code.code.TracebackEntry) → Optional[Source][source]
repr_args(entry: _pytest._code.code.TracebackEntry) → Optional[_pytest._code.code.ReprFuncArgs][source]
get_source(source: Source, line_index: int = - 1, excinfo: Optional[_pytest._code.code.ExceptionInfo] = None, short: bool = False) → List[str][source]

return formatted and marked up source lines.

get_exconly(excinfo: _pytest._code.code.ExceptionInfo, indent: int = 4, markall: bool = False) → List[str][source]
repr_locals(locals: Dict[str, object]) → Optional[_pytest._code.code.ReprLocals][source]
repr_traceback_entry(entry: _pytest._code.code.TracebackEntry, excinfo: Optional[_pytest._code.code.ExceptionInfo] = None)_pytest._code.code.ReprEntry[source]
_makepath(path)[source]
repr_traceback(excinfo: _pytest._code.code.ExceptionInfo)_pytest._code.code.ReprTraceback[source]
_truncate_recursive_traceback(traceback: _pytest._code.code.Traceback) → Tuple[_pytest._code.code.Traceback, Optional[str]][source]

Truncate the given recursive traceback trying to find the starting point of the recursion.

The detection is done by going through each traceback entry and finding the point in which the locals of the frame are equal to the locals of a previous frame (see recursionindex().

Handle the situation where the recursion process might raise an exception (for example comparing numpy arrays using equality raises a TypeError), in which case we do our best to warn the user of the error and show a limited traceback.

repr_excinfo(excinfo: _pytest._code.code.ExceptionInfo)_pytest._code.code.ExceptionChainRepr[source]
class TerminalRepr[source]

Bases: object

toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ExceptionRepr[source]

Bases: _pytest._code.code.TerminalRepr

addsection(name: str, content: str, sep: str = '-')None[source]
toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ExceptionChainRepr(chain: Sequence[Tuple[ReprTraceback, Optional[ReprFileLocation], Optional[str]]])[source]

Bases: _pytest._code.code.ExceptionRepr

toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ReprExceptionInfo(reprtraceback: _pytest._code.code.ReprTraceback, reprcrash: _pytest._code.code.ReprFileLocation)[source]

Bases: _pytest._code.code.ExceptionRepr

toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ReprTraceback(reprentries: Sequence[Union[ReprEntry, ReprEntryNative]], extraline: Optional[str], style: _TracebackStyle)[source]

Bases: _pytest._code.code.TerminalRepr

entrysep = '_ '
toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ReprTracebackNative(tblines: Sequence[str])[source]

Bases: _pytest._code.code.ReprTraceback

class ReprEntryNative(tblines: Sequence[str])[source]

Bases: _pytest._code.code.TerminalRepr

style: _TracebackStyle = 'native'
toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ReprEntry(lines: Sequence[str], reprfuncargs: Optional[ReprFuncArgs], reprlocals: Optional[ReprLocals], filelocrepr: Optional[ReprFileLocation], style: _TracebackStyle)[source]

Bases: _pytest._code.code.TerminalRepr

staticmethod _color_error_lines(tw: _pytest._io.TerminalWriter, lines: Sequence[str])None[source]
toterminal(tw: _pytest._io.TerminalWriter)None[source]
class ReprFileLocation(path, lineno: int, message: str)[source]

Bases: _pytest._code.code.TerminalRepr

_get_short_msg()str[source]
toterminal(tw: _pytest._io.TerminalWriter, style: _TracebackStyle = None)None[source]
class ReprLocals(lines: Sequence[str])[source]

Bases: _pytest._code.code.TerminalRepr

toterminal(tw: _pytest._io.TerminalWriter, indent='')None[source]
class ReprFuncArgs(args: Sequence[Tuple[str, object]])[source]

Bases: _pytest._code.code.TerminalRepr

toterminal(tw: _pytest._io.TerminalWriter)None[source]
getrawcode(obj, trycall: bool = True)[source]

return code object for given function.

filter_traceback(entry: _pytest._code.code.TracebackEntry)bool[source]

Return True if a TracebackEntry instance should be removed from tracebacks: * dynamically generated code (no code to show up for it); * internal traceback from pytest or its internal libraries, py and pluggy.