File: contextmanagers.py

package info (click to toggle)
python-kgb 7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 532 kB
  • sloc: python: 4,466; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,263 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""Standalone context managers for working with spies."""

from __future__ import unicode_literals

from contextlib import contextmanager

from kgb.agency import SpyAgency


@contextmanager
def spy_on(*args, **kwargs):
    """Spy on a function.

    By default, the spy will allow the call to go through to the original
    function. This can be disabled by passing ``call_original=False`` when
    initiating the spy. If disabled, the original function will never be
    called.

    This can also be passed a ``call_fake`` parameter pointing to another
    function to call instead of the original. If passed, this will take
    precedence over ``call_original``.

    The spy will only remain throughout the duration of the context.

    See :py:class:`~kgb.spies.FunctionSpy` for more details on arguments.

    Args:
        *args (tuple):
            Positional arguments to pass to
            :py:class:`~kgb.spies.FunctionSpy`.

        **kwargs (dict):
            Keyword arguments to pass to
            :py:class:`~kgb.spies.FunctionSpy`.

    Context:
        kgb.spies.FunctionSpy:
        The newly-created spy.
    """
    agency = SpyAgency()
    spy = agency.spy_on(*args, **kwargs)

    try:
        yield spy
    finally:
        spy.unspy()