Skip to content

Commit 4c06915

Browse files
committed
Rename _uasyncio to _asyncio.
Register _asyncio module in CP manner.
1 parent 2e4ad1a commit 4c06915

File tree

2 files changed

+43
-32
lines changed

2 files changed

+43
-32
lines changed

extmod/moduasyncio.c

Lines changed: 38 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "py/pairheap.h"
3030
#include "py/mphal.h"
3131

32-
#if !(defined(__unix__) || defined(__APPLE__))
32+
#if CIRCUITPY && !(defined(__unix__) || defined(__APPLE__))
3333
#include "shared-bindings/supervisor/__init__.h"
3434
#endif
3535

@@ -63,11 +63,12 @@ STATIC const mp_obj_type_t task_queue_type;
6363
STATIC const mp_obj_type_t task_type;
6464

6565
STATIC mp_obj_t task_queue_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args);
66+
STATIC mp_obj_t task_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf);
6667

6768
/******************************************************************************/
6869
// Ticks for task ordering in pairing heap
6970

70-
#if (defined(__unix__) || defined(__APPLE__))
71+
#if !CIRCUITPY || (defined(__unix__) || defined(__APPLE__))
7172
STATIC mp_obj_t ticks(void) {
7273
return MP_OBJ_NEW_SMALL_INT(mp_hal_ticks_ms() & (MICROPY_PY_UTIME_TICKS_PERIOD - 1));
7374
}
@@ -244,35 +245,6 @@ STATIC mp_obj_t task_cancel(mp_obj_t self_in) {
244245
}
245246
STATIC MP_DEFINE_CONST_FUN_OBJ_1(task_cancel_obj, task_cancel);
246247

247-
STATIC mp_obj_t task_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
248-
(void)iter_buf;
249-
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
250-
if (TASK_IS_DONE(self)) {
251-
// Signal that the completed-task has been await'ed on.
252-
self->state = TASK_STATE_DONE_WAS_WAITED_ON;
253-
} else if (self->state == TASK_STATE_RUNNING_NOT_WAITED_ON) {
254-
// Allocate the waiting queue.
255-
self->state = task_queue_make_new(&task_queue_type, 0, 0, NULL);
256-
}
257-
return self_in;
258-
}
259-
260-
STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
261-
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
262-
if (TASK_IS_DONE(self)) {
263-
// Task finished, raise return value to caller so it can continue.
264-
nlr_raise(self->data);
265-
} else {
266-
// Put calling task on waiting queue.
267-
mp_obj_t cur_task = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
268-
mp_obj_t args[2] = { self->state, cur_task };
269-
task_queue_push_sorted(2, args);
270-
// Set calling task's data to this task that it waits on, to double-link it.
271-
((mp_obj_task_t *)MP_OBJ_TO_PTR(cur_task))->data = self_in;
272-
}
273-
return mp_const_none;
274-
}
275-
276248
STATIC mp_obj_t task_await(mp_obj_t self_in) {
277249
return task_getiter(self_in, NULL);
278250
}
@@ -300,7 +272,6 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
300272
dest[0] = MP_OBJ_FROM_PTR(&task_await_obj);
301273
dest[1] = self_in;
302274
}
303-
304275
} else if (dest[1] != MP_OBJ_NULL) {
305276
// Store
306277
if (attr == MP_QSTR_data) {
@@ -313,6 +284,35 @@ STATIC void task_attr(mp_obj_t self_in, qstr attr, mp_obj_t *dest) {
313284
}
314285
}
315286

287+
STATIC mp_obj_t task_getiter(mp_obj_t self_in, mp_obj_iter_buf_t *iter_buf) {
288+
(void)iter_buf;
289+
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
290+
if (TASK_IS_DONE(self)) {
291+
// Signal that the completed-task has been await'ed on.
292+
self->state = TASK_STATE_DONE_WAS_WAITED_ON;
293+
} else if (self->state == TASK_STATE_RUNNING_NOT_WAITED_ON) {
294+
// Allocate the waiting queue.
295+
self->state = task_queue_make_new(&task_queue_type, 0, 0, NULL);
296+
}
297+
return self_in;
298+
}
299+
300+
STATIC mp_obj_t task_iternext(mp_obj_t self_in) {
301+
mp_obj_task_t *self = MP_OBJ_TO_PTR(self_in);
302+
if (TASK_IS_DONE(self)) {
303+
// Task finished, raise return value to caller so it can continue.
304+
nlr_raise(self->data);
305+
} else {
306+
// Put calling task on waiting queue.
307+
mp_obj_t cur_task = mp_obj_dict_get(uasyncio_context, MP_OBJ_NEW_QSTR(MP_QSTR_cur_task));
308+
mp_obj_t args[2] = { self->state, cur_task };
309+
task_queue_push_sorted(2, args);
310+
// Set calling task's data to this task that it waits on, to double-link it.
311+
((mp_obj_task_t *)MP_OBJ_TO_PTR(cur_task))->data = self_in;
312+
}
313+
return mp_const_none;
314+
}
315+
316316
STATIC const mp_obj_type_t task_type = {
317317
{ &mp_type_type },
318318
.flags = MP_TYPE_FLAG_EXTENDED,
@@ -329,7 +329,11 @@ STATIC const mp_obj_type_t task_type = {
329329
// C-level uasyncio module
330330

331331
STATIC const mp_rom_map_elem_t mp_module_uasyncio_globals_table[] = {
332+
#if CIRCUITPY
333+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__asyncio) },
334+
#else
332335
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__uasyncio) },
336+
#endif
333337
{ MP_ROM_QSTR(MP_QSTR_TaskQueue), MP_ROM_PTR(&task_queue_type) },
334338
{ MP_ROM_QSTR(MP_QSTR_Task), MP_ROM_PTR(&task_type) },
335339
};
@@ -340,4 +344,6 @@ const mp_obj_module_t mp_module_uasyncio = {
340344
.globals = (mp_obj_dict_t *)&mp_module_uasyncio_globals,
341345
};
342346

347+
MP_REGISTER_MODULE(MP_QSTR__asyncio, mp_module_uasyncio, MICROPY_PY_UASYNCIO);
348+
343349
#endif // MICROPY_PY_UASYNCIO

py/objmodule.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,13 @@ STATIC const mp_rom_map_elem_t mp_builtin_module_table[] = {
201201
// extmod modules
202202

203203
#if MICROPY_PY_UASYNCIO
204+
#if CIRCUITPY
205+
// CircuitPython: Defined in MICROPY_PORT_BUILTIN_MODULES, so not defined here.
206+
// TODO: move to shared-bindings/
207+
#else
204208
{ MP_ROM_QSTR(MP_QSTR__uasyncio), MP_ROM_PTR(&mp_module_uasyncio) },
205209
#endif
210+
#endif
206211
#if MICROPY_PY_UERRNO
207212
#if CIRCUITPY
208213
// CircuitPython: Defined in MICROPY_PORT_BUILTIN_MODULES, so not defined here.

0 commit comments

Comments
 (0)