From 7deb596bfc805684c17f38cee9f203aaef9d8020 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Thu, 17 Oct 2024 16:53:04 +0800 Subject: [PATCH] improve `find_bounding_rect` --- include/typings/array2d.pyi | 2 +- src/modules/array2d.c | 3 +-- tests/90_array2d.py | 6 +++++- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/typings/array2d.pyi b/include/typings/array2d.pyi index 8b1023c9..7a464440 100644 --- a/include/typings/array2d.pyi +++ b/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. diff --git a/src/modules/array2d.c b/src/modules/array2d.c index 7c31b03f..7b2f3964 100644 --- a/src/modules/array2d.c +++ b/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()); diff --git a/tests/90_array2d.py b/tests/90_array2d.py index c8eb2bc0..b34f2ea3 100644 --- a/tests/90_array2d.py +++ b/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