def _inject_context_manager_func(func, start_line, end_line, name):
    """
    injects a context manager into the given function

    e.g given:

        x = 5
        def foo():
            print x
            print '1'
            print '2'
            print '3'
        inject_context_manager_func(foo, 0, 2, 'cm')

    foo will now have the definition:

        def foo():
            with silk_profile('cm'):
                print x
                print '1'
                print '2'
            print '3'

    closures, globals & locals are honoured

    @param func: object of type<function> or type<instancemethod>
    @param start_line: line at which to inject 'with' statement. line num. is relative to the func, not the module.
    @param end_line: line at which to exit the context
    @param name: name of the profiler
    """
    source = _get_source_lines(func)
    start_line += 1
    end_line += 1
    ws = _get_ws(source[start_line])
    for i in range(start_line, end_line):
        try:
            source[i] = '  ' + source[i]
        except IndexError:
            raise IndexError('Function %s does not have line %d' % (func.__name__, i))

    source.insert(start_line, ws + "from silk.profiling.profiler import silk_profile\n")
    source.insert(start_line + 1, ws + name % "with silk_profile('%s', _dynamic=True):\n")
    return _new_func_from_source(source, func)
dataset/ETHPy150Open django-silk/silk/silk/profiling/dynamic.py/_inject_context_manager_func/swapped operands of `%`