Skip to content

Commit 6cb3e22

Browse files
fix: replace bare except clauses with except Exception (#1190)
Bare `except:` catches BaseException including KeyboardInterrupt and SystemExit. Replaced 26 instances with `except Exception:`. Co-authored-by: haosenwang1018 <haosenwang1018@users.noreply.github.com>
1 parent 9ce1ae4 commit 6cb3e22

7 files changed

Lines changed: 26 additions & 26 deletions

File tree

‎modules/index_func.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ def get_documents(file_src):
3232

3333
two_column = advance_docs["pdf"].get("two_column", False)
3434
pdftext = parse_pdf(filepath, two_column).text
35-
except:
35+
except Exception:
3636
pdftext = ""
3737
with open(filepath, "rb") as pdfFileObj:
3838
pdfReader = PyPDF2.PdfReader(pdfFileObj)

‎modules/models/GoogleGemma.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def __init__(self, model_name, api_key, user_name="") -> None:
3434
else:
3535
try:
3636
model_source = MODEL_METADATA[model_name]["repo_id"]
37-
except:
37+
except Exception:
3838
model_source = model_name
3939
dtype = torch.bfloat16
4040
GEMMA_TOKENIZER = AutoTokenizer.from_pretrained(

‎modules/models/OpenAIVision.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ def _get_response(self, stream=False):
218218
stream=stream,
219219
timeout=timeout,
220220
)
221-
except:
221+
except Exception:
222222
traceback.print_exc()
223223
return None
224224
return response
@@ -256,7 +256,7 @@ def _decode_chat_response(self, response):
256256
chunk_length = len(chunk)
257257
try:
258258
chunk = json.loads(chunk[6:])
259-
except:
259+
except Exception:
260260
print(i18n("JSON解析错误,收到的内容: ") + f"{chunk}")
261261
error_msg += chunk
262262
continue
@@ -279,7 +279,7 @@ def _decode_chat_response(self, response):
279279
except Exception as e:
280280
# logging.error(f"Error: {e}")
281281
continue
282-
except:
282+
except Exception:
283283
traceback.print_exc()
284284
print(f"ERROR: {chunk}")
285285
continue

‎modules/models/base_model.py‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def get_type(cls, model_name: str):
178178
try:
179179
assert MODEL_METADATA[model_name]["multimodal"] == True
180180
model_type = ModelType.OpenAIVision
181-
except:
181+
except Exception:
182182
if "instruct" in model_name_lower:
183183
model_type = ModelType.OpenAIInstruct
184184
elif "vision" in model_name_lower:
@@ -252,7 +252,7 @@ def download(repo_id, filename, retry=10):
252252
with open("./models/downloaded_models.json", "w") as f:
253253
json.dump(downloaded_models, f)
254254
break
255-
except:
255+
except Exception:
256256
print("Error downloading model, retrying...")
257257
retry -= 1
258258
if retry == 0:
@@ -1046,7 +1046,7 @@ def load_chat_history(self, new_history_file_path=None):
10461046
new_history.append(construct_assistant(item))
10471047
saved_json["history"] = new_history
10481048
logging.info(new_history)
1049-
except:
1049+
except Exception:
10501050
pass
10511051
if len(saved_json["chatbot"]) < len(saved_json["history"]) // 2:
10521052
logging.info("Trimming corrupted history...")
@@ -1105,7 +1105,7 @@ def load_chat_history(self, new_history_file_path=None):
11051105
gr.DownloadButton(value=tmp_json_for_download, interactive=True),
11061106
gr.DownloadButton(value=tmp_md_for_download, interactive=True),
11071107
)
1108-
except:
1108+
except Exception:
11091109
# 没有对话历史或者对话历史解析失败
11101110
logging.debug(f"没有找到对话历史记录 {self.history_file_path}")
11111111
self.reset()
@@ -1152,7 +1152,7 @@ def delete_chat_history(self, filename):
11521152
os.remove(history_file_path)
11531153
os.remove(md_history_file_path)
11541154
return i18n("删除对话历史成功"), get_history_list(self.user_name), []
1155-
except:
1155+
except Exception:
11561156
logging.info(f"删除对话历史失败 {history_file_path}")
11571157
return (
11581158
i18n("对话历史") + filename + i18n("已经被删除啦"),

‎modules/models/inspurai.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ def submit_API(self, prompt, trun=[]):
201201
txt = txt[:txt.index(tr)]
202202
else:
203203
continue
204-
except:
204+
except Exception:
205205
return txt
206206
return txt
207207

‎modules/models/minimax.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ def _get_response(self, stream=False):
122122
stream=stream,
123123
timeout=timeout,
124124
)
125-
except:
125+
except Exception:
126126
return None
127127

