From 70e824a6b695e963621664589d1d00aeb182c018 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 7 Jun 2025 02:19:55 +0800 Subject: [PATCH] fix #371 --- include/pocketpy/pocketpy.h | 2 +- src/interpreter/vm.c | 6 ++++-- src/public/modules.c | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 07410cef..8b5d084f 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -77,7 +77,7 @@ typedef struct py_Callbacks { /// Flush the output buffer of `print`. void (*flush)(); /// Used by `input` to get a character. - int (*getchar)(); + int (*getchr)(); /// Used by `gc.collect()` to mark extra objects for garbage collection. void (*gc_mark)(void (*f)(py_Ref val, void* ctx), void* ctx); } py_Callbacks; diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 7aa53196..9f6c7520 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -32,6 +32,8 @@ static void pk_default_print(const char* data) { printf("%s", data); } static void pk_default_flush() { fflush(stdout); } +static int pk_default_getchr() { return getchar(); } + void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event) { LineProfiler* self = &pk_current_vm->line_profiler; if(self->enabled && event == TRACE_EVENT_LINE) { LineProfiler__tracefunc_line(self, frame); } @@ -75,7 +77,7 @@ void VM__ctor(VM* self) { self->callbacks.importfile = pk_default_importfile; self->callbacks.print = pk_default_print; self->callbacks.flush = pk_default_flush; - self->callbacks.getchar = getchar; + self->callbacks.getchr = pk_default_getchr; self->last_retval = *py_NIL(); self->curr_exception = *py_NIL(); @@ -810,7 +812,7 @@ int py_replinput(char* buf, int max_size) { printf(">>> "); while(true) { - int c = pk_current_vm->callbacks.getchar(); + int c = pk_current_vm->callbacks.getchr(); if(c == EOF) return -1; if(c == '\n') { diff --git a/src/public/modules.c b/src/public/modules.c index 14c1431d..5eb84800 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -205,7 +205,7 @@ static bool builtins_input(int argc, py_Ref argv) { c11_sbuf buf; c11_sbuf__ctor(&buf); while(true) { - int c = py_callbacks()->getchar(); + int c = py_callbacks()->getchr(); if(c == '\n' || c == '\r') break; if(c == EOF) break; c11_sbuf__write_char(&buf, c);