Skip to content

Commit

Permalink
extmod/asyncio/uasyncio.py: Add backwards-compatible uasyncio alias.
Browse files Browse the repository at this point in the history
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 <jim.mussared@gmail.com>
  • Loading branch information
jimmo authored and dpgeorge committed Jun 19, 2023
1 parent 7979a4d commit ca79b49
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 0 deletions.
3 changes: 3 additions & 0 deletions extmod/asyncio/manifest.py
Expand Up @@ -13,3 +13,6 @@
base_path="..",
opt=3,
)

# Backwards-compatible uasyncio module.
module("uasyncio.py", opt=3)
8 changes: 8 additions & 0 deletions 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)
12 changes: 12 additions & 0 deletions 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)
2 changes: 2 additions & 0 deletions tests/extmod/asyncio_as_uasyncio.py.exp
@@ -0,0 +1,2 @@
set()
set()

0 comments on commit ca79b49

Please sign in to comment.