128128
return response

‎modules/utils.py‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -382,7 +382,7 @@ def detect_converted_mark(userinput): # deprecated
382382
return True
383383
else:
384384
return False
385-
except:
385+
except Exception:
386386
return True
387387

388388

@@ -569,7 +569,7 @@ def filter_history(user_name, keyword):
569569
try:
570570
history_names = [name for name in history_names if re.search(keyword, name, timeout=0.01)]
571571
return gr.update(choices=history_names)
572-
except:
572+
except Exception:
573573
return gr.update(choices=history_names)
574574

575575

@@ -615,7 +615,7 @@ def get_template_content(templates, selection, original_system_prompt):
615615
logging.debug(f"应用模板中,选择为{selection},原始系统提示为{original_system_prompt}")
616616
try:
617617
return templates[selection]
618-
except:
618+
except Exception:
619619
return original_system_prompt
620620

621621

@@ -691,7 +691,7 @@ def fetch_ip():
691691
with retrieve_proxy():
692692
response = requests.get("https://ipapi.co/json/", timeout=5)
693693
data = response.json()
694-
except:
694+
except Exception:
695695
data = {"error": True, "reason": "连接ipapi失败"}
696696
if "error" in data.keys():
697697
# logging.warning(f"无法获取IP地址信息。\n{data}")
@@ -903,7 +903,7 @@ def beautify_err_msg(err_msg):
903903
return i18n("请查看 config_example.json,配置 Azure OpenAI")
904904
try:
905905
err_msg = json.loads(err_msg)["error"]["message"]
906-
except:
906+
except Exception:
907907
pass
908908
return err_msg
909909

@@ -1069,7 +1069,7 @@ def set(self, config_items: List[ConfigItem], prompt: str):
10691069
print(generate_result_string(config_item, config_value))
10701070
try:
10711071
self.config[config_item.key] = int(config_value)
1072-
except:
1072+
except Exception:
10731073
print("输入的不是数字,将使用默认值。")
10741074
elif config_item.type == ConfigType.ListOfStrings:
10751075
# read one string at a time
@@ -1511,42 +1511,42 @@ def setPlaceholder(model_name: str | None = "", model: BaseLLMModel | None = Non
15111511
if model is None:
15121512
try:
15131513
model_logo = MODEL_METADATA[model_name]["placeholder"]["logo"]
1514-
except:
1514+
except Exception:
15151515
logo_class = "hideK"
15161516
try:
15171517
model_logo_round = MODEL_METADATA[model_name]["placeholder"]["logo_rounded"]
1518-
except:
1518+
except Exception:
15191519
pass
15201520
try:
15211521
model_slogan = i18n(MODEL_METADATA[model_name]["placeholder"]["slogan"])
1522-
except:
1522+
except Exception:
15231523
slogan_class = "hideK"
15241524
try:
15251525
model_question_1 = i18n(MODEL_METADATA[model_name]["placeholder"]["question_1"])
15261526
model_question_2 = i18n(MODEL_METADATA[model_name]["placeholder"]["question_2"])
15271527
model_question_3 = i18n(MODEL_METADATA[model_name]["placeholder"]["question_3"])
15281528
model_question_4 = i18n(MODEL_METADATA[model_name]["placeholder"]["question_4"])
1529-
except:
1529+
except Exception:
15301530
question_class = "hideK"
15311531
else:
15321532
try:
15331533
model_logo = model.placeholder["logo"]
1534-
except:
1534+
except Exception:
15351535
logo_class = "hideK"
15361536
try:
15371537
model_logo_round = model.placeholder["logo_rounded"]
1538-
except:
1538+
except Exception:
15391539
pass
15401540
try:
15411541
model_slogan = i18n(model.placeholder["slogan"])
1542-
except:
1542+
except Exception:
15431543
slogan_class = "hideK"
15441544
try:
15451545
model_question_1 = i18n(model.placeholder["question_1"])
15461546
model_question_2 = i18n(model.placeholder["question_2"])
15471547
model_question_3 = i18n(model.placeholder["question_3"])
15481548
model_question_4 = i18n(model.placeholder["question_4"])
1549-
except:
1549+
except Exception:
15501550
question_class = "hideK"
15511551

15521552
if logo_class == "hideK" and slogan_class == "hideK" and question_class == "hideK":

0 commit comments

Comments
 (0)