Skip to content

py: Make math and cmath modules extensible. #17670

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dlech
Copy link
Contributor

@dlech dlech commented Jul 13, 2025

Summary

Change math and cmath module registration from normal to extensible. This way users can extend the modules to add any missing functions.

At Pybricks, we actually want this to be able to import umath for backwards compatibility purposes, but this seems like something that would be useful for other users as well since they will likely expect these modules to be extensible like most other standard library modules are in MicroPython.

Testing

We tested this in Pybricks where we were able to import umath to get the math module.

Trade-offs and Alternatives

We'll see if there is any code size change. Alternative would be for Pybricks to keep carrying this patch in their fork.

Change math and cmath module registration from normal to extensible.
This way users can extend the modules to add any missing functions.

Signed-off-by: David Lechner <david@pybricks.com>
@dlech
Copy link
Contributor Author

dlech commented Jul 13, 2025

unix coverage test failure is just a flaky test: 1 tests failed: thread/thread_gc1.py

Copy link

Code size report:

   bare-arm:    +0 +0.000% 
minimal x86:    +0 +0.000% 
   unix x64:    +0 +0.000% standard
      stm32:    +0 +0.000% PYBV10
     mimxrt:    +0 +0.000% TEENSY40
        rp2:    +0 +0.000% RPI_PICO_W
       samd:    +0 +0.000% ADAFRUIT_ITSYBITSY_M4_EXPRESS
  qemu rv32:    +0 +0.000% VIRT_RV32

@dpgeorge
Copy link
Member

The downside here is that importing an extensible module is (a lot) slower than a non-extensible built in module. It's slower because it has to search the filesystem (everything in sys.path) first, and then fall back to searching built-in modules.

At Pybricks, we actually want this to be able to import umath for backwards compatibility purposes

Is that the only reason you need this change? Maybe there's another way to support this without slowing down the import.

Note that if this PR were accepted then the docs need updating to add these modules to the list of extensible ones; see the "Extending built-in libraries from Python" section in docs/library/index.rst.

Copy link

codecov bot commented Jul 14, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 98.44%. Comparing base (8f8f853) to head (f0ffe0e).
Report is 2 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #17670   +/-   ##
=======================================
  Coverage   98.44%   98.44%           
=======================================
  Files         171      171           
  Lines       22192    22192           
=======================================
  Hits        21847    21847           
  Misses        345      345           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dlech
Copy link
Contributor Author

dlech commented Jul 14, 2025

It's slower because it has to search the filesystem (everything in sys.path) first, and then fall back to searching built-in modules.

Ah, we didn't notice because we don't have a filesystem.

Is that the only reason you need this change? Maybe there's another way to support this without slowing down the import.

Sure, open to suggestions.

@dpgeorge
Copy link
Member

Maybe something like this (untested!):

--- a/py/objmodule.c
+++ b/py/objmodule.c
@@ -207,6 +207,12 @@ mp_obj_t mp_module_get_builtin(qstr module_name, bool extensible) {
         if (!elem) {
             return MP_OBJ_NULL;
         }
+        #if MICROPY_LEGACY_U_MODULE
+        elem = mp_map_lookup((mp_map_t *)&mp_builtin_module_map, MP_OBJ_NEW_QSTR(qstr_from_strn(module_name_str + 1, module_name_len - 1)), MP_MAP_LOOKUP);
+        if (!elem) {
+            return MP_OBJ_NULL;
+        }
+        #endif
     }
 
     #if MICROPY_MODULE_BUILTIN_INIT

@dpgeorge
Copy link
Member

Or can you simply do:

MP_REGISTER_MODULE(MP_QSTR_umath, mp_module_math);
MP_REGISTER_MODULE(MP_QSTR_ucmath, mp_module_cmath);

in your main.c?

@dpgeorge dpgeorge added the py-core Relates to py/ directory in source label Jul 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
py-core Relates to py/ directory in source
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy