diff --git a/docs/quick-start/installation.md b/docs/quick-start/installation.md index 2640da62..ed9d20df 100644 --- a/docs/quick-start/installation.md +++ b/docs/quick-start/installation.md @@ -160,4 +160,15 @@ These two fields are C function pointers `PrintFunc` with the following signatur ```cpp typedef void(*PrintFunc)(const char*, int) +``` + +Or you can override these two virtual functions: +```cpp + virtual void stdout_write(const Str& s){ + _stdout(s.data, s.size); + } + + virtual void stderr_write(const Str& s){ + _stderr(s.data, s.size); + } ``` \ No newline at end of file diff --git a/include/pocketpy/vm.h b/include/pocketpy/vm.h index 8f760584..c5275be4 100644 --- a/include/pocketpy/vm.h +++ b/include/pocketpy/vm.h @@ -205,10 +205,14 @@ public: void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2){ PUSH(_0); PUSH(_1); PUSH(_2); } void _push_varargs(PyObject* _0, PyObject* _1, PyObject* _2, PyObject* _3){ PUSH(_0); PUSH(_1); PUSH(_2); PUSH(_3); } - void stdout_write(const Str& s){ + virtual void stdout_write(const Str& s){ _stdout(s.data, s.size); } + virtual void stderr_write(const Str& s){ + _stderr(s.data, s.size); + } + template PyObject* call(PyObject* callable, Args&&... args){ PUSH(callable); diff --git a/src/vm.cpp b/src/vm.cpp index 7980b643..5f8540d2 100644 --- a/src/vm.cpp +++ b/src/vm.cpp @@ -164,21 +164,20 @@ namespace pkpy{ #endif return _exec(code, _module); }catch (const Exception& e){ - Str sum = e.summary() + "\n"; - _stderr(sum.data, sum.size); + stderr_write(e.summary() + "\n"); } #if !PK_DEBUG_FULL_EXCEPTION catch(const std::exception& e) { Str msg = "An std::exception occurred! It could be a bug.\n"; msg = msg + e.what() + "\n"; - _stderr(msg.data, msg.size); + stderr_write(msg); } catch(NeedMoreLines){ throw; } catch(...) { Str msg = "An unknown exception occurred! It could be a bug. Please report it to @blueloveTH on GitHub.\n"; - _stderr(msg.data, msg.size); + stderr_write(msg); } #endif callstack.clear();