Match SymbolTable repr with CPython format (#7139)

This commit is contained in:
Lee Dogeon
2026-02-14 22:59:42 +09:00
committed by GitHub
parent 93d83c1ce9
commit e8698aa19f
2 changed files with 17 additions and 4 deletions

View File

@@ -543,7 +543,6 @@ class SymtableTest(unittest.TestCase):
self.assertEqual(repr(class_A.lookup('x')),
"<symbol 'x': LOCAL, DEF_LOCAL|DEF_FREE_CLASS>")
@unittest.expectedFailure # TODO: RUSTPYTHON
def test_symtable_entry_repr(self):
expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>"
self.assertEqual(repr(self.top._table), expected)

View File

@@ -3,9 +3,10 @@ pub(crate) use _symtable::module_def;
#[pymodule]
mod _symtable {
use crate::{
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
builtins::{PyDictRef, PyStrRef},
compiler,
types::Representable,
};
use alloc::fmt;
use rustpython_codegen::symboltable::{
@@ -132,7 +133,7 @@ mod _symtable {
}
#[pyattr]
#[pyclass(name = "SymbolTable")]
#[pyclass(name = "symtable entry")]
#[derive(PyPayload)]
struct PySymbolTable {
symtable: SymbolTable,
@@ -144,7 +145,7 @@ mod _symtable {
}
}
#[pyclass]
#[pyclass(with(Representable))]
impl PySymbolTable {
#[pygetset]
fn name(&self) -> String {
@@ -210,6 +211,19 @@ mod _symtable {
}
}
impl Representable for PySymbolTable {
#[inline]
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
Ok(format!(
"<{} {}({}), line {}>",
Self::class(&vm.ctx).name(),
zelf.symtable.name,
zelf.id(),
zelf.symtable.line_number
))
}
}
#[pyattr]
#[pyclass(name = "Symbol")]
#[derive(PyPayload)]