From 45828643b70cc74d4716b00d54a14067f865e4d6 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Wed, 28 May 2025 17:33:03 +0800 Subject: [PATCH] fix line_profiler --- include/pocketpy/interpreter/line_profiler.h | 2 + src/interpreter/ceval.c | 3 - src/interpreter/vm.c | 5 ++ src2/main.c | 59 ++------------------ 4 files changed, 13 insertions(+), 56 deletions(-) diff --git a/include/pocketpy/interpreter/line_profiler.h b/include/pocketpy/interpreter/line_profiler.h index 036dd99a..1382a6ee 100644 --- a/include/pocketpy/interpreter/line_profiler.h +++ b/include/pocketpy/interpreter/line_profiler.h @@ -24,3 +24,5 @@ void LineProfiler__begin(LineProfiler* self); void LineProfiler__tracefunc_line(LineProfiler* self, py_Frame* frame); void LineProfiler__end(LineProfiler* self); void LineProfiler__reset(LineProfiler* self); + +void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event); \ No newline at end of file diff --git a/src/interpreter/ceval.c b/src/interpreter/ceval.c index 8c53e1db..dd4bc08f 100644 --- a/src/interpreter/ceval.c +++ b/src/interpreter/ceval.c @@ -105,9 +105,6 @@ FrameResult VM__run_top_frame(VM* self) { PK_INCREF(loc.src); self->trace_info.prev_loc = loc; self->trace_info.func(frame, TRACE_EVENT_LINE); - if(self->line_profiler.enabled) { - LineProfiler__tracefunc_line(&self->line_profiler, frame); - } } } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 2c36a1c7..d9986d8a 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -32,6 +32,11 @@ static void pk_default_print(const char* data) { printf("%s", data); } static void pk_default_flush() { fflush(stdout); } +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); } +} + static void py_TypeInfo__ctor(py_TypeInfo* self, py_Name name, py_Type index, diff --git a/src2/main.c b/src2/main.c index 9bae6141..5642ca46 100644 --- a/src2/main.c +++ b/src2/main.c @@ -6,30 +6,6 @@ #include "pocketpy.h" -#define py_interrupt() - -#ifdef _WIN32 -#define WIN32_LEAN_AND_MEAN -#include - -static BOOL WINAPI sigint_handler(DWORD dwCtrlType) { - if(dwCtrlType == CTRL_C_EVENT) { - py_interrupt(); - return TRUE; - } - return FALSE; -} - -#else - -// set ctrl+c handler -#include -#include - -static void sigint_handler(int sig) { py_interrupt(); } - -#endif - static char* read_file(const char* path) { FILE* file = fopen(path, "rb"); if(file == NULL) { @@ -45,57 +21,34 @@ static char* read_file(const char* path) { return buffer; } -static void tracefunc(py_Frame* frame, enum py_TraceEvent event) { - int line; - const char* filename = py_Frame_sourceloc(frame, &line); - const char* event_str; - switch(event) { - case TRACE_EVENT_LINE: - event_str = "line"; - break; - case TRACE_EVENT_EXCEPTION: - event_str = "exception"; - break; - case TRACE_EVENT_PUSH: - event_str = "push"; - break; - case TRACE_EVENT_POP: - event_str = "pop"; - break; - } - printf("\x1b[30m%s:%d, event=%s\x1b[0m\n", filename, line, event_str); -} - +void LineProfiler__tracefunc(py_Frame* frame, enum py_TraceEvent event); static char buf[2048]; int main(int argc, char** argv) { #if _WIN32 SetConsoleCP(CP_UTF8); SetConsoleOutputCP(CP_UTF8); - // SetConsoleCtrlHandler((PHANDLER_ROUTINE)sigint_handler, TRUE); -#else - // signal(SIGINT, sigint_handler); #endif - bool trace = false; + bool profile = false; const char* filename = NULL; for(int i = 1; i < argc; i++) { - if(strcmp(argv[i], "--trace") == 0) { - trace = true; + if(strcmp(argv[i], "--profile") == 0) { + profile = true; continue; } if(filename == NULL) { filename = argv[i]; continue; } - printf("Usage: pocketpy [--trace] filename\n"); + printf("Usage: pocketpy [--profile] filename\n"); } py_initialize(); py_sys_setargv(argc, argv); - if(trace) py_sys_settrace(tracefunc, true); + if(profile) py_sys_settrace(LineProfiler__tracefunc, true); if(filename == NULL) { printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");