def test_excel_operator_operand_fixup_errors(left_op, op, right_op, exc): error_messages = [] def capture_error_state(is_exception, msg): error_messages.append((is_exception, msg)) with pytest.raises(exc): build_operator_operand_fixup(capture_error_state)(left_op, op, right_op)
def load_function(excel_formula, name_space): """exec the code into our address space""" # the compiled expressions can call these functions if # referencing other cells or a range of cells name_space['_C_'] = evaluate name_space['_R_'] = evaluate_range name_space['_REF_'] = AddressRange.create name_space['pi'] = math.pi # function to fixup the operands name_space['excel_operator_operand_fixup'] = \ build_operator_operand_fixup(capture_error_state) # hook for the execed code to save the resulting lambda name_space['lambdas'] = lambdas = [] # get the compiled code and needed names compiled, names = excel_formula.compiled_python # load the needed names not_found = load_functions(names, name_space, modules) # exec the code to define the lambda exec(compiled, name_space, name_space) excel_formula.compiled_lambda = lambdas[0] del name_space['lambdas'] return not_found
def test_excel_operator_operand_fixup(left_op, op, right_op, expected): error_messages = [] def capture_error_state(is_exception, msg): error_messages.append((is_exception, msg)) assert expected == build_operator_operand_fixup(capture_error_state)( left_op, op, right_op) if expected == VALUE_ERROR: if expected == VALUE_ERROR and VALUE_ERROR not in (left_op, right_op): assert [(True, 'Values: {} {} {}'.format(left_op, op, right_op)) ] == error_messages elif expected == DIV0 and DIV0 not in (left_op, right_op): assert [(True, 'Values: {} {} {}'.format(left_op, op, right_op))] == error_messages
def test_excel_operator_operand_fixup(left_op, op, right_op, expected): error_messages = [] def capture_error_state(is_exception, msg): error_messages.append((is_exception, msg)) assert expected == build_operator_operand_fixup( capture_error_state)(left_op, op, right_op) if expected == VALUE_ERROR: if expected == VALUE_ERROR and VALUE_ERROR not in (left_op, right_op): assert [(True, 'Values: {} {} {}'.format( coerce_to_number(left_op, convert_all=True), op, right_op)) ] == error_messages elif expected == DIV0 and DIV0 not in (left_op, right_op): assert [(True, 'Values: {} {} {}'.format(left_op, op, right_op)) ] == error_messages
def load_function(excel_formula, name_space): """exec the code into our address space""" # the compiled expressions can call these functions if # referencing other cells or a range of cells name_space['_C_'] = evaluate name_space['_R_'] = evaluate_range name_space['_REF_'] = AddressRange.create name_space['pi'] = math.pi for name in ('int', 'abs', 'round'): name_space[name] = math_wrap(globals()['__builtins__'][name]) # function to fixup the operands name_space['excel_operator_operand_fixup'] = \ build_operator_operand_fixup(capture_error_state) # hook for the execed code to save the resulting lambda name_space['lambdas'] = lambdas = [] # get the compiled code and needed names compiled, names = excel_formula.compiled_python # load the needed names not_found = set() for name in names: if name not in name_space: funcs = ((getattr(module, name, None), module) for module in modules) func, module = next((f for f in funcs if f[0] is not None), (None, None)) if func is None: not_found.add(name) else: if module.__name__ == 'math': name_space[name] = math_wrap(func) else: name_space[name] = func # exec the code to define the lambda exec(compiled, name_space, name_space) excel_formula.compiled_lambda = lambdas[0] del name_space['lambdas'] return not_found