Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
new_co_consts = remove_duplicates(func_co_consts + pinned_pre_func_co_consts)
new_co_names = remove_duplicates(func_co_names + pinned_pre_func_co_names)
new_co_varnames = remove_duplicates(func_co_varnames + pinned_pre_func_co_varnames)
trans_co_consts = get_transitions(pinned_pre_func_co_consts, new_co_consts)
trans_co_names = get_transitions(pinned_pre_func_co_names, new_co_names)
trans_co_varnames = get_transitions(pinned_pre_func_co_varnames, new_co_varnames)
transitions = {
**get_b_transitions(trans_co_consts, OpCode.LOAD_CONST, OpCode.LOAD_CONST),
**get_b_transitions(trans_co_names, OpCode.LOAD_GLOBAL, OpCode.LOAD_GLOBAL),
**get_b_transitions(trans_co_names, OpCode.LOAD_METHOD, OpCode.LOAD_METHOD),
**get_b_transitions(trans_co_names, OpCode.LOAD_ATTR, OpCode.LOAD_ATTR),
**get_b_transitions(trans_co_names, OpCode.STORE_ATTR, OpCode.STORE_ATTR),
**get_b_transitions(trans_co_varnames, OpCode.LOAD_FAST, OpCode.LOAD_FAST),
**get_b_transitions(trans_co_varnames, OpCode.STORE_FAST, OpCode.STORE_FAST),
}
import ipdb
ipdb.set_trace()
pinned_pre_func_co_code = multiple_replace(pinned_pre_func_co_code, transitions)
pinned_prefunc_co_code_without_return = pinned_pre_func_co_code[
: len(prefunc_co_code_without_return)
]
new_co_code = pinned_prefunc_co_code_without_return + func_co_code
nfcode = new_func.__code__
JUMP_IF_TRUE_OR_POP, POP_JUMP_IF_FALSE & POP_JUMP_IF_TRUE by qty
"""
new_instructions_tuples = tuple(
bytes(
(
operation,
*tuple(
int.to_bytes(
int.from_bytes(bytes(value), "little") + qty, 2, "little"
)
),
)
)
if bytes((operation,))
in (
OpCode.JUMP_ABSOLUTE,
OpCode.JUMP_IF_FALSE_OR_POP,
OpCode.JUMP_IF_TRUE_OR_POP,
OpCode.POP_JUMP_IF_FALSE,
OpCode.POP_JUMP_IF_TRUE,
)
else bytes((operation, *value))
for operation, *value in get_instructions_tuples(func)
)
return bytes(tuple(chain.from_iterable(new_instructions_tuples)))
(
operation,
*tuple(
int.to_bytes(
int.from_bytes(bytes(value), "little") + qty, 2, "little"
)
),
)
)
if bytes((operation,))
in (
OpCode.JUMP_ABSOLUTE,
OpCode.JUMP_IF_FALSE_OR_POP,
OpCode.JUMP_IF_TRUE_OR_POP,
OpCode.POP_JUMP_IF_FALSE,
OpCode.POP_JUMP_IF_TRUE,
)
else bytes((operation, *value))
for operation, *value in get_instructions_tuples(func)
)
return bytes(tuple(chain.from_iterable(new_instructions_tuples)))
bytes(
(
operation,
*tuple(
int.to_bytes(
int.from_bytes(bytes(value), "little") + qty, 2, "little"
)
),
)
)
if bytes((operation,))
in (
OpCode.JUMP_ABSOLUTE,
OpCode.JUMP_IF_FALSE_OR_POP,
OpCode.JUMP_IF_TRUE_OR_POP,
OpCode.POP_JUMP_IF_FALSE,
OpCode.POP_JUMP_IF_TRUE,
)
else bytes((operation, *value))
for operation, *value in get_instructions_tuples(func)
)
return bytes(tuple(chain.from_iterable(new_instructions_tuples)))
new_instructions_tuples = tuple(
bytes(
(
operation,
*tuple(
int.to_bytes(
int.from_bytes(bytes(value), "little") + qty, 2, "little"
)
),
)
)
if bytes((operation,))
in (
OpCode.JUMP_ABSOLUTE,
OpCode.JUMP_IF_FALSE_OR_POP,
OpCode.JUMP_IF_TRUE_OR_POP,
OpCode.POP_JUMP_IF_FALSE,
OpCode.POP_JUMP_IF_TRUE,
)
else bytes((operation, *value))
for operation, *value in get_instructions_tuples(func)
)
return bytes(tuple(chain.from_iterable(new_instructions_tuples)))
def are_functions_equivalent(l_func, r_func):
"""Return True if `l_func` and `r_func` are equivalent"""
l_code, r_code = l_func.__code__, r_func.__code__
trans_co_consts = get_transitions(l_code.co_consts, r_code.co_consts)
trans_co_names = get_transitions(l_code.co_names, r_code.co_names)
trans_co_varnames = get_transitions(l_code.co_varnames, r_code.co_varnames)
transitions = {
**get_b_transitions(trans_co_consts, OpCode.LOAD_CONST, OpCode.LOAD_CONST),
**get_b_transitions(trans_co_names, OpCode.LOAD_GLOBAL, OpCode.LOAD_GLOBAL),
**get_b_transitions(trans_co_names, OpCode.LOAD_METHOD, OpCode.LOAD_METHOD),
**get_b_transitions(trans_co_names, OpCode.LOAD_ATTR, OpCode.LOAD_ATTR),
**get_b_transitions(trans_co_names, OpCode.STORE_ATTR, OpCode.STORE_ATTR),
**get_b_transitions(trans_co_varnames, OpCode.LOAD_FAST, OpCode.LOAD_FAST),
**get_b_transitions(trans_co_varnames, OpCode.STORE_FAST, OpCode.STORE_FAST),
}
new_l_co_code = multiple_replace(l_code.co_code, transitions)
co_code_cond = new_l_co_code == r_code.co_code
co_consts_cond = set(l_code.co_consts) == set(r_code.co_consts)
co_names_cond = set(l_code.co_names) == set(l_code.co_names)
co_varnames_cond = set(l_code.co_varnames) == set(l_code.co_varnames)
return co_code_cond and co_consts_cond and co_names_cond and co_varnames_cond
def are_functions_equivalent(l_func, r_func):
"""Return True if `l_func` and `r_func` are equivalent"""
l_code, r_code = l_func.__code__, r_func.__code__
trans_co_consts = get_transitions(l_code.co_consts, r_code.co_consts)
trans_co_names = get_transitions(l_code.co_names, r_code.co_names)
trans_co_varnames = get_transitions(l_code.co_varnames, r_code.co_varnames)
transitions = {
**get_b_transitions(trans_co_consts, OpCode.LOAD_CONST, OpCode.LOAD_CONST),
**get_b_transitions(trans_co_names, OpCode.LOAD_GLOBAL, OpCode.LOAD_GLOBAL),
**get_b_transitions(trans_co_names, OpCode.LOAD_METHOD, OpCode.LOAD_METHOD),
**get_b_transitions(trans_co_names, OpCode.LOAD_ATTR, OpCode.LOAD_ATTR),
**get_b_transitions(trans_co_names, OpCode.STORE_ATTR, OpCode.STORE_ATTR),
**get_b_transitions(trans_co_varnames, OpCode.LOAD_FAST, OpCode.LOAD_FAST),
**get_b_transitions(trans_co_varnames, OpCode.STORE_FAST, OpCode.STORE_FAST),
}
new_l_co_code = multiple_replace(l_code.co_code, transitions)
co_code_cond = new_l_co_code == r_code.co_code
co_consts_cond = set(l_code.co_consts) == set(r_code.co_consts)
co_names_cond = set(l_code.co_names) == set(l_code.co_names)
co_varnames_cond = set(l_code.co_varnames) == set(l_code.co_varnames)
return co_code_cond and co_consts_cond and co_names_cond and co_varnames_cond