Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#36]fix: 틀린문법시 응답값에 lineNumber 추가 #37

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
SongGwanSeok marked this conversation as resolved.
Show resolved Hide resolved


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

Loading