Skip to content

Commit 392e777

Browse files
committed
add, use a default exit object
This just delegates to .deinit(). Also, change default __enter__ to a macro that reuses the identically-beving "identity_obj", and harmonize a few sites so that the naming s consistent. Saves about 800 bytes on metro rp2350.
1 parent 95179be commit 392e777

File tree

47 files changed

+104
-314
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+104
-314
lines changed

ports/espressif/bindings/espcamera/Camera.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "shared-bindings/displayio/Bitmap.h"
1919
#include "shared-bindings/microcontroller/Pin.h"
2020
#include "shared-bindings/util.h"
21+
#include "shared/runtime/context_manager_helpers.h"
2122
#include "esp_camera.h"
2223
#include "sensor.h"
2324

@@ -169,11 +170,7 @@ static void check_for_deinit(espcamera_camera_obj_t *self) {
169170
//| :ref:`lifetime-and-contextmanagers` for more info."""
170171
//| ...
171172
//|
172-
static mp_obj_t espcamera_camera_obj___exit__(size_t n_args, const mp_obj_t *args) {
173-
(void)n_args;
174-
return espcamera_camera_deinit(args[0]);
175-
}
176-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espcamera_camera___exit___obj, 4, 4, espcamera_camera_obj___exit__);
173+
// Provided by context manager helper.
177174

178175
//| frame_available: bool
179176
//| """True if a frame is available, False otherwise"""
@@ -947,8 +944,8 @@ static const mp_rom_map_elem_t espcamera_camera_locals_table[] = {
947944
{ MP_ROM_QSTR(MP_QSTR_denoise), MP_ROM_PTR(&espcamera_camera_denoise_obj) },
948945
{ MP_ROM_QSTR(MP_QSTR_framebuffer_count), MP_ROM_PTR(&espcamera_camera_framebuffer_count_obj) },
949946
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&espcamera_camera_deinit_obj) },
950-
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
951-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espcamera_camera___exit___obj) },
947+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
948+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
952949
{ MP_ROM_QSTR(MP_QSTR_exposure_ctrl), MP_ROM_PTR(&espcamera_camera_exposure_ctrl_obj) },
953950
{ MP_ROM_QSTR(MP_QSTR_frame_available), MP_ROM_PTR(&espcamera_camera_frame_available_obj) },
954951
{ MP_ROM_QSTR(MP_QSTR_frame_size), MP_ROM_PTR(&espcamera_camera_frame_size_obj) },

ports/espressif/bindings/espnow/ESPNow.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "py/stream.h"
1313

1414
#include "shared-bindings/util.h"
15+
#include "shared/runtime/context_manager_helpers.h"
1516

1617
#include "bindings/espnow/ESPNow.h"
1718
#include "bindings/espnow/Peer.h"
@@ -92,11 +93,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(espnow_deinit_obj, espnow_deinit);
9293
//| :ref:`lifetime-and-contextmanagers` for more info."""
9394
//| ...
9495
//|
95-
static mp_obj_t espnow_obj___exit__(size_t n_args, const mp_obj_t *args) {
96-
(void)n_args;
97-
return espnow_deinit(args[0]);
98-
}
99-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espnow___exit___obj, 4, 4, espnow_obj___exit__);
96+
// Provided by context manager helper.
10097

10198
// --- Send and Read messages ---
10299

@@ -270,8 +267,8 @@ MP_PROPERTY_GETTER(espnow_peers_obj,
270267

271268
static const mp_rom_map_elem_t espnow_locals_dict_table[] = {
272269
// Context managers
273-
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
274-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espnow___exit___obj) },
270+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
271+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
275272

276273
// Deinit the object
277274
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espnow_deinit_obj) },

ports/espressif/bindings/espulp/ULP.c

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "shared-bindings/microcontroller/Pin.h"
88
#include "shared-bindings/util.h"
9+
#include "shared/runtime/context_manager_helpers.h"
910
#include "bindings/espulp/ULP.h"
1011

1112
#include "py/enum.h"
@@ -69,11 +70,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(espulp_ulp_deinit_obj, espulp_ulp_deinit);
6970
//| :ref:`lifetime-and-contextmanagers` for more info."""
7071
//| ...
7172
//|
72-
static mp_obj_t espulp_ulp_obj___exit__(size_t n_args, const mp_obj_t *args) {
73-
(void)n_args;
74-
return espulp_ulp_deinit(args[0]);
75-
}
76-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(espulp_ulp___exit___obj, 4, 4, espulp_ulp_obj___exit__);
73+
// Provided by context manager helper.
7774

