# -*- coding: utf-8 -*- """修上一个补丁弄坏的 get()。 坏成了这样(缩进错 + 多了一个 as resp): try: _req = urllib.request.Request(URL + path, headers=_UA) with urllib.request.urlopen(_req, timeout=timeout) as resp: as resp: return resp.status, resp.read(), 0.0 应该是: try: _req = urllib.request.Request(URL + path, headers=_UA) with urllib.request.urlopen(_req, timeout=timeout) as resp: return resp.status, resp.read(), 0.0 """ from __future__ import annotations import ast import re import sys from pathlib import Path TESTS = Path(r"C:\Users\shu\Desktop\deepseekagent\LanRemote\_tests") FILES = ["test_relay_e2e.py", "test_relay_local.py", "test_relay_admin_login.py"] # 坏掉的那两块 BROKEN = re.compile( r" try:\n" r"(?P _req = urllib\.request\.Request\([^\n]*\n)" r" with urllib\.request\.urlopen\(_req, (?Ptimeout=[^)]*)\) " r"as resp: as resp:\n" r" return resp\.status, resp\.read\(\), 0\.0\n") def main() -> int: ok = True for name in FILES: path = TESTS / name if not path.is_file(): continue text = path.read_text(encoding="utf-8") def rebuild(match): return (" try:\n" + match.group("build") + f" with urllib.request.urlopen(_req, " f"{match.group('timeout')}) as resp:\n" " return resp.status, resp.read(), 0.0\n") new_text, count = BROKEN.subn(rebuild, text) if count == 0: # 也许已经是对的,检查一下语法就行 try: ast.parse(text) print(f" {name}: 没有坏块,语法 OK") except SyntaxError as exc: print(f" {name}: 语法仍错 -> {exc}") ok = False continue path.write_text(new_text, encoding="utf-8", newline="\n") try: ast.parse(new_text) print(f" {name}: 修好 {count} 处,语法 OK") except SyntaxError as exc: print(f" {name}: 修完还是错 -> {exc}") ok = False return 0 if ok else 1 if __name__ == "__main__": sys.exit(main())