mirror of
				https://github.com/pocketpy/pocketpy
				synced 2025-11-04 02:30:17 +00:00 
			
		
		
		
	Compare commits
	
		
			4 Commits
		
	
	
		
			4e5021089c
			...
			24f3656356
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 
						 | 
					24f3656356 | ||
| 
						 | 
					19563e33d2 | ||
| 
						 | 
					5ad606859f | ||
| 
						 | 
					108d2fe969 | 
@ -1,41 +0,0 @@
 | 
				
			|||||||
---
 | 
					 | 
				
			||||||
icon: package
 | 
					 | 
				
			||||||
label: line_profiler
 | 
					 | 
				
			||||||
---
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
!!!
 | 
					 | 
				
			||||||
This module is optional. Set `PK_ENABLE_PROFILER` to `1` to enable it.
 | 
					 | 
				
			||||||
!!!
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
## Example
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```python
 | 
					 | 
				
			||||||
from line_profiler import LineProfiler
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
def my_func():
 | 
					 | 
				
			||||||
    a = 0
 | 
					 | 
				
			||||||
    for i in range(1000000):
 | 
					 | 
				
			||||||
        a += i
 | 
					 | 
				
			||||||
    return a
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
lp = LineProfiler()
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
lp.add_function(my_func)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
lp.runcall(my_func)
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
lp.print_stats()
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
```txt
 | 
					 | 
				
			||||||
Total time: 0.243s
 | 
					 | 
				
			||||||
File: 84_line_profiler.py
 | 
					 | 
				
			||||||
Function: my_func at line 3
 | 
					 | 
				
			||||||
Line #      Hits         Time  Per Hit   % Time  Line Contents
 | 
					 | 
				
			||||||
==============================================================
 | 
					 | 
				
			||||||
     3                                           def my_func():
 | 
					 | 
				
			||||||
     4         1            0        0      0.0      a = 0
 | 
					 | 
				
			||||||
     5   1000001           69        0     28.4      for i in range(1000000):
 | 
					 | 
				
			||||||
     6   1000001          174        0     71.6          a += i
 | 
					 | 
				
			||||||
     7         1            0        0      0.0      return a
 | 
					 | 
				
			||||||
```
 | 
					 | 
				
			||||||
@ -13,11 +13,6 @@
 | 
				
			|||||||
#define PK_ENABLE_OS                1
 | 
					#define PK_ENABLE_OS                1
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Enable `line_profiler` module and `breakpoint()` function
 | 
					 | 
				
			||||||
#ifndef PK_ENABLE_PROFILER          // can be overridden by cmake
 | 
					 | 
				
			||||||
#define PK_ENABLE_PROFILER          0
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
// GC min threshold
 | 
					// GC min threshold
 | 
				
			||||||
#ifndef PK_GC_MIN_THRESHOLD         // can be overridden by cmake
 | 
					#ifndef PK_GC_MIN_THRESHOLD         // can be overridden by cmake
 | 
				
			||||||
#define PK_GC_MIN_THRESHOLD         16384
 | 
					#define PK_GC_MIN_THRESHOLD         16384
 | 
				
			||||||
@ -46,9 +41,3 @@
 | 
				
			|||||||
#else
 | 
					#else
 | 
				
			||||||
    #define PK_PLATFORM_SEP '/'
 | 
					    #define PK_PLATFORM_SEP '/'
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
					 | 
				
			||||||
#ifdef NDEBUG
 | 
					 | 
				
			||||||
    #define PK_DEBUG 0
 | 
					 | 
				
			||||||
#else
 | 
					 | 
				
			||||||
    #define PK_DEBUG 1
 | 
					 | 
				
			||||||
#endif
 | 
					 | 
				
			||||||
@ -32,6 +32,7 @@ typedef struct VM {
 | 
				
			|||||||
    bool is_curr_exc_handled;  // handled by try-except block but not cleared yet
 | 
					    bool is_curr_exc_handled;  // handled by try-except block but not cleared yet
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    py_TValue reg[8];  // users' registers
 | 
					    py_TValue reg[8];  // users' registers
 | 
				
			||||||
 | 
					    void* ctx;         // user-defined context
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    py_StackRef __curr_class;
 | 
					    py_StackRef __curr_class;
 | 
				
			||||||
    py_StackRef __curr_function;
 | 
					    py_StackRef __curr_function;
 | 
				
			||||||
 | 
				
			|||||||
@ -92,6 +92,10 @@ PK_EXPORT int py_currentvm();
 | 
				
			|||||||
PK_EXPORT void py_switchvm(int index);
 | 
					PK_EXPORT void py_switchvm(int index);
 | 
				
			||||||
/// Reset the current VM.
 | 
					/// Reset the current VM.
 | 
				
			||||||
PK_EXPORT void py_resetvm();
 | 
					PK_EXPORT void py_resetvm();
 | 
				
			||||||
 | 
					/// Get the current VM context. This is used for user-defined data.
 | 
				
			||||||
 | 
					PK_EXPORT void* py_getvmctx();
 | 
				
			||||||
 | 
					/// Set the current VM context. This is used for user-defined data.
 | 
				
			||||||
 | 
					PK_EXPORT void py_setvmctx(void* ctx);
 | 
				
			||||||
/// Set `sys.argv`. Used for storing command-line arguments.
 | 
					/// Set `sys.argv`. Used for storing command-line arguments.
 | 
				
			||||||
PK_EXPORT void py_sys_setargv(int argc, char** argv);
 | 
					PK_EXPORT void py_sys_setargv(int argc, char** argv);
 | 
				
			||||||
/// Setup the callbacks for the current VM.
 | 
					/// Setup the callbacks for the current VM.
 | 
				
			||||||
@ -115,13 +119,13 @@ PK_EXPORT bool py_eval(const char* source, py_Ref module) PY_RAISE PY_RETURN;
 | 
				
			|||||||
/// Example:
 | 
					/// Example:
 | 
				
			||||||
/// `py_newstr(py_r0(), "abc");`
 | 
					/// `py_newstr(py_r0(), "abc");`
 | 
				
			||||||
/// `py_newint(py_r1(), 123);`
 | 
					/// `py_newint(py_r1(), 123);`
 | 
				
			||||||
/// `py_smartexec("print(_1, _2)", NULL, py_r0(), py_r1());`
 | 
					/// `py_smartexec("print(_0, _1)", NULL, py_r0(), py_r1());`
 | 
				
			||||||
/// `// "abc 123" will be printed`.
 | 
					/// `// "abc 123" will be printed`.
 | 
				
			||||||