7875
//| def set_wakeup_period(self, period_index: int, period_us: int) -> None:
7976
//| """Sets the wakeup period for the ULP.
@@ -188,8 +185,8 @@ MP_PROPERTY_GETTER(espulp_ulp_arch_obj,
188185

189186
static const mp_rom_map_elem_t espulp_ulp_locals_table[] = {
190187
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&espulp_ulp_deinit_obj) },
191-
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&mp_identity_obj) },
192-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&espulp_ulp___exit___obj) },
188+
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
189+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
193190
{ MP_ROM_QSTR(MP_QSTR_set_wakeup_period), MP_ROM_PTR(&espulp_ulp_set_wakeup_period_obj) },
194191
{ MP_ROM_QSTR(MP_QSTR_run), MP_ROM_PTR(&espulp_ulp_run_obj) },
195192
{ MP_ROM_QSTR(MP_QSTR_halt), MP_ROM_PTR(&espulp_ulp_halt_obj) },

ports/raspberrypi/bindings/rp2pio/StateMachine.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -378,12 +378,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(rp2pio_statemachine_deinit_obj, rp2pio_statemachine_ob
378378
//| :ref:`lifetime-and-contextmanagers` for more info."""
379379
//| ...
380380
//|
381-
static mp_obj_t rp2pio_statemachine_obj___exit__(size_t n_args, const mp_obj_t *args) {
382-
(void)n_args;
383-
common_hal_rp2pio_statemachine_deinit(args[0]);
384-
return mp_const_none;
385-
}
386-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2pio_statemachine_obj___exit___obj, 4, 4, rp2pio_statemachine_obj___exit__);
387381

388382

389383
static void check_for_deinit(rp2pio_statemachine_obj_t *self) {
@@ -1126,7 +1120,7 @@ MP_PROPERTY_GETTER(rp2pio_statemachine_last_write_obj,
11261120
static const mp_rom_map_elem_t rp2pio_statemachine_locals_dict_table[] = {
11271121
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&rp2pio_statemachine_deinit_obj) },
11281122
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
1129-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&rp2pio_statemachine_obj___exit___obj) },
1123+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
11301124

11311125
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&rp2pio_statemachine_stop_obj) },
11321126
{ MP_ROM_QSTR(MP_QSTR_restart), MP_ROM_PTR(&rp2pio_statemachine_restart_obj) },

ports/zephyr-cp/bindings/zephyr_serial/UART.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ static const mp_rom_map_elem_t _zephyr_serial_uart_locals_dict_table[] = {
259259
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&_zephyr_serial_uart_deinit_obj) },
260260
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&_zephyr_serial_uart_deinit_obj) },
261261
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
262-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&_zephyr_serial_uart___exit___obj) },
262+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
263263

264264
// Standard stream methods.
265265
{ MP_OBJ_NEW_QSTR(MP_QSTR_read), MP_ROM_PTR(&mp_stream_read_obj) },

shared-bindings/analogbufio/BufferedIn.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,7 @@ static void check_for_deinit(analogbufio_bufferedin_obj_t *self) {
9191
//| :ref:`lifetime-and-contextmanagers` for more info."""
9292
//| ...
9393
//|
94-
static mp_obj_t analogbufio_bufferedin___exit__(size_t n_args, const mp_obj_t *args) {
95-
(void)n_args;
96-
common_hal_analogbufio_bufferedin_deinit(args[0]);
97-
return mp_const_none;
98-
}
99-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogbufio_bufferedin___exit___obj, 4, 4, analogbufio_bufferedin___exit__);
94+
// Provided by context manager helper.
10095

10196
//| def readinto(self, buffer: WriteableBuffer, loop: bool = False) -> int:
10297
//| """Fills the provided buffer with ADC voltage values.
@@ -144,7 +139,7 @@ static const mp_rom_map_elem_t analogbufio_bufferedin_locals_dict_table[] = {
144139
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) },
145140
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogbufio_bufferedin_deinit_obj) },
146141
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
147-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogbufio_bufferedin___exit___obj) },
142+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
148143
{ MP_ROM_QSTR(MP_QSTR_readinto), MP_ROM_PTR(&analogbufio_bufferedin_readinto_obj)},
149144
};
150145

shared-bindings/analogio/AnalogIn.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,7 @@ static void check_for_deinit(analogio_analogin_obj_t *self) {
8787
//| :ref:`lifetime-and-contextmanagers` for more info."""
8888
//| ...
8989
//|
90-
static mp_obj_t analogio_analogin___exit__(size_t n_args, const mp_obj_t *args) {
91-
(void)n_args;
92-
common_hal_analogio_analogin_deinit(args[0]);
93-
return mp_const_none;
94-
}
95-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogin___exit___obj, 4, 4, analogio_analogin___exit__);
90+
// Provided by context manager helper.
9691

