Skip to content
Back to top
View this page
Edit this page

# xonsh.lib.inspectors

Tools for inspecting Python objects.

This file was forked from the IPython project:

- Copyright (c) 2008-2014, IPython Development Team

- Copyright (C) 2001-2007 Fernando Perez <fperez@colorado.edu>

- Copyright (c) 2001, Janko Hauser <jhauser@zscout.de>

- Copyright (c) 2001, Nathaniel Gray <n8gray@caltech.edu>

class xonsh.lib.inspectors.Inspector(str_detail_level=0)

  Inspects objects.

  info(obj, oname='', info=None, detail_level=0)

    Compute a dict with detailed information about an object.

    Optional arguments:

    - oname: name of the variable pointing to the object.

    - info: a structure with some information fields which may have been precomputed already.

    - detail_level: if set to 1, more information is given.

  noinfo(msg, oname)

    Generic message when no information is found.

  pdef(obj, oname='')

    Print the call signature for any callable object.

    If the object is a class, print the constructor information.

  pdoc(obj, oname='')

    Print the docstring for any object.

    Optional

    -formatter: a function to run the docstring through for specially formatted docstrings.

  pfile(obj, oname='')

    Show the whole file where an object was defined.

  pinfo(obj, oname='', info=None, detail_level=0)

    Show detailed information about an object.

    Parameters:
      obj object
      oname str, optional

        name of the variable pointing to the object.

      info dict, optional

        a structure with some information fields which may have been precomputed already.

      detail_level int, optional

        if set to 1, more information is given.

  psource(obj, oname='')

    Print the source code for an object.

  pinfo_fields1 = [('Type', 'type_name')]
  pinfo_fields2 = [('String form', 'string_form')]
  pinfo_fields3 = [('Length', 'length'), ('File', 'file'), ('Definition', 'definition')]
  pinfo_fields_obj = [('Class docstring', 'class_docstring'), ('Init docstring', 'init_docstring'), ('Call def', 'call_def'), ('Call docstring', 'call_docstring')]

xonsh.lib.inspectors.call_tip(oinfo, format_call=True)

  Extract call tip data from an oinfo dict.

  Parameters:
    oinfo dict
    format_call bool, optional

      If True, the call line is formatted and returned as a string. If not, a tuple of (name, argspec) is returned.

  Returns:
    call_info None, str or (str, dict) tuple.

      When format_call is True, the whole call information is formatted as a single string. Otherwise, the object’s name and its argspec dict are returned. If no call information is available, None is returned.

    docstring str or None

      The most relevant docstring for calling purposes is returned, if available. The priority is: call docstring for callable instances, then constructor docstring for classes, then main object’s docstring otherwise (regular functions).

xonsh.lib.inspectors.find_file(obj)

  Find the absolute path to the file where an object was defined.

  This is essentially a robust wrapper around inspect.getabsfile.

  Returns None if no file can be found.

  Parameters:
    obj any Python object
  Returns:
    fname str

      The absolute path to the file where the object was defined.

xonsh.lib.inspectors.find_source_lines(obj)

  Find the line number in a file where an object was defined.

  This is essentially a robust wrapper around inspect.getsourcelines.

  Returns None if no file can be found.

  Parameters:
    obj any Python object
  Returns:
    lineno int

      The line number where the object definition starts.

xonsh.lib.inspectors.format_argspec(argspec)

  Format argspec dict into a human-readable call signature string.

xonsh.lib.inspectors.formatargspec(args=(), varargs=None, varkw=None, defaults=())

  Reimplements `inspect.formatargspec` which was deprecated in Python 3.5 and removed in Python 3.11.

xonsh.lib.inspectors.get_encoding(obj)

  Get encoding for python source file defining obj

  Returns None if obj is not defined in a sourcefile.

xonsh.lib.inspectors.getargspec(obj)

  Wrapper around `inspect.getfullargspec()` on Python 3, and :func:inspect.getargspec` on Python 2.

  In addition to functions and methods, this can also handle objects with a `__call__` attribute.

xonsh.lib.inspectors.getdoc(obj)

  Stable wrapper around inspect.getdoc.

  This can’t crash because of attribute problems.

  It also attempts to call a getdoc() method on the given object. This allows objects which provide their docstrings via non-standard mechanisms (like Pyro proxies) to still be inspected by ipython’s ? system.

xonsh.lib.inspectors.getsource(obj, is_binary=False)

  Wrapper around inspect.getsource.

  This can be modified by other projects to provide customized source extraction.

  Inputs:

  - obj: an object whose source code we will attempt to extract.

  Optional inputs:

  - is_binary: whether the object is known to come from a binary source. This implementation will skip returning any output for binary objects, but custom extractors may know how to meaningfully process them.

xonsh.lib.inspectors.is_simple_callable(obj)

  True if obj is a function ()

xonsh.lib.inspectors.object_info(**kw)

  Make an object info dict with all fields present.