Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_get_b_transitions():
transitions = {1: 3, 2: 5, 3: 6}
byte_source = inliner.OpCode.LOAD_CONST
byte_dest = inliner.OpCode.STORE_FAST
bytes_transitions = inliner.get_b_transitions(transitions, byte_source, byte_dest)
expected = {
(byte_source + b"\x01"): (byte_dest + b"\x03"),
(byte_source + b"\x02"): (byte_dest + b"\x05"),
(byte_source + b"\x03"): (byte_dest + b"\x06"),
}
assert bytes_transitions == expected
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
new_co_consts = remove_duplicates(func_co_consts + tuple(arguments.values()))
new_co_varnames = tuple(item for item in func_co_varnames if item not in arguments)
trans_co_varnames2_co_consts = {
func_co_varnames.index(key): new_co_consts.index(value)
for key, value in arguments.items()
}
trans_co_varnames = get_transitions(func_co_varnames, new_co_varnames)
transitions = {
**get_b_transitions(
trans_co_varnames2_co_consts, OpCode.LOAD_FAST, OpCode.LOAD_CONST
),
**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_co_code = multiple_replace(func_co_code, transitions)
new_func = FunctionType(
func.__code__,
func.__globals__,
func.__name__,
func.__defaults__,
func.__closure__,
)
nfcode = new_func.__code__
new_func.__code__ = CodeType(
0,