mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-20 03:20:18 +00:00
open
default mode is 'r'
This commit is contained in:
parent
6e2af287ab
commit
014902b4bb
@ -20,7 +20,7 @@
|
||||
#include <variant>
|
||||
#include <type_traits>
|
||||
|
||||
#define PK_VERSION "1.1.1"
|
||||
#define PK_VERSION "1.1.2"
|
||||
|
||||
#include "config.h"
|
||||
#include "export.h"
|
||||
|
@ -18,57 +18,6 @@ namespace pkpy {
|
||||
|
||||
void init_builtins(VM* _vm);
|
||||
|
||||
struct PyREPL{
|
||||
PY_CLASS(PyREPL, sys, _repl)
|
||||
|
||||
REPL* repl;
|
||||
|
||||
PyREPL(VM* vm){ repl = new REPL(vm); }
|
||||
~PyREPL(){ delete repl; }
|
||||
|
||||
PyREPL(const PyREPL&) = delete;
|
||||
PyREPL& operator=(const PyREPL&) = delete;
|
||||
|
||||
PyREPL(PyREPL&& other) noexcept{
|
||||
repl = other.repl;
|
||||
other.repl = nullptr;
|
||||
}
|
||||
|
||||
struct TempOut{
|
||||
PrintFunc backup;
|
||||
VM* vm;
|
||||
TempOut(VM* vm, PrintFunc f){
|
||||
this->vm = vm;
|
||||
this->backup = vm->_stdout;
|
||||
vm->_stdout = f;
|
||||
}
|
||||
~TempOut(){
|
||||
vm->_stdout = backup;
|
||||
}
|
||||
TempOut(const TempOut&) = delete;
|
||||
TempOut& operator=(const TempOut&) = delete;
|
||||
TempOut(TempOut&&) = delete;
|
||||
TempOut& operator=(TempOut&&) = delete;
|
||||
};
|
||||
|
||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||
vm->bind_constructor<1>(type, [](VM* vm, ArgsView args){
|
||||
return VAR_T(PyREPL, vm);
|
||||
});
|
||||
|
||||
vm->bind_method<1>(type, "input", [](VM* vm, ArgsView args){
|
||||
PyREPL& self = _CAST(PyREPL&, args[0]);
|
||||
const Str& s = CAST(Str&, args[1]);
|
||||
PK_LOCAL_STATIC std::stringstream ss_out;
|
||||
ss_out.str("");
|
||||
TempOut _(vm, [](VM* vm, const Str& s){ ss_out << s; });
|
||||
bool ok = self.repl->input(s.str());
|
||||
return VAR(Tuple({VAR(ok), VAR(ss_out.str())}));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
void add_module_timeit(VM* vm);
|
||||
void add_module_time(VM* vm);
|
||||
void add_module_sys(VM* vm);
|
||||
|
@ -88,7 +88,7 @@ void add_module_io(VM* vm){
|
||||
#if PK_ENABLE_OS
|
||||
PyObject* mod = vm->new_module("io");
|
||||
FileIO::register_class(vm, mod);
|
||||
vm->bind_builtin_func<2>("open", [](VM* vm, ArgsView args){
|
||||
vm->bind(vm->builtins, "open(path, mode='r')", [](VM* vm, ArgsView args){
|
||||
PK_LOCAL_STATIC StrName m_io("io");
|
||||
PK_LOCAL_STATIC StrName m_FileIO("FileIO");
|
||||
return vm->call(vm->_modules[m_io]->attr(m_FileIO), args[0], args[1]);
|
||||
|
@ -1282,6 +1282,55 @@ void add_module_time(VM* vm){
|
||||
});
|
||||
}
|
||||
|
||||
struct PyREPL{
|
||||
PY_CLASS(PyREPL, sys, _repl)
|
||||
|
||||
REPL* repl;
|
||||
|
||||
PyREPL(VM* vm){ repl = new REPL(vm); }
|
||||
~PyREPL(){ delete repl; }
|
||||
|
||||
PyREPL(const PyREPL&) = delete;
|
||||
PyREPL& operator=(const PyREPL&) = delete;
|
||||
|
||||
PyREPL(PyREPL&& other) noexcept{
|
||||
repl = other.repl;
|
||||
other.repl = nullptr;
|
||||
}
|
||||
|
||||
struct TempOut{
|
||||
PrintFunc backup;
|
||||
VM* vm;
|
||||
TempOut(VM* vm, PrintFunc f){
|
||||
this->vm = vm;
|
||||
this->backup = vm->_stdout;
|
||||
vm->_stdout = f;
|
||||
}
|
||||
~TempOut(){
|
||||
vm->_stdout = backup;
|
||||
}
|
||||
TempOut(const TempOut&) = delete;
|
||||
TempOut& operator=(const TempOut&) = delete;
|
||||
TempOut(TempOut&&) = delete;
|
||||
TempOut& operator=(TempOut&&) = delete;
|
||||
};
|
||||
|
||||
static void _register(VM* vm, PyObject* mod, PyObject* type){
|
||||
vm->bind_constructor<1>(type, [](VM* vm, ArgsView args){
|
||||
return VAR_T(PyREPL, vm);
|
||||
});
|
||||
|
||||
vm->bind_method<1>(type, "input", [](VM* vm, ArgsView args){
|
||||
PyREPL& self = _CAST(PyREPL&, args[0]);
|
||||
const Str& s = CAST(Str&, args[1]);
|
||||
PK_LOCAL_STATIC std::stringstream ss_out;
|
||||
ss_out.str("");
|
||||
TempOut _(vm, [](VM* vm, const Str& s){ ss_out << s; });
|
||||
bool ok = self.repl->input(s.str());
|
||||
return VAR(Tuple({VAR(ok), VAR(ss_out.str())}));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
void add_module_sys(VM* vm){
|
||||
PyObject* mod = vm->new_module("sys");
|
||||
|
@ -15,7 +15,8 @@ with open('123.txt', 'rt') as f:
|
||||
with open('123.txt', 'a') as f:
|
||||
f.write('测试')
|
||||
|
||||
with open('123.txt', 'r') as f:
|
||||
# default mode is 'r'
|
||||
with open('123.txt') as f:
|
||||
assert f.read() == '123456' + '测试'
|
||||
|
||||
assert os.path.exists('123.txt')
|
||||
|
Loading…
x
Reference in New Issue
Block a user