Page 1 of 1

Submodules and Classes in C modules

Posted: Wed Jun 23, 2021 8:36 am
by zpif
Hi,

I am writing a custom C module for Micropython. I have two questions:
  • Is it possible to have submodules?
  • Is it possible to define classes?
Thanks,
Frank

Re: Submodules and Classes in C modules

Posted: Wed Jun 23, 2021 8:54 am
by stijn
The answers to both questions is 'yes', and if you look through the source code (search for module definitions e.g around code like ".base = { &mp_type_module }," you should find examples of both.

Re: Submodules and Classes in C modules

Posted: Wed Jun 23, 2021 10:26 am
by zpif
Thank you stijn for your quick response.

I did find plenty of examples where classes are defined, thank you. However, I did not find examples of modules that contain modules.

Could you please point me to one?

Thank you,
Frank

Re: Submodules and Classes in C modules

Posted: Wed Jun 23, 2021 12:19 pm
by stijn
Sorry my mistake, I did not actually try to look for nested modules. Principle would be that you create a module object just like any other, than add that object to the module you want it to be nested in. Pseudocode

Code: Select all

mp_obj_module_t nested = {
    .base = { &mp_type_module },
    .globals = ...
};

STATIC const mp_rom_map_elem_t mainmodule_globals_table[] = {
...
{ MP_ROM_QSTR(MP_QSTR_nested), MP_ROM_PTR(&nested) },
}

...
However this might not be what you're after; this works, but only in that if you do 'import mainmodule' you get access to 'mainmodule.nested'.

But looking at how the code for importing works this you cannot use 'import mainmodule.nested'. That only works for actual 'packages' (which AFAIK is the offcial name for a moule which contains other modules) and they are always filessystem-based it seems (did not check in detail though, someone might want to correct me here).

Anyway not a huge problem: mix Python and C modules for that. So in C you create 2 modules: '_mainmodule' and '_nested'. Then you add a directory which can be found via MICROPYPATH and contains:

Code: Select all

mainmodule\
    __init__.py
    nested.py


In __init__.py you put 'from _mainmodule import *' and in nested.py 'from _nested import *'

Re: Submodules and Classes in C modules

Posted: Thu Jun 24, 2021 5:54 am
by katesimon123
Try this one out:

Code: Select all

mypackage
├── __init__.py
├── one.py  # contains "import two"
└── two
    ├── __init__.py
    ├── two.py  # contains "import three"
    └── three
        ├── __init__.py
        └── three.py
And then, you can access the package with:

Code: Select all

import mypackage.one
import mypackage.one.two
import mypackage.one.two.three
You can read it in more detail from Introduction to Classes.

Re: Submodules and Classes in C modules

Posted: Thu Jun 24, 2021 8:39 am
by zpif
Actually, I was looking for stijn's first solution of nested modules, and your suggestion works well, thank you!

But I now realize that this set-up might not be very python-ish, and that I should use the other solution instead, to have python scripts with the proper directory structure set-up importing the C packages internally.

So thank you both!

Re: Submodules and Classes in C modules

Posted: Thu Jun 24, 2021 12:44 pm
by zpif
A follow-up question: I would like to run some initialization code as my module is loaded. I've tried adding an "__init__" method to the (toplevel) .globals dictionary, but the function is not called automatically.

Calling "mymodule.__init__()" manually after import works, so I am confident that this is not a typo.

Is this something that ought to work?

Re: Submodules and Classes in C modules

Posted: Thu Jun 24, 2021 1:10 pm
by stijn
You need a build which has MICROPY_MODULE_BUILTIN_INIT set to 1 for that, not all ports have it like that.

Re: Submodules and Classes in C modules

Posted: Thu Jun 24, 2021 1:56 pm
by zpif
Ah, that #define fixed it. Excellent, thank you again!