Skip to content

Commit 5fb578a

Browse files
timed re.search and _executor made global
1 parent aa2b89b commit 5fb578a

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

opencompass/datasets/mbpp.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import json
55
import multiprocessing
66
import os.path as osp
7-
import re
7+
import regex as re
88
import signal
99
import tempfile
1010
from collections import defaultdict
@@ -330,7 +330,7 @@ def _process_answer(self, text):
330330
r"'(.*)'\s*\[DONE\]",
331331
]
332332
for p in patterns:
333-
match = re.search(p, text, re.DOTALL)
333+
match = re.search(p, text, re.DOTALL, timeout=10.0)
334334
if match:
335335
text = match.group(1)
336336
break
@@ -383,6 +383,22 @@ def _process_answer(self, text):
383383
text = text[1:]
384384
return text
385385

386+
def _execution(programs, timeout, key):
387+
try:
388+
# Add exec globals to prevent the exec to raise
389+
# unnecessary NameError for correct answer
390+
exec_globals = {}
391+
with swallow_io():
392+
with time_limit(timeout):
393+
exec(programs, exec_globals)
394+
key.append('pass')
395+
except TimeOutException:
396+
key.append('timeout')
397+
except AssertionError:
398+
key.append('wrong_answer')
399+
except BaseException as e:
400+
print(e)
401+
key.append('failed')
386402

387403
def execution(programs, task_id, timeout):
388404
"""Execution function for running generation code.
@@ -400,29 +416,12 @@ def execution(programs, task_id, timeout):
400416
control the process.
401417
"""
402418

403-
def _execution(programs, timeout):
404-
try:
405-
# Add exec globals to prevent the exec to raise
406-
# unnecessary NameError for correct answer
407-
exec_globals = {}
408-
with swallow_io():
409-
with time_limit(timeout):
410-
exec(programs, exec_globals)
411-
key.append('pass')
412-
except TimeOutException:
413-
key.append('timeout')
414-
except AssertionError:
415-
key.append('wrong_answer')
416-
except BaseException as e:
417-
print(e)
418-
key.append('failed')
419-
420419
manager = multiprocessing.Manager()
421420
key = manager.list()
422421
# `signal` cannot be used in child thread, therefore, we
423422
# need to create a process in the thread.
424423
p = multiprocessing.Process(target=_execution,
425-
args=(programs, timeout - 1))
424+
args=(programs, timeout - 1, key))
426425
p.start()
427426
p.join(timeout=timeout)
428427
if p.is_alive():

0 commit comments

Comments
 (0)