-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: youknowone <69878+youknowone@users.noreply.github.com>
@unittest.expectedFailure from test_raise.py 's TestTracebackType.test_attrs. Run the test and fix the errors by editing traceback.rs
vm/src/builtins/traceback.rs
Outdated
| 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(); | ||
| } | ||
| } |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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>
Fixes the traceback
tb_nextsetter to properly detect and prevent circular references, enablingtest_raise.py::TestTracebackType.test_attrsto pass.Problem
The test
TestTracebackType.test_attrsintest_raise.pywas marked with@unittest.expectedFailurebecause the traceback implementation was missing circular reference detection. The test expects:But the current implementation allowed these assignments without validation.
Solution
Added circular reference detection to the
set_tb_nextmethod invm/src/builtins/traceback.rs:selfselfdoesn't appear anywhere in the chainstd::ptr::eqwith correct dereferencing to compare the underlyingPyTracebackobjectsThe implementation raises
ValueError("circular reference in traceback chain")when a circular reference is detected, matching CPython's behavior.Changes
@unittest.expectedFailuredecorator fromTestTracebackType.test_attrsPyTraceback::set_tb_next()std::ops::Derefimport for proper object dereferencingTesting
TestTracebackType.test_attrsnow passestest_raise.pycontinue to passThe 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.