_pytest.compat module

python version compatibility code

_format_args(func: Callable[[…], Any])str[source]
is_generator(func: object)bool[source]
iscoroutinefunction(func: object)bool[source]

Return True if func is a coroutine function (a function defined with async def syntax, and doesn’t contain yield), or a function decorated with @asyncio.coroutine.

Note: copied and modified from Python 3.5’s builtin couroutines.py to avoid importing asyncio directly, which in turns also initializes the “logging” module as a side-effect (see issue #8).

getlocation(function, curdir=None)str[source]
num_mock_patch_args(function)int[source]

return number of arguments used up by mock arguments (if any)

getfuncargnames(function: Callable[[…], Any], *, name: str = '', is_method: bool = False, cls: Optional[type] = None) → Tuple[str, …][source]

Returns the names of a function’s mandatory arguments.

This should return the names of all function arguments that:
  • Aren’t bound to an instance or type as in instance or class methods.

  • Don’t have default values.

  • Aren’t bound with functools.partial.

  • Aren’t replaced with mocks.

The is_method and cls arguments indicate that the function should be treated as a bound method even though it’s not unless, only in the case of cls, the function is a static method.

The name parameter should be the original name in which the function was collected.

get_default_arg_names(function: Callable[[…], Any]) → Tuple[str, …][source]
_translate_non_printable(s: str)str[source]
_bytes_to_ascii(val: bytes)str[source]
ascii_escaped(val: Union[bytes, str])str[source]

If val is pure ascii, returns it as a str(). Otherwise, escapes bytes objects into a sequence of escaped bytes:

b’ôÅÖ’ -> ‘xc3xb4xc5xd6’

and escapes unicode objects into a sequence of escaped unicode ids, e.g.:

‘4nVU00043efax0eMXWBx1eu3028u15fdxcdU0007d944’

note:

the obvious “v.decode(‘unicode-escape’)” will return valid utf-8 unicode if it finds them in bytes, but we want to return escaped bytes for any byte, even if they match a utf-8 string.

class _PytestWrapper(obj)[source]

Bases: object

Dummy wrapper around a function object for internal use only.

Used to correctly unwrap the underlying function object when we are creating fixtures, because we wrap the function object ourselves with a decorator to issue warnings when the fixture function is called directly.

get_real_func(obj)[source]

gets the real function object of the (possibly) wrapped object by functools.wraps or functools.partial.

get_real_method(obj, holder)[source]

Attempts to obtain the real function object that might be wrapping obj, while at the same time returning a bound method to holder if the original object was a bound method.

getimfunc(func)[source]
safe_getattr(object: Any, name: str, default: Any) → Any[source]

Like getattr but return default upon any Exception or any OutcomeException.

Attribute access can potentially fail for ‘evil’ Python objects. See issue #214. It catches OutcomeException because of #2490 (issue #580), new outcomes are derived from BaseException instead of Exception (for more details check #2707)

safe_isclass(obj: object)bool[source]

Ignore any exception via isinstance on Python 3.

_setup_collect_fakemodule() → ModuleType[source]

Setup pytest.collect fake module for backward compatibility.

class CaptureIO[source]

Bases: _io.TextIOWrapper

getvalue()str[source]
class CaptureAndPassthroughIO(other: IO)[source]

Bases: _pytest.compat.CaptureIO

write(s)int[source]

Write string to stream. Returns the number of characters written (which is always equal to the length of the string).