PK_EXPORT bool py_smartexec(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
 | 
					PK_EXPORT bool py_smartexec(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
 | 
				
			||||||
/// Evaluate a source string with smart interpretation.
 | 
					/// Evaluate a source string with smart interpretation.
 | 
				
			||||||
/// Example:
 | 
					/// Example:
 | 
				
			||||||
/// `py_newstr(py_r0(), "abc");`
 | 
					/// `py_newstr(py_r0(), "abc");`
 | 
				
			||||||
/// `py_smarteval("len(_1)", NULL, py_r0());`
 | 
					/// `py_smarteval("len(_)", NULL, py_r0());`
 | 
				
			||||||
/// `int res = py_toint(py_retval());`
 | 
					/// `int res = py_toint(py_retval());`
 | 
				
			||||||
/// `// res will be 3`.
 | 
					/// `// res will be 3`.
 | 
				
			||||||
PK_EXPORT bool py_smarteval(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
 | 
					PK_EXPORT bool py_smarteval(const char* source, py_Ref module, ...) PY_RAISE PY_RETURN;
 | 
				
			||||||
@ -229,6 +233,8 @@ PK_EXPORT py_f64 py_tofloat(py_Ref);
 | 
				
			|||||||
/// If successful, return true and set the value to `out`.
 | 
					/// If successful, return true and set the value to `out`.
 | 
				
			||||||
/// Otherwise, return false and raise `TypeError`.
 | 
					/// Otherwise, return false and raise `TypeError`.
 | 
				
			||||||
PK_EXPORT bool py_castfloat(py_Ref, py_f64* out) PY_RAISE;
 | 
					PK_EXPORT bool py_castfloat(py_Ref, py_f64* out) PY_RAISE;
 | 
				
			||||||
 | 
					/// 32-bit version of `py_castfloat`.
 | 
				
			||||||
 | 
					PK_EXPORT bool py_castfloat32(py_Ref, float* out) PY_RAISE;
 | 
				
			||||||
/// Cast a `int` object in python to `int64_t`.
 | 
					/// Cast a `int` object in python to `int64_t`.
 | 
				
			||||||
PK_EXPORT bool py_castint(py_Ref, py_i64* out) PY_RAISE;
 | 
					PK_EXPORT bool py_castint(py_Ref, py_i64* out) PY_RAISE;
 | 
				
			||||||
/// Convert a `bool` object in python to `bool`.
 | 
					/// Convert a `bool` object in python to `bool`.
 | 
				
			||||||
@ -544,7 +550,7 @@ PK_EXPORT bool py_isidentical(py_Ref, py_Ref);
 | 
				
			|||||||
/// The stack remains unchanged after the operation.
 | 
					/// The stack remains unchanged after the operation.
 | 
				
			||||||
PK_EXPORT bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
 | 
					PK_EXPORT bool py_call(py_Ref f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if PK_DEBUG
 | 
					#ifndef NDEBUG
 | 
				
			||||||
/// Call a `py_CFunction` in a safe way.
 | 
					/// Call a `py_CFunction` in a safe way.
 | 
				
			||||||
/// This function does extra checks to help you debug `py_CFunction`.
 | 
					/// This function does extra checks to help you debug `py_CFunction`.
 | 
				
			||||||
PK_EXPORT bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
 | 
					PK_EXPORT bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) PY_RAISE PY_RETURN;
 | 
				
			||||||
 | 
				
			|||||||
@ -87,7 +87,7 @@ FrameResult VM__run_top_frame(VM* self) {
 | 
				
			|||||||
    __NEXT_STEP:
 | 
					    __NEXT_STEP:
 | 
				
			||||||
        byte = *frame->ip;
 | 
					        byte = *frame->ip;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if PK_DEBUG
 | 
					#ifndef NDEBUG
 | 
				
			||||||
        pk_print_stack(self, frame, byte);
 | 
					        pk_print_stack(self, frame, byte);
 | 
				
			||||||
        // assert(!py_checkexc(true));
 | 
					        // assert(!py_checkexc(true));
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
 | 
				
			|||||||
@ -71,6 +71,7 @@ void VM__ctor(VM* self) {
 | 
				
			|||||||
    self->curr_exception = *py_NIL;
 | 
					    self->curr_exception = *py_NIL;
 | 
				
			||||||
    self->is_curr_exc_handled = false;
 | 
					    self->is_curr_exc_handled = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    self->ctx = NULL;
 | 
				
			||||||
    self->__curr_class = NULL;
 | 
					    self->__curr_class = NULL;
 | 
				
			||||||
    self->__curr_function = NULL;
 | 
					    self->__curr_function = NULL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
@ -24,6 +24,14 @@ bool py_castfloat(py_Ref self, double* out) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					bool py_castfloat32(py_Ref self, float *out){
 | 
				
			||||||
 | 
					    switch(self->type) {
 | 
				
			||||||
 | 
					        case tp_int: *out = (float)self->_i64; return true;
 | 
				
			||||||
 | 
					        case tp_float: *out = (float)self->_f64; return true;
 | 
				
			||||||
 | 
					        default: return TypeError("expected 'int' or 'float', got '%t'", self->type);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
bool py_castint(py_Ref self, int64_t* out) {
 | 
					bool py_castint(py_Ref self, int64_t* out) {
 | 
				
			||||||
    if(self->type == tp_int) {
 | 
					    if(self->type == tp_int) {
 | 
				
			||||||
        *out = self->_i64;
 | 
					        *out = self->_i64;
 | 
				
			||||||
 | 
				
			|||||||
@ -75,6 +75,14 @@ int py_currentvm() {
 | 
				
			|||||||
    return -1;
 | 
					    return -1;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void* py_getvmctx(){
 | 
				
			||||||
 | 
					    return pk_current_vm->ctx;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void py_setvmctx(void* ctx){
 | 
				
			||||||
 | 
					    pk_current_vm->ctx = ctx;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void py_sys_setargv(int argc, char** argv) {
 | 
					void py_sys_setargv(int argc, char** argv) {
 | 
				
			||||||
    py_GlobalRef sys = py_getmodule("sys");
 | 
					    py_GlobalRef sys = py_getmodule("sys");
 | 
				
			||||||
    py_Ref argv_list = py_getdict(sys, py_name("argv"));
 | 
					    py_Ref argv_list = py_getdict(sys, py_name("argv"));
 | 
				
			||||||
@ -110,7 +118,7 @@ bool py_call(py_Ref f, int argc, py_Ref argv) {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#if PK_DEBUG
 | 
					#ifndef NDEBUG
 | 
				
			||||||
bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
 | 
					bool py_callcfunc(py_CFunction f, int argc, py_Ref argv) {
 | 
				
			||||||
    py_StackRef p0 = py_peek(0);
 | 
					    py_StackRef p0 = py_peek(0);
 | 
				
			||||||
    py_newnil(py_retval());
 | 
					    py_newnil(py_retval());
 | 
				
			||||||
 | 
				
			|||||||
@ -536,14 +536,14 @@ static bool
 | 
				
			|||||||
    // [globals, locals, code]
 | 
					    // [globals, locals, code]
 | 
				
			||||||
    CodeObject* co = py_touserdata(py_peek(-1));
 | 
					    CodeObject* co = py_touserdata(py_peek(-1));
 | 
				
			||||||
    py_StackRef locals = py_peek(-2);
 | 
					    py_StackRef locals = py_peek(-2);
 | 
				
			||||||
    int max_index = 0;
 | 
					    int max_index = -1;
 | 
				
			||||||
    c11__foreach(Bytecode, &co->codes, bc) {
 | 
					    c11__foreach(Bytecode, &co->codes, bc) {
 | 
				
			||||||
        if(bc->op == OP_LOAD_NAME) {
 | 
					        if(bc->op == OP_LOAD_NAME) {
 | 
				
			||||||
            c11_sv name = py_name2sv(bc->arg);
 | 
					            c11_sv name = py_name2sv(bc->arg);
 | 
				
			||||||
            if(name.data[0] != '_') continue;
 | 
					            if(name.data[0] != '_') continue;
 | 
				
			||||||
            int index;
 | 
					            int index;
 | 
				
			||||||
            if(name.size == 1) {
 | 
					            if(name.size == 1) {
 | 
				
			||||||
                index = 1;
 | 
					                index = 0;
 | 
				
			||||||
            } else if(name.size == 2 && isdigit(name.data[1])) {
 | 
					            } else if(name.size == 2 && isdigit(name.data[1])) {
 | 
				
			||||||
                index = name.data[1] - '0';
 | 
					                index = name.data[1] - '0';
 | 
				
			||||||
            } else {
 | 
					            } else {
 | 
				
			||||||
@ -553,17 +553,17 @@ static bool
 | 
				
			|||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    if(max_index == 0) { return ValueError("no placeholder found in the source"); }
 | 
					    if(max_index == -1) return ValueError("no placeholder found in the source");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    for(int i = 1; i <= max_index; i++) {
 | 
					    for(int i = 0; i <= max_index; i++) {
 | 
				
			||||||
        py_Ref val = va_arg(args, py_Ref);
 | 
					        py_Ref val = va_arg(args, py_Ref);
 | 
				
			||||||
        char buf[3];
 | 
					        char buf[3];
 | 
				
			||||||
        buf[0] = '_';
 | 
					        buf[0] = '_';
 | 
				
			||||||
        buf[1] = '0' + i;
 | 
					        buf[1] = '0' + i;
 | 
				
			||||||
        buf[2] = '\0';
 | 
					        buf[2] = '\0';
 | 
				
			||||||
        py_dict_setitem_by_str(locals, buf, val);
 | 
					        py_dict_setitem_by_str(locals, buf, val);
 | 
				
			||||||
        if(i == 1) {
 | 
					        if(i == 0) {
 | 
				
			||||||
            // _ => _1
 | 
					            // _ => _0
 | 
				
			||||||
            py_dict_setitem_by_str(locals, "_", val);
 | 
					            py_dict_setitem_by_str(locals, "_", val);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -43,7 +43,7 @@ int main(int argc, char** argv) {
 | 
				
			|||||||
    if(argc == 1) {
 | 
					    if(argc == 1) {
 | 
				
			||||||
        printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
 | 
					        printf("pocketpy " PK_VERSION " (" __DATE__ ", " __TIME__ ") ");
 | 
				
			||||||
        printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
 | 
					        printf("[%d bit] on %s", (int)(sizeof(void*) * 8), PY_SYS_PLATFORM_STRING);
 | 
				
			||||||
#if PK_DEBUG
 | 
					#ifndef NDEBUG
 | 
				
			||||||
        printf(" (DEBUG)");
 | 
					        printf(" (DEBUG)");
 | 
				
			||||||
#endif
 | 
					#endif
 | 
				
			||||||
        printf("\n");
 | 
					        printf("\n");
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user