| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,8 +1,10 @@ | |||
| 1 | - from io import BufferedReader, FileIO | ||
| 1 | + from io import BufferedReader, FileIO, StringIO, BytesIO | ||
| 2 | 2 | import os | |
| 3 | 3 | ||
| 4 | 4 | fi = FileIO('README.md') | |
| 5 | + assert fi.seekable() | ||
| 5 | 6 | bb = BufferedReader(fi) | |
| 7 | + assert bb.seekable() | ||
| 6 | 8 | ||
| 7 | 9 | result = bb.read() | |
| 8 | 10 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -128,6 +128,10 @@ impl PyStringIORef { | |||
| 128 | 128 | } | |
| 129 | 129 | } | |
| 130 | 130 | ||
| 131 | + fn seekable(self, vm: &VirtualMachine) -> PyResult { | ||
| 132 | + Ok(vm.ctx.new_bool(true)) | ||
| 133 | + } | ||
| 134 | + | ||
| 131 | 135 | //Read k bytes from the object and return. | |
| 132 | 136 | //If k is undefined || k == -1, then we read all bytes until the end of the file. | |
| 133 | 137 | //This also increments the stream position by the value of k | |
@@ -205,6 +209,10 @@ impl PyBytesIORef { | |||
| 205 | 209 | None => Err(vm.new_value_error("Error Performing Operation".to_string())), | |
| 206 | 210 | } | |
| 207 | 211 | } | |
| 212 | + | ||
| 213 | + fn seekable(self, vm: &VirtualMachine) -> PyResult { | ||
| 214 | + Ok(vm.ctx.new_bool(true)) | ||
| 215 | + } | ||
| 208 | 216 | } | |
| 209 | 217 | ||
| 210 | 218 | fn bytes_io_new( | |
@@ -244,7 +252,11 @@ fn io_base_cm_exit(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |||
| 244 | 252 | } | |
| 245 | 253 | ||
| 246 | 254 | // 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 | + } | ||
| 248 | 260 | ||
| 249 | 261 | fn buffered_io_base_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |
| 250 | 262 | arg_check!(vm, args, required = [(buffered, None), (raw, None)]); | |
@@ -282,6 +294,10 @@ fn buffered_reader_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |||
| 282 | 294 | Ok(vm.ctx.new_bytes(result)) | |
| 283 | 295 | } | |
| 284 | 296 | ||
| 297 | + fn buffered_reader_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { | ||
| 298 | + Ok(vm.ctx.new_bool(true)) | ||
| 299 | + } | ||
| 300 | + | ||
| 285 | 301 | fn compute_c_flag(mode: &str) -> u32 { | |
| 286 | 302 | let flags = match mode.chars().next() { | |
| 287 | 303 | Some(mode) => match mode { | |
@@ -415,6 +431,10 @@ fn file_io_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |||
| 415 | 431 | } | |
| 416 | 432 | } | |
| 417 | 433 | ||
| 434 | + fn file_io_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { | ||
| 435 | + Ok(vm.ctx.new_bool(true)) | ||
| 436 | + } | ||
| 437 | + | ||
| 418 | 438 | fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |
| 419 | 439 | arg_check!( | |
| 420 | 440 | vm, | |
@@ -428,6 +448,10 @@ fn buffered_writer_write(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |||
| 428 | 448 | vm.call_method(&raw, "write", vec![obj.clone()]) | |
| 429 | 449 | } | |
| 430 | 450 | ||
| 451 | + fn buffered_writer_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { | ||
| 452 | + Ok(vm.ctx.new_bool(true)) | ||
| 453 | + } | ||
| 454 | + | ||
| 431 | 455 | fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |
| 432 | 456 | arg_check!( | |
| 433 | 457 | vm, | |
@@ -439,6 +463,10 @@ fn text_io_wrapper_init(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |||
| 439 | 463 | Ok(vm.get_none()) | |
| 440 | 464 | } | |
| 441 | 465 | ||
| 466 | + fn text_io_wrapper_seekable(vm: &VirtualMachine, _args: PyFuncArgs) -> PyResult { | ||
| 467 | + Ok(vm.new_bool(true)) | ||
| 468 | + } | ||
| 469 | + | ||
| 442 | 470 | fn text_io_base_read(vm: &VirtualMachine, args: PyFuncArgs) -> PyResult { | |
| 443 | 471 | arg_check!(vm, args, required = [(text_io_base, None)]); | |
| 444 | 472 | ||
@@ -627,6 +655,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { | |||
| 627 | 655 | let io_base = py_class!(ctx, "IOBase", ctx.object(), { | |
| 628 | 656 | "__enter__" => ctx.new_rustfunc(io_base_cm_enter), | |
| 629 | 657 | "__exit__" => ctx.new_rustfunc(io_base_cm_exit), | |
| 658 | + "seekable" => ctx.new_rustfunc(io_base_seekable), | ||
| 630 | 659 | "flush" => ctx.new_rustfunc(io_base_flush) | |
| 631 | 660 | }); | |
| 632 | 661 | ||
@@ -648,7 +677,8 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { | |||
| 648 | 677 | "name" => ctx.str_type(), | |
| 649 | 678 | "read" => ctx.new_rustfunc(file_io_read), | |
| 650 | 679 | "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) | ||
| 652 | 682 | }); | |
| 653 | 683 | ||
| 654 | 684 | // BufferedIOBase Subclasses | |
@@ -657,26 +687,30 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { | |||
| 657 | 687 | //consistent with the python model | |
| 658 | 688 | //For more info see: https://github.com/RustPython/RustPython/issues/547 | |
| 659 | 689 | "__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) | ||
| 661 | 692 | }); | |
| 662 | 693 | ||
| 663 | 694 | let buffered_writer = py_class!(ctx, "BufferedWriter", buffered_io_base.clone(), { | |
| 664 | 695 | //workaround till the buffered classes can be fixed up to be more | |
| 665 | 696 | //consistent with the python model | |
| 666 | 697 | //For more info see: https://github.com/RustPython/RustPython/issues/547 | |
| 667 | 698 | "__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) | ||
| 669 | 701 | }); | |
| 670 | 702 | ||
| 671 | 703 | //TextIOBase Subclass | |
| 672 | 704 | 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) | ||
| 674 | 707 | }); | |
| 675 | 708 | ||
| 676 | 709 | //StringIO: in-memory text | |
| 677 | 710 | let string_io = py_class!(ctx, "StringIO", text_io_base.clone(), { | |
| 678 | 711 | "__new__" => ctx.new_rustfunc(string_io_new), | |
| 679 | 712 | "seek" => ctx.new_rustfunc(PyStringIORef::seek), | |
| 713 | + "seekable" => ctx.new_rustfunc(PyStringIORef::seekable), | ||
| 680 | 714 | "read" => ctx.new_rustfunc(PyStringIORef::read), | |
| 681 | 715 | "write" => ctx.new_rustfunc(PyStringIORef::write), | |
| 682 | 716 | "getvalue" => ctx.new_rustfunc(PyStringIORef::getvalue) | |
@@ -688,6 +722,7 @@ pub fn make_module(vm: &VirtualMachine) -> PyObjectRef { | |||
| 688 | 722 | "read" => ctx.new_rustfunc(PyBytesIORef::read), | |
| 689 | 723 | "read1" => ctx.new_rustfunc(PyBytesIORef::read), | |
| 690 | 724 | "seek" => ctx.new_rustfunc(PyBytesIORef::seek), | |
| 725 | + "seekable" => ctx.new_rustfunc(PyBytesIORef::seekable), | ||
| 691 | 726 | "write" => ctx.new_rustfunc(PyBytesIORef::write), | |
| 692 | 727 | "getvalue" => ctx.new_rustfunc(PyBytesIORef::getvalue) | |
| 693 | 728 | }); | |
| Back | FazBrowse Home | New Git URL |
0 commit comments