@@ -222,26 +222,33 @@ static mp_obj_t i2ctarget_i2c_target_request_make_new(const mp_obj_type_t *type,
222
222
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 ]));
223
223
}
224
224
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
+
225
232
//| def deinit(self) -> None:
226
233
//| """Disconnects from parent `I2CTarget`.
227
234
//| Called by `__exit__()` to indicate the `I2CTargetRequest` is no longer useful."""
228
235
//| ...
229
236
//|
230
237
static mp_obj_t i2ctarget_i2c_target_request_deinit (mp_obj_t self_in ) {
231
238
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.
232
244
self -> target = NULL ;
233
245
246
+ common_hal_i2ctarget_i2c_target_close (target );
247
+
234
248
return mp_const_none ;
235
249
}
236
250
MP_DEFINE_CONST_FUN_OBJ_1 (i2ctarget_i2c_target_request_deinit_obj , i2ctarget_i2c_target_request_deinit );
237
251
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
-
245
252
//| address: int
246
253
//| """The I2C address of the request."""
247
254
static 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,
401
408
//| """Close and deinit the request."""
402
409
//| ...
403
410
//|
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.
416
412
417
413
static const mp_rom_map_elem_t i2ctarget_i2c_target_request_locals_dict_table [] = {
418
414
{ 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 ) },
420
416
{ MP_ROM_QSTR (MP_QSTR_deinit ), MP_ROM_PTR (& i2ctarget_i2c_target_request_deinit_obj ) },
421
417
{ MP_ROM_QSTR (MP_QSTR__del__ ), MP_ROM_PTR (& i2ctarget_i2c_target_request_deinit_obj ) },
422
418
{ MP_ROM_QSTR (MP_QSTR_address ), MP_ROM_PTR (& i2ctarget_i2c_target_request_address_obj ) },
0 commit comments