"""仅运行本文件内可信合成源码,分别定位解析、导入与构造故障。""" import ast import json from pathlib import Path import subprocess import sys from tempfile import TemporaryDirectory sources = { 'syntax': 'class SignalEngine(:\n pass\n', 'self_import': 'from signal_engine import SignalEngine\nclass SignalEngine: pass\n', 'top_level': 'raise RuntimeError("top-level side effect")\nclass SignalEngine: pass\n', 'constructor': 'class SignalEngine:\n def __init__(self, window): self.window=window\n', 'fixed': ('class SignalEngine:\n' ' def __init__(self, window=3): self.window=window\n' ' def generate(self, closes):\n' ' return [int(i >= self.window-1 and x > ' 'sum(closes[i-self.window+1:i+1])/self.window) ' 'for i,x in enumerate(closes)]\n'), } report = {} with TemporaryDirectory(prefix='r580-') as directory: path = Path(directory) / 'signal_engine.py' for label, source in sources.items(): try: ast.parse(source) except SyntaxError: report[label] = 'parse:SyntaxError' continue path.write_text(source, encoding='utf-8') command = ('import signal_engine; ' 'obj=signal_engine.SignalEngine(); ' 'print(obj.generate([10,10,12]))') result = subprocess.run([sys.executable, '-B', '-c', command], cwd=directory, text=True, capture_output=True, timeout=5) if result.returncode == 0: report[label] = result.stdout.strip() elif 'ImportError' in result.stderr: report[label] = 'import:ImportError' elif 'RuntimeError' in result.stderr: report[label] = 'import:RuntimeError' elif 'TypeError' in result.stderr: report[label] = 'construct:TypeError' else: raise AssertionError(result.stderr) assert report == {'syntax': 'parse:SyntaxError', 'self_import': 'import:ImportError', 'top_level': 'import:RuntimeError', 'constructor': 'construct:TypeError', 'fixed': '[0, 0, 1]'} print(json.dumps(report))