Просмотр исходного кода

add boilerplate for `line_profiler`

blueloveTH 2 лет назад
Родитель
Сommit
209b7f6831

+ 1 - 1
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"]
 ]
 

+ 9 - 0
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

+ 1 - 0
include/pocketpy/pocketpy.h

@@ -16,4 +16,5 @@
 #include "csv.h"
 #include "dataclasses.h"
 #include "array2d.h"
+#include "line_profiler.h"
 #include "modules.h"

+ 10 - 0
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: ...

+ 5 - 2
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];
             }

+ 35 - 0
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<void*> _functions;
+
+    static void _register(VM* vm, PyObject* mod, PyObject* type){
+        vm->bind_default_constructor<LineProfiler>(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

+ 1 - 0
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);

+ 3 - 0
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):