Remove del obj and add deinited

This commit is contained in:
gamblor21 2023-03-11 09:44:35 -06:00
parent 0ca6cc7741
commit 8b9f5e7f53
3 changed files with 45 additions and 1 deletions

View File

@ -30,6 +30,8 @@
#include "py/runtime.h"
#include "py/objproperty.h"
#include "shared/runtime/context_manager_helpers.h"
#include "shared-bindings/util.h"
#include "supervisor/shared/translate/translate.h"
#include "shared-bindings/gifio/OnDiskGif.h"
@ -125,11 +127,34 @@ STATIC mp_obj_t gifio_ondiskgif_make_new(const mp_obj_type_t *type, size_t n_arg
return MP_OBJ_FROM_PTR(self);
}
STATIC void check_for_deinit(gifio_ondiskgif_t *self) {
if (common_hal_gifio_ondiskgif_deinited(self)) {
raise_deinited_error();
}
}
//| def __enter__(self) -> OnDiskGif:
//| """No-op used by Context Managers."""
//| ...
// Provided by context manager helper.
//| def __exit__(self) -> None:
//| """Automatically deinitializes the GIF when exiting a context. See
//| :ref:`lifetime-and-contextmanagers` for more info."""
//| ...
STATIC mp_obj_t gifio_ondiskgif_obj___exit__(size_t n_args, const mp_obj_t *args) {
(void)n_args;
common_hal_gifio_ondiskgif_deinit(args[0]);
return mp_const_none;
}
STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(gifio_ondiskgif___exit___obj, 4, 4, gifio_ondiskgif_obj___exit__);
//| width: int
//| """Width of the gif. (read only)"""
STATIC mp_obj_t gifio_ondiskgif_obj_get_width(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_gifio_ondiskgif_get_width(self));
}
@ -143,6 +168,7 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_width_obj,
STATIC mp_obj_t gifio_ondiskgif_obj_get_height(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_gifio_ondiskgif_get_height(self));
}
@ -155,6 +181,8 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_height_obj,
//| """The bitmap used to hold the current frame."""
STATIC mp_obj_t gifio_ondiskgif_obj_get_bitmap(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return common_hal_gifio_ondiskgif_get_bitmap(self);
}
@ -168,6 +196,7 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_bitmap_obj,
STATIC mp_obj_t gifio_ondiskgif_obj_next_frame(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_float((float)common_hal_gifio_ondiskgif_next_frame(self, true) / 1000);
}
@ -179,6 +208,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(gifio_ondiskgif_next_frame_obj, gifio_ondiskgif_obj_ne
STATIC mp_obj_t gifio_ondiskgif_obj_get_duration(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_float((float)common_hal_gifio_ondiskgif_get_duration(self) / 1000);
}
@ -192,6 +222,7 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_duration_obj,
STATIC mp_obj_t gifio_ondiskgif_obj_get_frame_count(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return MP_OBJ_NEW_SMALL_INT(common_hal_gifio_ondiskgif_get_frame_count(self));
}
@ -205,6 +236,7 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_frame_count_obj,
STATIC mp_obj_t gifio_ondiskgif_obj_get_min_delay(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_float((float)common_hal_gifio_ondiskgif_get_min_delay(self) / 1000);
}
@ -219,6 +251,7 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_min_delay_obj,
STATIC mp_obj_t gifio_ondiskgif_obj_get_max_delay(mp_obj_t self_in) {
gifio_ondiskgif_t *self = MP_OBJ_TO_PTR(self_in);
check_for_deinit(self);
return mp_obj_new_float((float)common_hal_gifio_ondiskgif_get_max_delay(self) / 1000);
}
@ -228,6 +261,9 @@ MP_PROPERTY_GETTER(gifio_ondiskgif_max_delay_obj,
(mp_obj_t)&gifio_ondiskgif_get_max_delay_obj);
STATIC const mp_rom_map_elem_t gifio_ondiskgif_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&gifio_ondiskgif_deinit_obj) },
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&gifio_ondiskgif___exit___obj) },
{ MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&gifio_ondiskgif_height_obj) },
{ MP_ROM_QSTR(MP_QSTR_bitmap), MP_ROM_PTR(&gifio_ondiskgif_bitmap_obj) },
{ MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&gifio_ondiskgif_width_obj) },

View File

@ -45,4 +45,6 @@ int32_t common_hal_gifio_ondiskgif_get_duration(gifio_ondiskgif_t *self);
int32_t common_hal_gifio_ondiskgif_get_frame_count(gifio_ondiskgif_t *self);
int32_t common_hal_gifio_ondiskgif_get_min_delay(gifio_ondiskgif_t *self);
int32_t common_hal_gifio_ondiskgif_get_max_delay(gifio_ondiskgif_t *self);
void common_hal_gifio_ondiskgif_deinit(gifio_ondiskgif_t *self);
bool common_hal_gifio_ondiskgif_deinited(gifio_ondiskgif_t *self);
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_ONDISKGIF_H

View File

@ -154,7 +154,13 @@ void common_hal_gifio_ondiskgif_construct(gifio_ondiskgif_t *self, pyb_file_obj_
self->min_delay = info.iMinDelay;
self->max_delay = info.iMaxDelay;
// mp_printf(&mp_plat_print, "GIF_init returned %d %x\n", result, bitmap->data);
void common_hal_gifio_ondiskgif_deinit(gifio_ondiskgif_t *self) {
self->file = NULL;
self->bitmap = NULL;
}
bool common_hal_gifio_ondiskgif_deinited(gifio_ondiskgif_t *self) {
return self->bitmap == NULL;
}
uint16_t common_hal_gifio_ondiskgif_get_height(gifio_ondiskgif_t *self) {