def pkfail(fmt, *args, **kwargs): """Format message and raise AssertionError. Args: fmt (str): to be passed to `string.format` args (tuple): passed to format kwargs (dict): passed to format """ msg = fmt.format(*args, **kwargs) call = pkinspect.caller(ignore_modules=[contextlib]) raise AssertionError('{} {}'.format(call, msg))
def test_caller(): import inspect m1 = pkunit.import_module_from_data_dir('p1.m1') c = m1.caller() expect = inspect.currentframe().f_lineno - 1 assert expect == c.lineno, \ '{}: unexpected lineno, should be {}'.format(c.lineno, expect) expect = 'test_caller' assert expect == c.name, \ '{}: expected function name {}'.format(c.name, expect) this_module = sys.modules[__name__] c = m1.caller(ignore_modules=[this_module]) assert expect != c.name, \ '{}: should not be {}'.format(c.name, expect) my_caller = pkinspect.caller() expect = my_caller._module assert expect == c._module, \ '{}: should be {}'.format(c._module, expect)
def caller(ignore_modules=None): return pkinspect.caller(ignore_modules=ignore_modules)