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

Method _ofind

IPython/core/interactiveshell.py:1603–1698  ·  view source on GitHub ↗

Find an object in the available namespaces. self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic Has special code to detect magic functions.

(self, oname, namespaces=None)

Source from the content-addressed store, hash-verified

1601 #-------------------------------------------------------------------------
1602
1603 def _ofind(self, oname, namespaces=None):
1604 """Find an object in the available namespaces.
1605
1606 self._ofind(oname) -> dict with keys: found,obj,ospace,ismagic
1607
1608 Has special code to detect magic functions.
1609 """
1610 oname = oname.strip()
1611 if not oname.startswith(ESC_MAGIC) and \
1612 not oname.startswith(ESC_MAGIC2) and \
1613 not all(a.isidentifier() for a in oname.split(".")):
1614 return {'found': False}
1615
1616 if namespaces is None:
1617 # Namespaces to search in:
1618 # Put them in a list. The order is important so that we
1619 # find things in the same order that Python finds them.
1620 namespaces = [ ('Interactive', self.user_ns),
1621 ('Interactive (global)', self.user_global_ns),
1622 ('Python builtin', builtin_mod.__dict__),
1623 ]
1624
1625 ismagic = False
1626 isalias = False
1627 found = False
1628 ospace = None
1629 parent = None
1630 obj = None
1631
1632
1633 # Look for the given name by splitting it in parts. If the head is
1634 # found, then we look for all the remaining parts as members, and only
1635 # declare success if we can find them all.
1636 oname_parts = oname.split('.')
1637 oname_head, oname_rest = oname_parts[0],oname_parts[1:]
1638 for nsname,ns in namespaces:
1639 try:
1640 obj = ns[oname_head]
1641 except KeyError:
1642 continue
1643 else:
1644 for idx, part in enumerate(oname_rest):
1645 try:
1646 parent = obj
1647 # The last part is looked up in a special way to avoid
1648 # descriptor invocation as it may raise or have side
1649 # effects.
1650 if idx == len(oname_rest) - 1:
1651 obj = self._getattr_property(obj, part)
1652 else:
1653 obj = getattr(obj, part)
1654 except:
1655 # Blanket except b/c some badly implemented objects
1656 # allow __getattr__ to raise exceptions other than
1657 # AttributeError, which then crashes IPython.
1658 break
1659 else:
1660 # If we finish the for loop (no break), we got all members

Calls 3

_getattr_propertyMethod · 0.95
find_cell_magicMethod · 0.95
find_line_magicMethod · 0.95