4
4
import json
5
5
import multiprocessing
6
6
import os .path as osp
7
- import re
7
+ import regex as re
8
8
import signal
9
9
import tempfile
10
10
from collections import defaultdict
@@ -330,7 +330,7 @@ def _process_answer(self, text):
330
330
r"'(.*)'\s*\[DONE\]" ,
331
331
]
332
332
for p in patterns :
333
- match = re .search (p , text , re .DOTALL )
333
+ match = re .search (p , text , re .DOTALL , timeout = 10.0 )
334
334
if match :
335
335
text = match .group (1 )
336
336
break
@@ -383,6 +383,22 @@ def _process_answer(self, text):
383
383
text = text [1 :]
384
384
return text
385
385
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' )
386
402
387
403
def execution (programs , task_id , timeout ):
388
404
"""Execution function for running generation code.
@@ -400,29 +416,12 @@ def execution(programs, task_id, timeout):
400
416
control the process.
401
417
"""
402
418
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
-
420
419
manager = multiprocessing .Manager ()
421
420
key = manager .list ()
422
421
# `signal` cannot be used in child thread, therefore, we
423
422
# need to create a process in the thread.
424
423
p = multiprocessing .Process (target = _execution ,
425
- args = (programs , timeout - 1 ))
424
+ args = (programs , timeout - 1 , key ))
426
425
p .start ()
427
426
p .join (timeout = timeout )
428
427
if p .is_alive ():
0 commit comments