Exemple #1
0
def get_source(obj):
    src = None
    try:
        src = inspect.getsource(obj)
    except (TypeError, IOError):
        pass

    if src:
        return src

    try:
        from IPython.core import oinspect
    except ImportError:
        pass
    else:
        try:
            src = oinspect.getsource(obj)
        except TypeError:
            pass

    if src:
        return src

    main = sys.modules['__main__']
    pattern = re.compile('class %s\(' % obj.__class__.__name__)
    for src in reversed(main.In):
        if pattern.search(src):
            return src
Exemple #2
0
def get_source(obj):
    src = None
    try:
        src = inspect.getsource(obj)
    except (TypeError, IOError):
        pass

    if src:
        return src

    try:
        from IPython.core import oinspect
    except ImportError:
        pass
    else:
        try:
            src = oinspect.getsource(obj)
        except TypeError:
            pass

    if src:
        return src

    main = sys.modules['__main__']
    pattern = re.compile('class %s\(' % obj.__class__.__name__)
    for src in reversed(main.In):
        if pattern.search(src):
            return src
Exemple #3
0
def _getsource(obj):
    """Safe oinspect.getsource wrapper.

  **NOTE**: this function is may call repr(obj).

  Args:
    obj: object whose source we want to fetch.

  Returns:
    source code or None.
  """
    try:
        return oinspect.getsource(obj)
    except TypeError:
        return None