This commit is contained in:
blueloveTH 2023-04-22 22:09:39 +08:00
parent 41ef71a580
commit be273f55c9
3 changed files with 8 additions and 7 deletions

View File

@ -442,7 +442,8 @@ __NEXT_STEP:;
auto it = _lazy_modules.find(name);
if(it == _lazy_modules.end()){
bool ok = false;
source = _read_file_cwd(fmt(name, ".py"), &ok);
Bytes b = _read_file_cwd(fmt(name, ".py"), &ok);
source = Str(b._data);
if(!ok) _error("ImportError", fmt("module ", name.escape(), " not found"));
}else{
source = it->second;

View File

@ -11,18 +11,18 @@
namespace pkpy{
inline Str _read_file_cwd(const Str& name, bool* ok){
inline Bytes _read_file_cwd(const Str& name, bool* ok){
std::filesystem::path path(name.sv());
bool exists = std::filesystem::exists(path);
if(!exists){
*ok = false;
return Str();
return Bytes();
}
std::ifstream ifs(path);
std::string buffer((std::istreambuf_iterator<char>(ifs)), (std::istreambuf_iterator<char>()));
ifs.close();
*ok = true;
return Str(std::move(buffer));
return Bytes({std::move(buffer)});
}
struct FileIO {
@ -175,9 +175,9 @@ namespace pkpy{
inline void add_module_io(VM* vm){}
inline void add_module_os(VM* vm){}
inline Str _read_file_cwd(const Str& name, bool* ok){
inline Bytes _read_file_cwd(const Str& name, bool* ok){
*ok = false;
return Str();
return Bytes();
}
} // namespace pkpy

View File

@ -24,7 +24,7 @@ namespace pkpy{
#define POPX() (s_data.popx())
#define STACK_VIEW(n) (s_data.view(n))
Str _read_file_cwd(const Str& name, bool* ok);
Bytes _read_file_cwd(const Str& name, bool* ok);
#define DEF_NATIVE_2(ctype, ptype) \
template<> inline ctype py_cast<ctype>(VM* vm, PyObject* obj) { \