mirror of
https://github.com/pocketpy/pocketpy
synced 2026-05-06 10:13:37 +00:00
Compare commits
3 Commits
77e2401905
...
66a3aae2f9
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
66a3aae2f9 | ||
|
|
5d30381e19 | ||
|
|
6cc3d80233 |
@ -136,6 +136,7 @@ static bool io_FileIO__exit__(int argc, py_Ref argv) {
|
||||
|
||||
static bool io_FileIO_read(int argc, py_Ref argv) {
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
if(ud->file == NULL) return ValueError("I/O operation on closed file");
|
||||
bool is_binary = ud->mode[strlen(ud->mode) - 1] == 'b';
|
||||
int size;
|
||||
if(argc == 1) {
|
||||
@ -146,24 +147,37 @@ static bool io_FileIO_read(int argc, py_Ref argv) {
|
||||
} else if(argc == 2) {
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
size = py_toint(py_arg(1));
|
||||
if (size < 0) {
|
||||
long current = ftell(ud->file);
|
||||
fseek(ud->file, 0, SEEK_END);
|
||||
size = ftell(ud->file);
|
||||
fseek(ud->file, current, SEEK_SET);
|
||||
}
|
||||
} else {
|
||||
return TypeError("read() takes at most 2 arguments (%d given)", argc);
|
||||
}
|
||||
if(is_binary) {
|
||||
void* dst = py_newbytes(py_retval(), size);
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_bytes_resize(py_retval(), actual_size);
|
||||
if(size > 0) {
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_bytes_resize(py_retval(), actual_size);
|
||||
}
|
||||
} else {
|
||||
void* dst = PK_MALLOC(size);
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_newstrv(py_retval(), (c11_sv){dst, actual_size});
|
||||
PK_FREE(dst);
|
||||
if(size > 0) {
|
||||
void* dst = PK_MALLOC(size);
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_newstrv(py_retval(), (c11_sv){dst, actual_size});
|
||||
PK_FREE(dst);
|
||||
} else {
|
||||
py_newstr(py_retval(), "");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_tell(int argc, py_Ref argv) {
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
if(ud->file == NULL) return ValueError("I/O operation on closed file");
|
||||
py_newint(py_retval(), ftell(ud->file));
|
||||
return true;
|
||||
}
|
||||
@ -173,6 +187,7 @@ static bool io_FileIO_seek(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
if(ud->file == NULL) return ValueError("I/O operation on closed file");
|
||||
long cookie = py_toint(py_arg(1));
|
||||
int whence = py_toint(py_arg(2));
|
||||
py_newint(py_retval(), fseek(ud->file, cookie, whence));
|
||||
@ -193,6 +208,7 @@ static bool io_FileIO_close(int argc, py_Ref argv) {
|
||||
static bool io_FileIO_write(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
if(ud->file == NULL) return ValueError("I/O operation on closed file");
|
||||
size_t written_size;
|
||||
if(ud->mode[strlen(ud->mode) - 1] == 'b') {
|
||||
PY_CHECK_ARG_TYPE(1, tp_bytes);
|
||||
@ -211,11 +227,36 @@ static bool io_FileIO_write(int argc, py_Ref argv) {
|
||||
static bool io_FileIO_flush(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
if(ud->file == NULL) return ValueError("I/O operation on closed file");
|
||||
fflush(ud->file);
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_readlines(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
py_newlist(py_retval());
|
||||
char* buf = NULL;
|
||||
size_t buf_size = 0;
|
||||
size_t len = 0;
|
||||
int c;
|
||||
while(true) {
|
||||
len = 0;
|
||||
while((c = fgetc(ud->file)) != EOF) {
|
||||
if(len + 1 >= buf_size) {
|
||||
buf_size = (buf_size == 0) ? 64 : buf_size * 2;
|
||||
buf = PK_REALLOC(buf, buf_size);
|
||||
}
|
||||
buf[len++] = (char)c;
|
||||
if(c == '\n') break;
|
||||
}
|
||||
if(len == 0) break;
|
||||
py_newstrv(py_getreg(0), (c11_sv){buf, len});
|
||||
py_list_append(py_retval(), py_getreg(0));
|
||||
}
|
||||
if(buf) PK_FREE(buf);
|
||||
return true;
|
||||
}
|
||||
void pk__add_module_io() {
|
||||
py_Ref mod = py_newmodule("io");
|
||||
|
||||
@ -230,6 +271,7 @@ void pk__add_module_io() {
|
||||
py_bindmethod(FileIO, "tell", io_FileIO_tell);
|
||||
py_bindmethod(FileIO, "seek", io_FileIO_seek);
|
||||
py_bindmethod(FileIO, "flush", io_FileIO_flush);
|
||||
py_bindmethod(FileIO, "readlines", io_FileIO_readlines);
|
||||
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);
|
||||
|
||||
@ -38,7 +38,7 @@ with open('123.txt', 'rt') as f:
|
||||
assert f.read() == ''
|
||||
f.seek(3, io.SEEK_SET)
|
||||
assert f.tell() == 3
|
||||
assert f.read() == '456'
|
||||
assert f.read(-1) == '456'
|
||||
assert f.tell() == 6
|
||||
|
||||
with open('123.txt', 'a') as f:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user