Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Draft
Show file tree
Hide file tree
Changes from 2 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
2 changes: 0 additions & 2 deletions Lib/test/test_raise.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,8 +233,6 @@ class TestTracebackType(unittest.TestCase):
def raiser(self):
raise ValueError

# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_attrs(self):
try:
self.raiser()
Expand Down
20 changes: 19 additions & 1 deletion vm/src/builtins/traceback.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustpython_common::lock::PyMutex;
use std::ops::Deref;

use super::{PyType, PyTypeRef};
use crate::{
Expand Down Expand Up @@ -63,8 +64,25 @@ impl PyTraceback {
}

#[pygetset(setter)]
fn set_tb_next(&self, value: Option<PyRef<Self>>) {
fn set_tb_next(&self, value: Option<PyRef<Self>>, vm: &VirtualMachine) -> PyResult<()> {
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.


*self.next.lock() = value;
Ok(())
}
}

Expand Down
Loading