mirror of
https://github.com/pocketpy/pocketpy
synced 2025-11-07 20:20:17 +00:00
Compare commits
3 Commits
335783b3ab
...
570f323af3
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
570f323af3 | ||
|
|
d6435f151b | ||
|
|
b49e769581 |
@ -515,7 +515,7 @@ PK_API void py_clearexc(py_StackRef p0);
|
||||
#define NameError(n) py_exception(tp_NameError, "name '%n' is not defined", (n))
|
||||
#define TypeError(...) py_exception(tp_TypeError, __VA_ARGS__)
|
||||
#define RuntimeError(...) py_exception(tp_RuntimeError, __VA_ARGS__)
|
||||
#define IOError(...) py_exception(tp_IOError, __VA_ARGS__)
|
||||
#define OSError(...) py_exception(tp_OSError, __VA_ARGS__)
|
||||
#define ValueError(...) py_exception(tp_ValueError, __VA_ARGS__)
|
||||
#define IndexError(...) py_exception(tp_IndexError, __VA_ARGS__)
|
||||
#define ImportError(...) py_exception(tp_ImportError, __VA_ARGS__)
|
||||
@ -709,7 +709,6 @@ enum py_PredefinedTypes {
|
||||
tp_StopIteration,
|
||||
tp_SyntaxError,
|
||||
tp_StackOverflowError,
|
||||
tp_IOError,
|
||||
tp_OSError,
|
||||
tp_NotImplementedError,
|
||||
tp_TypeError,
|
||||
|
||||
@ -153,7 +153,6 @@ void VM__ctor(VM* self) {
|
||||
|
||||
INJECT_BUILTIN_EXC(SyntaxError, tp_Exception);
|
||||
INJECT_BUILTIN_EXC(StackOverflowError, tp_Exception);
|
||||
INJECT_BUILTIN_EXC(IOError, tp_Exception);
|
||||
INJECT_BUILTIN_EXC(OSError, tp_Exception);
|
||||
INJECT_BUILTIN_EXC(NotImplementedError, tp_Exception);
|
||||
INJECT_BUILTIN_EXC(TypeError, tp_Exception);
|
||||
|
||||
126
src/modules/os.c
126
src/modules/os.c
@ -1,6 +1,9 @@
|
||||
#include "pocketpy/objects/base.h"
|
||||
#include "pocketpy/pocketpy.h"
|
||||
#include "pocketpy/interpreter/vm.h"
|
||||
|
||||
#if PK_ENABLE_OS == 1
|
||||
|
||||
#include <errno.h>
|
||||
|
||||
#if PY_SYS_PLATFORM == 0
|
||||
@ -10,18 +13,23 @@ int platform_chdir(const char* path) { return _chdir(path); }
|
||||
|
||||
bool platform_getcwd(char* buf, size_t size) { return _getcwd(buf, size) != NULL; }
|
||||
|
||||
bool platform_path_exists(const char* path) { return _access(path, 0) == 0; }
|
||||
|
||||
#elif PY_SYS_PLATFORM == 3 || PY_SYS_PLATFORM == 5
|
||||
#include <unistd.h>
|
||||
|
||||
int platform_chdir(const char* path) { return chdir(path); }
|
||||
|
||||
bool platform_getcwd(char* buf, size_t size) { return getcwd(buf, size) != NULL; }
|
||||
|
||||
bool platform_path_exists(const char* path) { return access(path, F_OK) == 0; }
|
||||
#else
|
||||
|
||||
int platform_chdir(const char* path) { return -1; }
|
||||
|
||||
bool platform_getcwd(char* buf, size_t size) { return false; }
|
||||
|
||||
bool platform_path_exists(const char* path) { return false; }
|
||||
#endif
|
||||
|
||||
static bool os_chdir(int argc, py_Ref argv) {
|
||||
@ -29,14 +37,17 @@ static bool os_chdir(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||
const char* path = py_tostr(py_arg(0));
|
||||
int code = platform_chdir(path);
|
||||
if(code != 0) return py_exception(tp_OSError, "chdir() failed: %d", code);
|
||||
if(code != 0) {
|
||||
const char* msg = strerror(errno);
|
||||
return OSError("[Errno %d] %s: '%s'", errno, msg, path);
|
||||
}
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool os_getcwd(int argc, py_Ref argv) {
|
||||
char buf[1024];
|
||||
if(!platform_getcwd(buf, sizeof(buf))) return py_exception(tp_OSError, "getcwd() failed");
|
||||
if(!platform_getcwd(buf, sizeof(buf))) return OSError("getcwd() failed");
|
||||
py_newstr(py_retval(), buf);
|
||||
return true;
|
||||
}
|
||||
@ -50,22 +61,41 @@ static bool os_system(int argc, py_Ref argv) {
|
||||
py_newint(py_retval(), code);
|
||||
return true;
|
||||
#else
|
||||
return py_exception(tp_OSError, "system() is not supported on this platform");
|
||||
return OSError("system() is not supported on this platform");
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool os_remove(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||
const char* path = py_tostr(py_arg(0));
|
||||
int code = remove(path);
|
||||
if(code != 0) {
|
||||
const char* msg = strerror(errno);
|
||||
return OSError("[Errno %d] %s: '%s'", errno, msg, path);
|
||||
}
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool os_path_exists(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
PY_CHECK_ARG_TYPE(0, tp_str);
|
||||
const char* path = py_tostr(py_arg(0));
|
||||
py_newbool(py_retval(), platform_path_exists(path));
|
||||
return true;
|
||||
}
|
||||
|
||||
void pk__add_module_os() {
|
||||
py_Ref mod = py_newmodule("os");
|
||||
py_bindfunc(mod, "chdir", os_chdir);
|
||||
py_bindfunc(mod, "getcwd", os_getcwd);
|
||||
py_bindfunc(mod, "system", os_system);
|
||||
}
|
||||
py_bindfunc(mod, "remove", os_remove);
|
||||
|
||||
void pk__add_module_sys() {
|
||||
py_Ref mod = py_newmodule("sys");
|
||||
py_newstr(py_emplacedict(mod, py_name("platform")), PY_SYS_PLATFORM_STRING);
|
||||
py_newstr(py_emplacedict(mod, py_name("version")), PK_VERSION);
|
||||
py_newlist(py_emplacedict(mod, py_name("argv")));
|
||||
py_ItemRef path_object = py_emplacedict(mod, py_name("path"));
|
||||
py_newobject(path_object, tp_object, -1, 0);
|
||||
py_bindfunc(path_object, "exists", os_path_exists);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
@ -86,7 +116,7 @@ static bool io_FileIO__new__(int argc, py_Ref argv) {
|
||||
ud->file = fopen(ud->path, ud->mode);
|
||||
if(ud->file == NULL) {
|
||||
const char* msg = strerror(errno);
|
||||
return IOError("[Errno %d] %s: %s", errno, msg, ud->path);
|
||||
return OSError("[Errno %d] %s: '%s'", errno, msg, ud->path);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@ -102,22 +132,52 @@ static bool io_FileIO__exit__(int argc, py_Ref argv) {
|
||||
fclose(ud->file);
|
||||
ud->file = NULL;
|
||||
}
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_read(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(1);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
fseek(ud->file, 0, SEEK_END);
|
||||
int filesize = ftell(ud->file);
|
||||
fseek(ud->file, 0, SEEK_SET);
|
||||
if(ud->mode[strlen(ud->mode) - 1] == 'b') {
|
||||
void* dst = py_newbytes(py_retval(), filesize);
|
||||
fread(dst, 1, filesize, ud->file);
|
||||
bool is_binary = ud->mode[strlen(ud->mode) - 1] == 'b';
|
||||
int size;
|
||||
if(argc == 1) {
|
||||
long current = ftell(ud->file);
|
||||
fseek(ud->file, 0, SEEK_END);
|
||||
size = ftell(ud->file);
|
||||
fseek(ud->file, current, SEEK_SET);
|
||||
} else if(argc == 2) {
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
size = py_toint(py_arg(1));
|
||||
} else {
|
||||
void* dst = py_newstrn(py_retval(), filesize);
|
||||
fread(dst, 1, filesize, ud->file);
|
||||
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);
|
||||
} else {
|
||||
void* dst = malloc(size);
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_newstrv(py_retval(), (c11_sv){dst, actual_size});
|
||||
free(dst);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_tell(int argc, py_Ref argv) {
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
py_newint(py_retval(), ftell(ud->file));
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_seek(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(3);
|
||||
PY_CHECK_ARG_TYPE(1, tp_int);
|
||||
PY_CHECK_ARG_TYPE(2, tp_int);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
long cookie = py_toint(py_arg(1));
|
||||
int whence = py_toint(py_arg(2));
|
||||
py_newint(py_retval(), fseek(ud->file, cookie, whence));
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -128,22 +188,25 @@ static bool io_FileIO_close(int argc, py_Ref argv) {
|
||||
fclose(ud->file);
|
||||
ud->file = NULL;
|
||||
}
|
||||
py_newnone(py_retval());
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool io_FileIO_write(int argc, py_Ref argv) {
|
||||
PY_CHECK_ARGC(2);
|
||||
io_FileIO* ud = py_touserdata(py_arg(0));
|
||||
size_t written_size;
|
||||
if(ud->mode[strlen(ud->mode) - 1] == 'b') {
|
||||
PY_CHECK_ARG_TYPE(1, tp_bytes);
|
||||
int filesize;
|
||||
unsigned char* data = py_tobytes(py_arg(1), &filesize);
|
||||
fwrite(data, 1, filesize, ud->file);
|
||||
written_size = fwrite(data, 1, filesize, ud->file);
|
||||
} else {
|
||||
PY_CHECK_ARG_TYPE(1, tp_str);
|
||||
c11_sv sv = py_tosv(py_arg(1));
|
||||
fwrite(sv.data, 1, sv.size, ud->file);
|
||||
written_size = fwrite(sv.data, 1, sv.size, ud->file);
|
||||
}
|
||||
py_newint(py_retval(), written_size);
|
||||
return true;
|
||||
}
|
||||
|
||||
@ -158,6 +221,27 @@ void pk__add_module_io() {
|
||||
py_bindmethod(FileIO, "read", io_FileIO_read);
|
||||
py_bindmethod(FileIO, "write", io_FileIO_write);
|
||||
py_bindmethod(FileIO, "close", io_FileIO_close);
|
||||
py_bindmethod(FileIO, "tell", io_FileIO_tell);
|
||||
py_bindmethod(FileIO, "seek", io_FileIO_seek);
|
||||
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);
|
||||
py_newint(py_emplacedict(mod, py_name("SEEK_END")), SEEK_END);
|
||||
|
||||
py_setdict(&pk_current_vm->builtins, py_name("open"), py_tpobject(FileIO));
|
||||
}
|
||||
|
||||
#else
|
||||
|
||||
void pk__add_module_os() {}
|
||||
|
||||
void pk__add_module_io() {}
|
||||
|
||||
#endif
|
||||
|
||||
void pk__add_module_sys() {
|
||||
py_Ref mod = py_newmodule("sys");
|
||||
py_newstr(py_emplacedict(mod, py_name("platform")), PY_SYS_PLATFORM_STRING);
|
||||
py_newstr(py_emplacedict(mod, py_name("version")), PK_VERSION);
|
||||
py_newlist(py_emplacedict(mod, py_name("argv")));
|
||||
}
|
||||
@ -1,10 +1,8 @@
|
||||
exit()
|
||||
|
||||
try:
|
||||
import os
|
||||
import io
|
||||
print("[`os` Test Enabled]")
|
||||
except ImportError:
|
||||
print('os is not enabled, skipping test...')
|
||||
exit(0)
|
||||
|
||||
a = open('123.txt', 'wt')
|
||||
@ -46,8 +44,7 @@ with open('123.txt', 'rt') as f:
|
||||
with open('123.txt', 'a') as f:
|
||||
f.write('测试')
|
||||
|
||||
# default mode is 'r'
|
||||
with open('123.txt') as f:
|
||||
with open('123.txt', 'r') as f:
|
||||
assert f.read() == '123456' + '测试'
|
||||
|
||||
assert os.path.exists('123.txt')
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user