(self, query: str)
| 95 | signal.alarm(0) # Disable the alarm |
| 96 | |
| 97 | def __call__(self, query: str) -> str: |
| 98 | query = query.strip().split("\n") |
| 99 | if "print(" not in query[-1]: |
| 100 | query[-1] = "print(" + query[-1] + ")" |
| 101 | query = "\n".join(query) |
| 102 | |
| 103 | with open(f'{self.tmp_file}.py', "w") as f: |
| 104 | f.write(query) |
| 105 | |
| 106 | with self.time_limit(self.timeout): |
| 107 | result = subprocess.run( |
| 108 | ['python3', f'{self.tmp_file}.py'], capture_output=True, check=False, text=True, timeout=self.timeout) |
| 109 | |
| 110 | if result.returncode == 0: |
| 111 | output = result.stdout |
| 112 | return True, output.strip() |
| 113 | else: |
| 114 | error_msg = result.stderr.strip() |
| 115 | msgs = error_msg.split("\n") |
| 116 | new_msgs = [] |
| 117 | want_next = False |
| 118 | for m in msgs: |
| 119 | if "Traceback" in m: |
| 120 | new_msgs.append(m) |
| 121 | elif m == msgs[-1]: |
| 122 | new_msgs.append(m) |
| 123 | elif self.tmp_file in m: |
| 124 | st = m.index('"/') + 1 if '"/' in m else 0 |
| 125 | ed = m.index(f'/{self.tmp_file}.py') + 1 if f'/{self.tmp_file}.py' in m else None |
| 126 | clr = m[st:ed] if not ed else m[st:] |
| 127 | m = m.replace(clr, "") |
| 128 | new_msgs.append(m) |
| 129 | want_next = True |
| 130 | elif want_next: |
| 131 | new_msgs.append(m) |
| 132 | want_next = False |
| 133 | error_msg = "\n".join(new_msgs) |
| 134 | return False, error_msg.strip() |
| 135 | |
| 136 | |
| 137 | def postprocess_completion(executor, completion): |
nothing calls this directly
no test coverage detected