When embedding it is common to want to freeze custom modules without copying code from the python core itself related to also having the builtin frozen modules with them.
As such I propose API’s for appending frozen modules to these internal list structures from within the python core itself similar to AppendInitTab
Each of these apis would need to either insert the entries one place prior to the sentinels in each pointer/list the pointers point to or simply realloc
them to extend the memory in them and then insert where the old sentinel was before inserting a new sentinel each time one is added.
New API’s (stable):
- PyAPI_FUNC(int) PyImport_AppendFrozenModules(_frozen *);
- Works exactly like
PyImport_FrozenModules
except the python core can handle the complex logic of setting it. Also setting a manual list for it and then setting the pointer would not be needed then when embedding. Allows appending both a single frozen entry along with a list of frozen entries to the existing list of frozen entries.
- Works exactly like
- PyAPI_FUNC(int) PyImport_AppendFrozenAliases(_module_alias *);
- Works exactly like the private
_PyImport_FrozenAliases
pointer except when one wants to add their own aliases to their frozen module they can avoid private API’s that can change or be replaced with something similar. This new API could replace this instead though as then there would be no point in depending on this private symbol from the python core when embedding. Allows appending both a single frozen alias entry along with a list of frozen alias entries to the existing list of frozen alias entries.
- Works exactly like the private
- PyAPI_FUNC(int) PyImport_AppendFrozenTest(_frozen *);
- Works exactly like the private
_PyImport_FrozenTest
pointer except that it allows one to append their own frozen test modules to the end of the builtin frozen test modules (without having to manually provide them yourself when embedding). Also allows appending both a single entry as well as a list of entries.
- Works exactly like the private
- PyAPI_FUNC(int) PyImport_AppendFrozenBootstrap(_frozen *);
- Works exactly like the private
_PyImport_FrozenBootstrap
pointer, except allows one to append their own frozen import hook to the list of default frozen bootstrap modules (currently_frozen_importlib
,_frozen_importlib_external
, andzipimport
). Also allows appending both a single entry as well as a list of entries.
- Works exactly like the private
- PyAPI_FUNC(int) PyImport_AppendFrozenStdlib(_frozen *);
- Works for when one wants to freeze the entire default standard library instead of shipping their embedded interpreters with a zip file containing these modules or even freezing a specific few of them on top of the default frozen ones within the python core. This also allows to append a single entry as well as a list of entries.
Not only can this make embedding real world applications easier, it could also simplify some of the tests that might append to the frozen lists. This also makes it new C API user friendly as well.
Likewise with AppendInitTab
this must error when one attempts to append entries to these after Py_Initialize
.
Specific versions of Python I have in mind for this PEP:
- Python 3.14.0a1+