Skip to content

Commit e8698aa

Browse files
authored
Match SymbolTable repr with CPython format (#7139)
1 parent 93d83c1 commit e8698aa

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Lib/test/test_symtable.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,6 @@ def test_symbol_repr(self):
543543
self.assertEqual(repr(class_A.lookup('x')),
544544
"<symbol 'x': LOCAL, DEF_LOCAL|DEF_FREE_CLASS>")
545545

546-
@unittest.expectedFailure # TODO: RUSTPYTHON
547546
def test_symtable_entry_repr(self):
548547
expected = f"<symtable entry top({self.top.get_id()}), line {self.top.get_lineno()}>"
549548
self.assertEqual(repr(self.top._table), expected)

crates/vm/src/stdlib/symtable.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ pub(crate) use _symtable::module_def;
33
#[pymodule]
44
mod _symtable {
55
use crate::{
6-
PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
6+
Py, PyObjectRef, PyPayload, PyRef, PyResult, VirtualMachine,
77
builtins::{PyDictRef, PyStrRef},
88
compiler,
9+
types::Representable,
910
};
1011
use alloc::fmt;
1112
use rustpython_codegen::symboltable::{
@@ -132,7 +133,7 @@ mod _symtable {
132133
}
133134

134135
#[pyattr]
135-
#[pyclass(name = "SymbolTable")]
136+
#[pyclass(name = "symtable entry")]
136137
#[derive(PyPayload)]
137138
struct PySymbolTable {
138139
symtable: SymbolTable,
@@ -144,7 +145,7 @@ mod _symtable {
144145
}
145146
}
146147

147-
#[pyclass]
148+
#[pyclass(with(Representable))]
148149
impl PySymbolTable {
149150
#[pygetset]
150151
fn name(&self) -> String {
@@ -210,6 +211,19 @@ mod _symtable {
210211
}
211212
}
212213

214+
impl Representable for PySymbolTable {
215+
#[inline]
216+
fn repr_str(zelf: &Py<Self>, vm: &VirtualMachine) -> PyResult<String> {
217+
Ok(format!(
218+
"<{} {}({}), line {}>",
219+
Self::class(&vm.ctx).name(),
220+
zelf.symtable.name,
221+
zelf.id(),
222+
zelf.symtable.line_number
223+
))
224+
}
225+
}
226+
213227
#[pyattr]
214228
#[pyclass(name = "Symbol")]
215229
#[derive(PyPayload)]

0 commit comments

Comments
 (0)