Skip to content

Commit

Permalink
[#36]fix: lineNumber 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
ujkkk committed Nov 6, 2024
1 parent 1d38458 commit 4068905
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions app/route/execute/service/execute_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

0 comments on commit 4068905

Please sign in to comment.