_pytest.compat module¶
python version compatibility code
-
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).
-
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.
-
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 toholder
if the original object was a bound method.
-
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)