Compare commits

...

4 Commits

Author SHA1 Message Date
Tanisha Vasudeva
677c2e8ac1
Merge 6cc3d8023350163e72b83d49981ad2c4b08d39b4 into 8424d29fb228e108f895b6854e7c1e033ba1217d 2026-04-20 16:06:22 +00:00
BLUELOVETH
8424d29fb2
Merge pull request #500 from ngoyal88/main
fix(core): prevent memory leak in py_execo error path
2026-04-19 12:45:13 +08:00
Nikhil Goyal
973bbdfea4 fix(core): prevent memory leak in py_execo error path 2026-04-17 20:45:27 +05:30
tanisha
6cc3d80233 Added readline() 2026-02-28 20:28:04 +05:30
2 changed files with 29 additions and 2 deletions

View File

@ -232,7 +232,31 @@ static bool io_FileIO_flush(int argc, py_Ref argv) {
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");
@ -247,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);

View File

@ -159,7 +159,9 @@ bool py_execo(const void* data, int size, const char* filename, py_Ref module) {
CodeObject__dtor(&co);
return ok;
} else {
return RuntimeError("bad code object %s: %s", filename, err);
bool ok = RuntimeError("bad code object %s: %s", filename, err);
PK_FREE(err);
return ok;
}
}