8000 __await__ magic method and async/await by kvc0 · Pull Request #3540 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

__await__ magic method and async/await #3540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-10-06 13:26-0400\n"
"POT-Creation-Date: 2020-10-10 23:49-0700\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
Expand Down Expand Up @@ -246,6 +246,10 @@ msgstr ""
msgid "'return' outside function"
msgstr ""

#: py/compile.c
msgid "'yield from' inside async function"
msgstr ""

#: py/compile.c
msgid "'yield' outside function"
msgstr ""
Expand Down Expand Up @@ -289,6 +293,7 @@ msgid "All I2C peripherals are in use"
msgstr ""

#: ports/atmel-samd/common-hal/canio/Listener.c
#: ports/stm/common-hal/canio/Listener.c
msgid "All RX FIFOs in use"
msgstr ""

Expand Down Expand Up @@ -896,6 +901,7 @@ msgid "File exists"
msgstr ""

#: ports/atmel-samd/common-hal/canio/Listener.c
#: ports/stm/common-hal/canio/Listener.c
msgid "Filters too complex"
msgstr ""

Expand Down Expand Up @@ -932,7 +938,8 @@ msgid "Group full"
msgstr ""

#: ports/mimxrt10xx/common-hal/busio/SPI.c ports/stm/common-hal/busio/I2C.c
#: ports/stm/common-hal/busio/SPI.c ports/stm/common-hal/sdioio/SDCard.c
#: ports/stm/common-hal/busio/SPI.c ports/stm/common-hal/canio/CAN.c
#: ports/stm/common-hal/sdioio/SDCard.c
msgid "Hardware busy, try alternative pins"
msgstr ""

Expand Down Expand Up @@ -1002,7 +1009,8 @@ msgid "Invalid %q pin"
msgstr ""

#: ports/stm/common-hal/busio/I2C.c ports/stm/common-hal/busio/SPI.c
#: ports/stm/common-hal/busio/UART.c ports/stm/common-hal/sdioio/SDCard.c
#: ports/stm/common-hal/busio/UART.c ports/stm/common-hal/canio/CAN.c
#: ports/stm/common-hal/sdioio/SDCard.c
msgid "Invalid %q pin selection"
msgstr ""

Expand Down Expand Up @@ -3426,6 +3434,10 @@ msgstr ""
msgid "type object '%q' has no attribute '%q'"
msgstr ""

#: py/objgenerator.c
msgid "type object 'generator' has no attribute '__await__'"
msgstr ""

#: py/objtype.c
msgid "type takes 1 or 3 arguments"
msgstr ""
Expand Down
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/arduino_mkr1300/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0
SUPEROPT_GC = 0
MICROPY_PY_ASYNC_AWAIT = 0
3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/arduino_mkrzero/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/arduino_nano_33_iot/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/arduino_zero/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/bast_pro_mini_m0/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/catwan_usbstick/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0
CIRCUITPY_COUNTIO = 0
CIRCUITPY_ROTARYIO = 0

MICROPY_PY_ASYNC_AWAIT = 0

Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
CIRCUITPY_VECTORIO = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0

Expand All @@ -33,3 +34,4 @@ RELEASE_NEEDS_CLEAN_BUILD = 1
CFLAGS_INLINE_LIMIT = 35
SUPEROPT_VM = 0
endif

Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LONGINT_IMPL = MPZ
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0
CFLAGS_INLINE_LIMIT = 55
Expand Down
3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/datum_distance/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/datum_imu/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/datum_light/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/datum_weather/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/feather_m0_adalogger/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/feather_m0_basic/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/feather_m0_rfm69/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@ CIRCUITPY_SAMD = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_TOUCHIO = 0
MICROPY_PY_ASYNC_AWAIT = 0
CFLAGS_INLINE_LIMIT = 35

# Make more room.
SUPEROPT_GC = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM69

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/feather_m0_rfm9x/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ CIRCUITPY_SAMD = 0
CIRCUITPY_USB_MIDI = 0
CIRCUITPY_USB_HID = 0
CIRCUITPY_TOUCHIO = 0
MICROPY_PY_ASYNC_AWAIT = 0

CFLAGS_INLINE_LIMIT = 35
# Make more room.
SUPEROPT_GC = 0

# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_RFM9x

2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/gemma_m0/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0

Expand All @@ -21,3 +22,4 @@ ifeq ($(TRANSLATION), de_DE)
RELEASE_NEEDS_CLEAN_BUILD = 1
CFLAGS_INLINE_LIMIT = 35
endif

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/pewpew10/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,6 @@ SUPEROPT_GC = 0
FROZEN_MPY_DIRS += $(TOP)/frozen/pew-pewpew-standalone-10.x

CFLAGS_BOARD = --param max-inline-insns-auto=15

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/seeeduino_xiao/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/shirtty/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ CIRCUITPY_I2CPERIPHERAL = 1
CIRCUITPY_TOUCHIO = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE

CIRCUITPY_FULL_BUILD = 0
SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/sparkfun_samd21_dev/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/sparkfun_samd21_mini/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0

SUPEROPT_GC = 0

MICROPY_PY_ASYNC_AWAIT = 0

2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/trinket_m0/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ CHIP_FAMILY = samd21
INTERNAL_FLASH_FILESYSTEM = 1
LONGINT_IMPL = NONE
CIRCUITPY_FULL_BUILD = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0

Expand All @@ -22,3 +23,4 @@ RELEASE_NEEDS_CLEAN_BUILD = 1
CFLAGS_INLINE_LIMIT = 35
SUPEROPT_VM = 0
endif

2 changes: 2 additions & 0 deletions ports/atmel-samd/boards/trinket_m0_haxpress/mpconfigboard.mk
< 10000 div class="js-file-content Details-content--hidden position-relative" data-hydro-view="{"event_type":"pull_request.select_diff_range","payload":{"actor_id":null,"pull_request_id":501226427,"repository_id":66166069,"diff_type":"UNIFIED","whitespace_ignored":false,"originating_url":"https://github.com/adafruit/circuitpython/pull/3540/files/3c46e3a6aab8895f19302c4fe165d61d30f1a45e","user_id":null}}" data-hydro-view-hmac="4d4a356151b49f2b94fb37bf9d082b7ae6b4fe213bbb3b4c54d76f5e588a0a3b">
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ CIRCUITPY_COUNTIO = 0
CIRCUITPY_RTC = 0
CIRCUITPY_FREQUENCYIO = 0
CIRCUITPY_I2CPERIPHERAL = 0
MICROPY_PY_ASYNC_AWAIT = 0

SUPEROPT_GC = 0

Expand All @@ -34,3 +35,4 @@ RELEASE_NEEDS_CLEAN_BUILD = 1
CFLAGS_INLINE_LIMIT = 35
SUPEROPT_VM = 0
endif

1 change: 1 addition & 0 deletions ports/atmel-samd/boards/uchip/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ CIRCUITPY_FULL_BUILD = 0
CFLAGS_INLINE_LIMIT = 45

SUPEROPT_GC = 0
MICROPY_PY_ASYNC_AWAIT = 0
3 changes: 3 additions & 0 deletions ports/atmel-samd/boards/xinabox_cs11/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ CIRCUITPY_COUNTIO=0
# Include these Python libraries in firmware.
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_BusDevice
FROZEN_MPY_DIRS += $(TOP)/frozen/Adafruit_CircuitPython_SD

MICROPY_PY_ASYNC_AWAIT = 0

2 changes: 2 additions & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ SUPEROPT_GC = 0

# Override optimization to keep binary small
OPTIMIZATION_FLAGS = -Os
MICROPY_PY_ASYNC_AWAIT = 0

3 changes: 3 additions & 0 deletions ports/nrf/boards/sparkfun_nrf52840_mini/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,6 @@ USB_MANUFACTURER = "SparkFun Electronics"
MCU_CHIP = nrf52840

INTERNAL_FLASH_FILESYSTEM = 1

MICROPY_PY_ASYNC_AWAIT = 0

1 change: 0 additions & 1 deletion py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@

#define MICROPY_PY_ARRAY (1)
#define MICROPY_PY_ARRAY_SLICE_ASSIGN (1)
#define MICROPY_PY_ASYNC_AWAIT (0)
#define MICROPY_PY_ATTRTUPLE (1)

#define MICROPY_PY_BUILTINS_BYTEARRAY (1)
Expand Down
5 changes: 5 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
CIRCUITPY_FULL_BUILD ?= 1
CFLAGS += -DCIRCUITPY_FULL_BUILD=$(CIRCUITPY_FULL_BUILD)

MICROPY_PY_ASYNC_AWAIT ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DMICROPY_PY_ASYNC_AWAIT=$(MICROPY_PY_ASYNC_AWAIT)

CIRCUITPY_AESIO ?= 0
CFLAGS += -DCIRCUITPY_AESIO=$(CIRCUITPY_AESIO)
Expand Down Expand Up @@ -290,3 +292,6 @@ CFLAGS += -DCIRCUITPY_WIFI=$(CIRCUITPY_WIFI)
# Enabled micropython.native decorator (experimental)
CIRCUITPY_ENABLE_MPY_NATIVE ?= 0
CFLAGS += -DCIRCUITPY_ENABLE_MPY_NATIVE=$(CIRCUITPY_ENABLE_MPY_NATIVE)

# async/await language keyword support
MICROPY_PY_ASYNC_AWAIT ?= $(CIRCUITPY_FULL_BUILD)
19 changes: 17 additions & 2 deletions py/compile.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

#if MICROPY_ENABLE_COMPILER

#define DEBUG_PRINT(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)

// TODO need to mangle __attr names

#define INVALID_LABEL (0xffff)
Expand Down Expand Up @@ -853,7 +855,7 @@ STATIC void compile_decorated(compiler_t *comp, mp_parse_node_struct_t *pns) {
mp_parse_node_struct_t *pns0 = (mp_parse_node_struct_t*)pns_body->nodes[0];
body_name = compile_funcdef_helper(comp, pns0, emit_options);
scope_t *fscope = (scope_t*)pns0->nodes[4];
fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR;
fscope->scope_flags |= MP_SCOPE_FLAG_GENERATOR | MP_SCOPE_FLAG_ASYNC;
#endif
} else {
assert(MP_PARSE_NODE_STRUCT_KIND(pns_body) == PN_classdef); // should be
Expand Down Expand Up @@ -2632,6 +2634,12 @@ STATIC void compile_yield_expr(compiler_t *comp, mp_parse_node_struct_t *pns) {
EMIT_ARG(yield, MP_EMIT_YIELD_VALUE);
} else if (MP_PARSE_NODE_IS_STRUCT_KIND(pns->nodes[0], PN_yield_arg_from)) {
pns = (mp_parse_node_struct_t*)pns->nodes[0];
#if MICROPY_PY_ASYNC_AWAIT
if(comp->scope_cur->scope_flags & MP_SCOPE_FLAG_ASYNC) {
compile_syntax_error(comp, (mp_parse_node_t)pns, translate("'yield from' inside async function"));
return;
}
#endif
compile_node(comp, pns->nodes[0]);
compile_yield_from(comp);
} else {
Expand All @@ -2648,7 +2656,14 @@ STATIC void compile_atom_expr_await(compiler_t *comp, mp_parse_node_struct_t *pn
}
compile_require_async_context(comp, pns);
compile_atom_expr_normal(comp, pns);
compile_yield_from(comp);

// If it's an awaitable thing, need to reach for the __await__ method for the coroutine.
// async def functions' __await__ return themselves, which are able to receive a send(),
// while other types with custom __await__ implementations return async generators.
EMIT_ARG(load_method, MP_QSTR___await__, false);
EMIT_ARG(call_method, 0, 0, 0);
EMIT_ARG(load_const_tok, MP_TOKEN_KW_NONE);
EMIT_ARG(yield, MP_EMIT_YIELD_FROM);
}
#endif

Expand Down
Loading
0