Kaydet (Commit) 9bd39fca authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

Inital

üst
# Bytecode Tools
Utilities i use for hacking (c)python bytecode.
import dis
from io import StringIO
from types import CodeType
CODE_FIELDS = (
"argcount",
"kwonlyargcount",
"nlocals",
"stacksize",
"flags",
"code",
"consts",
"names",
"varnames",
"filename",
"name",
"firstlineno",
"lnotab",
"freevars",
"cellvars",
)
def assemble(*instrs):
buf = StringIO()
for op, arg in instrs:
buf.write(chr(dis.opmap[op]) + chr(arg or 0))
return bytes(ord(char) for char in buf.getvalue())
def reassemble_code(code, **options):
fields = {field: getattr(code, f"co_{field}") for field in CODE_FIELDS}
fields.update(options)
code = CodeType(**fields)
return func
def mutate_func(func, **options):
func.__code__ = reassemble_code(func.__code__, **options)
return func
def sb_exec(code, *args, **kwargs):
fn = (lambda *args, **kwargs: None)
fn.__code__ = code
return fn(*args, **kwargs)
def add_to(tup, *items):
pos = []
for item in items:
if item not in tup:
tup = *tup, item
pos.append(tup.index(item))
return (*tup, *pos)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment