Skip to content

all: Rename uasyncio to asyncio. #11740

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 6 commits into from
Jun 19, 2023
Merged

Conversation

jimmo
Copy link
Member

@jimmo jimmo commented Jun 8, 2023

Follow up to #9018 (and #9069)

It's CPython-compatible enough that it deserves to be called asyncio. I don't want to see any more code that does import uasyncio as asyncio.

This PR replaces uasyncio with asyncio everywhere, renames files, etc. A tiny lazy-loading uasyncio.py is added to the frozen manifest that allows for backwards compatibility with existing code.

This work was funded through GitHub Sponsors.

@github-actions
Copy link

github-actions bot commented Jun 8, 2023

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:  +440 +0.055% standard[incl +192(data)]
      stm32:  +112 +0.029% PYBV10
     mimxrt:  +104 +0.029% TEENSY40
        rp2:  +132 +0.040% PICO
       samd:  +108 +0.040% ADAFRUIT_ITSYBITSY_M4_EXPRESS

@codecov
Copy link

codecov bot commented Jun 8, 2023

Codecov Report

Merging #11740 (ca79b49) into master (ed962f1) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master   #11740   +/-   ##
=======================================
  Coverage   98.40%   98.40%           
=======================================
  Files         155      155           
  Lines       20543    20543           
=======================================
  Hits        20215    20215           
  Misses        328      328           
Impacted Files Coverage Δ
extmod/modasyncio.c 99.20% <100.00%> (ø)

@dpgeorge dpgeorge added the extmod Relates to extmod/ directory in source label Jun 8, 2023
@dpgeorge
Copy link
Member

dpgeorge commented Jun 8, 2023

Strange that there is such a large increase in code size...

@jimmo
Copy link
Member Author

jimmo commented Jun 8, 2023

Strange that there is such a large increase in code size...

It's entirely the uasyncio.py wrapper. It compiles to an 83 byte .mpy file.
In my local build, firmware goes from 362804 bytes without it to 362936 bytes with it frozen (i.e. +132 bytes).

We could maybe add a reverse-weak-links-for-frozen-modules feature (i.e. an alias() command in manifest.py?)

@dpgeorge
Copy link
Member

We could maybe add a reverse-weak-links-for-frozen-modules feature

Yes, if it's purely done for backwards compatibility and there's a way to do it in fewer bytes in C, then I think we should try that.

@jimmo
Copy link
Member Author

jimmo commented Jun 15, 2023

We could maybe add a reverse-weak-links-for-frozen-modules feature

Yes, if it's purely done for backwards compatibility and there's a way to do it in fewer bytes in C, then I think we should try that.

An alias is a bit tricky because there are 7 asyncio modules frozen.

A very simple option is to detect an import path starting with uasyncio and map it. e.g. in builtinimport.c

    #if MICROPY_PY_ASYNCIO
    // If the import is for "uasyncio" or "uasyncio/...", then map to "asyncio".
    if (strncmp(module_name, "uasyncio", 8) == 0 && (module_name[8] == '.' || module_name[8] == 0)) {
        ++module_name;
        --module_name_len;
    }
    #endif

This comes out about 100 bytes better than adding uasyncio.py. However it's maybe a bit weird to unconditionally remap uasyncio at the top-level import. (Although this is only a backwards compatibility break if someone previously had an uasyncio.py in their filesystem and had MICROPY_PY_UASYNCIO enabled, which would be a bit strange).

You could also do this in frozenmod.c but then you have a situation where import asyncio followed by import uasyncio will cause a double-import.

@dpgeorge
Copy link
Member

Let's keep the frozen uasyncio.py as-is, and optimise frozen code some more later on.

jimmo added 6 commits June 19, 2023 17:33
The asyncio module now has much better CPython compatibility and
deserves to be just called "asyncio".

This will avoid people having to write `from uasyncio import asyncio`.

Renames all files, and updates port manifests to use the new path. Also
renames the built-in _uasyncio to _asyncio.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
Mostly updates comments, but also renames the UASYNCIO enum value to
ASYNCIO.

This work was funded through GitHub Sponsors.

Signed-off-by: Jim Mussared <jim.mussared@gmail.com>
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>
@dpgeorge dpgeorge merged commit ca79b49 into micropython:master Jun 19, 2023
@dpgeorge
Copy link
Member

Merged!

@andrewleech
Copy link
Contributor

I'm very happy this has been merged, thanks @jimmo

Minor note for anyone playing along at home - I have an existing project where I wanted compatibility with an existing cpython library using asyncio so in my main.py I have

import uasyncio as asyncio
sys.modules["asyncio"] = asyncio

before importing the external library, such that when it does import asyncio it "just worked".

With micropython updated to include this MR, well my snippet above blows up with an infinite recurtion error in the new uasyncio __getattr__! Certainly an easy fix, just had to remove my old workaround!

dpgeorge pushed a commit to andrewleech/micropython-lib that referenced this pull request Jul 21, 2023
With the recent MicroPython change to remove the u prefix by default on
builtins (micropython/micropython#11740) the format checker in fnmatch
which was detecting ure no longer works.

This commit updates the module to filter the regex automatically as needed.

Signed-off-by: Andrew Leech <andrew.leech@planetinnovation.com.au>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extmod Relates to extmod/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants