Skip to content

Commit a3b402a

Browse files
committed
add os module
fix color for GC DUMP add os module and test support ESP32
1 parent a367b65 commit a3b402a

File tree

14 files changed

+898
-2
lines changed

14 files changed

+898
-2
lines changed

examples/os/os_test1.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import os
2+
origin = os.getcwd()
3+
os.chdir("test/out")
4+
os.getcwd()
5+
6+
try:
7+
# cleanup
8+
os.remove("_testdir/testfile")
9+
os.rmdir("_testdir")
10+
except:
11+
pass
12+
13+
os.mkdir("_testdir")
14+
os.chdir("_testdir")
15+
16+
17+
f = os.open("testfile", os.O_CREAT | os.O_RDWR)
18+
assert os.write(f, b"Hello World!") == 12
19+
assert os.lseek(f, 0, 0) == 0
20+
print(os.read(f, 100))
21+
os.close(f)
22+
os.chdir("..")
23+
os.chdir(origin)
24+
print("PASS")

package/os/os.c

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "os.h"
2+
#include <stdio.h>
3+
#include <stdlib.h>
4+
#include "PikaObj.h"
5+
#include "PikaStdData_List.h"
6+
#include "TinyObj.h"
7+
#include "os_fileStat.h"
8+
#include "os_platform.h"
9+
10+
void os___init__(PikaObj* self) {
11+
// obj_newDirectObj(self,"os",New_TinyObj);
12+
13+
obj_setInt(self, "O_RDONLY", FILE_RDONLY);
14+
obj_setInt(self, "O_WRONLY", FILE_WRONLY);
15+
obj_setInt(self, "O_RDWR", FILE_RDWR);
16+
obj_setInt(self, "O_APPEND", FILE_APPEND);
17+
obj_setInt(self, "O_CREAT", FILE_CREAT);
18+
}
19+
//#undef _WIN32
20+
21+
int os_fileStat_st_size(PikaObj* self) {
22+
int size = obj_getInt(self, "st_size");
23+
return size;
24+
}
25+
26+
PikaObj* os_fstat(PikaObj* self, PikaObj* fd) {
27+
int size = 0;
28+
size = os_getFileSize(fd);
29+
30+
PikaObj* stat_obj = newNormalObj(New_os_fileStat);
31+
32+
obj_setInt(stat_obj, "st_size", size);
33+
return stat_obj;
34+
}
35+
36+
PikaObj* os_open(PikaObj* self, char* filename, int flags) {
37+
return os_open_platform(filename, flags);
38+
}
39+
40+
char* os_read(PikaObj* self, PikaObj* fd, int len) {
41+
return os_read_platform(self, fd, len);
42+
}
43+
44+
int os_write(PikaObj* self, PikaObj* fd, Arg* buf) {
45+
if (arg_getType(buf) != ARG_TYPE_BYTES) {
46+
obj_setErrorCode(self, PIKA_RES_ERR_INVALID_PARAM);
47+
pika_platform_printf(
48+
"TypeError: a bytes-like object is required, not 'str'\r\n");
49+
return -1;
50+
}
51+
return os_write_platform(arg_getBytes(buf), arg_getBytesSize(buf), fd);
52+
}
53+
54+
int os_lseek(PikaObj* self, PikaObj* fd, int how, int pos) {
55+
return os_lseek_platform(fd, how, pos);
56+
}
57+
58+
void os_close(PikaObj* self, PikaObj* fd) {
59+
if (os_close_platform(fd) < 0) {
60+
obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
61+
pika_platform_printf("close file error\r\n");
62+
}
63+
}
64+
65+
char* os_getcwd(PikaObj* self) {
66+
return os_getcwd_platform(self);
67+
}
68+
69+
PikaObj* os_listdir(PikaObj* self, char* path) {
70+
return os_listdir_platform(path);
71+
}
72+
73+
void os_mkdir(PikaObj* self, char* path, PikaTuple* mode) {
74+
int iMode = 0;
75+
if (pikaTuple_getSize(mode) == 0) {
76+
iMode = 0777;
77+
} else {
78+
iMode = pikaTuple_getInt(mode, 0);
79+
}
80+
if (os_mkdir_platform(iMode, path) < 0) {
81+
obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
82+
pika_platform_printf("mkdir error\r\n");
83+
}
84+
}
85+
86+
void os_chdir(PikaObj* self, char* path) {
87+
if (os_chdir_platform(path) < 0) {
88+
obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
89+
pika_platform_printf("chdir error\r\n");
90+
}
91+
}
92+
93+
void os_rmdir(PikaObj* self, char* path) {
94+
if (os_rmdir_platform(path) < 0) {
95+
obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
96+
pika_platform_printf("rmdir error\r\n");
97+
}
98+
}
99+
100+
void os_remove(PikaObj* self, char* filename) {
101+
if (os_remove_platform(filename) < 0) {
102+
obj_setErrorCode(self, PIKA_RES_ERR_IO_ERROR);
103+
pika_platform_printf("remove error\r\n");
104+
}
105+
}

package/os/os.pyi

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
O_RDONLY: int
2+
O_WRONLY: int
3+
O_RDWR: int
4+
O_APPEND: int
5+
O_CREAT: int
6+
7+
8+
class fileStat:
9+
def st_size(self) -> int:
10+
pass
11+
12+
13+
def __init__(self):
14+
pass
15+
16+
17+
def mkdir(self, path: str, *mode):
18+
pass
19+
20+
21+
def rmdir(self, path: str):
22+
pass
23+
24+
25+
def chdir(self, path: str):
26+
pass
27+
28+
29+
def listdir(self, path: str) -> list:
30+
pass
31+
32+
33+
def getcwd(self) -> str:
34+
pass
35+
36+
37+
def open(self, filename: str, flags: int) -> FILE:
38+
pass
39+
40+
41+
def read(self, fd: FILE, len: int) -> str:
42+
pass
43+
44+
45+
def write(self, fd: FILE, buf: any) -> int:
46+
pass
47+
48+
49+
def lseek(self, fd: FILE, pos: int, how: int) -> int:
50+
pass
51+
52+
53+
def close(self, fd: FILE):
54+
pass
55+
56+
57+
def fstat(self, fd: FILE) -> fileStat:
58+
pass
59+
60+
61+
def remove(self, filename: str):
62+
pass

0 commit comments

Comments
 (0)