I used the dis ' module to overwrite the compiled script (.pyc). I understand the difference between JUMP_FORWARD and JUMP_ABSOLUTE. As far as I know, the IF statement will be closed by JUMP_FORWARD:
>>> def f(): if a: print '' >>> from dis import dis >>> dis(f) 2 0 LOAD_GLOBAL 0 (a) 3 JUMP_IF_FALSE 9 (to 15) 6 POP_TOP 3 7 LOAD_CONST 1 ('') 10 PRINT_ITEM 11 PRINT_NEWLINE 12 JUMP_FORWARD 1 (to 16) >> 15 POP_TOP >> 16 LOAD_CONST 0 (None) 19 RETURN_VALUE
And JUMP_ABSOLUTE will appear if the IF statement is at the end of another loop. For example:
>>> def f1(): if a: if b: print '' >>> dis(f1) 2 0 LOAD_GLOBAL 0 (a) 3 JUMP_IF_FALSE 20 (to 26) 6 POP_TOP 3 7 LOAD_GLOBAL 1 (b) 10 JUMP_IF_FALSE 9 (to 22) 13 POP_TOP 4 14 LOAD_CONST 1 ('') 17 PRINT_ITEM 18 PRINT_NEWLINE 19 JUMP_ABSOLUTE 27 >> 22 POP_TOP 23 JUMP_FORWARD 1 (to 27) >> 26 POP_TOP >> 27 LOAD_CONST 0 (None) 30 RETURN_VALUE
From the Bytecode I'm reading for writing code, there is JUMP_ABSOLUTE that surprises me:
121 228 LOAD_FAST 11 (a) 231 LOAD_CONST 9 (100) 234 COMPARE_OP 0 (<) 237 JUMP_IF_FALSE 23 (to 263) 240 POP_TOP 241 LOAD_FAST 11 (b) 244 LOAD_CONST 11 (10) 247 COMPARE_OP 4 (>) 250 JUMP_IF_FALSE 10 (to 263) 253 POP_TOP 122 254 LOAD_CONST 3 (1) 257 STORE_FAST 4 (ok) 260 JUMP_ABSOLUTE 27 >> 263 POP_TOP
I think the code is as follows:
if a<100 and b>10: ok=1
but it calls JUMP_FORWARD, not JUMP_ABSOLUTE. I know this is not a WHILE loop, not a FOR statement, because they both create a SETUP_LOOP string in Bytecode.
My question is: what am I missing? Why do I get FORWARD instead of ABSOLUTE transition?
EDIT: An absolute jump to index 27 indicates the beginning of a loop (WHILE?), In which the two lines 121 and 122 belong:
106 24 SETUP_LOOP 297 (to 324) >> 27 LOAD_FAST 4 (ok) 30 LOAD_CONST 1 (0) 33 COMPARE_OP 2 (==) 36 JUMP_IF_FALSE 283 (to 322) 39 POP_TOP
Before this line there is an IF-operator and one more. Here is the code before, with the same closure of the JUMP_ABSOLUTE statement.
115 170 LOAD_FAST 3 (q) 173 LOAD_CONST 10 (1) 176 COMPARE_OP 0 (<) 179 JUMP_IF_FALSE 45 (to 227) 182 POP_TOP 183 LOAD_FAST 11 (z) 186 LOAD_CONST 11 (10) 189 COMPARE_OP 4 (>) 192 JUMP_IF_FALSE 32 (to 227) 195 POP_TOP 116 196 LOAD_CONST 1 (0) 199 STORE_FAST 4 (ok) 117 202 LOAD_FAST 5 (u) 205 LOAD_CONST 3 (1) 208 BINARY_ADD 209 STORE_FAST 5 (u) 118 212 LOAD_CONST 1 (0) 215 STORE_FAST 3 (k) 119 218 LOAD_CONST 3 (10) 221 STORE_FAST 6 (dv) 224 JUMP_ABSOLUTE 27 >> 227 POP_TOP
JUMP_FORWARD says go to the next line, and JUMP_ABSOLUTE says go back to the beginning of the WHILE loop. The problem is that I donβt know how to replicate the code that will give the same bytecode as above.
Thanks!