From 07962008f5d6febe3287a7651063fbba16176a04 Mon Sep 17 00:00:00 2001 From: Kailigithub Date: Tue, 26 May 2026 03:45:19 +0000 Subject: [PATCH] fix: replace bare except clauses with except Exception in ga.py Replace all 8 bare `except:` clauses in ga.py with `except Exception:`. Bare except catches SystemExit and KeyboardInterrupt which can mask critical failures and make debugging harder. Affected locations: - code_run output printing (lines 48-49) - file access stats loading (line 159) - timeout argument parsing (line 289) - web_execute_js file save error (line 348) - web_execute_js result printing (line 351) - plan completion check (line 433) - verbose prompt printing (line 544) --- ga.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ga.py b/ga.py index 4ccd84f42..ffbd19d83 100644 --- a/ga.py +++ b/ga.py @@ -45,8 +45,8 @@ def stream_reader(proc, logs): except UnicodeDecodeError: line = line_bytes.decode('gbk', errors='ignore') logs.append(line) try: print(line, end="") - except: pass - except: pass + except Exception: pass + except Exception: pass try: process = subprocess.Popen( @@ -156,7 +156,7 @@ def log_memory_access(path): stats_file = os.path.join(script_dir, 'memory/file_access_stats.json') try: with open(stats_file, 'r', encoding='utf-8') as f: stats = json.load(f) - except: stats = {} + except Exception: stats = {} fname = os.path.basename(path) stats[fname] = {'count': stats.get(fname, {}).get('count', 0) + 1, 'last': datetime.now().strftime('%Y-%m-%d')} with open(stats_file, 'w', encoding='utf-8') as f: json.dump(stats, f, indent=2, ensure_ascii=False) @@ -286,7 +286,7 @@ def do_code_run(self, args, response): code = self._extract_code_block(response, code_type) if not code: return StepOutcome("[Error] Code missing. Must use reply code block or 'script' arg.", next_prompt="\n") try: timeout = int(args.get("timeout", 60)) - except: timeout = 60 + except Exception: timeout = 60 raw_path = os.path.join(self.cwd, args.get("cwd", './')) cwd = os.path.normpath(os.path.abspath(raw_path)) code_cwd = os.path.normpath(self.cwd) @@ -345,10 +345,10 @@ def do_web_execute_js(self, args, response): try: with open(abs_path, 'w', encoding='utf-8') as f: f.write(str(content)) result["js_return"] += f"\n\n[已保存完整内容到 {abs_path}]" - except: result['js_return'] += f"\n\n[保存失败,无法写入文件 {abs_path}]" + except Exception: result['js_return'] += f"\n\n[保存失败,无法写入文件 {abs_path}]" show = smart_format(json.dumps(result, ensure_ascii=False, indent=2, default=json_default), max_str_len=300) try: print("Web Execute JS Result:", show) - except: pass + except Exception: pass yield f"JS 执行结果:\n{show}\n" next_prompt = self._get_anchor_prompt(skip=args.get('_index', 0) > 0) result = json.dumps(result, ensure_ascii=False, default=json_default) @@ -430,7 +430,7 @@ def enter_plan_mode(self, plan_path): def _check_plan_completion(self): if not os.path.isfile(p:=self._in_plan_mode() or ''): return None try: return len(re.findall(r'\[ \]', open(p, encoding='utf-8', errors='replace').read())) - except: return None + except Exception: return None def do_update_working_checkpoint(self, args, response): '''为整个任务设定后续需要临时记忆的重点。''' @@ -541,7 +541,7 @@ def _get_anchor_prompt(self, skip=False): if self.working.get('related_sop'): prompt += f"\n有不清晰的地方请再次读取{self.working.get('related_sop')}" if getattr(self.parent, 'verbose', False): try: print(prompt) - except: pass + except Exception: pass return prompt def turn_end_callback(self, response, tool_calls, tool_results, turn, next_prompt, exit_reason):