9792
//| value: int
9893
//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (read-only)
@@ -135,7 +130,7 @@ MP_PROPERTY_GETTER(analogio_analogin_reference_voltage_obj,
135130
static const mp_rom_map_elem_t analogio_analogin_locals_dict_table[] = {
136131
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogin_deinit_obj) },
137132
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
138-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogin___exit___obj) },
133+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
139134
{ MP_ROM_QSTR(MP_QSTR_value), MP_ROM_PTR(&analogio_analogin_value_obj)},
140135
{ MP_ROM_QSTR(MP_QSTR_reference_voltage), MP_ROM_PTR(&analogio_analogin_reference_voltage_obj)},
141136
};

shared-bindings/analogio/AnalogOut.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,7 @@ static MP_DEFINE_CONST_FUN_OBJ_1(analogio_analogout_deinit_obj, analogio_analogo
7373
//| :ref:`lifetime-and-contextmanagers` for more info."""
7474
//| ...
7575
//|
76-
static mp_obj_t analogio_analogout___exit__(size_t n_args, const mp_obj_t *args) {
77-
(void)n_args;
78-
common_hal_analogio_analogout_deinit(args[0]);
79-
return mp_const_none;
80-
}
81-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(analogio_analogout___exit___obj, 4, 4, analogio_analogout___exit__);
76+
// Provided by context manager helper.
8277

8378
//| value: int
8479
//| """The value on the analog pin between 0 and 65535 inclusive (16-bit). (write-only)
@@ -107,7 +102,7 @@ static const mp_rom_map_elem_t analogio_analogout_locals_dict_table[] = {
107102
// instance methods
108103
{ MP_OBJ_NEW_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&analogio_analogout_deinit_obj) },
109104
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
110-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&analogio_analogout___exit___obj) },
105+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
111106

112107
// Properties
113108
{ MP_OBJ_NEW_QSTR(MP_QSTR_value), (mp_obj_t)&analogio_analogout_value_obj },

shared-bindings/audiobusio/I2SOut.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -135,12 +135,7 @@ static void check_for_deinit(audiobusio_i2sout_obj_t *self) {
135135
//| :ref:`lifetime-and-contextmanagers` for more info."""
136136
//| ...
137137
//|
138-
static mp_obj_t audiobusio_i2sout_obj___exit__(size_t n_args, const mp_obj_t *args) {
139-
(void)n_args;
140-
common_hal_audiobusio_i2sout_deinit(args[0]);
141-
return mp_const_none;
142-
}
143-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_i2sout___exit___obj, 4, 4, audiobusio_i2sout_obj___exit__);
138+
// Provided by context manager helper.
144139

145140

146141
//| def play(self, sample: circuitpython_typing.AudioSample, *, loop: bool = False) -> None:
@@ -248,7 +243,7 @@ static const mp_rom_map_elem_t audiobusio_i2sout_locals_dict_table[] = {
248243
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) },
249244
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiobusio_i2sout_deinit_obj) },
250245
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
251-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiobusio_i2sout___exit___obj) },
246+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
252247
{ MP_ROM_QSTR(MP_QSTR_play), MP_ROM_PTR(&audiobusio_i2sout_play_obj) },
253248
{ MP_ROM_QSTR(MP_QSTR_stop), MP_ROM_PTR(&audiobusio_i2sout_stop_obj) },
254249
{ MP_ROM_QSTR(MP_QSTR_pause), MP_ROM_PTR(&audiobusio_i2sout_pause_obj) },

shared-bindings/audiobusio/PDMIn.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,7 @@ static void check_for_deinit(audiobusio_pdmin_obj_t *self) {
152152
//| """Automatically deinitializes the hardware when exiting a context."""
153153
//| ...
154154
//|
155-
static mp_obj_t audiobusio_pdmin_obj___exit__(size_t n_args, const mp_obj_t *args) {
156-
(void)n_args;
157-
common_hal_audiobusio_pdmin_deinit(args[0]);
158-
return mp_const_none;
159-
}
160-
static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(audiobusio_pdmin___exit___obj, 4, 4, audiobusio_pdmin_obj___exit__);
155+
// Provided by context manager helper.
161156

162157

163158
//| def record(self, destination: WriteableBuffer, destination_length: int) -> None:
@@ -220,7 +215,7 @@ static const mp_rom_map_elem_t audiobusio_pdmin_locals_dict_table[] = {
220215
{ MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&audiobusio_pdmin_deinit_obj) },
221216
{ MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&audiobusio_pdmin_deinit_obj) },
222217
{ MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&default___enter___obj) },
223-
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&audiobusio_pdmin___exit___obj) },
218+
{ MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&default___exit___obj) },
224219
{ MP_ROM_QSTR(MP_QSTR_record), MP_ROM_PTR(&audiobusio_pdmin_record_obj) },
225220
{ MP_ROM_QSTR(MP_QSTR_sample_rate), MP_ROM_PTR(&audiobusio_pdmin_sample_rate_obj) }
226221
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy