Przeglądaj źródła

improve `find_bounding_rect`

blueloveTH 1 rok temu
rodzic
commit
7deb596bfc
3 zmienionych plików z 7 dodań i 4 usunięć
  1. 1 1
      include/typings/array2d.pyi
  2. 1 2
      src/modules/array2d.c
  3. 5 1
      tests/90_array2d.py

+ 1 - 1
include/typings/array2d.pyi

@@ -61,7 +61,7 @@ class array2d(Generic[T]):
     def count_neighbors(self, value: T, neighborhood: Neighborhood) -> 'array2d[int]':
         """Counts the number of neighbors with the given value for each cell."""
 
-    def find_bounding_rect(self, value: T) -> tuple[int, int, int, int] | None:
+    def find_bounding_rect(self, value: T) -> tuple[int, int, int, int]:
         """Finds the bounding rectangle of the given value.
         
         Returns a tuple `(x, y, width, height)` or `None` if the value is not found.

+ 1 - 2
src/modules/array2d.c

@@ -1,7 +1,6 @@
 #include "pocketpy/pocketpy.h"
 
 #include "pocketpy/common/utils.h"
-#include "pocketpy/objects/object.h"
 #include "pocketpy/common/sstream.h"
 #include "pocketpy/interpreter/vm.h"
 
@@ -371,7 +370,7 @@ static bool array2d_find_bounding_rect(int argc, py_Ref argv) {
     int width = right - left + 1;
     int height = bottom - top + 1;
     if(width <= 0 || height <= 0) {
-        py_newnone(py_retval());
+        return ValueError("value not found");
     } else {
         py_newtuple(py_retval(), 4);
         py_TValue* data = py_tuple_data(py_retval());

+ 5 - 1
tests/90_array2d.py

@@ -134,8 +134,12 @@ assert a.count(1) == 3*2
 
 assert a.find_bounding_rect(1) == (1, 1, 3, 2)
 assert a.find_bounding_rect(0) == (0, 0, 5, 5)
-assert a.find_bounding_rect(2) == None
 
+try:
+    a.find_bounding_rect(2)
+    exit(1)
+except ValueError:
+    pass
 
 a = array2d(3, 2, default='?')
 # int/float/str/bool/None