Skip to content

Commit 8548bbd

Browse files
authored
Merge pull request RustPython#3723 from deantvv/io-buffered-args
io: enhance error message in Buffered* class
2 parents 01a2127 + 121b35d commit 8548bbd

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
@@ -1367,6 +1367,7 @@ mod _io {
13671367

13681368
#[pyimpl]
13691369
trait BufferedMixin: PyPayload {
1370+
const CLASS_NAME: &'static str;
13701371
const READABLE: bool;
13711372
const WRITABLE: bool;
13721373
const SEEKABLE: bool = false;
@@ -1380,7 +1381,11 @@ mod _io {
13801381
#[pyslot]
13811382
fn slot_init(zelf: PyObjectRef, args: FuncArgs, vm: &VirtualMachine) -> PyResult<()> {
13821383
let zelf: PyRef<Self> = zelf.try_into_value(vm)?;
1383-
let (raw, BufferSize { buffer_size }): (PyObjectRef, _) = args.bind(vm)?;
1384+
let (raw, BufferSize { buffer_size }): (PyObjectRef, _) =
1385+
args.bind(vm).map_err(|e| {
1386+
let msg = format!("{}() {}", Self::CLASS_NAME, *e.str(vm));
1387+
vm.new_exception_msg(e.class().clone(), msg)
1388+
})?;
13841389
zelf.init(raw, BufferSize { buffer_size }, vm)
13851390
}
13861391

@@ -1663,6 +1668,7 @@ mod _io {
16631668
data: PyThreadMutex<BufferedData>,
16641669
}
16651670
impl BufferedMixin for BufferedReader {
1671+
const CLASS_NAME: &'static str = "BufferedReader";
16661672
const READABLE: bool = true;
16671673
const WRITABLE: bool = false;
16681674
fn data(&self) -> &PyThreadMutex<BufferedData> {
@@ -1712,6 +1718,7 @@ mod _io {
17121718
data: PyThreadMutex<BufferedData>,
17131719
}
17141720
impl BufferedMixin for BufferedWriter {
1721+
const CLASS_NAME: &'static str = "BufferedWriter";
17151722
const READABLE: bool = false;
17161723
const WRITABLE: bool = true;
17171724
fn data(&self) -> &PyThreadMutex<BufferedData> {
@@ -1740,6 +1747,7 @@ mod _io {
17401747
data: PyThreadMutex<BufferedData>,
17411748
}
17421749
impl BufferedMixin for BufferedRandom {
1750+
const CLASS_NAME: &'static str = "BufferedRandom";
17431751
const READABLE: bool = true;
17441752
const WRITABLE: bool = true;
17451753
const SEEKABLE: bool = true;

0 commit comments

Comments
 (0)