os.c 7.2 KB

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