Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko mitsuhiko/foo.py

Created Jul 13, 2018
Embed
What would you like to do?
def make_failing_function(exception, function_name, filename):
code = compile('def f(): raise exception', filename, 'exec')
namespace = {'exception': exception}
eval(code, namespace)
code = namespace['f'].__code__
CodeType = type(code)
namespace['f'].__code__ = CodeType(
code.co_argcount,
code.co_nlocals,
code.co_stacksize,
code.co_flags,
code.co_code,
code.co_consts,
code.co_names,
code.co_varnames,
code.co_filename,
'my::weird::function::name',
code.co_firstlineno,
code.co_lnotab,
code.co_freevars,
code.co_cellvars
)
return namespace['f']
func = make_failing_function(ZeroDivisionError(), 'my::weird::function', 'test.rs')
func()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.