浏览代码

`open` default mode is `'r'`

blueloveTH 2 年之前
父节点
当前提交
014902b4bb
共有 5 个文件被更改,包括 53 次插入54 次删除
  1. 1 1
      include/pocketpy/common.h
  2. 0 51
      include/pocketpy/pocketpy.h
  3. 1 1
      src/io.cpp
  4. 49 0
      src/pocketpy.cpp
  5. 2 1
      tests/70_file.py

+ 1 - 1
include/pocketpy/common.h

@@ -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"

+ 0 - 51
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);

+ 1 - 1
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]);

+ 49 - 0
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");

+ 2 - 1
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')