io.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. #pragma once
  2. #include "ceval.h"
  3. #include "cffi.h"
  4. #include "common.h"
  5. #if PK_ENABLE_OS
  6. #include <filesystem>
  7. #include <cstdio>
  8. namespace pkpy{
  9. inline Bytes _default_import_handler(const Str& name){
  10. std::filesystem::path path(name.sv());
  11. bool exists = std::filesystem::exists(path);
  12. if(!exists) return Bytes();
  13. std::string cname = name.str();
  14. FILE* fp = fopen(cname.c_str(), "rb");
  15. if(!fp) return Bytes();
  16. fseek(fp, 0, SEEK_END);
  17. std::vector<char> buffer(ftell(fp));
  18. fseek(fp, 0, SEEK_SET);
  19. fread(buffer.data(), 1, buffer.size(), fp);
  20. fclose(fp);
  21. return Bytes(std::move(buffer));
  22. };
  23. struct FileIO {
  24. PY_CLASS(FileIO, io, FileIO)
  25. Str file;
  26. Str mode;
  27. FILE* fp;
  28. bool is_text() const { return mode != "rb" && mode != "wb" && mode != "ab"; }
  29. FileIO(VM* vm, std::string file, std::string mode): file(file), mode(mode) {
  30. fp = fopen(file.c_str(), mode.c_str());
  31. if(!fp) vm->IOError(strerror(errno));
  32. }
  33. void close(){
  34. if(fp == nullptr) return;
  35. fclose(fp);
  36. fp = nullptr;
  37. }
  38. static void _register(VM* vm, PyObject* mod, PyObject* type){
  39. vm->bind_constructor<3>(type, [](VM* vm, ArgsView args){
  40. return VAR_T(FileIO,
  41. vm, CAST(Str&, args[1]).str(), CAST(Str&, args[2]).str()
  42. );
  43. });
  44. vm->bind_method<0>(type, "read", [](VM* vm, ArgsView args){
  45. FileIO& io = CAST(FileIO&, args[0]);
  46. fseek(io.fp, 0, SEEK_END);
  47. std::vector<char> buffer(ftell(io.fp));
  48. fseek(io.fp, 0, SEEK_SET);
  49. fread(buffer.data(), 1, buffer.size(), io.fp);
  50. Bytes b(std::move(buffer));
  51. if(io.is_text()) return VAR(Str(b.str()));
  52. return VAR(std::move(b));
  53. });
  54. vm->bind_method<1>(type, "write", [](VM* vm, ArgsView args){
  55. FileIO& io = CAST(FileIO&, args[0]);
  56. if(io.is_text()){
  57. Str& s = CAST(Str&, args[1]);
  58. fwrite(s.data, 1, s.length(), io.fp);
  59. }else{
  60. Bytes& buffer = CAST(Bytes&, args[1]);
  61. fwrite(buffer.data(), 1, buffer.size(), io.fp);
  62. }
  63. return vm->None;
  64. });
  65. vm->bind_method<0>(type, "close", [](VM* vm, ArgsView args){
  66. FileIO& io = CAST(FileIO&, args[0]);
  67. io.close();
  68. return vm->None;
  69. });
  70. vm->bind_method<0>(type, "__exit__", [](VM* vm, ArgsView args){
  71. FileIO& io = CAST(FileIO&, args[0]);
  72. io.close();
  73. return vm->None;
  74. });
  75. vm->bind_method<0>(type, "__enter__", CPP_LAMBDA(vm->None));
  76. }
  77. };
  78. inline void add_module_io(VM* vm){
  79. PyObject* mod = vm->new_module("io");
  80. FileIO::register_class(vm, mod);
  81. vm->bind_builtin_func<2>("open", [](VM* vm, ArgsView args){
  82. static StrName m_io("io");
  83. static StrName m_FileIO("FileIO");
  84. return vm->call(vm->_modules[m_io]->attr(m_FileIO), args[0], args[1]);
  85. });
  86. }
  87. inline void add_module_os(VM* vm){
  88. PyObject* mod = vm->new_module("os");
  89. PyObject* path_obj = vm->heap.gcnew<DummyInstance>(vm->tp_object, {});
  90. mod->attr().set("path", path_obj);
  91. // Working directory is shared by all VMs!!
  92. vm->bind_func<0>(mod, "getcwd", [](VM* vm, ArgsView args){
  93. return VAR(std::filesystem::current_path().string());
  94. });
  95. vm->bind_func<1>(mod, "chdir", [](VM* vm, ArgsView args){
  96. std::filesystem::path path(CAST(Str&, args[0]).sv());
  97. std::filesystem::current_path(path);
  98. return vm->None;
  99. });
  100. vm->bind_func<1>(mod, "listdir", [](VM* vm, ArgsView args){
  101. std::filesystem::path path(CAST(Str&, args[0]).sv());
  102. std::filesystem::directory_iterator di;
  103. try{
  104. di = std::filesystem::directory_iterator(path);
  105. }catch(std::filesystem::filesystem_error& e){
  106. std::string msg = e.what();
  107. auto pos = msg.find_last_of(":");
  108. if(pos != std::string::npos) msg = msg.substr(pos + 1);
  109. vm->IOError(Str(msg).lstrip());
  110. }
  111. List ret;
  112. for(auto& p: di) ret.push_back(VAR(p.path().filename().string()));
  113. return VAR(ret);
  114. });
  115. vm->bind_func<1>(mod, "remove", [](VM* vm, ArgsView args){
  116. std::filesystem::path path(CAST(Str&, args[0]).sv());
  117. bool ok = std::filesystem::remove(path);
  118. if(!ok) vm->IOError("operation failed");
  119. return vm->None;
  120. });
  121. vm->bind_func<1>(mod, "mkdir", [](VM* vm, ArgsView args){
  122. std::filesystem::path path(CAST(Str&, args[0]).sv());
  123. bool ok = std::filesystem::create_directory(path);
  124. if(!ok) vm->IOError("operation failed");
  125. return vm->None;
  126. });
  127. vm->bind_func<1>(mod, "rmdir", [](VM* vm, ArgsView args){
  128. std::filesystem::path path(CAST(Str&, args[0]).sv());
  129. bool ok = std::filesystem::remove(path);
  130. if(!ok) vm->IOError("operation failed");
  131. return vm->None;
  132. });
  133. vm->bind_func<-1>(path_obj, "join", [](VM* vm, ArgsView args){
  134. std::filesystem::path path;
  135. for(int i=0; i<args.size(); i++){
  136. path /= CAST(Str&, args[i]).sv();
  137. }
  138. return VAR(path.string());
  139. });
  140. vm->bind_func<1>(path_obj, "exists", [](VM* vm, ArgsView args){
  141. std::filesystem::path path(CAST(Str&, args[0]).sv());
  142. bool exists = std::filesystem::exists(path);
  143. return VAR(exists);
  144. });
  145. vm->bind_func<1>(path_obj, "basename", [](VM* vm, ArgsView args){
  146. std::filesystem::path path(CAST(Str&, args[0]).sv());
  147. return VAR(path.filename().string());
  148. });
  149. }
  150. } // namespace pkpy
  151. #else
  152. namespace pkpy{
  153. inline void add_module_io(void* vm){}
  154. inline void add_module_os(void* vm){}
  155. inline Bytes _default_import_handler(const Str& name) { return Bytes(); }
  156. } // namespace pkpy
  157. #endif