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 has_no_return(func: FunctionType) -> bool:
"""Return True if `func` returns nothing, else return False"""
code = func.__code__
co_code = code.co_code
co_consts = code.co_consts
instructions_tuples = tuple(get_instructions_tuples(func))
return_offsets = multiple_find(co_code, OpCode.RETURN_VALUE)
load_const_none = OpCode.LOAD_CONST + bytes((co_consts.index(None),))
return len(return_offsets) == 1 and instructions_tuples[-2][0:2] == load_const_none
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