Content-Length: 322496 | pFad | http://github.com/RustPython/RustPython/pull/5995

2A Fix traceback circular reference detection to pass test_raise.py by Copilot · Pull Request #5995 · RustPython/RustPython · GitHub
Skip to content

Fix traceback circular reference detection to pass test_raise.py #5995

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

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jul 17, 2025

Fixes the traceback tb_next setter to properly detect and prevent circular references, enabling test_raise.py::TestTracebackType.test_attrs to pass.

Problem

The test TestTracebackType.test_attrs in test_raise.py was marked with @unittest.expectedFailure because the traceback implementation was missing circular reference detection. The test expects:

# These should raise ValueError due to circular references
tb.tb_next = tb  # Direct self-assignment 
tb.tb_next.tb_next = tb  # Indirect loop through chain

But the current implementation allowed these assignments without validation.

Solution

Added circular reference detection to the set_tb_next method in vm/src/builtins/traceback.rs:

  1. Direct loop detection: Checks if the new traceback being assigned is the same object as self
  2. Indirect loop detection: Walks the traceback chain from the new object to ensure self doesn't appear anywhere in the chain
  3. Proper pointer comparison: Uses std::ptr::eq with correct dereferencing to compare the underlying PyTraceback objects

The implementation raises ValueError("circular reference in traceback chain") when a circular reference is detected, matching CPython's behavior.

Changes

  • Removed @unittest.expectedFailure decorator from TestTracebackType.test_attrs
  • Added loop detection logic to PyTraceback::set_tb_next()
  • Added std::ops::Deref import for proper object dereferencing

Testing

  • TestTracebackType.test_attrs now passes
  • ✅ All 36 tests in test_raise.py continue to pass
  • ✅ No clippy warnings introduced

The fix is minimal and surgical, only adding the necessary validation without changing any existing working functionality.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Remove @unittest.expectedFailure from test_raise.py 's TestTracebackType.test_attrs. Run the test and fix the errors by editing traceback.rs Fix traceback circular reference detection to pass test_raise.py Jul 17, 2025
@Copilot Copilot AI requested a review from youknowone July 17, 2025 02:17
Copilot finished work on behalf of youknowone July 17, 2025 02:17
Comment on lines 68 to 82
if let Some(ref new_tb) = value {
// Check for direct loop (tb.tb_next = tb)
if std::ptr::eq(self as *const _, new_tb.deref().deref() as *const _) {
return Err(vm.new_value_error("circular reference in traceback chain".to_owned()));
}

// Check for indirect loops by walking the chain
let mut current = new_tb.tb_next();
while let Some(ref tb) = current {
if std::ptr::eq(self as *const _, tb.deref().deref() as *const _) {
return Err(vm.new_value_error("circular reference in traceback chain".to_owned()));
}
current = tb.tb_next();
}
}
Copy link
Member

Choose a reason for hiding this comment

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

This is too ad-hoc. You must create a more general logic to detect circular reference in tb_next until the end.

Copy link
Author

Choose a reason for hiding this comment

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

Implemented a more general cycle detection algorithm using Floyd's cycle detection (tortoise and hare). The new approach simulates the assignment and detects any cycles in the resulting chain rather than only checking for specific patterns. See commit ce5fe45.

Replace ad-hoc circular reference checking with Floyd's cycle detection algorithm for more comprehensive cycle detection.

Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
Copilot finished work on behalf of youknowone July 17, 2025 05:10
@Copilot Copilot AI requested a review from youknowone July 17, 2025 05:10
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.

2 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/RustPython/RustPython/pull/5995

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy