Skip to content

Commit 8b31ee5

Browse files
authored
Merge pull request RustPython#1539 from palaviv/io-new-arg-style
Convert io to new arg style
2 parents 6270344 + dda4ab0 commit 8b31ee5

File tree

1 file changed

+74
-107
lines changed

1 file changed

+74
-107
lines changed

vm/src/stdlib/io.rs

Lines changed: 74 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -230,49 +230,37 @@ fn bytes_io_new(
230230
.into_ref_with_type(vm, cls)
231231
}
232232

233-
fn io_base_cm_enter(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
234-
arg_check!(vm, args, required = [(instance, None)]);
235-
Ok(instance.clone())
233+
fn io_base_cm_enter(instance: PyObjectRef, _vm: &VirtualMachine) -> PyObjectRef {
234+
instance.clone()
236235
}
237236

238-
fn io_base_cm_exit(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
239-
arg_check!(
240-
vm,
241-
args,
242-
// The context manager protocol requires these, but we don't use them
243-
required = [
244-
(_instance, None),
245-
(_exception_type, None),
246-
(_exception_value, None),
247-
(_traceback, None)
248-
]
249-
);
250-
Ok(vm.get_none())
251-
}
237+
fn io_base_cm_exit(_args: PyFuncArgs, _vm: &VirtualMachine) {}
252238

253239
// TODO Check if closed, then if so raise ValueError
254240
fn io_base_flush(_self: PyObjectRef, _vm: &VirtualMachine) {}
255241

256-
fn io_base_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
257-
Ok(vm.ctx.new_bool(false))
242+
fn io_base_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
243+
false
258244
}
259245

260-
fn buffered_io_base_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
261-
arg_check!(vm, args, required = [(buffered, None), (raw, None)]);
262-
vm.set_attr(buffered, "raw", raw.clone())?;
263-
Ok(vm.get_none())
246+
fn buffered_io_base_init(
247+
instance: PyObjectRef,
248+
raw: PyObjectRef,
249+
vm: &VirtualMachine,
250+
) -> PyResult<()> {
251+
vm.set_attr(&instance, "raw", raw.clone())?;
252+
Ok(())
264253
}
265254

266-
fn buffered_reader_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
267-
arg_check!(vm, args, required = [(buffered, None)]);
255+
fn buffered_reader_read(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<Vec<u8>> {
268256
let buff_size = 8 * 1024;
269257
let buffer = vm.ctx.new_bytearray(vec![0; buff_size]);
270258

271259
//buffer method
272260
let mut result = vec![];
273261
let mut length = buff_size;
274262

275-
let raw = vm.get_attribute(buffered.clone(), "raw").unwrap();
263+
let raw = vm.get_attribute(instance.clone(), "raw").unwrap();
276264

277265
//Iterates through the raw class, invoking the readinto method
278266
//to obtain buff_size many bytes. Exit when less than buff_size many
@@ -290,11 +278,11 @@ fn buffered_reader_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
290278
length = objint::get_value(&py_len).to_usize().unwrap();
291279
}
292280

293-
Ok(vm.ctx.new_bytes(result))
281+
Ok(result)
294282
}
295283

296-
fn buffered_reader_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
297-
Ok(vm.ctx.new_bool(true))
284+
fn buffered_reader_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
285+
true
298286
}
299287

300288
fn compute_c_flag(mode: &str) -> u32 {
@@ -342,55 +330,50 @@ fn file_io_init(
342330
Ok(vm.get_none())
343331
}
344332

345-
fn file_io_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
346-
arg_check!(
347-
vm,
348-
args,
349-
required = [(file_io, None)],
350-
optional = [(read_byte, Some(vm.ctx.int_type()))]
351-
);
352-
353-
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
333+
fn file_io_read(
334+
instance: PyObjectRef,
335+
read_byte: OptionalArg<usize>,
336+
vm: &VirtualMachine,
337+
) -> PyResult<Vec<u8>> {
338+
let file_no = vm.get_attribute(instance.clone(), "fileno")?;
354339
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
355340

356341
let mut handle = os::rust_file(raw_fd);
357342

358343
let bytes = match read_byte {
359-
None => {
344+
OptionalArg::Missing => {
360345
let mut bytes = vec![];
361346
handle
362347
.read_to_end(&mut bytes)
363348
.map_err(|_| vm.new_value_error("Error reading from Buffer".to_string()))?;
364349
bytes
365350
}
366-
Some(read_byte) => {
367-
let mut bytes = vec![0; objint::get_value(&read_byte).to_usize().unwrap()];
351+
OptionalArg::Present(read_byte) => {
352+
let mut bytes = vec![0; read_byte];
368353
handle
369354
.read_exact(&mut bytes)
370355
.map_err(|_| vm.new_value_error("Error reading from Buffer".to_string()))?;
371356
let updated = os::raw_file_number(handle);
372-
vm.set_attr(file_io, "fileno", vm.ctx.new_int(updated))?;
357+
vm.set_attr(&instance, "fileno", vm.ctx.new_int(updated))?;
373358
bytes
374359
}
375360
};
376361

377-
Ok(vm.ctx.new_bytes(bytes))
362+
Ok(bytes)
378363
}
379364

380-
fn file_io_readinto(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
381-
arg_check!(vm, args, required = [(file_io, None), (obj, None)]);
382-
365+
fn file_io_readinto(instance: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
383366
if !obj.readonly() {
384-
return Ok(vm.new_type_error(
367+
return Err(vm.new_type_error(
385368
"readinto() argument must be read-write bytes-like object".to_string(),
386369
));
387370
}
388371

389372
//extract length of buffer
390-
let py_length = vm.call_method(obj, "__len__", PyFuncArgs::default())?;
373+
let py_length = vm.call_method(&obj, "__len__", PyFuncArgs::default())?;
391374
let length = objint::get_value(&py_length).to_u64().unwrap();
392375

393-
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
376+
let file_no = vm.get_attribute(instance.clone(), "fileno")?;
394377
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
395378

396379
//extract unix file descriptor.
@@ -409,14 +392,12 @@ fn file_io_readinto(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
409392
};
410393

411394
let updated = os::raw_file_number(f.into_inner());
412-
vm.set_attr(file_io, "fileno", vm.ctx.new_int(updated))?;
413-
Ok(vm.get_none())
395+
vm.set_attr(&instance, "fileno", vm.ctx.new_int(updated))?;
396+
Ok(())
414397
}
415398

416-
fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
417-
arg_check!(vm, args, required = [(file_io, None), (obj, None)]);
418-
419-
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
399+
fn file_io_write(instance: PyObjectRef, obj: PyObjectRef, vm: &VirtualMachine) -> PyResult<usize> {
400+
let file_no = vm.get_attribute(instance.clone(), "fileno")?;
420401
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
421402

422403
//unsafe block - creates file handle from the UNIX file descriptor
@@ -437,85 +418,73 @@ fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
437418
Ok(len) => {
438419
//reset raw fd on the FileIO object
439420
let updated = os::raw_file_number(handle);
440-
vm.set_attr(file_io, "fileno", vm.ctx.new_int(updated))?;
421+
vm.set_attr(&instance, "fileno", vm.ctx.new_int(updated))?;
441422

442423
//return number of bytes written
443-
Ok(vm.ctx.new_int(len))
424+
Ok(len)
444425
}
445426
Err(_) => Err(vm.new_value_error("Error Writing Bytes to Handle".to_string())),
446427
}
447428
}
448429

449430
#[cfg(windows)]
450-
fn file_io_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
431+
fn file_io_close(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
451432
use std::os::windows::io::IntoRawHandle;
452-
arg_check!(vm, args, required = [(file_io, None)]);
453-
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
433+
let file_no = vm.get_attribute(instance.clone(), "fileno")?;
454434
let raw_fd = objint::get_value(&file_no).to_i64().unwrap();
455435
let handle = os::rust_file(raw_fd);
456436
let raw_handle = handle.into_raw_handle();
457437
unsafe {
458438
kernel32::CloseHandle(raw_handle);
459439
}
460-
vm.set_attr(file_io, "closefd", vm.new_bool(true))?;
461-
vm.set_attr(file_io, "closed", vm.new_bool(true))?;
462-
Ok(vm.ctx.none())
440+
vm.set_attr(&instance, "closefd", vm.new_bool(true))?;
441+
vm.set_attr(&instance, "closed", vm.new_bool(true))?;
442+
Ok(())
463443
}
464444

465445
#[cfg(unix)]
466-
fn file_io_close(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
467-
arg_check!(vm, args, required = [(file_io, None)]);
468-
let file_no = vm.get_attribute(file_io.clone(), "fileno")?;
446+
fn file_io_close(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<()> {
447+
let file_no = vm.get_attribute(instance.clone(), "fileno")?;
469448
let raw_fd = objint::get_value(&file_no).to_i32().unwrap();
470449
unsafe {
471450
libc::close(raw_fd);
472451
}
473-
vm.set_attr(file_io, "closefd", vm.new_bool(true))?;
474-
vm.set_attr(file_io, "closed", vm.new_bool(true))?;
475-
Ok(vm.ctx.none())
452+
vm.set_attr(&instance, "closefd", vm.new_bool(true))?;
453+
vm.set_attr(&instance, "closed", vm.new_bool(true))?;
454+
Ok(())
476455
}
477456

478-
fn file_io_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
479-
Ok(vm.ctx.new_bool(true))
457+
fn file_io_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
458+
true
480459
}
481460

482-
fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
483-
arg_check!(
484-
vm,
485-
args,
486-
required = [(buffered, None), (obj, Some(vm.ctx.bytes_type()))]
487-
);
488-
489-
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();
490463

491464
//This should be replaced with a more appropriate chunking implementation
492465
vm.call_method(&raw, "write", vec![obj.clone()])
493466
}
494467

495-
fn buffered_writer_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
496-
Ok(vm.ctx.new_bool(true))
468+
fn buffered_writer_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
469+
true
497470
}
498471

499-
fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
500-
arg_check!(
501-
vm,
502-
args,
503-
required = [(text_io_wrapper, None), (buffer, None)]
504-
);
505-
506-
vm.set_attr(text_io_wrapper, "buffer", buffer.clone())?;
507-
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(())
508479
}
509480

510-
fn text_io_wrapper_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
511-
Ok(vm.new_bool(true))
481+
fn text_io_wrapper_seekable(_self: PyObjectRef, _vm: &VirtualMachine) -> bool {
482+
true
512483
}
513484

514-
fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
515-
arg_check!(vm, args, required = [(text_io_base, None)]);
516-
485+
fn text_io_base_read(instance: PyObjectRef, vm: &VirtualMachine) -> PyResult<String> {
517486
let buffered_reader_class = vm.try_class("_io", "BufferedReader")?;
518-
let raw = vm.get_attribute(text_io_base.clone(), "buffer").unwrap();
487+
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
519488

520489
if !objtype::isinstance(&raw, &buffered_reader_class) {
521490
// TODO: this should be io.UnsupportedOperation error which derives both from ValueError *and* OSError
@@ -532,30 +501,28 @@ fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
532501
e.utf8_error().valid_up_to()
533502
))
534503
})?;
535-
Ok(vm.ctx.new_str(rust_string))
504+
Ok(rust_string)
536505
} else {
537506
Err(vm.new_value_error("Error unpacking Bytes".to_string()))
538507
}
539508
}
540509

541-
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> {
542515
use std::str::from_utf8;
543516

544-
arg_check!(
545-
vm,
546-
args,
547-
required = [(text_io_base, None), (obj, Some(vm.ctx.str_type()))]
548-
);
549-
550517
let buffered_writer_class = vm.try_class("_io", "BufferedWriter")?;
551-
let raw = vm.get_attribute(text_io_base.clone(), "buffer").unwrap();
518+
let raw = vm.get_attribute(instance.clone(), "buffer").unwrap();
552519

553520
if !objtype::isinstance(&raw, &buffered_writer_class) {
554521
// TODO: this should be io.UnsupportedOperation error which derives from ValueError and OSError
555522
return Err(vm.new_value_error("not writable".to_string()));
556523
}
557524

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

560527
let len = vm.call_method(&raw, "write", vec![vm.ctx.new_bytes(bytes.clone())])?;
561528
let len = objint::get_value(&len).to_usize().ok_or_else(|| {
@@ -567,7 +534,7 @@ fn text_io_base_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
567534
.unwrap_or_else(|e| from_utf8(&bytes[..e.valid_up_to()]).unwrap())
568535
.chars()
569536
.count();
570-
Ok(vm.ctx.new_int(len))
537+
Ok(len)
571538
}
572539

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

0 commit comments

Comments
 (0)