mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-10 05:30:16 +00:00
change read behaviour, test seek, implement read(size=-1) later
This commit is contained in:
parent
c38a94da53
commit
042a5ef85f
@ -55,10 +55,11 @@ unsigned char* _default_import_handler(const char* name_p, int name_size, int* o
|
||||
|
||||
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`
|
||||
|
||||
@ -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()
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user