Skip to content

Commit 121b35d

Browse files
committed
io: enhance error message in Buffered* class
1 parent 9fd4f7b commit 121b35d

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

Lib/test/test_io.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1593,8 +1593,6 @@ def test_garbage_collection(self):
15931593
support.gc_collect()
15941594
self.assertIsNone(wr(), wr)
15951595

1596-
# TODO: RUSTPYTHON
1597-
@unittest.expectedFailure
15981596
def test_args_error(self):
15991597
# Issue #17275
16001598
with self.assertRaisesRegex(TypeError, "BufferedReader"):
@@ -1974,8 +1972,6 @@ def test_garbage_collection(self):
19741972
with self.open(os_helper.TESTFN, "rb") as f:
19751973
self.assertEqual(f.read(), b"123xxx")
19761974

1977-
# TODO: RUSTPYTHON
1978-
@unittest.expectedFailure
19791975
def test_args_error(self):
19801976
# Issue #17275
19811977
with self.assertRaisesRegex(TypeError, "BufferedWriter"):
@@ -2460,8 +2456,6 @@ def test_garbage_collection(self):
24602456
CBufferedReaderTest.test_garbage_collection(self)
24612457
CBufferedWriterTest.test_garbage_collection(self)
24622458

2463-
# TODO: RUSTPYTHON
2464-
@unittest.expectedFailure
24652459
def test_args_error(self):
24662460
# Issue #17275
24672461
with self.assertRaisesRegex(TypeError, "BufferedRandom"):

vm/src/stdlib/io.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1361,6 +1361,7 @@ mod _io {
13611361

13621362
#[pyimpl]
13631363
trait BufferedMixin: PyPayload {
1364+
const CLASS_NAME: &'static str;
13641365
const READABLE: bool;
13651366
const WRITABLE: bool;
13661367
const SEEKABLE: bool = false;
@@ -1374,7 +1375,11 @@ mod _io {
13741375
#[pyslot]
13751376
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
13761377
let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
1377-
let (raw, BufferSize { buffer_size }): (PyObjectRef, _) = args.bind(vm)?;
1378+
let (raw, BufferSize { buffer_size }): (PyObjectRef, _) =
1379+
args.bind(vm).map_err(|e| {
1380+
let msg = format!("{}() {}", Self::CLASS_NAME, *e.str(vm));
1381+
vm.new_exception_msg(e.class().clone(), msg)
1382+
})?;
13781383
zelf.init(raw, BufferSize { buffer_size }, vm)
13791384
}
13801385

@@ -1657,6 +1662,7 @@ mod _io {
16571662
data: PyThreadMutex<BufferedData>,
16581663
}
16591664
impl BufferedMixin for BufferedReader {
1665+
const CLASS_NAME: &'static str = "BufferedReader";
16601666
const READABLE: bool = true;
16611667
const WRITABLE: bool = false;
16621668
fn data(&self) -> &PyThreadMutex<BufferedData> {
@@ -1706,6 +1712,7 @@ mod _io {
17061712
data: PyThreadMutex<BufferedData>,
17071713
}
17081714
impl BufferedMixin for BufferedWriter {
1715+
const CLASS_NAME: &'static str = "BufferedWriter";
17091716
const READABLE: bool = false;
17101717
const WRITABLE: bool = true;
17111718
fn data(&self) -> &PyThreadMutex<BufferedData> {
@@ -1734,6 +1741,7 @@ mod _io {
17341741
data: PyThreadMutex<BufferedData>,
17351742
}
17361743
impl BufferedMixin for BufferedRandom {
1744+
const CLASS_NAME: &'static str = "BufferedRandom";
17371745
const READABLE: bool = true;
17381746
const WRITABLE: bool = true;
17391747
const SEEKABLE: bool = true;

0 commit comments

Comments
 (0)