Scott Sanderson, Joe Jevnik - Playing with Python Bytecode - PyCon 2016
Why Stack Overflow recommends it
Stack Overflow users link it as a reference when answering questions about inspecting or rewriting Python bytecode and code objects at runtime.
Recommended by 1 people across 1 different months, 2017 to 2020.
What it is
A PyCon 2016 talk by Scott Sanderson and Joe Jevnik that explains how CPython represents code internally and shows how to inspect and modify code objects and bytecode. It walks through practical techniques for rewriting functions at the bytecode level.
What people said
Recommends HTF · 2020-11 · Stack Overflow · on “Python change Exception printable output, eg overload __builtins__”
I'll just explain the behaviour you described: exc.__repr__() This will just call your lambda function and return the expected string. Btw you should return the string, not print it in your lambda functions. print(repr(exc)) Now, this is going a different route in CPython and you can see this in a GDB session, it's something like this: Python/bltinmodule.c:builtin_repr will call Objects/object.c:PyObject_Repr - this function gets the PyObject *v as the only parameter that it will use to get and call a function that implements the built-in function repr() , BaseException_repr in this case. This function will format the error message based on a value from args structure field: (gdb) p ((PyBase
Cites Martijn Pieters · 2017-04 · Stack Overflow · on “Modify function at runtime (pulling local variable out)”
There is a 4th option: replace the print() global: printed = [] print = lambda *args: printed.extend(args) modify() del print modified = printed[0] It is otherwise possible to produce modified bytecode, but this can easily lead to bugs that blow up the interpreter (there is zero protection from invalid bytecode), so be warned. You can create a new function object with a new code object with updated bytecode; based on the offsets in the dis you showed, I manually created new bytecode that would return the local variable at index 0: >>> altered_bytecode = modify.__code__.co_code[:8] + bytes( ... [dis.opmap['LOAD_FAST'], 0, # load local variable 0 onto the stack ... dis.opmap['RETURN_VALUE']]))
Cites Andrew Morozko · 2019-06 · Stack Overflow · on “Using Inspect module to execute specific code”
First of all, you need to know that what you’re doing is bad. Programming languages designed do execute source code, not to limit the execution. Editing live bytecode is evil and will produce undebuggable errors. Disclaimer aside, you can achieve this effect more cleanly by using patch [LINK] from standard lib to replace standard implementations of exit and print , but you need to execute something like from unittest.mock import patch import sys def my_print(*args, **kwargs): # do whatever return def my_exit(*args, **kwargs): # do whatever return def perform_patch(): patchers = [ patch('__main__.print', my_print), patch('sys.exit', my_exit) ] for patcher in patchers: patcher.start() return p