mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
allow import
from file
This commit is contained in:
parent
f0b66c43e6
commit
b69bef0912
13
src/ceval.h
13
src/ceval.h
@ -5,6 +5,8 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
Str _read_file_cwd(const Str& name, bool* ok);
|
||||||
|
|
||||||
PyVar VM::run_frame(Frame* frame){
|
PyVar VM::run_frame(Frame* frame){
|
||||||
while(frame->has_next_bytecode()){
|
while(frame->has_next_bytecode()){
|
||||||
const Bytecode& byte = frame->next_bytecode();
|
const Bytecode& byte = frame->next_bytecode();
|
||||||
@ -298,18 +300,21 @@ PyVar VM::run_frame(Frame* frame){
|
|||||||
StrName name = frame->co->names[byte.arg].first;
|
StrName name = frame->co->names[byte.arg].first;
|
||||||
PyVar* ext_mod = _modules.try_get(name);
|
PyVar* ext_mod = _modules.try_get(name);
|
||||||
if(ext_mod == nullptr){
|
if(ext_mod == nullptr){
|
||||||
|
Str source;
|
||||||
auto it2 = _lazy_modules.find(name);
|
auto it2 = _lazy_modules.find(name);
|
||||||
if(it2 == _lazy_modules.end()){
|
if(it2 == _lazy_modules.end()){
|
||||||
_error("ImportError", "module " + name.str().escape(true) + " not found");
|
bool ok = false;
|
||||||
|
source = _read_file_cwd(name.str() + ".py", &ok);
|
||||||
|
if(!ok) _error("ImportError", "module " + name.str().escape(true) + " not found");
|
||||||
}else{
|
}else{
|
||||||
const Str& source = it2->second;
|
source = it2->second;
|
||||||
|
_lazy_modules.erase(it2);
|
||||||
|
}
|
||||||
CodeObject_ code = compile(source, name.str(), EXEC_MODE);
|
CodeObject_ code = compile(source, name.str(), EXEC_MODE);
|
||||||
PyVar new_mod = new_module(name);
|
PyVar new_mod = new_module(name);
|
||||||
_exec(code, new_mod);
|
_exec(code, new_mod);
|
||||||
frame->push(new_mod);
|
frame->push(new_mod);
|
||||||
_lazy_modules.erase(it2);
|
|
||||||
new_mod->attr()._try_perfect_rehash();
|
new_mod->attr()._try_perfect_rehash();
|
||||||
}
|
|
||||||
}else{
|
}else{
|
||||||
frame->push(*ext_mod);
|
frame->push(*ext_mod);
|
||||||
}
|
}
|
||||||
|
20
src/io.h
20
src/io.h
@ -10,6 +10,20 @@
|
|||||||
|
|
||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
|
|
||||||
|
Str _read_file_cwd(const Str& name, bool* ok){
|
||||||
|
std::filesystem::path path(name.c_str());
|
||||||
|
bool exists = std::filesystem::exists(path);
|
||||||
|
if(!exists){
|
||||||
|
*ok = false;
|
||||||
|
return Str();
|
||||||
|
}
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
struct FileIO {
|
struct FileIO {
|
||||||
PY_CLASS(FileIO, io, FileIO)
|
PY_CLASS(FileIO, io, FileIO)
|
||||||
|
|
||||||
@ -138,6 +152,12 @@ void add_module_os(VM* vm){
|
|||||||
namespace pkpy{
|
namespace pkpy{
|
||||||
void add_module_io(VM* vm){}
|
void add_module_io(VM* vm){}
|
||||||
void add_module_os(VM* vm){}
|
void add_module_os(VM* vm){}
|
||||||
|
|
||||||
|
Str _read_file_cwd(const Str& name, bool* ok){
|
||||||
|
*ok = false;
|
||||||
|
return Str();
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace pkpy
|
} // namespace pkpy
|
||||||
|
|
||||||
#endif
|
#endif
|
Loading…
x
Reference in New Issue
Block a user