How do I see the code emitted by decorators bytecode, native or viper?
In "normal" python I can see the bytecode with foo.func_code.co_code, or import dis and use dis.dis(foo).
I would like to see the dis-like output that D. George shows on https://gotocon.com/dl/goto-amsterdam-2 ... Things.pdf
How do I do that?
see code emitted by bytecode, native, viper decorators?
Re: see code emitted by bytecode, native, viper decorators?
If you add this line to ports/stm32/mpconfigport.h: and then edit py/emitglue.c and change the initial value of mp_verbose_flag to 2: https://github.com/micropython/micropyt ... glue.c#L49
Then when code is compiled it will show the bytecode:
The first bit of bytecode is the REPL storing the function foo and the second bit is the foo function itself.
Code: Select all
#define MICROPY_DEBUG_PRINTERS (1)
Then when code is compiled it will show the bytecode:
Code: Select all
>>> def foo():
... print('test')
...
File <stdin>, code block '<module>' (descriptor: 20003080, bytecode @20003260 21 bytes)
Raw bytecode (code_info_size=7, bytecode_size=14):
01 00 00 00 00 00 07 46 00 4d 00 00 00 ff 60 00
24 8d 05 11 5b
arg names:
(N_STATE 1)
(N_EXC_STACK 0)
bc=-1 line=1
00 MAKE_FUNCTION 20003090
02 STORE_NAME foo
05 LOAD_CONST_NONE
06 RETURN_VALUE
File <stdin>, code block 'foo' (descriptor: 20003090, bytecode @20003280 26 bytes)
Raw bytecode (code_info_size=8, bytecode_size=18):
02 00 00 00 00 00 08 8d 05 4d 00 21 00 00 ff 1c
04 04 16 8e 05 64 01 32 11 5b
arg names:
(N_STATE 2)
(N_EXC_STACK 0)
bc=-1 line=1
bc=0 line=2
00 LOAD_GLOBAL print
03 LOAD_CONST_STRING 'test'
06 CALL_FUNCTION n=1 nkw=0
08 POP_TOP
09 LOAD_CONST_NONE
10 RETURN_VALUE
>>>
Re: see code emitted by bytecode, native, viper decorators?
I'm not sure how you see native or viper code.
Re: see code emitted by bytecode, native, viper decorators?
I found this topic after I watched https://www.youtube.com/watch?v=hHec4qL00x0, played around with "dis" in cpython and thought "hey that's quite cool"
Any plans on adding a real "dis"-like module? I don't think it's a long way from the current "mp_bytecode_print" function.
Any plans on adding a real "dis"-like module? I don't think it's a long way from the current "mp_bytecode_print" function.
Re: see code emitted by bytecode, native, viper decorators?
Not what you're looking for but a very cool feature that was added recently is sys.settrace. You can use it to extract the bytecode from a function. Disassembly still left as an exercise for the reader, but could potentially be done in a separate tool. Unfortunately doesn't work for native/viper/asm though.
prints
Code: Select all
import sys
def foo(a, b):
x = a + b
return x
def trace(frame, event, arg):
print(event, frame.f_code.co_name, frame.f_code.co_code)
sys.settrace(trace)
foo(1, 2)
sys.settrace(None)
Code: Select all
call foo b'\xb0\xb1\xf2\xc2\xb2c'