diff --git a/app/route/execute/service/execute_service.py b/app/route/execute/service/execute_service.py index edfa33c..b8273d6 100644 --- a/app/route/execute/service/execute_service.py +++ b/app/route/execute/service/execute_service.py @@ -19,7 +19,7 @@ def execute_code(source_code: str, user_input: str): process = subprocess.run( args=["python3", "-c", source_code], input=user_input, - capture_output=True, # stdout, stderr 별도의 Pipe에서 처리 + capture_output=True, # stdout, stderr 별도의 Pipe에서 처리 timeout=3, # limit child process execute time check=True, # CalledProcessError exception if return_code is 0 text=True @@ -28,17 +28,25 @@ def execute_code(source_code: str, user_input: str): # 프로세스 실행 중 비정상 종료 except subprocess.CalledProcessError as e: - raise CodeSyntaxError(ErrorEnum.CODE_SYNTAX_ERROR, {"error": e.stderr}) + line_number = _get_error_line_number(e.stderr) + raise CodeSyntaxError(ErrorEnum.CODE_SYNTAX_ERROR, {"lineNumber": line_number, "error": e.stderr}) except Exception as e: logger.error("[Unexpected Exception] execute_code()") raise TaskFailException(ErrorEnum.CODE_EXEC_SERVER_ERROR, dict(e.args)) +def _get_error_line_number(error_msg): + # pattern : 'line '숫자', in' + matches = re.findall(r'line (\d+), in', error_msg) + if matches: + return int(matches[-1]) + return 1 + + def _contains_forbidden_imports(code: str) -> bool: # 금지된 모듈이 코드에 있는지 확인 for module in FORBIDDEN_IMPORTS: if re.search(rf'\bimport\s+{module}\b', code): return True return False -