xonsh.lib.inspectors

Tools for inspecting Python objects.

This file was forked from the IPython project:

class xonsh.lib.inspectors.Inspector(str_detail_level=0)[source]

Inspects objects.

info(obj, oname='', info=None, detail_level=0)[source]

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)[source]

Generic message when no information is found.

pdef(obj, oname='')[source]

Print the call signature for any callable object.

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

pdoc(obj, oname='')[source]

Print the docstring for any object.

Optional

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

pfile(obj, oname='')[source]

Show the whole file where an object was defined.

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

Show detailed information about an object.

Parameters:
objobject
onamestr, optional

name of the variable pointing to the object.

infodict, optional

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

detail_levelint, optional

if set to 1, more information is given.

psource(obj, oname='')[source]

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)[source]

Extract call tip data from an oinfo dict.

Parameters:
oinfodict
format_callbool, optional

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

Returns:
call_infoNone, 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.

docstringstr 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)[source]

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:
objany Python object
Returns:
fnamestr

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

xonsh.lib.inspectors.find_source_lines(obj)[source]

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:
objany Python object
Returns:
linenoint

The line number where the object definition starts.

xonsh.lib.inspectors.format_argspec(argspec)[source]

Format argspect, convenience wrapper around inspect’s.

This takes a dict instead of ordered arguments and calls inspect.format_argspec with the arguments in the necessary order.

xonsh.lib.inspectors.get_encoding(obj)[source]

Get encoding for python source file defining obj

Returns None if obj is not defined in a sourcefile.

xonsh.lib.inspectors.getargspec(obj)[source]

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)[source]

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)[source]

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)[source]

True if obj is a function ()

xonsh.lib.inspectors.object_info(**kw)[source]

Make an object info dict with all fields present.