Skip to content

Conversation

konentung
Copy link
Collaborator

  1. 新增unittest處理我的測試程式
  2. 完成debug路由讓使用者可以進行測試
  3. 送出也需要debug,為AC或WA才可以送出避免惡意注入
  4. 新增chardet套件防止不支援的字元進入測試程式

1. 新增unittest處理我的測試程式
2. 完成debug路由讓使用者可以進行測試
3. 送出也需要debug,為AC或WA才可以送出避免惡意注入
4. 新增chardet套件防止不支援的字元進入測試程式
@konentung konentung self-assigned this Aug 17, 2025
@konentung konentung requested a review from Copilot August 17, 2025 04:30
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements a comprehensive debug verification system for a coding platform. It adds unittest functionality to test user submissions, creates debug routes for testing, validates submissions with debug results (AC/WA only), and adds encoding support to prevent character encoding issues.

  • Adds unittest framework for automated code testing with timeout and encoding detection
  • Implements debug functionality with modal UI for displaying test results
  • Enforces submission validation requiring AC or WA status to prevent malicious code injection
  • Adds chardet package support and encoding normalization for handling various character encodings

Reviewed Changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
templates/questions/create.html Minor template formatting fix for output example label
templates/answers/answer.html Updates debug button styling and adds comprehensive debug result modal
static/js/answers/answer.js Enhances AJAX handling for debug/submit with improved error handling and modal display
judge/unittest.py New unittest framework with encoding detection, timeout handling, and test case generation
answers/views.py Implements debug endpoint and submission validation with encoding normalization
answers/models.py Adds Transcript and Debug models for tracking submissions and debug attempts
answers/migrations/0001_initial.py Database migration for new models and constraint updates
answers/admin.py Admin interface for new Transcript and Debug models

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

return exception[0]

return result[0]

Copy link
Preview

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The thread cannot be forcefully terminated using thread.join(). Consider using a different approach like subprocess with timeout or signal-based timeout handling for better resource cleanup.

Suggested change
def target(queue):
try:
result = func()
queue.put(('result', result))
except Exception as e:
queue.put(('exception', e))
queue = multiprocessing.Queue()
process = multiprocessing.Process(target=target, args=(queue,))
process.start()
process.join(timeout_seconds)
if process.is_alive():
process.terminate()
process.join()
return TimeoutError(f"程式執行超過 {timeout_seconds} 秒")
if not queue.empty():
status, value = queue.get()
if status == 'exception':
return value
else:
return value
else:
return TimeoutError("未知錯誤:子進程未返回結果")

Copilot uses AI. Check for mistakes.

return "MLE", False
elif 'recursion' in error_str or 'stack overflow' in error_str:
return "RE", False
elif 'syntax' in error_str or 'indentation' in error_str:
Copy link
Preview

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String matching for error classification is unreliable as it depends on specific error message formats. Consider using isinstance() checks for specific exception types instead of string matching.

Suggested change
elif 'syntax' in error_str or 'indentation' in error_str:
# Use isinstance checks for error classification
if isinstance(test_result, MemoryError):
return "MLE", False
elif isinstance(test_result, RecursionError):
return "RE", False
elif isinstance(test_result, (SyntaxError, IndentationError)):

Copilot uses AI. Check for mistakes.

__builtins__.input = mock_input

# 執行學生程式碼
{chr(10).join(" " + line for line in code.split(chr(10)))}
Copy link
Preview

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Modifying builtins globally can affect other parts of the application and create security risks. Consider using a more isolated approach like exec() with a custom namespace or subprocess execution.

Suggested change
{chr(10).join(" " + line for line in code.split(chr(10)))}
sys.stdout = StringIO()
try:
# 在自定義命名空間中執行學生程式碼,覆蓋 input
local_ns = {{"input": mock_input}}
{chr(10).join(" " + line for line in code.split(chr(10)))}
exec({chr(34)}{chr(10).join(line for line in code.split(chr(10)))}{chr(34)}, {{}}, local_ns)

Copilot uses AI. Check for mistakes.

__builtins__.input = mock_input

# 執行學生程式碼
{chr(10).join(" " + line for line in code.split(chr(10)))}
Copy link
Preview

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using chr(10) for newlines is unclear. Use '\n' or os.linesep for better readability and maintainability.

Suggested change
{chr(10).join(" " + line for line in code.split(chr(10)))}
{'\n'.join(" " + line for line in code.split('\n'))}

Copilot uses AI. Check for mistakes.

console.log('無法解析錯誤回應:', xhr.responseText);
}
}

Copy link
Preview

Copilot AI Aug 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling code is duplicated in two places (lines 89-97 and 139-147). Consider extracting this into a reusable function to reduce code duplication.

Suggested change
const errorMessage = extractErrorMessage(xhr, '送出失敗,請稍後再試。');

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant