MCPcopy
hub / github.com/hylang/hy / require

Function require

hy/macros.py:189–271  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

187
188
189def 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:

Callers 4

__init__Method · 0.90
run_commandFunction · 0.90
compile_requireFunction · 0.90
require_valsFunction · 0.85

Calls 7

mangleFunction · 0.90
HyRequireErrorClass · 0.90
derive_target_moduleFunction · 0.85
_same_modulesFunction · 0.85
keysMethod · 0.80
warn_on_core_shadowMethod · 0.80

Tested by

no test coverage detected