os.c 6.5 KB

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