Python else分支运行时触发UnboundLocalError报错排查
问题根因
报错和缩进、全局变量作用域无关,是两处代码逻辑错误导致:
- 所有
*_key_flag匹配标志位没有提前初始化,只有当日志某一行命中对应key时,变量才会被赋值。如果某个key在整个日志文件里完全没出现,对应的flag变量从未被创建,循环结束后做全量flag判断时,就会触发UnboundLocalError: local variable 'xxx' referenced before assignment错误。 - 逐行匹配的逻辑写反:当前代码每遍历一行,只要该行不包含目标key就往错误列表追加报错,会导致即使key在其他行已经命中,只要某一行不匹配就重复写入错误信息,最终错误列表会充斥大量无效重复内容,完全不符合“仅记录缺失key”的预期。
修复步骤
- 进入日志遍历逻辑前,先把所有
*_key_flag初始化为0(代表未匹配状态),同时清空全局错误列表,避免上次函数调用的残留信息干扰本次结果 - 遍历日志行时仅做匹配操作:命中对应key就把相关flag置为
1,不在逐行循环内做错误信息追加 - 整个日志文件遍历完成后,再统一检查所有flag的状态,对值为
0(未匹配)的key,追加对应的错误信息到列表 - 最终判断所有flag是否均为
1,是则返回True并打印校验通过提示,否则返回False并打印缺失key的错误列表
修复后完整代码
import datetime import os.path import logging recipe_name = 'Sanity_CS.py' NR_log = 'NR_' + str(datetime.datetime.now()).split()[0] + '.log' lst = [] def pre_conditions(): # 清空历史错误记录 lst.clear() # 提前初始化所有匹配标志位为未匹配状态 name_key_flag = 0 app_key_flag = 0 api_key_flag = 0 lot_key_flag = 0 recipe_key_flag = 0 wafer_key_flag = 0 recipe_pause_key_flag = 0 program_key_flag = 0 recipe_creation_key_flag = 0 # 定义待匹配key name_key = recipe_name app_key = 'Application was powered-up successfully, mode is: Review' api_key = 'API recipe was chosen' lot_key = 'Lot was created successfully' recipe_key = 'Recipe execution started' wafer_key = 'The Wafer was loaded successfully' recipe_pause_key = 'Recipe run is paused' program_key = 'Moving to Program mode' recipe_creation_key = 'Recipe was saved successfully under the name: sanity_2022-06-22_Ver_5.1' with open(NR_log, 'r') as logfile: for line in logfile: # 逐行匹配,命中则更新对应标志位 if name_key in line: name_key_flag = 1 if app_key in line: app_key_flag = 1 if api_key in line: api_key_flag = 1 if lot_key in line: lot_key_flag = 1 if recipe_key in line: recipe_key_flag = 1 if wafer_key in line: wafer_key_flag = 1 if recipe_pause_key in line: recipe_pause_key_flag = 1 if program_key in line: program_key_flag = 1 if recipe_creation_key in line: recipe_creation_key_flag = 1 # 遍历完所有日志后,统一收集未匹配项的错误信息 if name_key_flag != 1: lst.append('\nError: Script was not successfully executed \n') if app_key_flag != 1: lst.append('\nError: Application was failed to power up.\n') if api_key_flag != 1: lst.append("\nError: Recipe type [API] was not successfully chosen\n") if lot_key_flag != 1: lst.append("\nError: A lot was not successfully created.\n") if recipe_key_flag != 1: lst.append("\nError: A timeout, recipe was not executed\n") if wafer_key_flag != 1: lst.append("\nError: The wafer was not loaded.\n") if recipe_pause_key_flag != 1: lst.append("\nError: The recipe was not paused.\n") if program_key_flag != 1: lst.append("\nError: The script was not switch to program key.\n") if recipe_creation_key_flag != 1: lst.append("\nError: The recipe was not saved.\n") # 最终校验结果判断 all_match = all([ name_key_flag == 1, app_key_flag == 1, api_key_flag == 1, lot_key_flag == 1, recipe_key_flag == 1, wafer_key_flag == 1, recipe_pause_key_flag == 1, program_key_flag == 1, recipe_creation_key_flag == 1 ]) if all_match: print("Pre conditions are OK.") return True, lst else: print("Missing keys found:", lst) return False, lst
内容的提问来源于stack exchange,提问作者I'm not a robot
相关产品推荐
相关产品推荐

