(seconds)
| 25 | # Custom context manager for code execution timeout |
| 26 | @contextmanager |
| 27 | def timeout(seconds): |
| 28 | |
| 29 | if is_windows_os(): |
| 30 | # Windows timeout using threading.Timer |
| 31 | def raise_timeout(): |
| 32 | raise TimeoutException("Code execution timed out") |
| 33 | timer = threading.Timer(seconds, raise_timeout) |
| 34 | timer.start() |
| 35 | try: |
| 36 | yield |
| 37 | finally: |
| 38 | timer.cancel() |
| 39 | |
| 40 | else: |
| 41 | def timeout_handler(signum, frame): |
| 42 | raise TimeoutException("Code execution timed out") |
| 43 | |
| 44 | # Set the timeout handler |
| 45 | original_handler = signal.signal(signal.SIGALRM, timeout_handler) |
| 46 | signal.alarm(seconds) |
| 47 | |
| 48 | try: |
| 49 | yield |
| 50 | finally: |
| 51 | # Restore the original handler and disable the alarm |
| 52 | signal.alarm(0) |
| 53 | signal.signal(signal.SIGALRM, original_handler) |
| 54 | |
| 55 | |
| 56 | class Python_Code_Generator_Tool(BaseTool): |
no test coverage detected