diff --git a/src/io.cpp b/src/io.cpp index 172f6815..82c7e50f 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -48,17 +48,18 @@ unsigned char* _default_import_handler(const char* name_p, int name_size, int* o #if PK_ENABLE_OS void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){ vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){ - return VAR_T(FileIO, + return VAR_T(FileIO, vm, CAST(Str&, args[1]).str(), CAST(Str&, args[2]).str() ); }); vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){ FileIO& io = CAST(FileIO&, args[0]); + int cur_pos = ftell(io.fp); fseek(io.fp, 0, SEEK_END); int buffer_size = ftell(io.fp); unsigned char* buffer = new unsigned char[buffer_size]; - fseek(io.fp, 0, SEEK_SET); + fseek(io.fp, cur_pos, SEEK_SET); size_t actual_size = io_fread(buffer, 1, buffer_size, io.fp); PK_ASSERT(actual_size <= buffer_size); // in text mode, CR may be dropped, which may cause `actual_size < buffer_size` @@ -132,7 +133,7 @@ void add_module_os(VM* vm){ PyObject* mod = vm->new_module("os"); PyObject* path_obj = vm->heap.gcnew(vm->tp_object); mod->attr().set("path", path_obj); - + // Working directory is shared by all VMs!! vm->bind_func<0>(mod, "getcwd", [](VM* vm, ArgsView args){ return VAR(std::filesystem::current_path().string()); diff --git a/tests/70_file.py b/tests/70_file.py index 10eced7a..f3475f9b 100644 --- a/tests/70_file.py +++ b/tests/70_file.py @@ -41,9 +41,17 @@ os.remove('123.bin') assert not os.path.exists('123.bin') f = open("123.txt","w+") +# read 0 sized file +assert( f.read() == "") + +# write, rewind() and read whole back f.write("123456") f.seek(0) assert ( f.read() == "123456" ) + +f.seek(3) +assert ( f.read() == "456" ) + # cannot test seek(>0) then read() for now f.close()