Created
June 11, 2019 08:55
-
-
Save laike9m/6527fa614c208a3686a46f3dff87c61e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Tests how EXTENDED_ARG works | |
import bytecode | |
import dis | |
import sys | |
from bytecode import ConcreteInstr, ConcreteBytecode | |
CONST_ARG = 0x1234567 # The real argument we want to set. | |
cbc = bytecode.ConcreteBytecode() | |
cbc.consts = [None] * (CONST_ARG + 1) # Make sure co_consts is big enough. | |
cbc.consts[CONST_ARG] = "foo" # Sets co_consts manually. | |
if sys.version_info >= (3, 6): | |
cbc.extend( | |
[ | |
ConcreteInstr("EXTENDED_ARG", 0x1), | |
ConcreteInstr("EXTENDED_ARG", 0x23), | |
ConcreteInstr("EXTENDED_ARG", 0x45), | |
ConcreteInstr("LOAD_CONST", 0x67), | |
ConcreteInstr("RETURN_VALUE"), | |
] | |
) | |
else: | |
cbc.extend( | |
[ | |
ConcreteInstr("EXTENDED_ARG", 0x123), | |
ConcreteInstr("LOAD_CONST", 0x4567), | |
ConcreteInstr("RETURN_VALUE"), | |
] | |
) | |
code = cbc.to_code() | |
dis.dis(code) | |
ext0, ext1, ext2, load_const = list(dis.get_instructions(code))[:4] | |
print("ext0 arg is: ", ext0.arg) | |
print("ext1 arg is: ", ext1.arg) | |
print("ext2 arg is: ", ext2.arg) | |
print("ext2 arg is: ", load_const.arg, ", value is:", load_const.argval) | |
for raw_byte in code.co_code: | |
print("raw code is: ", raw_byte) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment