mirror of
https://github.com/RustPython/RustPython.git
synced 2026-06-09 22:49:57 +09:00
Merge pull request #2641 from deantvv/direntry-repr
os: implement `repr` for `DirEntry`
This commit is contained in:
@@ -3853,8 +3853,6 @@ class TestScandir(unittest.TestCase):
|
||||
finally:
|
||||
os.chdir(old_dir)
|
||||
|
||||
# TODO: RUSTPYTHON
|
||||
@unittest.expectedFailure
|
||||
def test_repr(self):
|
||||
entry = self.create_file_entry()
|
||||
self.assertEqual(repr(entry), "<DirEntry 'file.txt'>")
|
||||
|
||||
@@ -28,7 +28,7 @@ use crate::pyobject::{
|
||||
PyStructSequence, PyValue, StaticType, TryFromObject, TypeProtocol,
|
||||
};
|
||||
use crate::slots::PyIter;
|
||||
use crate::vm::VirtualMachine;
|
||||
use crate::vm::{ReprGuard, VirtualMachine};
|
||||
|
||||
#[cfg(unix)]
|
||||
use std::os::unix::ffi as ffi_ext;
|
||||
@@ -830,6 +830,33 @@ mod _os {
|
||||
fn fspath(&self, vm: &VirtualMachine) -> PyResult {
|
||||
self.path(vm)
|
||||
}
|
||||
|
||||
#[pymethod(magic)]
|
||||
fn repr(zelf: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
|
||||
let name = match vm.get_attribute(zelf.clone(), "name") {
|
||||
Ok(name) => Some(name),
|
||||
Err(e)
|
||||
if e.isinstance(&vm.ctx.exceptions.attribute_error)
|
||||
|| e.isinstance(&vm.ctx.exceptions.value_error) =>
|
||||
{
|
||||
None
|
||||
}
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
if let Some(name) = name {
|
||||
if let Some(_guard) = ReprGuard::enter(vm, &zelf) {
|
||||
let repr = vm.to_repr(&name)?;
|
||||
Ok(format!("<{} {}>", zelf.class(), repr))
|
||||
} else {
|
||||
Err(vm.new_runtime_error(format!(
|
||||
"reentrant call inside {}.__repr__",
|
||||
zelf.class()
|
||||
)))
|
||||
}
|
||||
} else {
|
||||
Ok(format!("<{}>", zelf.class()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[pyattr]
|
||||
|
||||
Reference in New Issue
Block a user