os.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include "pocketpy/objects/base.h"
  2. #include "pocketpy/pocketpy.h"
  3. #include "pocketpy/interpreter/vm.h"
  4. #if PK_ENABLE_OS == 1
  5. #include <errno.h>
  6. #if PY_SYS_PLATFORM == 0
  7. #include <direct.h>
  8. int platform_chdir(const char* path) { return _chdir(path); }
  9. bool platform_getcwd(char* buf, size_t size) { return _getcwd(buf, size) != NULL; }
  10. bool platform_path_exists(const char* path) { return _access(path, 0) == 0; }
  11. #elif PY_SYS_PLATFORM == 3 || PY_SYS_PLATFORM == 5
  12. #include <unistd.h>
  13. int platform_chdir(const char* path) { return chdir(path); }
  14. bool platform_getcwd(char* buf, size_t size) { return getcwd(buf, size) != NULL; }
  15. bool platform_path_exists(const char* path) { return access(path, F_OK) == 0; }
  16. #else
  17. int platform_chdir(const char* path) { return -1; }
  18. bool platform_getcwd(char* buf, size_t size) { return false; }
  19. bool platform_path_exists(const char* path) { return false; }
  20. #endif
  21. static bool os_chdir(int argc, py_Ref argv) {
  22. PY_CHECK_ARGC(1);
  23. PY_CHECK_ARG_TYPE(0, tp_str);
  24. const char* path = py_tostr(py_arg(0));
  25. int code = platform_chdir(path);
  26. if(code != 0) {
  27. const char* msg = strerror(errno);
  28. return OSError("[Errno %d] %s: '%s'", errno, msg, path);
  29. }
  30. py_newnone(py_retval());
  31. return true;
  32. }
  33. static bool os_getcwd(int argc, py_Ref argv) {
  34. char buf[1024];
  35. if(!platform_getcwd(buf, sizeof(buf))) return OSError("getcwd() failed");
  36. py_newstr(py_retval(), buf);
  37. return true;
  38. }
  39. static bool os_system(int argc, py_Ref argv) {
  40. PY_CHECK_ARGC(1);
  41. PY_CHECK_ARG_TYPE(0, tp_str);
  42. #if PK_IS_DESKTOP_PLATFORM
  43. const char* cmd = py_tostr(py_arg(0));
  44. int code = system(cmd);
  45. py_newint(py_retval(), code);
  46. return true;
  47. #else
  48. return OSError("system() is not supported on this platform");
  49. #endif
  50. }
  51. static bool os_remove(int argc, py_Ref argv) {
  52. PY_CHECK_ARGC(1);
  53. PY_CHECK_ARG_TYPE(0, tp_str);
  54. const char* path = py_tostr(py_arg(0));
  55. int code = remove(path);
  56. if(code != 0) {
  57. const char* msg = strerror(errno);
  58. return OSError("[Errno %d] %s: '%s'", errno, msg, path);
  59. }
  60. py_newnone(py_retval());
  61. return true;
  62. }
  63. static bool os_path_exists(int argc, py_Ref argv) {
  64. PY_CHECK_ARGC(1);
  65. PY_CHECK_ARG_TYPE(0, tp_str);
  66. const char* path = py_tostr(py_arg(0));
  67. py_newbool(py_retval(), platform_path_exists(path));
  68. return true;
  69. }
  70. void pk__add_module_os() {
  71. py_Ref mod = py_newmodule("os");
  72. py_bindfunc(mod, "chdir", os_chdir);
  73. py_bindfunc(mod, "getcwd", os_getcwd);
  74. py_bindfunc(mod, "system", os_system);
  75. py_bindfunc(mod, "remove", os_remove);
  76. py_ItemRef path_object = py_emplacedict(mod, py_name("path"));
  77. py_newobject(path_object, tp_object, -1, 0);
  78. py_bindfunc(path_object, "exists", os_path_exists);
  79. }
  80. typedef struct {
  81. const char* path;
  82. const char* mode;
  83. FILE* file;
  84. } io_FileIO;
  85. static bool io_FileIO__new__(int argc, py_Ref argv) {
  86. // __new__(cls, file, mode)
  87. PY_CHECK_ARGC(3);
  88. PY_CHECK_ARG_TYPE(1, tp_str);
  89. PY_CHECK_ARG_TYPE(2, tp_str);
  90. py_Type cls = py_totype(argv);
  91. io_FileIO* ud = py_newobject(py_retval(), cls, 0, sizeof(io_FileIO));
  92. ud->path = py_tostr(py_arg(1));
  93. ud->mode = py_tostr(py_arg(2));
  94. ud->file = fopen(ud->path, ud->mode);
  95. if(ud->file == NULL) {
  96. const char* msg = strerror(errno);
  97. return OSError("[Errno %d] %s: '%s'", errno, msg, ud->path);
  98. }
  99. return true;
  100. }
  101. static bool io_FileIO__enter__(int argc, py_Ref argv) {
  102. py_assign(py_retval(), py_arg(0));
  103. return true;
  104. }
  105. static bool io_FileIO__exit__(int argc, py_Ref argv) {
  106. io_FileIO* ud = py_touserdata(py_arg(0));
  107. if(ud->file != NULL) {
  108. fclose(ud->file);
  109. ud->file = NULL;
  110. }
  111. py_newnone(py_retval());
  112. return true;
  113. }
  114. static bool io_FileIO_read(int argc, py_Ref argv) {
  115. io_FileIO* ud = py_touserdata(py_arg(0));
  116. bool is_binary = ud->mode[strlen(ud->mode) - 1] == 'b';
  117. int size;
  118. if(argc == 1) {
  119. long current = ftell(ud->file);
  120. fseek(ud->file, 0, SEEK_END);
  121. size = ftell(ud->file);
  122. fseek(ud->file, current, SEEK_SET);
  123. } else if(argc == 2) {
  124. PY_CHECK_ARG_TYPE(1, tp_int);
  125. size = py_toint(py_arg(1));
  126. } else {
  127. return TypeError("read() takes at most 2 arguments (%d given)", argc);
  128. }
  129. if(is_binary) {
  130. void* dst = py_newbytes(py_retval(), size);
  131. int actual_size = fread(dst, 1, size, ud->file);
  132. py_bytes_resize(py_retval(), actual_size);
  133. } else {
  134. void* dst = malloc(size);
  135. int actual_size = fread(dst, 1, size, ud->file);
  136. py_newstrv(py_retval(), (c11_sv){dst, actual_size});
  137. free(dst);
  138. }
  139. return true;
  140. }
  141. static bool io_FileIO_tell(int argc, py_Ref argv) {
  142. io_FileIO* ud = py_touserdata(py_arg(0));
  143. py_newint(py_retval(), ftell(ud->file));
  144. return true;
  145. }
  146. static bool io_FileIO_seek(int argc, py_Ref argv) {
  147. PY_CHECK_ARGC(3);
  148. PY_CHECK_ARG_TYPE(1, tp_int);
  149. PY_CHECK_ARG_TYPE(2, tp_int);
  150. io_FileIO* ud = py_touserdata(py_arg(0));
  151. long cookie = py_toint(py_arg(1));
  152. int whence = py_toint(py_arg(2));
  153. py_newint(py_retval(), fseek(ud->file, cookie, whence));
  154. return true;
  155. }
  156. static bool io_FileIO_close(int argc, py_Ref argv) {
  157. PY_CHECK_ARGC(1);
  158. io_FileIO* ud = py_touserdata(py_arg(0));
  159. if(ud->file != NULL) {
  160. fclose(ud->file);
  161. ud->file = NULL;
  162. }
  163. py_newnone(py_retval());
  164. return true;
  165. }
  166. static bool io_FileIO_write(int argc, py_Ref argv) {
  167. PY_CHECK_ARGC(2);
  168. io_FileIO* ud = py_touserdata(py_arg(0));
  169. size_t written_size;
  170. if(ud->mode[strlen(ud->mode) - 1] == 'b') {
  171. PY_CHECK_ARG_TYPE(1, tp_bytes);
  172. int filesize;
  173. unsigned char* data = py_tobytes(py_arg(1), &filesize);
  174. written_size = fwrite(data, 1, filesize, ud->file);
  175. } else {
  176. PY_CHECK_ARG_TYPE(1, tp_str);
  177. c11_sv sv = py_tosv(py_arg(1));
  178. written_size = fwrite(sv.data, 1, sv.size, ud->file);
  179. }
  180. py_newint(py_retval(), written_size);
  181. return true;
  182. }
  183. void pk__add_module_io() {
  184. py_Ref mod = py_newmodule("io");
  185. py_Type FileIO = pk_newtype("FileIO", tp_object, mod, NULL, false, true);
  186. py_bindmagic(FileIO, __new__, io_FileIO__new__);
  187. py_bindmagic(FileIO, __enter__, io_FileIO__enter__);
  188. py_bindmagic(FileIO, __exit__, io_FileIO__exit__);
  189. py_bindmethod(FileIO, "read", io_FileIO_read);
  190. py_bindmethod(FileIO, "write", io_FileIO_write);
  191. py_bindmethod(FileIO, "close", io_FileIO_close);
  192. py_bindmethod(FileIO, "tell", io_FileIO_tell);
  193. py_bindmethod(FileIO, "seek", io_FileIO_seek);
  194. py_newint(py_emplacedict(mod, py_name("SEEK_SET")), SEEK_SET);
  195. py_newint(py_emplacedict(mod, py_name("SEEK_CUR")), SEEK_CUR);
  196. py_newint(py_emplacedict(mod, py_name("SEEK_END")), SEEK_END);
  197. py_setdict(&pk_current_vm->builtins, py_name("open"), py_tpobject(FileIO));
  198. }
  199. #else
  200. void pk__add_module_os() {}
  201. void pk__add_module_io() {}
  202. #endif
  203. void pk__add_module_sys() {
  204. py_Ref mod = py_newmodule("sys");
  205. py_newstr(py_emplacedict(mod, py_name("platform")), PY_SYS_PLATFORM_STRING);
  206. py_newstr(py_emplacedict(mod, py_name("version")), PK_VERSION);
  207. py_newlist(py_emplacedict(mod, py_name("argv")));
  208. }