Skip to content

Instantly share code, notes, and snippets.

@akellehe
Last active September 18, 2017 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akellehe/112a43d9b5afc104723c15f964673fe7 to your computer and use it in GitHub Desktop.
Save akellehe/112a43d9b5afc104723c15f964673fe7 to your computer and use it in GitHub Desktop.
Example of for(;;) entered multiple times, nested code blocks, and nested frames.
Line # Opcode Oparg
-----------------------------------------------------------------
1 0 LOAD_CONST 0 (<code object foo>)
2 LOAD_CONST 1 ('foo')
4 MAKE_FUNCTION 0
6 STORE_NAME 0 (foo)
7 8 LOAD_CONST 2 (<code object a_minus_b>)
10 LOAD_CONST 3 ('a_minus_b')
12 MAKE_FUNCTION 0
14 STORE_NAME 1 (a_minus_b)
10 16 LOAD_NAME 0 (foo)
18 LOAD_CONST 4 (1)
20 LOAD_CONST 5 (2)
22 CALL_FUNCTION 2
24 POP_TOP
26 LOAD_CONST 6 (None)
28 RETURN_VALUE
Disassembly of <code object foo>:
Line # Opcode Oparg
-----------------------------------------------------------------
2 0 SETUP_FINALLY 10 (to 12)
3 2 LOAD_GLOBAL 0 (a_minus_b)
4 LOAD_FAST 0 (a)
6 LOAD_FAST 1 (b)
8 CALL_FUNCTION 2
10 RETURN_VALUE
5 >> 12 END_FINALLY /* NOTE: >> Is a jump target marker! */
14 LOAD_CONST 0 (None)
16 RETURN_VALUE
Disassembly of <code object a_minus_b at 0x10d277660, file "<dis>", line 7>:
8 0 LOAD_FAST 0 (a)
2 LOAD_FAST 1 (b)
4 BINARY_SUBTRACT
6 RETURN_VALUE
def foo(a, b):
try:
return a_minus_b(a, b)
finally:
pass
def a_minus_b(a, b):
return a - b
foo(1, 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment