Skip to content

Commit 0e95f51

Browse files
authored
Merge pull request #1173 from corona10/seekable
stdlib.io: Implement seekable()
2 parents fe44179 + 29248fd commit 0e95f51

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

tests/snippets/stdlib_io.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
from io import BufferedReader, FileIO
1+
from io import BufferedReader, FileIO, StringIO, BytesIO
22
import os
33

44
fi = FileIO('README.md')
5+
assert fi.seekable()
56
bb = BufferedReader(fi)
7+
assert bb.seekable()
68

79
result = bb.read()
810

vm/src/stdlib/io.rs

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ impl PyStringIORef {
128128
}
129129
}
130130

131+
fn seekable(self, _vm: &VirtualMachine) -> bool {
132+
true
133+
}
134+
131135
//Read k bytes from the object and return.
132136
//If k is undefined || k == -1, then we read all bytes until the end of the file.
133137
//This also increments the stream position by the value of k
@@ -205,6 +209,10 @@ impl PyBytesIORef {
205209
None => Err(vm.new_value_error("Error Performing Operation".to_string())),
206210
}
207211
}
212+
213+
fn seekable(self, _vm: &VirtualMachine) -> bool {
214+
true
215+
}
208216
}
209217

210218
fn bytes_io_new(
@@ -244,7 +252,11 @@ fn io_base_cm_exit(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
244252
}
245253

246254
// TODO Check if closed, then if so raise ValueError
247-
fn io_base_flush(_zelf: PyObjectRef, _vm: &VirtualMachine) {}
255+
fn io_base_flush(_self: PyObjectRef, _vm: &VirtualMachine) {}
256+
257+
fn io_base_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
258+
Ok(vm.ctx.new_bool(false))
259+
}
248260

249261
fn buffered_io_base_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
250262
arg_check!(vm, args, required = [(buffered, None), (raw, None)]);
@@ -282,6 +294,10 @@ fn buffered_reader_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
282294
Ok(vm.ctx.new_bytes(result))
283295
}
284296

297+
fn buffered_reader_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
298+
Ok(vm.ctx.new_bool(true))
299+
}
300+
285301
fn compute_c_flag(mode: &str) -> u32 {
286302
let flags = match mode.chars().next() {
287303
Some(mode) => match mode {
@@ -415,6 +431,10 @@ fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
415431
}
416432
}
417433

434+
fn file_io_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
435+
Ok(vm.ctx.new_bool(true))
436+
}
437+
418438
fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
419439
arg_check!(
420440
vm,
@@ -428,6 +448,10 @@ fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
428448
vm.call_method(&raw, "write", vec![obj.clone()])
429449
}
430450

451+
fn buffered_writer_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
452+
Ok(vm.ctx.new_bool(true))
453+
}
454+
431455
fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
432456
arg_check!(
433457
vm,
@@ -439,6 +463,10 @@ fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
439463
Ok(vm.get_none())
440464
}
441465

466+
fn text_io_wrapper_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult {
467+
Ok(vm.new_bool(true))
468+
}
469+
442470
fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult {
443471
arg_check!(vm, args, required = [(text_io_base, None)]);
444472

@@ -627,6 +655,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
627655
let io_base = py_class!(ctx, "IOBase", ctx.object(), {
628656
"__enter__" => ctx.new_rustfunc(io_base_cm_enter),
629657
"__exit__" => ctx.new_rustfunc(io_base_cm_exit),
658+
"seekable" => ctx.new_rustfunc(io_base_seekable),
630659
"flush" => ctx.new_rustfunc(io_base_flush)
631660
});
632661

@@ -648,7 +677,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
648677
"name" => ctx.str_type(),
649678
"read" => ctx.new_rustfunc(file_io_read),
650679
"readinto" => ctx.new_rustfunc(file_io_readinto),
651-
"write" => ctx.new_rustfunc(file_io_write)
680+
"write" => ctx.new_rustfunc(file_io_write),
681+
"seekable" => ctx.new_rustfunc(file_io_seekable)
652682
});
653683

654684
// BufferedIOBase Subclasses
@@ -657,26 +687,30 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
657687
//consistent with the python model
658688
//For more info see: https://github.com/RustPython/RustPython/issues/547
659689
"__init__" => ctx.new_rustfunc(buffered_io_base_init),
660-
"read" => ctx.new_rustfunc(buffered_reader_read)
690+
"read" => ctx.new_rustfunc(buffered_reader_read),
691+
"seekable" => ctx.new_rustfunc(buffered_reader_seekable)
661692
});
662693

663694
let buffered_writer = py_class!(ctx, "BufferedWriter", buffered_io_base.clone(), {
664695
//workaround till the buffered classes can be fixed up to be more
665696
//consistent with the python model
666697
//For more info see: https://github.com/RustPython/RustPython/issues/547
667698
"__init__" => ctx.new_rustfunc(buffered_io_base_init),
668-
"write" => ctx.new_rustfunc(buffered_writer_write)
699+
"write" => ctx.new_rustfunc(buffered_writer_write),
700+
"seekable" => ctx.new_rustfunc(buffered_writer_seekable)
669701
});
670702

671703
//TextIOBase Subclass
672704
let text_io_wrapper = py_class!(ctx, "TextIOWrapper", text_io_base.clone(), {
673-
"__init__" => ctx.new_rustfunc(text_io_wrapper_init)
705+
"__init__" => ctx.new_rustfunc(text_io_wrapper_init),
706+
"seekable" => ctx.new_rustfunc(text_io_wrapper_seekable)
674707
});
675708

676709
//StringIO: in-memory text
677710
let string_io = py_class!(ctx, "StringIO", text_io_base.clone(), {
678711
"__new__" => ctx.new_rustfunc(string_io_new),
679712
"seek" => ctx.new_rustfunc(PyStringIORef::seek),
713+
"seekable" => ctx.new_rustfunc(PyStringIORef::seekable),
680714
"read" => ctx.new_rustfunc(PyStringIORef::read),
681715
"write" => ctx.new_rustfunc(PyStringIORef::write),
682716
"getvalue" => ctx.new_rustfunc(PyStringIORef::getvalue)
@@ -688,6 +722,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef {
688722
"read" => ctx.new_rustfunc(PyBytesIORef::read),
689723
"read1" => ctx.new_rustfunc(PyBytesIORef::read),
690724
"seek" => ctx.new_rustfunc(PyBytesIORef::seek),
725+
"seekable" => ctx.new_rustfunc(PyBytesIORef::seekable),
691726
"write" => ctx.new_rustfunc(PyBytesIORef::write),
692727
"getvalue" => ctx.new_rustfunc(PyBytesIORef::getvalue)
693728
});

0 commit comments

Comments
 (0)