MCPcopy Create free account
hub / github.com/ipython/ipython / MagicAlias

Class MagicAlias

IPython/core/magic.py:664–703  ·  view source on GitHub ↗

An alias to another magic function. An alias is determined by its magic name and magic kind. Lookup is done at call time, so if the underlying magic changes the alias will call the new function. Use the :meth:`MagicsManager.register_alias` method or the `%alias_magic` magic fun

Source from the content-addressed store, hash-verified

662
663
664class MagicAlias(object):
665 """An alias to another magic function.
666
667 An alias is determined by its magic name and magic kind. Lookup
668 is done at call time, so if the underlying magic changes the alias
669 will call the new function.
670
671 Use the :meth:`MagicsManager.register_alias` method or the
672 `%alias_magic` magic function to create and register a new alias.
673 """
674 def __init__(self, shell, magic_name, magic_kind, magic_params=None):
675 self.shell = shell
676 self.magic_name = magic_name
677 self.magic_params = magic_params
678 self.magic_kind = magic_kind
679
680 self.pretty_target = '%s%s' % (magic_escapes[self.magic_kind], self.magic_name)
681 self.__doc__ = "Alias for `%s`." % self.pretty_target
682
683 self._in_call = False
684
685 def __call__(self, *args, **kwargs):
686 """Call the magic alias."""
687 fn = self.shell.find_magic(self.magic_name, self.magic_kind)
688 if fn is None:
689 raise UsageError("Magic `%s` not found." % self.pretty_target)
690
691 # Protect against infinite recursion.
692 if self._in_call:
693 raise UsageError("Infinite recursion detected; "
694 "magic aliases cannot call themselves.")
695 self._in_call = True
696 try:
697 if self.magic_params:
698 args_list = list(args)
699 args_list[0] = self.magic_params + " " + args[0]
700 args = tuple(args_list)
701 return fn(*args, **kwargs)
702 finally:
703 self._in_call = False

Callers 1

register_aliasMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected