Skip to content

Commit e6c7bd2

Browse files
committed
pybricks/pybricks: Auto-import non-Pybricks modules.
Import the module, not their contents.
1 parent 27775ab commit e6c7bd2

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
model-based estimate. For most use cases, this is a more intuitive result
3333
because this speed value is not affected by mechanical load.
3434
- When using the REPL, everything from all Pybricks modules was automatically
35-
imported for convenience. Now, everything from all available MicroPython
36-
modules is also automatically imported ([support#741]).
35+
imported for convenience. Now, MicroPython modules are also automatically
36+
imported ([support#741]).
3737

3838
### Fixed
3939
- Fixed motors going out of sync when starting program ([support#679]).

pybricks/pybricks.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,23 @@ MP_REGISTER_MODULE(MP_QSTR_pybricks, pb_package_pybricks);
6363

6464
#if PYBRICKS_OPT_COMPILER
6565
/**
66-
* import * from all builtin Pybricks and MicroPython modules.
66+
* Import all MicroPython modules and import * from Pybricks modules.
6767
*/
6868
static void pb_package_import_all(void) {
6969

7070
// Go through all modules in mp_builtin_module_map.
7171
for (size_t i = 0; i < mp_builtin_module_map.used; i++) {
7272
// This is a constant map of modules, so we can skip checks for
73-
// filled slots and module types.
74-
mp_import_all(mp_builtin_module_map.table[i].value);
73+
// filled slots or confirming that we have module types.
74+
qstr module_name = MP_OBJ_QSTR_VALUE(mp_builtin_module_map.table[i].key);
75+
mp_obj_t module = mp_builtin_module_map.table[i].value;
76+
if (!strncmp("pybricks", qstr_str(module_name), 8)) {
77+
// Import everything from a Pybricks module.
78+
mp_import_all(module);
79+
} else {
80+
// Otherwise import just the module.
81+
mp_store_global(module_name, module);
82+
}
7583
}
7684

7785
#if PYBRICKS_PY_HUBS

0 commit comments

Comments
 (0)