mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 11:30:18 +00:00
up
This commit is contained in:
parent
7c6ef3caba
commit
dc72796e2c
7
src/io.h
7
src/io.h
@ -88,10 +88,17 @@ void add_module_io(VM* vm){
|
|||||||
|
|
||||||
void add_module_os(VM* vm){
|
void add_module_os(VM* vm){
|
||||||
PyVar mod = vm->new_module("os");
|
PyVar mod = vm->new_module("os");
|
||||||
|
// Working directory is shared by all VMs!!
|
||||||
vm->bind_func<0>(mod, "getcwd", [](VM* vm, const Args& args){
|
vm->bind_func<0>(mod, "getcwd", [](VM* vm, const Args& args){
|
||||||
return VAR(std::filesystem::current_path().string());
|
return VAR(std::filesystem::current_path().string());
|
||||||
});
|
});
|
||||||
|
|
||||||
|
vm->bind_func<1>(mod, "chdir", [](VM* vm, const Args& args){
|
||||||
|
std::filesystem::path path(CAST(Str&, args[0]).c_str());
|
||||||
|
std::filesystem::current_path(path);
|
||||||
|
return vm->None;
|
||||||
|
});
|
||||||
|
|
||||||
vm->bind_func<1>(mod, "listdir", [](VM* vm, const Args& args){
|
vm->bind_func<1>(mod, "listdir", [](VM* vm, const Args& args){
|
||||||
std::filesystem::path path(CAST(Str&, args[0]).c_str());
|
std::filesystem::path path(CAST(Str&, args[0]).c_str());
|
||||||
std::filesystem::directory_iterator di;
|
std::filesystem::directory_iterator di;
|
||||||
|
21
src/main.cpp
21
src/main.cpp
@ -1,6 +1,7 @@
|
|||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include "pocketpy.h"
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include "pocketpy.h"
|
||||||
|
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
|
|
||||||
@ -58,17 +59,23 @@ int main(int argc, char** argv){
|
|||||||
}
|
}
|
||||||
|
|
||||||
if(argc == 2){
|
if(argc == 2){
|
||||||
std::string filename = argv[1];
|
std::string argv_1 = argv[1];
|
||||||
if(filename == "-h" || filename == "--help") goto __HELP;
|
if(argv_1 == "-h" || argv_1 == "--help") goto __HELP;
|
||||||
|
|
||||||
std::ifstream file(filename);
|
std::filesystem::path filepath(argv[1]);
|
||||||
if(!file.is_open()){
|
if(!std::filesystem::exists(filepath)){
|
||||||
std::cerr << "File not found: " << filename << std::endl;
|
std::cerr << "File not found: " << argv_1 << std::endl;
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
std::ifstream file(filepath);
|
||||||
|
if(!file.is_open()) return 1;
|
||||||
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
|
||||||
|
|
||||||
|
// set parent path as cwd
|
||||||
|
std::filesystem::current_path(filepath.parent_path());
|
||||||
|
|
||||||
pkpy::PyVarOrNull ret = nullptr;
|
pkpy::PyVarOrNull ret = nullptr;
|
||||||
ret = vm->exec(src.c_str(), filename, pkpy::EXEC_MODE);
|
ret = vm->exec(src.c_str(), argv_1, pkpy::EXEC_MODE);
|
||||||
pkpy_delete(vm);
|
pkpy_delete(vm);
|
||||||
return ret != nullptr ? 0 : 1;
|
return ret != nullptr ? 0 : 1;
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,6 @@ with open('123.txt', 'a') as f:
|
|||||||
with open('123.txt', 'r') as f:
|
with open('123.txt', 'r') as f:
|
||||||
assert f.read() == '123456' + '测试'
|
assert f.read() == '123456' + '测试'
|
||||||
|
|
||||||
|
assert os.path_exists('123.txt')
|
||||||
os.remove('123.txt')
|
os.remove('123.txt')
|
||||||
|
assert not os.path_exists('123.txt')
|
||||||
|
Loading…
x
Reference in New Issue
Block a user