diff --git a/include/pocketpy/common.h b/include/pocketpy/common.h index 094c5c4b..37fb000b 100644 --- a/include/pocketpy/common.h +++ b/include/pocketpy/common.h @@ -20,7 +20,7 @@ #include #include -#define PK_VERSION "1.1.1" +#define PK_VERSION "1.1.2" #include "config.h" #include "export.h" diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 62038860..a16e6454 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.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); diff --git a/src/io.cpp b/src/io.cpp index 63329f8a..e02f8d43 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -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]); diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 12f3b5bd..40891505 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -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"); diff --git a/tests/70_file.py b/tests/70_file.py index 094503f8..29773db5 100644 --- a/tests/70_file.py +++ b/tests/70_file.py @@ -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')