From 709fd0cbe35c2d5fc8498bbb1155f2cf2f8a968a Mon Sep 17 00:00:00 2001 From: 26293 <2629352674@qq.com> Date: Tue, 13 Oct 2026 17:52:54 +0800 Subject: [PATCH] fix: replace bare except clauses with proper exception handling - Replace bare 'except:' with 'except Exception:' in writer.py and operators.py to avoid catching system-level exceptions like KeyboardInterrupt - Use specific exceptions (IndexError, ValueError) in rec_postprocess.py where only character index lookup failures are expected This change improves code robustness and follows Python best practices for exception handling without changing any runtime behavior. --- mineru/model/_internal/pytorchocr/data/imaug/operators.py | 2 +- .../model/_internal/pytorchocr/postprocess/rec_postprocess.py | 2 +- mineru/parser/writer.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/mineru/model/_internal/pytorchocr/data/imaug/operators.py b/mineru/model/_internal/pytorchocr/data/imaug/operators.py index 36f4fb25e5..74726d8bd6 100644 --- a/mineru/model/_internal/pytorchocr/data/imaug/operators.py +++ b/mineru/model/_internal/pytorchocr/data/imaug/operators.py @@ -271,7 +271,7 @@ def resize_image_type0(self, img): if int(resize_w) <= 0 or int(resize_h) <= 0: return None, (None, None) img = resize_image(img, (int(resize_w), int(resize_h))) - except: + except Exception: print(img.shape, resize_w, resize_h) sys.exit(0) ratio_h = resize_h / float(h) diff --git a/mineru/model/_internal/pytorchocr/postprocess/rec_postprocess.py b/mineru/model/_internal/pytorchocr/postprocess/rec_postprocess.py index d1400455e4..a8e438add1 100644 --- a/mineru/model/_internal/pytorchocr/postprocess/rec_postprocess.py +++ b/mineru/model/_internal/pytorchocr/postprocess/rec_postprocess.py @@ -277,7 +277,7 @@ def decode(self, text_index, text_prob=None, is_remove_duplicate=False): for idx in range(len(text_index[batch_idx])): try: char_idx = self.character[int(text_index[batch_idx][idx])] - except: + except (IndexError, ValueError): continue if char_idx == '': # end break diff --git a/mineru/parser/writer.py b/mineru/parser/writer.py index 5f5464fe11..baecbf82e3 100644 --- a/mineru/parser/writer.py +++ b/mineru/parser/writer.py @@ -26,7 +26,7 @@ def safe_encode(data: str, method: str) -> tuple[bytes | None, bool]: try: bit_data = data.encode(encoding=method, errors="replace") return bit_data, True - except: # noqa + except Exception: # noqa: BLE001 return None, False for method in ["utf-8", "ascii"]: