已合并
fix: #112 消除 click_publish.py 裸 except,显式捕获并记录真实堆栈 #16
grape_bot创建于 10月21日
fix: #112 消除 click_publish.py 裸 except,显式捕获并记录真实堆栈 #16
已合并
共 2 个文件变更+57-4
| @@ -2,9 +2,12 @@ | |||
| 2 | """ | 2 | """ |
| 3 | 用鼠标点击方式发布 | 3 | 用鼠标点击方式发布 |
| 4 | """ | 4 | """ |
| 5 | -import json, time, urllib.request | 5 | +import json, logging, time, urllib.request |
| 6 | from websocket import create_connection | 6 | from websocket import create_connection |
| 7 | 7 | ||
| 8 | +logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(name)s: %(message)s") | ||
| 9 | +logger = logging.getLogger("click_publish") | ||
| 10 | + | ||
| 8 | tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json").read()) | 11 | tabs = json.loads(urllib.request.urlopen("http://127.0.0.1:9222/json").read()) |
| 9 | target = None | 12 | target = None |
| 10 | for tab in tabs: | 13 | for tab in tabs: |
| @@ -30,8 +33,11 @@ def sc(m, p=None): | |||
| 30 | 33 | ||
| 31 | def ev(js): | 34 | def ev(js): |
| 32 | r = sc("Runtime.evaluate", {"expression": js, "returnByValue": True}) | 35 | r = sc("Runtime.evaluate", {"expression": js, "returnByValue": True}) |
| 33 | - try: return r["result"]["result"]["value"] | 36 | + try: |
| 34 | - except: return None | 37 | + return r["result"]["result"]["value"] |
| 38 | + except (KeyError, TypeError) as e: | ||
| 39 | + logger.warning("Runtime.evaluate 结果缺少取值字段: %r", e, exc_info=True) | ||
| 40 | + return None | ||
| 35 | 41 | ||
| 36 | # 1. 找xhs-publish-btn的坐标 | 42 | # 1. 找xhs-publish-btn的坐标 |
| 37 | btn_rect = ev(""" | 43 | btn_rect = ev(""" |
| @@ -87,7 +93,8 @@ try: | |||
| 87 | print("❌ 找不到发布按钮") | 93 | print("❌ 找不到发布按钮") |
| 88 | ws.close() | 94 | ws.close() |
| 89 | exit(1) | 95 | exit(1) |
| 90 | -except: | 96 | +except (ValueError, KeyError, TypeError) as e: |
| 97 | + logger.error("解析发布按钮坐标失败: %s", e, exc_info=True) | ||
| 91 | print("❌ 解析坐标失败") | 98 | print("❌ 解析坐标失败") |
| 92 | ws.close() | 99 | ws.close() |
| 93 | exit(1) | 100 | exit(1) |
| @@ -0,0 +1,46 @@ | |||
| 1 | +#!/usr/bin/env python3 | ||
| 2 | +"""Issue #112 验收测试:click_publish.py 无裸 except,异常显式绑定并记录堆栈。""" | ||
| 3 | +import ast | ||
| 4 | +import unittest | ||
| 5 | +from pathlib import Path | ||
| 6 | + | ||
| 7 | +TARGET = Path(__file__).resolve().parents[1] / "scripts" / "archive" / "click_publish.py" | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +class NoBareExceptTest(unittest.TestCase): | ||
| 11 | + def setUp(self): | ||
| 12 | + self.tree = ast.parse(TARGET.read_text(encoding="utf-8")) | ||
| 13 | + self.handlers = [ | ||
| 14 | + node for node in ast.walk(self.tree) if isinstance(node, ast.ExceptHandler) | ||
| 15 | + ] | ||
| 16 | + | ||
| 17 | + def test_no_bare_except(self): | ||
| 18 | + bare = [h for h in self.handlers if h.type is None] | ||
| 19 | + self.assertEqual(bare, [], "存在裸 except:,会吞掉 KeyboardInterrupt/SystemExit") | ||
| 20 | + | ||
| 21 | + def test_handlers_bind_exception_object(self): | ||
| 22 | + unbound = [h for h in self.handlers if h.name is None] | ||
| 23 | + self.assertEqual(unbound, [], "存在未 as e 绑定异常对象的 except 处理器") | ||
| 24 | + | ||
| 25 | + def test_handlers_use_explicit_types(self): | ||
| 26 | + for handler in self.handlers: | ||
| 27 | + self.assertIsNotNone(handler.type, "except 必须显式列出异常类型") | ||
| 28 | + joined = ast.dump(handler.type) | ||
| 29 | + self.assertNotIn("KeyboardInterrupt", joined) | ||
| 30 | + self.assertNotIn("SystemExit", joined) | ||
| 31 | + | ||
| 32 | + def test_real_stack_logging(self): | ||
| 33 | + source = TARGET.read_text(encoding="utf-8") | ||
| 34 | + self.assertIn("exc_info=True", source, "异常处理需记录真实堆栈") | ||
| 35 | + | ||
| 36 | + def test_keyboardinterrupt_not_caught(self): | ||
| 37 | + source = ast.parse(TARGET.read_text(encoding="utf-8")) | ||
| 38 | + for node in ast.walk(source): | ||
| 39 | + if isinstance(node, ast.ExceptHandler): | ||
| 40 | + dumped = ast.dump(node.type or ast.Constant(value=None)) | ||
| 41 | + self.assertNotIn("KeyboardInterrupt", dumped) | ||
| 42 | + self.assertNotIn("SystemExit", dumped) | ||
| 43 | + | ||
| 44 | + | ||
| 45 | +if __name__ == "__main__": | ||
| 46 | + unittest.main() | ||