Skip to content

Commit dda4ab0

Browse files
committed
Convert io.{TextIOWrapper, BufferedReader} to new arg style
1 parent e8b0185 commit dda4ab0

File tree

1 file changed

+24
-36
lines changed

1 file changed

+24
-36
lines changed

vm/src/stdlib/io.rs

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -458,43 +458,33 @@ fn file_io_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
458458
true
459459
}
460460

461-
fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
462-
arg_check!(
463-
vm,
464-
args,
465-
required = [(buffered, None), (obj, Some(vm.ctx.bytes_type()))]
466-
);
467-
468-
let raw = vm.get_attribute(buffered.clone(), "raw").unwrap();
461+
fn buffered_writer_write(instance: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult {
462+
let raw = vm.get_attribute(instance, "raw").unwrap();
469463

470464
//This should be replaced with a more appropriate chunking implementation
471465
vm.call_method(&raw, "write", vec![obj.clone()])
472466
}
473467

474-
fn buffered_writer_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
475-
Ok(vm.ctx.new_bool(true))
468+
fn buffered_writer_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
469+
true
476470
}
477471

478-
fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
479-
arg_check!(
480-
vm,
481-
args,
482-
required = [(text_io_wrapper, None), (buffer, None)]
483-
);
484-
485-
vm.set_attr(text_io_wrapper, "buffer", buffer.clone())?;
486-
Ok(vm.get_none())
472+
fn text_io_wrapper_init(
473+
instance: PyObjectRef,
474+
buffer: PyObjectRef,
475+
vm: &VirtualMachine,
476+
) -> PyResult<()> {
477+
vm.set_attr(&instance, "buffer", buffer.clone())?;
478+
Ok(())
487479
}
488480

489-
fn text_io_wrapper_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
490-
Ok(vm.new_bool(true))
481+
fn text_io_wrapper_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
482+
true
491483
}
492484

493-
fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
494-
arg_check!(vm, args, required = [(text_io_base, None)]);
495-
485+
fn text_io_base_read(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
496486
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
497-
let raw = vm.get_attribute(text_io_base.clone(), "buffer").unwrap();
487+
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
498488

499489
if !objtype::isinstance(&raw, &buffered_reader_class) {
500490
// TODO: this should be io.UnsupportedOperation error which derives both from ValueError *and* OSError
@@ -511,30 +501,28 @@ fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
511501
e.utf8_error().valid_up_to()
512502
))
513503
})?;
514-
Ok(vm.ctx.new_str(rust_string))
504+
Ok(rust_string)
515505
} else {
516506
Err(vm.new_value_error("Error unpacking Bytes".to_string()))
517507
}
518508
}
519509

520-
fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
510+
fn text_io_base_write(
511+
instance: PyObjectRef,
512+
obj: PyStringRef,
513+
vm: &VirtualMachine,
514+
) -> PyResult<usize> {
521515
use std::str::from_utf8;
522516

523-
arg_check!(
524-
vm,
525-
args,
526-
required = [(text_io_base, None), (obj, Some(vm.ctx.str_type()))]
527-
);
528-
529517
let buffered_writer_class = vm.try_class("_io", "BufferedWriter")?;
530-
let raw = vm.get_attribute(text_io_base.clone(), "buffer").unwrap();
518+
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
531519

532520
if !objtype::isinstance(&raw, &buffered_writer_class) {
533521
// TODO: this should be io.UnsupportedOperation error which derives from ValueError and OSError
534522
return Err(vm.new_value_error("not writable".to_string()));
535523
}
536524

537-
let bytes = objstr::get_value(obj).into_bytes();
525+
let bytes = obj.as_str().to_string().into_bytes();
538526

539527
let len = vm.call_method(&raw, "write", vec![vm.ctx.new_bytes(bytes.clone())])?;
540528
let len = objint::get_value(&len).to_usize().ok_or_else(|| {
@@ -546,7 +534,7 @@ fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
546534
.unwrap_or_else(|e| from_utf8(&bytes[..e.valid_up_to()]).unwrap())
547535
.chars()
548536
.count();
549-
Ok(vm.ctx.new_int(len))
537+
Ok(len)
550538
}
551539

552540
fn split_mode_string(mode_string: String) -> Result<(String, String), String> {

0 commit comments

Comments
 (0)