mirror of
https://github.com/RustPython/RustPython.git
synced 2026-10-28 10:44:36 +08:00
Match SymbolTable repr with CPython format (#7139)
This commit is contained in:
1 parent
c5d1a78ea2
commit
dd05bac4a1
2 files changed
+17
-4
No files matched your search
Vendored
-1
@@ -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)
|
||||
|
||||
@@ -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)]
|
||||
|
||||
Reference in new issue
Block a user