PyTraceback Constructor (#5958)

This commit is contained in:
Jeong, YunWon
2025-07-13 10:20:07 +09:00
committed by GitHub
parent 52d46326de
commit 16aaad7aeb
2 changed files with 16 additions and 5 deletions

View File

@@ -270,8 +270,6 @@ class TestTracebackType(unittest.TestCase):
tb.tb_next = new_tb
self.assertIs(tb.tb_next, new_tb)
# TODO: RUSTPYTHON
@unittest.expectedFailure
def test_constructor(self):
other_tb = get_tb()
frame = sys._getframe()

View File

@@ -1,8 +1,9 @@
use rustpython_common::lock::PyMutex;
use super::PyType;
use super::{PyType, PyTypeRef};
use crate::{
Context, Py, PyPayload, PyRef, class::PyClassImpl, frame::FrameRef, source::LineNumber,
Context, Py, PyPayload, PyRef, PyResult, VirtualMachine, class::PyClassImpl, frame::FrameRef,
source::LineNumber, types::Constructor,
};
#[pyclass(module = false, name = "traceback", traverse)]
@@ -25,7 +26,7 @@ impl PyPayload for PyTraceback {
}
}
#[pyclass]
#[pyclass(with(Constructor))]
impl PyTraceback {
pub const fn new(
next: Option<PyRef<Self>>,
@@ -67,6 +68,18 @@ impl PyTraceback {
}
}
impl Constructor for PyTraceback {
type Args = (Option<PyRef<PyTraceback>>, FrameRef, u32, usize);
fn py_new(cls: PyTypeRef, args: Self::Args, vm: &VirtualMachine) -> PyResult {
let (next, frame, lasti, lineno) = args;
let lineno = LineNumber::new(lineno)
.ok_or_else(|| vm.new_value_error("lineno must be positive".to_owned()))?;
let tb = PyTraceback::new(next, frame, lasti, lineno);
tb.into_ref_with_type(vm, cls).map(Into::into)
}
}
impl PyTracebackRef {
pub fn iter(&self) -> impl Iterator<Item = Self> {
std::iter::successors(Some(self.clone()), |tb| tb.next.lock().clone())