Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2019 08:55
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
# 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):
ConcreteInstr("EXTENDED_ARG", 0x1),
ConcreteInstr("EXTENDED_ARG", 0x23),
ConcreteInstr("EXTENDED_ARG", 0x45),
ConcreteInstr("LOAD_CONST", 0x67),
ConcreteInstr("EXTENDED_ARG", 0x123),
ConcreteInstr("LOAD_CONST", 0x4567),
code = cbc.to_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