change read behaviour, test seek, implement read(size=-1) later

This commit is contained in:
pmp-p 2023-12-22 11:54:26 +01:00
parent c38a94da53
commit 042a5ef85f
2 changed files with 12 additions and 3 deletions

View File

@ -48,17 +48,18 @@ unsigned char* _default_import_handler(const char* name_p, int name_size, int* o
#if PK_ENABLE_OS #if PK_ENABLE_OS
void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){ void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){
vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){ 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, CAST(Str&, args[1]).str(), CAST(Str&, args[2]).str()
); );
}); });
vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){ vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){
FileIO& io = CAST(FileIO&, args[0]); FileIO& io = CAST(FileIO&, args[0]);
int cur_pos = ftell(io.fp);
fseek(io.fp, 0, SEEK_END); fseek(io.fp, 0, SEEK_END);
int buffer_size = ftell(io.fp); int buffer_size = ftell(io.fp);
unsigned char* buffer = new unsigned char[buffer_size]; 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); size_t actual_size = io_fread(buffer, 1, buffer_size, io.fp);
PK_ASSERT(actual_size <= buffer_size); PK_ASSERT(actual_size <= buffer_size);
// in text mode, CR may be dropped, which may cause `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* mod = vm->new_module("os");
PyObject* path_obj = vm->heap.gcnew<DummyInstance>(vm->tp_object); PyObject* path_obj = vm->heap.gcnew<DummyInstance>(vm->tp_object);
mod->attr().set("path", path_obj); mod->attr().set("path", path_obj);
// Working directory is shared by all VMs!! // Working directory is shared by all VMs!!
vm->bind_func<0>(mod, "getcwd", [](VM* vm, ArgsView args){ vm->bind_func<0>(mod, "getcwd", [](VM* vm, ArgsView args){
return VAR(std::filesystem::current_path().string()); return VAR(std::filesystem::current_path().string());

View File

@ -41,9 +41,17 @@ os.remove('123.bin')
assert not os.path.exists('123.bin') assert not os.path.exists('123.bin')
f = open("123.txt","w+") f = open("123.txt","w+")
# read 0 sized file
assert( f.read() == "")
# write, rewind() and read whole back
f.write("123456") f.write("123456")
f.seek(0) f.seek(0)
assert ( f.read() == "123456" ) assert ( f.read() == "123456" )
f.seek(3)
assert ( f.read() == "456" )
# cannot test seek(>0) then read() for now # cannot test seek(>0) then read() for now
f.close() f.close()