diff --git a/amalgamate.py b/amalgamate.py index 6df868b4..46c899c3 100644 --- a/amalgamate.py +++ b/amalgamate.py @@ -9,7 +9,7 @@ pipeline = [ ["config.h", "export.h", "common.h", "memory.h", "vector.h", "str.h", "tuplelist.h", "namedict.h", "error.h"], ["obj.h", "dict.h", "codeobject.h", "frame.h"], ["gc.h", "vm.h", "ceval.h", "lexer.h", "expr.h", "compiler.h", "repl.h"], - ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"], + ["_generated.h", "cffi.h", "bindings.h", "iter.h", "base64.h", "csv.h", "collections.h", "line_profiler.h", "array2d.h", "dataclasses.h", "random.h", "linalg.h", "easing.h", "io.h", "modules.h"], ["pocketpy.h", "pocketpy_c.h"] ] diff --git a/include/pocketpy/line_profiler.h b/include/pocketpy/line_profiler.h new file mode 100644 index 00000000..93356245 --- /dev/null +++ b/include/pocketpy/line_profiler.h @@ -0,0 +1,9 @@ +#pragma once + +#include "bindings.h" + +namespace pkpy { + +void add_module_line_profiler(VM* vm); + +} // namespace pkpy diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 89e11d07..57df6ceb 100644 --- a/include/pocketpy/pocketpy.h +++ b/include/pocketpy/pocketpy.h @@ -16,4 +16,5 @@ #include "csv.h" #include "dataclasses.h" #include "array2d.h" +#include "line_profiler.h" #include "modules.h" diff --git a/include/typings/line_profiler.pyi b/include/typings/line_profiler.pyi new file mode 100644 index 00000000..53a66878 --- /dev/null +++ b/include/typings/line_profiler.pyi @@ -0,0 +1,10 @@ +from typing import Callable + +class LineProfiler: + def __init__(self): ... + + def add_function(self, func: Callable) -> None: ... + + def runcall(self, func: Callable, *args, **kw) -> None: ... + + def print_stats(self) -> None: ... diff --git a/src/array2d.cpp b/src/array2d.cpp index a91c5dab..2b605bc7 100644 --- a/src/array2d.cpp +++ b/src/array2d.cpp @@ -168,8 +168,11 @@ struct Array2d{ vm->bind(type, "copy_(self, other)", [](VM* vm, ArgsView args){ Array2d& self = PK_OBJ_GET(Array2d, args[0]); Array2d& other = CAST(Array2d&, args[1]); - delete self.data; - self.init(other.n_cols, other.n_rows); + // if self and other have different sizes, re-initialize self + if(self.n_cols != other.n_cols || self.n_rows != other.n_rows){ + delete self.data; + self.init(other.n_cols, other.n_rows); + } for(int i = 0; i < self.numel; i++){ self.data[i] = other.data[i]; } diff --git a/src/line_profiler.cpp b/src/line_profiler.cpp new file mode 100644 index 00000000..d5848004 --- /dev/null +++ b/src/line_profiler.cpp @@ -0,0 +1,35 @@ +#include "pocketpy/line_profiler.h" + +namespace pkpy{ + +struct LineProfiler{ + PY_CLASS(LineProfiler, line_profiler, LineProfiler) + + std::set _functions; + + static void _register(VM* vm, PyObject* mod, PyObject* type){ + vm->bind_default_constructor(type); + + vm->bind(type, "add_function(self, func)", [](VM* vm, ArgsView args){ + // ... + return vm->None; + }); + + vm->bind(type, "runcall(self, func, *args, **kw)", [](VM* vm, ArgsView args){ + // ... + return vm->None; + }); + + vm->bind(type, "print_stats(self)", [](VM* vm, ArgsView args){ + // ... + return vm->None; + }); + } +}; + +void add_module_line_profiler(VM *vm){ + PyObject* mod = vm->new_module("line_profiler"); + LineProfiler::register_class(vm, mod); +} + +} // namespace pkpy \ No newline at end of file diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 363f3e75..866d3e2b 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -1529,6 +1529,7 @@ void VM::post_init(){ add_module_easing(this); add_module_collections(this); add_module_array2d(this); + add_module_line_profiler(this); #ifdef PK_USE_CJSON add_module_cjson(this); diff --git a/tests/80_array2d.py b/tests/80_array2d.py index d526f441..1d4728b3 100644 --- a/tests/80_array2d.py +++ b/tests/80_array2d.py @@ -89,6 +89,9 @@ assert d == array2d(2, 4, default=0) # test copy_ a.copy_(d) assert a == d and a is not d +x = array2d(4, 4, default=0) +x.copy_(d) +assert x == d and x is not d # test subclass array2d class A(array2d):