Skip to content

Commit a445a22

Browse files
authored
fix compiler (#6615)
1 parent 020ff30 commit a445a22

File tree

4 files changed

+44
-10
lines changed

4 files changed

+44
-10
lines changed

Lib/test/test_inspect/test_inspect.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -957,8 +957,6 @@ def test_getsource_stdlib_decimal(self):
957957

958958
class TestGetsourceInteractive(unittest.TestCase):
959959
@support.force_not_colorized
960-
# TODO: RUSTPYTHON
961-
@unittest.expectedFailure
962960
def test_getclasses_interactive(self):
963961
# bpo-44648: simulate a REPL session;
964962
# there is no `__file__` in the __main__ module

Lib/test/test_unittest/test_case.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1970,8 +1970,6 @@ def testNoCycles(self):
19701970
del case
19711971
self.assertFalse(wr())
19721972

1973-
# TODO: RUSTPYTHON
1974-
@unittest.expectedFailure
19751973
def test_no_exception_leak(self):
19761974
# Issue #19880: TestCase.run() should not keep a reference
19771975
# to the exception

crates/vm/src/frame.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1739,7 +1739,42 @@ impl ExecutingFrame<'_> {
17391739
// We do not have any more blocks to unwind. Inspect the reason we are here:
17401740
match reason {
17411741
UnwindReason::Raising { exception } => Err(exception),
1742-
UnwindReason::Returning { value } => Ok(Some(ExecutionResult::Return(value))),
1742+
UnwindReason::Returning { value } => {
1743+
// Clear tracebacks of exceptions in fastlocals to break reference cycles.
1744+
// This is needed because when returning from inside an except block,
1745+
// the exception cleanup code (e = None; del e) is skipped, leaving the
1746+
// exception with a traceback that references this frame, which references
1747+
// the exception in fastlocals, creating a cycle that can't be collected
1748+
// since RustPython doesn't have a tracing GC.
1749+
//
1750+
// We only clear tracebacks of exceptions that:
1751+
// 1. Are not the return value itself (will be needed by caller)
1752+
// 2. Are not the current active exception (still being handled)
1753+
// 3. Have a traceback whose top frame is THIS frame (we created it)
1754+
let current_exc = vm.current_exception();
1755+
let fastlocals = self.fastlocals.lock();
1756+
for obj in fastlocals.iter().flatten() {
1757+
// Skip if this object is the return value
1758+
if obj.is(&value) {
1759+
continue;
1760+
}
1761+
if let Ok(exc) = obj.clone().downcast::<PyBaseException>() {
1762+
// Skip if this is the current active exception
1763+
if current_exc.as_ref().is_some_and(|cur| exc.is(cur)) {
1764+
continue;
1765+
}
1766+
// Only clear if traceback's top frame is this frame
1767+
if exc
1768+
.__traceback__()
1769+
.is_some_and(|tb| core::ptr::eq::<Py<Frame>>(&*tb.frame, self.object))
1770+
{
1771+
exc.set_traceback_typed(None);
1772+
}
1773+
}
1774+
}
1775+
drop(fastlocals);
1776+
Ok(Some(ExecutionResult::Return(value)))
1777+
}
17431778
UnwindReason::Break { .. } | UnwindReason::Continue { .. } => {
17441779
self.fatal("break or continue must occur within a loop block.")
17451780
} // UnwindReason::NoWorries => Ok(None),

crates/vm/src/vm/compile.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,14 @@ impl VirtualMachine {
115115
.compile(source, compiler::Mode::Exec, source_path.clone())
116116
.map_err(|err| self.new_syntax_error(&err, Some(source)))?;
117117
// trace!("Code object: {:?}", code_obj.borrow());
118-
scope.globals.set_item(
119-
identifier!(self, __file__),
120-
self.new_pyobj(source_path),
121-
self,
122-
)?;
118+
// Only set __file__ for real file paths, not pseudo-paths like <string>
119+
if !(source_path.starts_with('<') && source_path.ends_with('>')) {
120+
scope.globals.set_item(
121+
identifier!(self, __file__),
122+
self.new_pyobj(source_path),
123+
self,
124+
)?;
125+
}
123126
self.run_code_obj(code_obj, scope)
124127
}
125128

0 commit comments

Comments
 (0)