-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
base: master
Are you sure you want to change the base?
Conversation
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>
unix coverage test failure is just a flaky test: |
Code size report:
|
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
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 |
Codecov ReportAll modified and coverable lines are covered by tests ✅
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. 🚀 New features to boost your workflow:
|
Ah, we didn't notice because we don't have a filesystem.
Sure, open to suggestions. |
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 |
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 |
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 themath
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.