From ca79b4961953be587a290bc995ae925e664c9fbc Mon Sep 17 00:00:00 2001 From: Jim Mussared Date: Thu, 8 Jun 2023 16:42:19 +1000 Subject: [PATCH] extmod/asyncio/uasyncio.py: Add backwards-compatible uasyncio alias. This allows existing code that does `import uasyncio` or `import uasyncio as asyncio` to continue working. It uses the same lazy-loading as asyncio to prevent loading of unused features. This work was funded through GitHub Sponsors. Signed-off-by: Jim Mussared --- extmod/asyncio/manifest.py | 3 +++ extmod/asyncio/uasyncio.py | 8 ++++++++ tests/extmod/asyncio_as_uasyncio.py | 12 ++++++++++++ tests/extmod/asyncio_as_uasyncio.py.exp | 2 ++ 4 files changed, 25 insertions(+) create mode 100644 extmod/asyncio/uasyncio.py create mode 100644 tests/extmod/asyncio_as_uasyncio.py create mode 100644 tests/extmod/asyncio_as_uasyncio.py.exp diff --git a/extmod/asyncio/manifest.py b/extmod/asyncio/manifest.py index 0aba5b1856..e8dc764325 100644 --- a/extmod/asyncio/manifest.py +++ b/extmod/asyncio/manifest.py @@ -13,3 +13,6 @@ package( base_path="..", opt=3, ) + +# Backwards-compatible uasyncio module. +module("uasyncio.py", opt=3) diff --git a/extmod/asyncio/uasyncio.py b/extmod/asyncio/uasyncio.py new file mode 100644 index 0000000000..67e6ddcfa3 --- /dev/null +++ b/extmod/asyncio/uasyncio.py @@ -0,0 +1,8 @@ +# This module just allows `import uasyncio` to work. It lazy-loads from +# `asyncio` without duplicating its globals dict. + + +def __getattr__(attr): + import asyncio + + return getattr(asyncio, attr) diff --git a/tests/extmod/asyncio_as_uasyncio.py b/tests/extmod/asyncio_as_uasyncio.py new file mode 100644 index 0000000000..612292299c --- /dev/null +++ b/tests/extmod/asyncio_as_uasyncio.py @@ -0,0 +1,12 @@ +try: + import uasyncio + import asyncio +except ImportError: + print("SKIP") + raise SystemExit + +x = set(dir(uasyncio)) +y = set(dir(asyncio)) - set(["event", "lock", "stream", "funcs"]) + +print(x - y) +print(y - x) diff --git a/tests/extmod/asyncio_as_uasyncio.py.exp b/tests/extmod/asyncio_as_uasyncio.py.exp new file mode 100644 index 0000000000..9405b80109 --- /dev/null +++ b/tests/extmod/asyncio_as_uasyncio.py.exp @@ -0,0 +1,2 @@ +set() +set()