From 2370ef062ee64a9e14d83d03dcd0b43f13485239 Mon Sep 17 00:00:00 2001 From: Mikey Sklar Date: Mon, 2 Nov 2026 17:37:25 -0800 Subject: [PATCH] displayio: OnDiskBitmap takes only a filename, add deinit and with Passing an open file has been deprecated since 7.0. A file closed early could silently draw the wrong image on unmapped builds. OnDiskBitmap now owns its file, so deinit() and a with block can close it. Co-Authored-By: Claude Opus 5.5 --- locale/circuitpython.pot | 3 +- .../zephyr_display/test_zephyr_display.py | 43 +++++++++++++ shared-bindings/displayio/OnDiskBitmap.c | 64 ++++++++++++++----- shared-bindings/displayio/OnDiskBitmap.h | 2 + shared-module/displayio/OnDiskBitmap.c | 19 +++++- 5 files changed, 111 insertions(+), 20 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index 24c9901fa8a..9f438cd37c3 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -4044,8 +4044,7 @@ msgstr "" msgid "%q and %q must be different" msgstr "" -#: shared-bindings/audiomp3/MP3Decoder.c -#: shared-bindings/displayio/OnDiskBitmap.c shared-bindings/gifio/OnDiskGif.c +#: shared-bindings/audiomp3/MP3Decoder.c shared-bindings/gifio/OnDiskGif.c #: shared-bindings/synthio/__init__.c shared-module/gifio/GifWriter.c msgid "file must be a file opened in byte mode" msgstr "" diff --git a/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py b/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py index 6700c42ec4a..43eb69e999e 100644 --- a/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py +++ b/ports/zephyr-cp/tests/zephyr_display/test_zephyr_display.py @@ -297,6 +297,49 @@ def test_displayio_color_gradient_pixel_format(pixel_format, request, circuitpyt _golden_compare_or_update(request, circuitpython.display_capture_paths(), golden) +DISPLAYIO_ONDISKBITMAP_LIFETIME_CODE = """\ +import board +import displayio + +with open("/gradient.bmp", "rb") as f: + try: + displayio.OnDiskBitmap(f) + except TypeError: + print("open file rejected") + +with displayio.OnDiskBitmap("/gradient.bmp") as odb: + print("width", odb.width) +try: + odb.width +except ValueError: + print("with block deinited") + +odb = displayio.OnDiskBitmap("/gradient.bmp") +g = displayio.Group() +g.append(displayio.TileGrid(odb, pixel_shader=odb.pixel_shader)) +board.DISPLAY.root_group = g +odb.deinit() +odb.deinit() +board.DISPLAY.refresh() +print("drawn after deinit") +""" + + +@pytest.mark.circuitpy_drive( + {"code.py": DISPLAYIO_ONDISKBITMAP_LIFETIME_CODE, "gradient.bmp": _GRADIENT_BMP} +) +@pytest.mark.display +@pytest.mark.duration(8) +def test_ondiskbitmap_lifetime(circuitpython): + circuitpython.wait_until_done() + + output = circuitpython.serial.all_output + assert "open file rejected" in output + assert "width 320" in output + assert "with block deinited" in output + assert "drawn after deinit" in output + + DISPLAYIO_GOLDEN_STRIPES_CODE = """\ import board import displayio diff --git a/shared-bindings/displayio/OnDiskBitmap.c b/shared-bindings/displayio/OnDiskBitmap.c index 407b9d88351..2169e78a9f9 100644 --- a/shared-bindings/displayio/OnDiskBitmap.c +++ b/shared-bindings/displayio/OnDiskBitmap.c @@ -11,8 +11,10 @@ #include "py/runtime.h" #include "extmod/vfs.h" #include "py/objproperty.h" +#include "shared/runtime/context_manager_helpers.h" #include "shared-bindings/displayio/OnDiskBitmap.h" +#include "shared-bindings/util.h" //| class OnDiskBitmap: //| """Loads values straight from disk. This minimizes memory use but can lead to @@ -48,40 +50,64 @@ //| while True: //| pass""" //| -//| def __init__(self, file: Union[str, typing.BinaryIO]) -> None: +//| def __init__(self, file: str) -> None: //| """Create an OnDiskBitmap object with the given file. //| -//| :param file file: The name of the bitmap file. For backwards compatibility, a file opened in binary mode may also be passed. +//| :param str file: The name of the bitmap file. //| -//| Older versions of CircuitPython required a file opened in binary -//| mode. CircuitPython 7.0 modified OnDiskBitmap so that it takes a -//| filename instead, and opens the file internally. A future version -//| of CircuitPython will remove the ability to pass in an opened file. +//| CircuitPython 11 no longer accepts a file opened in binary mode. Pass the filename instead. +//| OnDiskBitmap opens the file itself and keeps it open until `deinit()` or +//| until the object is garbage collected. //| """ //| ... //| static mp_obj_t displayio_ondiskbitmap_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { mp_arg_check_num(n_args, n_kw, 1, 1, false); - mp_obj_t arg = all_args[0]; - - if (mp_obj_is_str(arg)) { - arg = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), arg, MP_ROM_QSTR(MP_QSTR_rb)); - } - if (!mp_obj_is_fileio(arg)) { - mp_raise_TypeError(MP_ERROR_TEXT("file must be a file opened in byte mode")); - } + mp_obj_t filename = mp_arg_validate_type_string(all_args[0], MP_QSTR_file); + mp_obj_t file = mp_call_function_2(MP_OBJ_FROM_PTR(&mp_builtin_open_obj), filename, MP_ROM_QSTR(MP_QSTR_rb)); displayio_ondiskbitmap_t *self = mp_obj_malloc(displayio_ondiskbitmap_t, &displayio_ondiskbitmap_type); - common_hal_displayio_ondiskbitmap_construct(self, arg); + common_hal_displayio_ondiskbitmap_construct(self, file); return MP_OBJ_FROM_PTR(self); } +static void check_for_deinit(displayio_ondiskbitmap_t *self) { + if (common_hal_displayio_ondiskbitmap_deinited(self)) { + raise_deinited_error(); + } +} + +//| def deinit(self) -> None: +//| """Close the file. The object can no longer be used. If a `TileGrid` still shows +//| this bitmap, areas redrawn after this use color 0, so remove the `TileGrid` first.""" +//| ... +//| +static mp_obj_t displayio_ondiskbitmap_obj_deinit(mp_obj_t self_in) { + displayio_ondiskbitmap_t *self = MP_OBJ_TO_PTR(self_in); + common_hal_displayio_ondiskbitmap_deinit(self); + return mp_const_none; +} +static MP_DEFINE_CONST_FUN_OBJ_1(displayio_ondiskbitmap_deinit_obj, displayio_ondiskbitmap_obj_deinit); + +//| def __enter__(self) -> OnDiskBitmap: +//| """No-op used by Context Managers.""" +//| ... +//| +// Provided by context manager helper. + +//| def __exit__(self) -> None: +//| """Automatically deinitializes the object when exiting a context. See +//| :ref:`lifetime-and-contextmanagers` for more info.""" +//| ... +//| +// Provided by context manager helper. + //| width: int //| """Width of the bitmap. (read only)""" static mp_obj_t displayio_ondiskbitmap_obj_get_width(mp_obj_t self_in) { displayio_ondiskbitmap_t *self = MP_OBJ_TO_PTR(self_in); - + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskbitmap_get_width(self)); } @@ -94,7 +120,7 @@ MP_PROPERTY_GETTER(displayio_ondiskbitmap_width_obj, //| """Height of the bitmap. (read only)""" static mp_obj_t displayio_ondiskbitmap_obj_get_height(mp_obj_t self_in) { displayio_ondiskbitmap_t *self = MP_OBJ_TO_PTR(self_in); - + check_for_deinit(self); return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_ondiskbitmap_get_height(self)); } @@ -111,6 +137,7 @@ MP_PROPERTY_GETTER(displayio_ondiskbitmap_height_obj, //| static mp_obj_t displayio_ondiskbitmap_obj_get_pixel_shader(mp_obj_t self_in) { displayio_ondiskbitmap_t *self = MP_OBJ_TO_PTR(self_in); + check_for_deinit(self); return common_hal_displayio_ondiskbitmap_get_pixel_shader(self); } @@ -120,6 +147,9 @@ MP_PROPERTY_GETTER(displayio_ondiskbitmap_pixel_shader_obj, (mp_obj_t)&displayio_ondiskbitmap_get_pixel_shader_obj); static const mp_rom_map_elem_t displayio_ondiskbitmap_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&displayio_ondiskbitmap_deinit_obj) }, + { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) }, + { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_ondiskbitmap_height_obj) }, { MP_ROM_QSTR(MP_QSTR_pixel_shader), MP_ROM_PTR(&displayio_ondiskbitmap_pixel_shader_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_ondiskbitmap_width_obj) }, diff --git a/shared-bindings/displayio/OnDiskBitmap.h b/shared-bindings/displayio/OnDiskBitmap.h index 0222399e419..fbfc112eaa8 100644 --- a/shared-bindings/displayio/OnDiskBitmap.h +++ b/shared-bindings/displayio/OnDiskBitmap.h @@ -12,6 +12,8 @@ extern const mp_obj_type_t displayio_ondiskbitmap_type; void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, mp_obj_t file); +void common_hal_displayio_ondiskbitmap_deinit(displayio_ondiskbitmap_t *self); +bool common_hal_displayio_ondiskbitmap_deinited(displayio_ondiskbitmap_t *self); uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *bitmap, int16_t x, int16_t y); diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c index 32bb6e508e0..abaa169b6d9 100644 --- a/shared-module/displayio/OnDiskBitmap.c +++ b/shared-module/displayio/OnDiskBitmap.c @@ -209,9 +209,26 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, } +void common_hal_displayio_ondiskbitmap_deinit(displayio_ondiskbitmap_t *self) { + if (common_hal_displayio_ondiskbitmap_deinited(self)) { + return; + } + mp_stream_close(self->file); + self->file = NULL; + #if CIRCUITPY_STORAGE_MAP_FILE + // A TileGrid may still draw this bitmap, so it must stop reading rows from mapped flash. + self->rows_mapped = false; + #endif +} + +bool common_hal_displayio_ondiskbitmap_deinited(displayio_ondiskbitmap_t *self) { + return self->file == NULL; +} + uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *self, int16_t x, int16_t y) { - if (x < 0 || x >= self->width || y < 0 || y >= self->height) { + if (common_hal_displayio_ondiskbitmap_deinited(self) || + x < 0 || x >= self->width || y < 0 || y >= self->height) { return 0; }