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
py: support mutable attributes on sys module #7591
Conversation
I assume that's because it is easy to implement/ fast to lookup, but it doesn't look very convenient when creating modules via But for the rest the idea is sound. |
Yes. It could also go as the first entry, but then every normal dict lookup would have to search that entry unnecessarily.
I didn't think of this case. But is it really needed to support it? In the situation where |
1d190a8
to
5d90183
Compare
Good point, probably not. load/store to a module is probably always going to implemented as such, i.e. just load and store, no custom hook function, so indeed in case of a RAM dict the default load/store should be ok. |
Actually, considering a constant/fixed and linear (not hash table) dict is fully searched to find an attribute before failing and falling back to this delegation function, it could be possible to, as part of the initial search, remember the special |
5d90183
to
16962e9
Compare
e08a61a
to
efd489b
Compare
efd489b
to
3c0c439
Compare
I removed the mutable As it stands, this PR adds
Performance is unchanged (at least within measurement errors). Tests have been added and docs updated. This is ready to merge. |
This commit adds generic support for mutable module attributes on built in modules, by adding support for an optional hook function for module attribute lookup. If a module wants to support additional attribute load/ store/delete (beyond what is in the constant, globals dict) then it should add at the very end of its globals dict MP_MODULE_ATTR_DELEGATION_ENTRY(). This should point to a custom function which will handle any additional attributes. The mp_module_generic_attr() function is provided as a helper function for additional attributes: it requires an array of qstrs (terminated in MP_QSTRnull) and a corresponding array of objects (with a 1-1 mapping between qstrs and objects). If the qstr is found in the array then the corresponding object is loaded/stored/deleted. Signed-off-by: Damien George <damien@micropython.org>
To be enabled when needed by specific sys attributes. Signed-off-by: Damien George <damien@micropython.org>
With behaviour as per CPython. Signed-off-by: Damien George <damien@micropython.org>
This allows customising the REPL prompt strings. Signed-off-by: Damien George <damien@micropython.org>
3c0c439
to
ac22931
Compare
This PR adds generic support for mutable module attributes on built in modules, by adding support for an optional hook function for module attribute lookup. If a module wants to support additional attribute load/store/delete (beyond what is in the constant, globals dict) then it should define at the end of its globals dict an entry with
MP_QSTR_NULL
key and value a pointer to a function of typemp_attr_fun_t
. Then that function will handle any additional attributes (which can include stores).To show how this works, this PR also adds mutable attributes to the sys module, where they are really needed:
sys.stdout
is now writablesys.tracebacklimit
can be set to an integer to limit the stored traceback (set to 0 disables traceback information, to save RAM)sys.ps1
andsys.ps2
can be modified to customise the REPLThis is WIP and RFC... there are many ways to make module attributes mutable and the way proposed here aims to minimise RAM usage, keep code size down to a minimum, and make it general for other modules to use.