@@ -222,26 +222,33 @@ static mp_obj_t i2ctarget_i2c_target_request_make_new(const mp_obj_type_t *type,
222222 return mp_obj_new_i2ctarget_i2c_target_request (args [0 ], mp_obj_get_int (args [1 ]), mp_obj_is_true (args [2 ]), mp_obj_is_true (args [3 ]));
223223}
224224
225+ static void target_request_check_for_deinit (i2ctarget_i2c_target_request_obj_t * self ) {
226+ if (self -> target == NULL ) {
227+ raise_deinited_error ();
228+ }
229+ check_for_deinit (self -> target );
230+ }
231+
225232//| def deinit(self) -> None:
226233//| """Disconnects from parent `I2CTarget`.
227234//| Called by `__exit__()` to indicate the `I2CTargetRequest` is no longer useful."""
228235//| ...
229236//|
230237static mp_obj_t i2ctarget_i2c_target_request_deinit (mp_obj_t self_in ) {
231238 i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (self_in );
239+ target_request_check_for_deinit (self );
240+
241+ i2ctarget_i2c_target_obj_t * target = self -> target ;
242+
243+ // Deinit I2CTargetRequest first in case _close() fails.
232244 self -> target = NULL ;
233245
246+ common_hal_i2ctarget_i2c_target_close (target );
247+
234248 return mp_const_none ;
235249}
236250MP_DEFINE_CONST_FUN_OBJ_1 (i2ctarget_i2c_target_request_deinit_obj , i2ctarget_i2c_target_request_deinit );
237251
238- static void target_request_check_for_deinit (i2ctarget_i2c_target_request_obj_t * self ) {
239- if (self -> target == NULL ) {
240- raise_deinited_error ();
241- }
242- check_for_deinit (self -> target );
243- }
244-
245252//| address: int
246253//| """The I2C address of the request."""
247254static mp_obj_t i2ctarget_i2c_target_request_get_address (mp_obj_t self_in ) {
@@ -401,22 +408,11 @@ MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(i2ctarget_i2c_target_request_ack_obj, 1, 2,
401408//| """Close and deinit the request."""
402409//| ...
403410//|
404- static mp_obj_t i2ctarget_i2c_target_request__exit__ (size_t n_args , const mp_obj_t * args ) {
405- i2ctarget_i2c_target_request_obj_t * self = MP_OBJ_TO_PTR (args [0 ]);
406- target_request_check_for_deinit (self );
407-
408- i2ctarget_i2c_target_obj_t * target = self -> target ;
409- // Deinit target request first in case _close() fails.
410- i2ctarget_i2c_target_request_deinit (args [0 ]);
411-
412- common_hal_i2ctarget_i2c_target_close (target );
413- return mp_const_none ;
414- }
415- static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN (i2ctarget_i2c_target_request___exit___obj , 4 , 4 , i2ctarget_i2c_target_request__exit__ ) ;
411+ // Provided by context manager helper.
416412
417413static const mp_rom_map_elem_t i2ctarget_i2c_target_request_locals_dict_table [] = {
418414 { MP_ROM_QSTR (MP_QSTR___enter__ ), MP_ROM_PTR (& default___enter___obj ) },
419- { MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& i2ctarget_i2c_target_request___exit___obj ) },
415+ { MP_ROM_QSTR (MP_QSTR___exit__ ), MP_ROM_PTR (& default___exit___obj ) },
420416 { MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& i2ctarget_i2c_target_request_deinit_obj ) },
421417 { MP_ROM_QSTR (MP_QSTR__del__ ), MP_ROM_PTR (& i2ctarget_i2c_target_request_deinit_obj ) },
422418 { MP_ROM_QSTR (MP_QSTR_address ), MP_ROM_PTR (& i2ctarget_i2c_target_request_address_obj ) },
0 commit comments