Load macros from a module. Return a list of (new name, source name, macro object) tuples, including only macros that were actually transferred. - `target` can be a a string (naming a module), a module object, a dictionary, or `None` (meaning the calling module). - `assignments
(source_module, target, assignments, prefix="", target_module_name=None, compiler=None)
| 187 | |
| 188 | |
| 189 | def require(source_module, target, assignments, prefix="", target_module_name=None, compiler=None): |
| 190 | """Load macros from a module. Return a list of (new name, source |
| 191 | name, macro object) tuples, including only macros that were |
| 192 | actually transferred. |
| 193 | |
| 194 | - `target` can be a a string (naming a module), a module object, |
| 195 | a dictionary, or `None` (meaning the calling module). |
| 196 | - `assignments` can be "ALL", "EXPORTS", or a list of (macro |
| 197 | name, alias) pairs.""" |
| 198 | |
| 199 | if type(target) is dict: |
| 200 | target_module = None |
| 201 | else: |
| 202 | target_module, target_namespace = derive_target_module( |
| 203 | target, inspect.stack()[1][0] |
| 204 | ) |
| 205 | # Let's do a quick check to make sure the source module isn't actually |
| 206 | # the module being compiled (e.g. when `runpy` executes a module's code |
| 207 | # in `__main__`). |
| 208 | # We use the module's underlying filename for this (when they exist), since |
| 209 | # it's the most "fixed" attribute. |
| 210 | if _same_modules(source_module, target_module): |
| 211 | return [] |
| 212 | |
| 213 | if not inspect.ismodule(source_module): |
| 214 | source_module = import_module_from_string(source_module, |
| 215 | target_module_name or target_module or '') |
| 216 | |
| 217 | source_macros = source_module.__dict__.setdefault("_hy_macros", {}) |
| 218 | source_exports = getattr( |
| 219 | source_module, |
| 220 | "_hy_export_macros", |
| 221 | [k for k in source_macros.keys() if not k.startswith("_")], |
| 222 | ) |
| 223 | |
| 224 | if not source_module._hy_macros: |
| 225 | if assignments in ("ALL", "EXPORTS"): |
| 226 | return [] |
| 227 | out = [] |
| 228 | for name, alias in assignments: |
| 229 | try: |
| 230 | out.extend(require( |
| 231 | f"{source_module.__name__}.{mangle(name)}", |
| 232 | target_module or target, |
| 233 | "ALL", |
| 234 | prefix=alias, |
| 235 | )) |
| 236 | except HyRequireError as e: |
| 237 | raise HyRequireError( |
| 238 | f"Cannot import name '{name}'" |
| 239 | f" from '{source_module.__name__}'" |
| 240 | f" ({source_module.__file__})" |
| 241 | ) |
| 242 | return out |
| 243 | |
| 244 | target_macros = target_namespace.setdefault("_hy_macros", {}) if target_module else target |
| 245 | |
| 246 | if prefix: |
no test coverage detected