rust_macro.hook
- class rust_macro.hook.ExpandMacros
The main class that provides a context manager interface to the main import hook.
- __enter__() rust_macro.ExpandMacros
Enables macro expansion on import
- Returns
self
- __exit__(exception_type, exception_value, exception_traceback, /) None
Disables macro expansion on import
- exception rust_macro.hook.MacroFindError(msg: str)
An Exception that is raised when a module does not define macros when another module expects it.
- exception rust_macro.hook.MacroNotFoundError(name: str)
A subclass of
NameError
that is raised when a macro cannot be found in the current scope.
- class rust_macro.hook.MacroExpander(fullname: str, path: str)
A subclass of
importlib.abc.SourceLoader
that is responsible for processing macros in modules and loading processed modulesWarning
Do not use this class unless you know exactly what you are doing. If you do use this class, then do not call any of its methods directly. This class’s interface may change at any time and without warning. The only guarantee is the existence of the methods
MacroExpander.get_data
andMacroExpander.get_filename
and the class being a subclass ofimportlib.abc.SourceLoader
. This class may also be deprecated in the future.- fullname: str = fullname
The full name of the module that
self
is responsible for loading
- path: str = path
The path of the module that
self
is responsible for loading
- macros: dict[str, Callable[[Iterable[Token]], Union[Iterable[Token], str]]] = {}
The mapping of names to macros that
self
uses to expand macros
- add_macros(fullname: str) None:
Update
self
’s macro mapping with the contents of the module{fullname}.__macros__
.- Parameters
fullname (str) – The full name of the module to import macros from
- Raises
MacroFindError – if the module at
fullname
does not have a__macros__
atributeModuleNotFoundError – when the module path doesn’t exist
- get_filename(fullname: str) str
Gets the path of the file that
self
is responsible for loading.- Returns
self.path
- expand_macros(self, tokens: MutableSequence[Token]) MutableSequence[Token]
Expands all registered macros in the token list
- raises MacroNotFoundError
when there is an attempt to expand a macro that isn’t defined in the current scope
- recursive_expand(self, code: MutableSequence[Token], *, depth_limit: int = 50) MutableSequence[Token]
Recursively expands macros that are in the token list.
- Raises
MacroNotFoundError – when there is an attempt to expand a macro that isn’t defined in the current scope
MacroError – when the
depth_limit
is exceeded
- get_data(filename: str) str
Gets the source code for the final processed module.
- Parameters
filename (str) – the file path that is opened