oinspect.py
1283 lines
| 41.7 KiB
| text/x-python
|
PythonLexer
Ville M. Vainio
|
r1032 | """Tools for inspecting Python objects. | ||
Uses syntax highlighting for presenting the various information elements. | ||||
Similar in spirit to the inspect module, but all calls take a name argument to | ||||
reference the name under which an object is being read. | ||||
""" | ||||
MinRK
|
r16579 | # Copyright (c) IPython Development Team. | ||
# Distributed under the terms of the Modified BSD License. | ||||
Ville M. Vainio
|
r1032 | __all__ = ['Inspector','InspectColors'] | ||
# stdlib modules | ||||
Matthias Bussonnier
|
r28195 | from dataclasses import dataclass | ||
Min RK
|
r23020 | from inspect import signature | ||
Matthias Bussonnier
|
r28195 | from textwrap import dedent | ||
import ast | ||||
Jason Grout
|
r27940 | import html | ||
Matthias Bussonnier
|
r28195 | import inspect | ||
import io as stdlib_io | ||||
Ville M. Vainio
|
r1032 | import linecache | ||
import os | ||||
Fernando Perez
|
r1413 | import types | ||
Matthias Bussonnier
|
r28195 | import warnings | ||
Matthias Bussonnier
|
r25338 | |||
Matthias Bussonnier
|
r28662 | |||
from typing import ( | ||||
cast, | ||||
Any, | ||||
Optional, | ||||
Dict, | ||||
Union, | ||||
List, | ||||
TypedDict, | ||||
TypeAlias, | ||||
Tuple, | ||||
) | ||||
Fernando Perez
|
r1413 | |||
Matthias Bussonnier
|
r28591 | import traitlets | ||
Ville M. Vainio
|
r1032 | # IPython's own | ||
Brian Granger
|
r2830 | from IPython.core import page | ||
immerrr
|
r17023 | from IPython.lib.pretty import pretty | ||
Paul Ivanov
|
r22961 | from IPython.testing.skipdoctest import skip_doctest | ||
Matthias Bussonnier
|
r28662 | from IPython.utils import PyColorize, openpy | ||
Jeffrey Tratner
|
r12965 | from IPython.utils.dir2 import safe_hasattr | ||
Thomas Kluyver
|
r20569 | from IPython.utils.path import compress_user | ||
Brian Granger
|
r2498 | from IPython.utils.text import indent | ||
Matthias Bussonnier
|
r28662 | from IPython.utils.wildcard import list_namespace, typestr2type | ||
Matthias Bussonnier
|
r28591 | from IPython.utils.coloransi import TermColors | ||
Matthias Bussonnier
|
r22109 | from IPython.utils.colorable import Colorable | ||
Srinivas Reddy Thatiparthy
|
r23035 | from IPython.utils.decorators import undoc | ||
Ville M. Vainio
|
r1032 | |||
Matthias Bussonnier
|
r22492 | from pygments import highlight | ||
from pygments.lexers import PythonLexer | ||||
from pygments.formatters import HtmlFormatter | ||||
Matthias Bussonnier
|
r28195 | HOOK_NAME = "__custom_documentations__" | ||
UnformattedBundle: TypeAlias = Dict[str, List[Tuple[str, str]]] # List of (title, body) | ||||
Bundle: TypeAlias = Dict[str, str] | ||||
Matthias Bussonnier
|
r28165 | |||
@dataclass | ||||
class OInfo: | ||||
ismagic: bool | ||||
isalias: bool | ||||
found: bool | ||||
Matthias Bussonnier
|
r28167 | namespace: Optional[str] | ||
Matthias Bussonnier
|
r28165 | parent: Any | ||
obj: Any | ||||
Matthias Bussonnier
|
r28252 | def get(self, field): | ||
"""Get a field from the object for backward compatibility with before 8.12 | ||||
see https://github.com/h5py/h5py/issues/2253 | ||||
""" | ||||
# We need to deprecate this at some point, but the warning will show in completion. | ||||
# Let's comment this for now and uncomment end of 2023 ish | ||||
# warnings.warn( | ||||
# f"OInfo dataclass with fields access since IPython 8.12 please use OInfo.{field} instead." | ||||
# "OInfo used to be a dict but a dataclass provide static fields verification with mypy." | ||||
# "This warning and backward compatibility `get()` method were added in 8.13.", | ||||
# DeprecationWarning, | ||||
# stacklevel=2, | ||||
# ) | ||||
return getattr(self, field) | ||||
Matthias Bussonnier
|
r22492 | def pylight(code): | ||
return highlight(code, PythonLexer(), HtmlFormatter(noclasses=True)) | ||||
MinRK
|
r14835 | # builtin docstrings to ignore | ||
_func_call_docstring = types.FunctionType.__call__.__doc__ | ||||
_object_init_docstring = object.__init__.__doc__ | ||||
_builtin_type_docstrings = { | ||||
immerrr
|
r17025 | inspect.getdoc(t) for t in (types.ModuleType, types.MethodType, | ||
types.FunctionType, property) | ||||
MinRK
|
r14835 | } | ||
Thomas Kluyver
|
r15362 | |||
_builtin_func_type = type(all) | ||||
_builtin_meth_type = type(str.upper) # Bound methods have the same type as builtin functions | ||||
Ville M. Vainio
|
r1032 | #**************************************************************************** | ||
# Builtin color schemes | ||||
Colors = TermColors # just a shorthand | ||||
Matthias Bussonnier
|
r21774 | InspectColors = PyColorize.ANSICodeColors | ||
Ville M. Vainio
|
r1032 | |||
#**************************************************************************** | ||||
Fernando Perez
|
r2931 | # Auxiliary functions and objects | ||
krassowski
|
r28650 | |||
class InfoDict(TypedDict): | ||||
type_name: Optional[str] | ||||
base_class: Optional[str] | ||||
string_form: Optional[str] | ||||
namespace: Optional[str] | ||||
length: Optional[str] | ||||
file: Optional[str] | ||||
definition: Optional[str] | ||||
docstring: Optional[str] | ||||
source: Optional[str] | ||||
init_definition: Optional[str] | ||||
class_docstring: Optional[str] | ||||
init_docstring: Optional[str] | ||||
call_def: Optional[str] | ||||
call_docstring: Optional[str] | ||||
subclasses: Optional[str] | ||||
# These won't be printed but will be used to determine how to | ||||
# format the object | ||||
ismagic: bool | ||||
isalias: bool | ||||
isclass: bool | ||||
found: bool | ||||
name: str | ||||
Matthias Bussonnier
|
r28662 | _info_fields = list(InfoDict.__annotations__.keys()) | ||
def __getattr__(name): | ||||
if name == "info_fields": | ||||
warnings.warn( | ||||
"IPython.core.oinspect's `info_fields` is considered for deprecation and may be removed in the Future. ", | ||||
DeprecationWarning, | ||||
stacklevel=2, | ||||
) | ||||
return _info_fields | ||||
raise AttributeError(f"module {__name__!r} has no attribute {name!r}") | ||||
krassowski
|
r28650 | |||
@dataclass | ||||
class InspectorHookData: | ||||
"""Data passed to the mime hook""" | ||||
obj: Any | ||||
info: Optional[OInfo] | ||||
info_dict: InfoDict | ||||
detail_level: int | ||||
omit_sections: list[str] | ||||
Fernando Perez
|
r2931 | |||
krassowski
|
r28650 | @undoc | ||
Matthias Bussonnier
|
r28662 | def object_info( | ||
*, | ||||
name: str, | ||||
found: bool, | ||||
isclass: bool = False, | ||||
isalias: bool = False, | ||||
ismagic: bool = False, | ||||
**kw, | ||||
) -> InfoDict: | ||||
krassowski
|
r28658 | """Make an object info dict with all fields present.""" | ||
Matthias Bussonnier
|
r28662 | infodict = kw | ||
infodict = {k: None for k in _info_fields if k not in infodict} | ||||
infodict["name"] = name # type: ignore | ||||
infodict["found"] = found # type: ignore | ||||
infodict["isclass"] = isclass # type: ignore | ||||
infodict["isalias"] = isalias # type: ignore | ||||
infodict["ismagic"] = ismagic # type: ignore | ||||
return InfoDict(**infodict) # type:ignore | ||||
Fernando Perez
|
r2931 | |||
Jörgen Stenarson
|
r8322 | def get_encoding(obj): | ||
"""Get encoding for python source file defining obj | ||||
Returns None if obj is not defined in a sourcefile. | ||||
""" | ||||
ofile = find_file(obj) | ||||
# run contents of file through pager starting at line where the object | ||||
# is defined, as long as the file isn't binary and is actually on the | ||||
# filesystem. | ||||
if ofile is None: | ||||
return None | ||||
elif ofile.endswith(('.so', '.dll', '.pyd')): | ||||
return None | ||||
elif not os.path.isfile(ofile): | ||||
return None | ||||
else: | ||||
# Print only text files, not extension binaries. Note that | ||||
# getsourcelines returns lineno with 1-offset and page() uses | ||||
# 0-offset, so we must adjust. | ||||
Thomas Kluyver
|
r15467 | with stdlib_io.open(ofile, 'rb') as buffer: # Tweaked to use io.open for Python 2 | ||
Matthias Bussonnier
|
r28591 | encoding, _lines = openpy.detect_encoding(buffer.readline) | ||
Jörgen Stenarson
|
r8322 | return encoding | ||
krassowski
|
r28650 | |||
Matthias Bussonnier
|
r28662 | def getdoc(obj) -> Union[str, None]: | ||
Ville M. Vainio
|
r1032 | """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 | ||||
Sylvain Corlay
|
r22460 | (like Pyro proxies) to still be inspected by ipython's ? system. | ||
""" | ||||
Ville M. Vainio
|
r1032 | # Allow objects to offer customized documentation via a getdoc method: | ||
try: | ||||
Thomas Kluyver
|
r5535 | ds = obj.getdoc() | ||
except Exception: | ||||
Ville M. Vainio
|
r1032 | pass | ||
else: | ||||
Srinivas Reddy Thatiparthy
|
r23037 | if isinstance(ds, str): | ||
Thomas Kluyver
|
r5536 | return inspect.cleandoc(ds) | ||
Antony Lee
|
r24004 | docstr = inspect.getdoc(obj) | ||
Matthias Bussonnier
|
r25333 | return docstr | ||
Ville M. Vainio
|
r1032 | |||
Fernando Perez
|
r1413 | |||
Matthias Bussonnier
|
r25333 | def getsource(obj, oname='') -> Union[str,None]: | ||
Ville M. Vainio
|
r1032 | """Wrapper around inspect.getsource. | ||
This can be modified by other projects to provide customized source | ||||
extraction. | ||||
immerrr
|
r17023 | Parameters | ||
---------- | ||||
obj : object | ||||
an object whose source code we will attempt to extract | ||||
oname : str | ||||
(optional) a name under which the object is known | ||||
Ville M. Vainio
|
r1032 | |||
immerrr
|
r17023 | Returns | ||
------- | ||||
src : unicode or None | ||||
Ville M. Vainio
|
r1032 | |||
immerrr
|
r17023 | """ | ||
Ville M. Vainio
|
r1032 | |||
immerrr
|
r17023 | if isinstance(obj, property): | ||
sources = [] | ||||
for attrname in ['fget', 'fset', 'fdel']: | ||||
fn = getattr(obj, attrname) | ||||
if fn is not None: | ||||
encoding = get_encoding(fn) | ||||
oname_prefix = ('%s.' % oname) if oname else '' | ||||
Matthias Bussonnier
|
r25333 | sources.append(''.join(('# ', oname_prefix, attrname))) | ||
immerrr
|
r17023 | if inspect.isfunction(fn): | ||
Matthias Bussonnier
|
r28167 | _src = getsource(fn) | ||
if _src: | ||||
# assert _src is not None, "please mypy" | ||||
sources.append(dedent(_src)) | ||||
immerrr
|
r17023 | else: | ||
# Default str/repr only prints function name, | ||||
# pretty.pretty prints module name too. | ||||
Matthias Bussonnier
|
r25333 | sources.append( | ||
'%s%s = %s\n' % (oname_prefix, attrname, pretty(fn)) | ||||
) | ||||
immerrr
|
r17023 | if sources: | ||
return '\n'.join(sources) | ||||
else: | ||||
return None | ||||
Ben Edwards
|
r4266 | |||
Ville M. Vainio
|
r1032 | else: | ||
immerrr
|
r17023 | # Get source for non-property objects. | ||
Min RK
|
r21505 | obj = _get_wrapped(obj) | ||
immerrr
|
r17023 | |||
Fernando Perez
|
r1228 | try: | ||
src = inspect.getsource(obj) | ||||
except TypeError: | ||||
immerrr
|
r17023 | # The object itself provided no meaningful source, try looking for | ||
# its class definition instead. | ||||
Nikita Kniazev
|
r27233 | try: | ||
src = inspect.getsource(obj.__class__) | ||||
Nikita Kniazev
|
r27234 | except (OSError, TypeError): | ||
Nikita Kniazev
|
r27233 | return None | ||
Nikita Kniazev
|
r27234 | except OSError: | ||
return None | ||||
immerrr
|
r17023 | |||
Matthias Bussonnier
|
r25333 | return src | ||
Ville M. Vainio
|
r1032 | |||
Thomas Kluyver
|
r15362 | |||
def is_simple_callable(obj): | ||||
"""True if obj is a function ()""" | ||||
return (inspect.isfunction(obj) or inspect.ismethod(obj) or \ | ||||
isinstance(obj, _builtin_func_type) or isinstance(obj, _builtin_meth_type)) | ||||
Matthias Bussonnier
|
r25244 | @undoc | ||
Fernando Perez
|
r1413 | def getargspec(obj): | ||
Terry Davis
|
r25450 | """Wrapper around :func:`inspect.getfullargspec` | ||
Chris Mentzel
|
r24820 | |||
Thomas Kluyver
|
r15335 | In addition to functions and methods, this can also handle objects with a | ||
``__call__`` attribute. | ||||
Matthias Bussonnier
|
r25244 | |||
DEPRECATED: Deprecated since 7.10. Do not use, will be removed. | ||||
Thomas Kluyver
|
r15335 | """ | ||
Matthias Bussonnier
|
r25244 | |||
warnings.warn('`getargspec` function is deprecated as of IPython 7.10' | ||||
'and will be removed in future versions.', DeprecationWarning, stacklevel=2) | ||||
Thomas Kluyver
|
r15362 | if safe_hasattr(obj, '__call__') and not is_simple_callable(obj): | ||
Thomas Kluyver
|
r15335 | obj = obj.__call__ | ||
Srinivas Reddy Thatiparthy
|
r23086 | return inspect.getfullargspec(obj) | ||
Fernando Perez
|
r1413 | |||
Matthias Bussonnier
|
r25244 | @undoc | ||
Fernando Perez
|
r3051 | def format_argspec(argspec): | ||
"""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. | ||||
Matthias Bussonnier
|
r25244 | |||
Matthias Bussonnier
|
r27217 | DEPRECATED (since 7.10): Do not use; will be removed in future versions. | ||
Fernando Perez
|
r3051 | """ | ||
Matthias Bussonnier
|
r25244 | |||
warnings.warn('`format_argspec` function is deprecated as of IPython 7.10' | ||||
'and will be removed in future versions.', DeprecationWarning, stacklevel=2) | ||||
Fernando Perez
|
r3051 | return inspect.formatargspec(argspec['args'], argspec['varargs'], | ||
argspec['varkw'], argspec['defaults']) | ||||
Srinivas Reddy Thatiparthy
|
r23035 | @undoc | ||
Fernando Perez
|
r3051 | def call_tip(oinfo, format_call=True): | ||
Matthias Bussonnier
|
r27217 | """DEPRECATED since 6.0. Extract call tip data from an oinfo dict.""" | ||
warnings.warn( | ||||
"`call_tip` function is deprecated as of IPython 6.0" | ||||
"and will be removed in future versions.", | ||||
DeprecationWarning, | ||||
stacklevel=2, | ||||
) | ||||
Fernando Perez
|
r3051 | # Get call definition | ||
MinRK
|
r3934 | argspec = oinfo.get('argspec') | ||
Fernando Perez
|
r3051 | if argspec is None: | ||
call_line = None | ||||
else: | ||||
# Callable objects will have 'self' as their first argument, prune | ||||
# it out if it's there for clarity (since users do *not* pass an | ||||
# extra first argument explicitly). | ||||
try: | ||||
has_self = argspec['args'][0] == 'self' | ||||
except (KeyError, IndexError): | ||||
pass | ||||
else: | ||||
if has_self: | ||||
argspec['args'] = argspec['args'][1:] | ||||
Bernardo B. Marques
|
r4872 | |||
Fernando Perez
|
r3051 | call_line = oinfo['name']+format_argspec(argspec) | ||
# Now get docstring. | ||||
# The priority is: call docstring, constructor docstring, main one. | ||||
MinRK
|
r3934 | doc = oinfo.get('call_docstring') | ||
Fernando Perez
|
r3051 | if doc is None: | ||
MinRK
|
r3934 | doc = oinfo.get('init_docstring') | ||
Fernando Perez
|
r3051 | if doc is None: | ||
MinRK
|
r3934 | doc = oinfo.get('docstring','') | ||
Fernando Perez
|
r3051 | |||
return call_line, doc | ||||
Fernando Perez
|
r1413 | |||
Min RK
|
r21505 | def _get_wrapped(obj): | ||
Thomas Kluyver
|
r21905 | """Get the original object if wrapped in one or more @decorators | ||
Some objects automatically construct similar objects on any unrecognised | ||||
attribute access (e.g. unittest.mock.call). To protect against infinite loops, | ||||
this will arbitrarily cut off after 100 levels of obj.__wrapped__ | ||||
attribute access. --TK, Jan 2016 | ||||
""" | ||||
orig_obj = obj | ||||
i = 0 | ||||
Min RK
|
r21505 | while safe_hasattr(obj, '__wrapped__'): | ||
obj = obj.__wrapped__ | ||||
Thomas Kluyver
|
r21905 | i += 1 | ||
if i > 100: | ||||
# __wrapped__ is probably a lie, so return the thing we started with | ||||
return orig_obj | ||||
Min RK
|
r21505 | return obj | ||
Matthias Bussonnier
|
r28591 | def find_file(obj) -> Optional[str]: | ||
Fernando Perez
|
r7290 | """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 | ||||
Matthias Bussonnier
|
r27289 | The absolute path to the file where the object was defined. | ||
Fernando Perez
|
r7290 | """ | ||
Min RK
|
r21505 | obj = _get_wrapped(obj) | ||
Bradley M. Froehle
|
r7520 | |||
Matthias Bussonnier
|
r28591 | fname: Optional[str] = None | ||
Fernando Perez
|
r7290 | try: | ||
fname = inspect.getabsfile(obj) | ||||
Nikita Kniazev
|
r27233 | except TypeError: | ||
Fernando Perez
|
r7290 | # For an instance, the file that matters is where its class was | ||
# declared. | ||||
Nikita Kniazev
|
r27233 | try: | ||
fname = inspect.getabsfile(obj.__class__) | ||||
except (OSError, TypeError): | ||||
# Can happen for builtins | ||||
pass | ||||
Nikita Kniazev
|
r27234 | except OSError: | ||
Bradley M. Froehle
|
r7520 | pass | ||
Nikita Kniazev
|
r27234 | |||
Matthias Bussonnier
|
r28591 | return fname | ||
Fernando Perez
|
r7290 | |||
def 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 | ||||
Matthias Bussonnier
|
r27289 | The line number where the object definition starts. | ||
Fernando Perez
|
r7290 | """ | ||
Min RK
|
r21505 | obj = _get_wrapped(obj) | ||
Chris Mentzel
|
r24820 | |||
Fernando Perez
|
r7290 | try: | ||
Nikita Kniazev
|
r27234 | lineno = inspect.getsourcelines(obj)[1] | ||
except TypeError: | ||||
# For instances, try the class object like getsource() does | ||||
Fernando Perez
|
r7290 | try: | ||
Nikita Kniazev
|
r27233 | lineno = inspect.getsourcelines(obj.__class__)[1] | ||
Nikita Kniazev
|
r27234 | except (OSError, TypeError): | ||
return None | ||||
except OSError: | ||||
Fernando Perez
|
r7290 | return None | ||
return lineno | ||||
Matthias Bussonnier
|
r22109 | class Inspector(Colorable): | ||
Sylvain Corlay
|
r22460 | |||
Matthias Bussonnier
|
r28591 | mime_hooks = traitlets.Dict( | ||
config=True, | ||||
help="dictionary of mime to callable to add informations into help mimebundle dict", | ||||
).tag(config=True) | ||||
def __init__( | ||||
self, | ||||
color_table=InspectColors, | ||||
code_color_table=PyColorize.ANSICodeColors, | ||||
scheme=None, | ||||
str_detail_level=0, | ||||
parent=None, | ||||
config=None, | ||||
): | ||||
Matthias Bussonnier
|
r22109 | super(Inspector, self).__init__(parent=parent, config=config) | ||
Ville M. Vainio
|
r1032 | self.color_table = color_table | ||
Matthias Bussonnier
|
r22109 | self.parser = PyColorize.Parser(out='str', parent=self, style=scheme) | ||
Ville M. Vainio
|
r1032 | self.format = self.parser.format | ||
self.str_detail_level = str_detail_level | ||||
self.set_active_scheme(scheme) | ||||
Matthias Bussonnier
|
r25333 | def _getdef(self,obj,oname='') -> Union[str,None]: | ||
Bradley M. Froehle
|
r8707 | """Return the call signature for any callable object. | ||
Ville M. Vainio
|
r1032 | |||
If any exception is generated, None is returned instead and the | ||||
exception is suppressed.""" | ||||
Zoltan Farkas
|
r28463 | if not callable(obj): | ||
Zoltan Farkas
|
r28464 | return None | ||
Ville M. Vainio
|
r1032 | try: | ||
Matthias Bussonnier
|
r25333 | return _render_signature(signature(obj), oname) | ||
Ville M. Vainio
|
r1032 | except: | ||
return None | ||||
Bernardo B. Marques
|
r4872 | |||
Matthias Bussonnier
|
r25333 | def __head(self,h) -> str: | ||
Ville M. Vainio
|
r1032 | """Return a header string with proper colors.""" | ||
return '%s%s%s' % (self.color_table.active_colors.header,h, | ||||
self.color_table.active_colors.normal) | ||||
Fernando Perez
|
r7290 | def set_active_scheme(self, scheme): | ||
michaelpacer
|
r23425 | if scheme is not None: | ||
self.color_table.set_active_scheme(scheme) | ||||
self.parser.color_table.set_active_scheme(scheme) | ||||
Bernardo B. Marques
|
r4872 | |||
Fernando Perez
|
r7290 | def noinfo(self, msg, oname): | ||
Ville M. Vainio
|
r1032 | """Generic message when no information is found.""" | ||
Matthias BUSSONNIER
|
r7817 | print('No %s found' % msg, end=' ') | ||
Ville M. Vainio
|
r1032 | if oname: | ||
Matthias BUSSONNIER
|
r7817 | print('for %s' % oname) | ||
Ville M. Vainio
|
r1032 | else: | ||
Matthias BUSSONNIER
|
r7817 | print() | ||
Bernardo B. Marques
|
r4872 | |||
Fernando Perez
|
r7290 | def pdef(self, obj, oname=''): | ||
Bradley M. Froehle
|
r8707 | """Print the call signature for any callable object. | ||
Ville M. Vainio
|
r1032 | |||
If the object is a class, print the constructor information.""" | ||||
if not callable(obj): | ||||
Matthias BUSSONNIER
|
r7817 | print('Object is not callable.') | ||
Ville M. Vainio
|
r1032 | return | ||
header = '' | ||||
if inspect.isclass(obj): | ||||
header = self.__head('Class constructor information:\n') | ||||
Srinivas Reddy Thatiparthy
|
r23087 | |||
Ville M. Vainio
|
r1032 | |||
Fernando Perez
|
r2929 | output = self._getdef(obj,oname) | ||
Ville M. Vainio
|
r1032 | if output is None: | ||
self.noinfo('definition header',oname) | ||||
else: | ||||
Thomas Kluyver
|
r22192 | print(header,self.format(output), end=' ') | ||
Ville M. Vainio
|
r1032 | |||
Paul Ivanov
|
r22961 | # In Python 3, all classes are new-style, so they all have __init__. | ||
@skip_doctest | ||||
def pdoc(self, obj, oname='', formatter=None): | ||||
"""Print the docstring for any object. | ||||
Optional: | ||||
-formatter: a function to run the docstring through for specially | ||||
formatted docstrings. | ||||
Examples | ||||
-------- | ||||
In [1]: class NoInit: | ||||
...: pass | ||||
In [2]: class NoDoc: | ||||
...: def __init__(self): | ||||
...: pass | ||||
In [3]: %pdoc NoDoc | ||||
No documentation found for NoDoc | ||||
In [4]: %pdoc NoInit | ||||
No documentation found for NoInit | ||||
In [5]: obj = NoInit() | ||||
In [6]: %pdoc obj | ||||
No documentation found for obj | ||||
In [5]: obj2 = NoDoc() | ||||
In [6]: %pdoc obj2 | ||||
No documentation found for obj2 | ||||
""" | ||||
head = self.__head # For convenience | ||||
lines = [] | ||||
ds = getdoc(obj) | ||||
if formatter: | ||||
ds = formatter(ds).get('plain/text', ds) | ||||
if ds: | ||||
lines.append(head("Class docstring:")) | ||||
lines.append(indent(ds)) | ||||
if inspect.isclass(obj) and hasattr(obj, '__init__'): | ||||
init_ds = getdoc(obj.__init__) | ||||
if init_ds is not None: | ||||
lines.append(head("Init docstring:")) | ||||
lines.append(indent(init_ds)) | ||||
elif hasattr(obj,'__call__'): | ||||
call_ds = getdoc(obj.__call__) | ||||
if call_ds: | ||||
lines.append(head("Call docstring:")) | ||||
lines.append(indent(call_ds)) | ||||
if not lines: | ||||
self.noinfo('documentation',oname) | ||||
else: | ||||
page.page('\n'.join(lines)) | ||||
immerrr
|
r17023 | def psource(self, obj, oname=''): | ||
Ville M. Vainio
|
r1032 | """Print the source code for an object.""" | ||
# Flush the source cache because inspect can return out-of-date source | ||||
linecache.checkcache() | ||||
try: | ||||
immerrr
|
r17023 | src = getsource(obj, oname=oname) | ||
except Exception: | ||||
src = None | ||||
if src is None: | ||||
self.noinfo('source', oname) | ||||
Ville M. Vainio
|
r1032 | else: | ||
jstenar
|
r8312 | page.page(self.format(src)) | ||
Ville M. Vainio
|
r1032 | |||
Fernando Perez
|
r7290 | def pfile(self, obj, oname=''): | ||
Ville M. Vainio
|
r1032 | """Show the whole file where an object was defined.""" | ||
Sylvain Corlay
|
r22460 | |||
Fernando Perez
|
r7290 | lineno = find_source_lines(obj) | ||
if lineno is None: | ||||
self.noinfo('file', oname) | ||||
Fernando Perez
|
r1228 | return | ||
Fernando Perez
|
r7290 | ofile = find_file(obj) | ||
# run contents of file through pager starting at line where the object | ||||
# is defined, as long as the file isn't binary and is actually on the | ||||
# filesystem. | ||||
Matthias Bussonnier
|
r28662 | if ofile is None: | ||
print("Could not find file for object") | ||||
elif ofile.endswith((".so", ".dll", ".pyd")): | ||||
print("File %r is binary, not printing." % ofile) | ||||
Fernando Perez
|
r1228 | elif not os.path.isfile(ofile): | ||
Matthias BUSSONNIER
|
r7817 | print('File %r does not exist, not printing.' % ofile) | ||
Ville M. Vainio
|
r1032 | else: | ||
Fernando Perez
|
r1228 | # Print only text files, not extension binaries. Note that | ||
# getsourcelines returns lineno with 1-offset and page() uses | ||||
# 0-offset, so we must adjust. | ||||
Jörgen Stenarson
|
r8304 | page.page(self.format(openpy.read_py_file(ofile, skip_encoding_cookie=False)), lineno - 1) | ||
Bernardo B. Marques
|
r4872 | |||
Matthias Bussonnier
|
r25338 | |||
def _mime_format(self, text:str, formatter=None) -> dict: | ||||
Sylvain Corlay
|
r22460 | """Return a mime bundle representation of the input text. | ||
Sylvain Corlay
|
r22459 | |||
Sylvain Corlay
|
r22460 | - if `formatter` is None, the returned mime bundle has | ||
Matthias Bussonnier
|
r27623 | a ``text/plain`` field, with the input text. | ||
a ``text/html`` field with a ``<pre>`` tag containing the input text. | ||||
Sylvain Corlay
|
r22460 | |||
Matthias Bussonnier
|
r27623 | - if ``formatter`` is not None, it must be a callable transforming the | ||
input text into a mime bundle. Default values for ``text/plain`` and | ||||
``text/html`` representations are the ones described above. | ||||
Sylvain Corlay
|
r22460 | |||
Note: | ||||
Formatters returning strings are supported but this behavior is deprecated. | ||||
""" | ||||
defaults = { | ||||
Jason Grout
|
r27945 | "text/plain": text, | ||
"text/html": f"<pre>{html.escape(text)}</pre>", | ||||
Sylvain Corlay
|
r22460 | } | ||
if formatter is None: | ||||
return defaults | ||||
else: | ||||
formatted = formatter(text) | ||||
if not isinstance(formatted, dict): | ||||
# Handle the deprecated behavior of a formatter returning | ||||
# a string instead of a mime bundle. | ||||
Jason Grout
|
r27945 | return {"text/plain": formatted, "text/html": f"<pre>{formatted}</pre>"} | ||
Sylvain Corlay
|
r22460 | |||
else: | ||||
return dict(defaults, **formatted) | ||||
Matthias Bussonnier
|
r28195 | def format_mime(self, bundle: UnformattedBundle) -> Bundle: | ||
Jason Grout
|
r27936 | """Format a mimebundle being created by _make_info_unformatted into a real mimebundle""" | ||
Jason Grout
|
r27937 | # Format text/plain mimetype | ||
Matthias Bussonnier
|
r28195 | assert isinstance(bundle["text/plain"], list) | ||
for item in bundle["text/plain"]: | ||||
assert isinstance(item, tuple) | ||||
Matthias Bussonnier
|
r22537 | |||
Matthias Bussonnier
|
r28195 | new_b: Bundle = {} | ||
lines = [] | ||||
_len = max(len(h) for h, _ in bundle["text/plain"]) | ||||
Matthias Bussonnier
|
r22537 | |||
Matthias Bussonnier
|
r28195 | for head, body in bundle["text/plain"]: | ||
body = body.strip("\n") | ||||
delim = "\n" if "\n" in body else " " | ||||
lines.append( | ||||
f"{self.__head(head+':')}{(_len - len(head))*' '}{delim}{body}" | ||||
Jason Grout
|
r27938 | ) | ||
Matthias Bussonnier
|
r28195 | |||
new_b["text/plain"] = "\n".join(lines) | ||||
if "text/html" in bundle: | ||||
assert isinstance(bundle["text/html"], list) | ||||
for item in bundle["text/html"]: | ||||
assert isinstance(item, tuple) | ||||
# Format the text/html mimetype | ||||
if isinstance(bundle["text/html"], (list, tuple)): | ||||
# bundle['text/html'] is a list of (head, formatted body) pairs | ||||
new_b["text/html"] = "\n".join( | ||||
(f"<h1>{head}</h1>\n{body}" for (head, body) in bundle["text/html"]) | ||||
) | ||||
for k in bundle.keys(): | ||||
if k in ("text/html", "text/plain"): | ||||
continue | ||||
else: | ||||
Matthias Bussonnier
|
r28593 | new_b[k] = bundle[k] # type:ignore | ||
Matthias Bussonnier
|
r28195 | return new_b | ||
Matthias Bussonnier
|
r22537 | |||
Jason Grout
|
r27938 | def _append_info_field( | ||
Matthias Bussonnier
|
r28195 | self, | ||
bundle: UnformattedBundle, | ||||
title: str, | ||||
key: str, | ||||
info, | ||||
Matthias Bussonnier
|
r28662 | omit_sections: List[str], | ||
Matthias Bussonnier
|
r28195 | formatter, | ||
Jason Grout
|
r27938 | ): | ||
Jason Grout
|
r27936 | """Append an info value to the unformatted mimebundle being constructed by _make_info_unformatted""" | ||
if title in omit_sections or key in omit_sections: | ||||
return | ||||
field = info[key] | ||||
if field is not None: | ||||
formatted_field = self._mime_format(field, formatter) | ||||
Jason Grout
|
r27938 | bundle["text/plain"].append((title, formatted_field["text/plain"])) | ||
bundle["text/html"].append((title, formatted_field["text/html"])) | ||||
Jason Grout
|
r27936 | |||
Matthias Bussonnier
|
r28195 | def _make_info_unformatted( | ||
self, obj, info, formatter, detail_level, omit_sections | ||||
) -> UnformattedBundle: | ||||
Jason Grout
|
r27936 | """Assemble the mimebundle as unformatted lists of information""" | ||
Matthias Bussonnier
|
r28195 | bundle: UnformattedBundle = { | ||
Jason Grout
|
r27938 | "text/plain": [], | ||
"text/html": [], | ||||
Sylvain Corlay
|
r22460 | } | ||
Jason Grout
|
r27936 | # A convenience function to simplify calls below | ||
Matthias Bussonnier
|
r28195 | def append_field( | ||
bundle: UnformattedBundle, title: str, key: str, formatter=None | ||||
): | ||||
Jason Grout
|
r27938 | self._append_info_field( | ||
bundle, | ||||
title=title, | ||||
key=key, | ||||
info=info, | ||||
omit_sections=omit_sections, | ||||
formatter=formatter, | ||||
) | ||||
Sylvain Corlay
|
r22460 | |||
Matthias Bussonnier
|
r28195 | def code_formatter(text) -> Bundle: | ||
Sylvain Corlay
|
r22460 | return { | ||
'text/plain': self.format(text), | ||||
Matthias Bussonnier
|
r22469 | 'text/html': pylight(text) | ||
Sylvain Corlay
|
r22460 | } | ||
Thomas Kluyver
|
r20568 | |||
Jason Grout
|
r27938 | if info["isalias"]: | ||
append_field(bundle, "Repr", "string_form") | ||||
Thomas Kluyver
|
r20568 | |||
elif info['ismagic']: | ||||
Sylvain Corlay
|
r22460 | if detail_level > 0: | ||
Jason Grout
|
r27938 | append_field(bundle, "Source", "source", code_formatter) | ||
Thomas Kluyver
|
r20697 | else: | ||
Jason Grout
|
r27938 | append_field(bundle, "Docstring", "docstring", formatter) | ||
append_field(bundle, "File", "file") | ||||
Thomas Kluyver
|
r20568 | |||
elif info['isclass'] or is_simple_callable(obj): | ||||
# Functions, methods, classes | ||||
Jason Grout
|
r27938 | append_field(bundle, "Signature", "definition", code_formatter) | ||
append_field(bundle, "Init signature", "init_definition", code_formatter) | ||||
append_field(bundle, "Docstring", "docstring", formatter) | ||||
if detail_level > 0 and info["source"]: | ||||
append_field(bundle, "Source", "source", code_formatter) | ||||
Thomas Kluyver
|
r20568 | else: | ||
Jason Grout
|
r27938 | append_field(bundle, "Init docstring", "init_docstring", formatter) | ||
Thomas Kluyver
|
r20568 | |||
Jason Grout
|
r27938 | append_field(bundle, "File", "file") | ||
append_field(bundle, "Type", "type_name") | ||||
append_field(bundle, "Subclasses", "subclasses") | ||||
Thomas Kluyver
|
r20568 | |||
Ville M. Vainio
|
r1032 | else: | ||
Thomas Kluyver
|
r20568 | # General Python objects | ||
Jason Grout
|
r27938 | append_field(bundle, "Signature", "definition", code_formatter) | ||
append_field(bundle, "Call signature", "call_def", code_formatter) | ||||
append_field(bundle, "Type", "type_name") | ||||
append_field(bundle, "String form", "string_form") | ||||
Thomas Kluyver
|
r20568 | |||
# Namespace | ||||
Jason Grout
|
r27938 | if info["namespace"] != "Interactive": | ||
append_field(bundle, "Namespace", "namespace") | ||||
Thomas Kluyver
|
r20568 | |||
Jason Grout
|
r27938 | append_field(bundle, "Length", "length") | ||
append_field(bundle, "File", "file") | ||||
Chris Mentzel
|
r24820 | |||
Thomas Kluyver
|
r20568 | # Source or docstring, depending on detail level and whether | ||
# source found. | ||||
Jason Grout
|
r27938 | if detail_level > 0 and info["source"]: | ||
append_field(bundle, "Source", "source", code_formatter) | ||||
Sylvain Corlay
|
r22460 | else: | ||
Jason Grout
|
r27938 | append_field(bundle, "Docstring", "docstring", formatter) | ||
Jason Grout
|
r27936 | |||
Jason Grout
|
r27938 | append_field(bundle, "Class docstring", "class_docstring", formatter) | ||
append_field(bundle, "Init docstring", "init_docstring", formatter) | ||||
append_field(bundle, "Call docstring", "call_docstring", formatter) | ||||
Jason Grout
|
r27936 | return bundle | ||
Matthias Bussonnier
|
r22537 | |||
Chris Mentzel
|
r24820 | |||
Jason Grout
|
r27936 | def _get_info( | ||
Matthias Bussonnier
|
r28195 | self, | ||
obj: Any, | ||||
oname: str = "", | ||||
formatter=None, | ||||
info: Optional[OInfo] = None, | ||||
Matthias Bussonnier
|
r28662 | detail_level: int = 0, | ||
omit_sections: Union[List[str], Tuple[()]] = (), | ||||
Matthias Bussonnier
|
r28195 | ) -> Bundle: | ||
Jason Grout
|
r27936 | """Retrieve an info dict and format it. | ||
Sylvain Corlay
|
r22460 | |||
Jason Grout
|
r27936 | Parameters | ||
---------- | ||||
obj : any | ||||
Object to inspect and return info from | ||||
oname : str (default: ''): | ||||
Name of the variable pointing to `obj`. | ||||
formatter : callable | ||||
info | ||||
already computed information | ||||
detail_level : integer | ||||
Granularity of detail level, if set to 1, give more information. | ||||
Matthias Bussonnier
|
r28662 | omit_sections : list[str] | ||
Jason Grout
|
r27936 | Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`) | ||
""" | ||||
Matthias Bussonnier
|
r28195 | info_dict = self.info(obj, oname=oname, info=info, detail_level=detail_level) | ||
Matthias Bussonnier
|
r28662 | omit_sections = list(omit_sections) | ||
krassowski
|
r28650 | |||
Jason Grout
|
r27938 | bundle = self._make_info_unformatted( | ||
Matthias Bussonnier
|
r28195 | obj, | ||
info_dict, | ||||
formatter, | ||||
detail_level=detail_level, | ||||
omit_sections=omit_sections, | ||||
Jason Grout
|
r27938 | ) | ||
krassowski
|
r28650 | if self.mime_hooks: | ||
hook_data = InspectorHookData( | ||||
obj=obj, | ||||
info=info, | ||||
info_dict=info_dict, | ||||
detail_level=detail_level, | ||||
omit_sections=omit_sections, | ||||
) | ||||
Matthias Bussonnier
|
r28662 | for key, hook in self.mime_hooks.items(): # type:ignore | ||
krassowski
|
r28650 | required_parameters = [ | ||
parameter | ||||
for parameter in inspect.signature(hook).parameters.values() | ||||
if parameter.default != inspect.Parameter.default | ||||
] | ||||
if len(required_parameters) == 1: | ||||
res = hook(hook_data) | ||||
else: | ||||
warnings.warn( | ||||
"MIME hook format changed in IPython 8.22; hooks should now accept" | ||||
" a single parameter (InspectorHookData); support for hooks requiring" | ||||
" two-parameters (obj and info) will be removed in a future version", | ||||
DeprecationWarning, | ||||
stacklevel=2, | ||||
) | ||||
res = hook(obj, info) | ||||
if res is not None: | ||||
bundle[key] = res | ||||
Jason Grout
|
r27936 | return self.format_mime(bundle) | ||
Sylvain Corlay
|
r22460 | |||
Ahmed Fasih
|
r27110 | def pinfo( | ||
self, | ||||
obj, | ||||
oname="", | ||||
formatter=None, | ||||
Matthias Bussonnier
|
r28195 | info: Optional[OInfo] = None, | ||
Ahmed Fasih
|
r27110 | detail_level=0, | ||
enable_html_pager=True, | ||||
Ahmed Fasih
|
r27187 | omit_sections=(), | ||
Ahmed Fasih
|
r27110 | ): | ||
MinRK
|
r16579 | """Show detailed information about an object. | ||
Optional arguments: | ||||
- oname: name of the variable pointing to the object. | ||||
Ville M. Vainio
|
r1032 | |||
Sylvain Corlay
|
r22460 | - formatter: callable (optional) | ||
A special formatter for docstrings. | ||||
The formatter is a callable that takes a string as an input | ||||
and returns either a formatted string or a mime type bundle | ||||
luzpaz
|
r24084 | in the form of a dictionary. | ||
Sylvain Corlay
|
r22460 | |||
Although the support of custom formatter returning a string | ||||
instead of a mime type bundle is deprecated. | ||||
MinRK
|
r16579 | |||
- info: a structure with some information fields which may have been | ||||
precomputed already. | ||||
- detail_level: if set to 1, more information is given. | ||||
Ahmed Fasih
|
r27109 | |||
- omit_sections: set of section keys and titles to omit | ||||
MinRK
|
r16579 | """ | ||
Matthias Bussonnier
|
r28195 | assert info is not None | ||
info_b: Bundle = self._get_info( | ||||
Ahmed Fasih
|
r27110 | obj, oname, formatter, info, detail_level, omit_sections=omit_sections | ||
) | ||||
Sylvain Corlay
|
r22472 | if not enable_html_pager: | ||
Matthias Bussonnier
|
r28195 | del info_b["text/html"] | ||
page.page(info_b) | ||||
Sylvain Corlay
|
r22460 | |||
Matthias Bussonnier
|
r27344 | def _info(self, obj, oname="", info=None, detail_level=0): | ||
""" | ||||
Matthias Bussonnier
|
r27351 | Inspector.info() was likely improperly marked as deprecated | ||
Matthias Bussonnier
|
r27344 | while only a parameter was deprecated. We "un-deprecate" it. | ||
""" | ||||
warnings.warn( | ||||
Matthias Bussonnier
|
r27351 | "The `Inspector.info()` method has been un-deprecated as of 8.0 " | ||
"and the `formatter=` keyword removed. `Inspector._info` is now " | ||||
"an alias, and you can just call `.info()` directly.", | ||||
Matthias Bussonnier
|
r27344 | DeprecationWarning, | ||
stacklevel=2, | ||||
) | ||||
return self.info(obj, oname=oname, info=info, detail_level=detail_level) | ||||
Sylvain Corlay
|
r22460 | |||
krassowski
|
r28650 | def info(self, obj, oname="", info=None, detail_level=0) -> InfoDict: | ||
Fernando Perez
|
r2931 | """Compute a dict with detailed information about an object. | ||
Matthias Bussonnier
|
r23673 | Parameters | ||
Matthias Bussonnier
|
r27289 | ---------- | ||
obj : any | ||||
Matthias Bussonnier
|
r23673 | An object to find information about | ||
Matthias Bussonnier
|
r27289 | oname : str (default: '') | ||
Matthias Bussonnier
|
r23673 | Name of the variable pointing to `obj`. | ||
Matthias Bussonnier
|
r27289 | info : (default: None) | ||
Matthias Bussonnier
|
r23673 | A struct (dict like with attr access) with some information fields | ||
which may have been precomputed already. | ||||
Matthias Bussonnier
|
r27289 | detail_level : int (default:0) | ||
Matthias Bussonnier
|
r23673 | If set to 1, more information is given. | ||
Returns | ||||
Matthias Bussonnier
|
r27289 | ------- | ||
krassowski
|
r28650 | An object info dict with known fields from `info_fields` (see `InfoDict`). | ||
Fernando Perez
|
r2931 | """ | ||
if info is None: | ||||
Matthias Bussonnier
|
r23673 | ismagic = False | ||
isalias = False | ||||
Fernando Perez
|
r2931 | ospace = '' | ||
else: | ||||
ismagic = info.ismagic | ||||
isalias = info.isalias | ||||
ospace = info.namespace | ||||
Fernando Perez
|
r3051 | |||
Fernando Perez
|
r2931 | # Get docstring, special-casing aliases: | ||
Matthias Bussonnier
|
r28195 | att_name = oname.split(".")[-1] | ||
parents_docs = None | ||||
prelude = "" | ||||
Erik Welch
|
r28207 | if info and info.parent is not None and hasattr(info.parent, HOOK_NAME): | ||
Matthias Bussonnier
|
r28195 | parents_docs_dict = getattr(info.parent, HOOK_NAME) | ||
parents_docs = parents_docs_dict.get(att_name, None) | ||||
krassowski
|
r28650 | out: InfoDict = cast( | ||
InfoDict, | ||||
{ | ||||
Matthias Bussonnier
|
r28662 | **{field: None for field in _info_fields}, | ||
krassowski
|
r28650 | **{ | ||
"name": oname, | ||||
"found": True, | ||||
"isalias": isalias, | ||||
"ismagic": ismagic, | ||||
"subclasses": None, | ||||
}, | ||||
}, | ||||
Matthias Bussonnier
|
r28195 | ) | ||
if parents_docs: | ||||
ds = parents_docs | ||||
elif isalias: | ||||
Fernando Perez
|
r2931 | if not callable(obj): | ||
try: | ||||
ds = "Alias to the system command:\n %s" % obj[1] | ||||
except: | ||||
ds = "Alias: " + str(obj) | ||||
else: | ||||
ds = "Alias to " + str(obj) | ||||
if obj.__doc__: | ||||
ds += "\nDocstring:\n" + obj.__doc__ | ||||
else: | ||||
Matthias Bussonnier
|
r28167 | ds_or_None = getdoc(obj) | ||
if ds_or_None is None: | ||||
Fernando Perez
|
r2931 | ds = '<no docstring>' | ||
Matthias Bussonnier
|
r28167 | else: | ||
ds = ds_or_None | ||||
Fernando Perez
|
r2931 | |||
Matthias Bussonnier
|
r28195 | ds = prelude + ds | ||
Fernando Perez
|
r3051 | # store output in a dict, we initialize it here and fill it as we go | ||
Bernardo B. Marques
|
r4872 | |||
Fernando Perez
|
r2931 | string_max = 200 # max size of strings to show (snipped if longer) | ||
Sylvain Corlay
|
r22460 | shalf = int((string_max - 5) / 2) | ||
Fernando Perez
|
r2931 | |||
if ismagic: | ||||
Matthias Bussonnier
|
r23673 | out['type_name'] = 'Magic function' | ||
Fernando Perez
|
r2931 | elif isalias: | ||
Matthias Bussonnier
|
r23673 | out['type_name'] = 'System alias' | ||
Fernando Perez
|
r2931 | else: | ||
Matthias Bussonnier
|
r23673 | out['type_name'] = type(obj).__name__ | ||
Fernando Perez
|
r2931 | |||
try: | ||||
bclass = obj.__class__ | ||||
out['base_class'] = str(bclass) | ||||
Matthias Bussonnier
|
r23673 | except: | ||
pass | ||||
Fernando Perez
|
r2931 | |||
# String form, but snip if too long in ? form (full in ??) | ||||
if detail_level >= self.str_detail_level: | ||||
try: | ||||
ostr = str(obj) | ||||
krassowski
|
r28650 | if not detail_level and len(ostr) > string_max: | ||
Fernando Perez
|
r2931 | ostr = ostr[:shalf] + ' <...> ' + ostr[-shalf:] | ||
krassowski
|
r28650 | # TODO: `'string_form'.expandtabs()` seems wrong, but | ||
# it was (nearly) like this since the first commit ever. | ||||
ostr = ("\n" + " " * len("string_form".expandtabs())).join( | ||||
q.strip() for q in ostr.split("\n") | ||||
) | ||||
out["string_form"] = ostr | ||||
Fernando Perez
|
r2931 | except: | ||
pass | ||||
if ospace: | ||||
out['namespace'] = ospace | ||||
# Length (for strings and lists) | ||||
try: | ||||
out['length'] = str(len(obj)) | ||||
Matthias Bussonnier
|
r23673 | except Exception: | ||
pass | ||||
Fernando Perez
|
r2931 | |||
# Filename where object was defined | ||||
binary_file = False | ||||
Fernando Perez
|
r7290 | fname = find_file(obj) | ||
if fname is None: | ||||
Fernando Perez
|
r2931 | # if anything goes wrong, we don't want to show source, so it's as | ||
# if the file was binary | ||||
binary_file = True | ||||
Fernando Perez
|
r7290 | else: | ||
if fname.endswith(('.so', '.dll', '.pyd')): | ||||
binary_file = True | ||||
elif fname.endswith('<string>'): | ||||
fname = 'Dynamically generated function. No source code available.' | ||||
Thomas Kluyver
|
r20569 | out['file'] = compress_user(fname) | ||
Bernardo B. Marques
|
r4872 | |||
immerrr
|
r17023 | # Original source code for a callable, class or property. | ||
Fernando Perez
|
r2931 | if detail_level: | ||
# Flush the source cache because inspect can return out-of-date | ||||
# source | ||||
linecache.checkcache() | ||||
try: | ||||
immerrr
|
r17023 | if isinstance(obj, property) or not binary_file: | ||
src = getsource(obj, oname) | ||||
if src is not None: | ||||
src = src.rstrip() | ||||
out['source'] = src | ||||
Thomas Kluyver
|
r3856 | except Exception: | ||
Thomas Kluyver
|
r3857 | pass | ||
Bernardo B. Marques
|
r4872 | |||
immerrr
|
r17023 | # Add docstring only if no source is to be shown (avoid repetitions). | ||
Antony Lee
|
r24118 | if ds and not self._source_contains_docstring(out.get('source'), ds): | ||
immerrr
|
r17023 | out['docstring'] = ds | ||
Fernando Perez
|
r2931 | |||
# Constructor docstring for classes | ||||
if inspect.isclass(obj): | ||||
Thomas Kluyver
|
r3856 | out['isclass'] = True | ||
Min RK
|
r22171 | |||
Min RK
|
r22176 | # get the init signature: | ||
Min RK
|
r22171 | try: | ||
init_def = self._getdef(obj, oname) | ||||
except AttributeError: | ||||
init_def = None | ||||
Min RK
|
r22176 | |||
Min RK
|
r22171 | # get the __init__ docstring | ||
Fernando Perez
|
r2931 | try: | ||
Min RK
|
r22171 | obj_init = obj.__init__ | ||
Fernando Perez
|
r2931 | except AttributeError: | ||
Min RK
|
r22176 | init_ds = None | ||
Fernando Perez
|
r2931 | else: | ||
Min RK
|
r22539 | if init_def is None: | ||
# Get signature from init if top-level sig failed. | ||||
# Can happen for built-in types (list, etc.). | ||||
try: | ||||
init_def = self._getdef(obj_init, oname) | ||||
except AttributeError: | ||||
pass | ||||
Min RK
|
r22176 | init_ds = getdoc(obj_init) | ||
Fernando Perez
|
r2931 | # Skip Python's auto-generated docstrings | ||
MinRK
|
r14835 | if init_ds == _object_init_docstring: | ||
Fernando Perez
|
r2931 | init_ds = None | ||
Min RK
|
r22539 | if init_def: | ||
out['init_definition'] = init_def | ||||
Min RK
|
r22171 | if init_ds: | ||
out['init_docstring'] = init_ds | ||||
Fernando Perez
|
r3051 | |||
Matthias Bussonnier
|
r24946 | names = [sub.__name__ for sub in type.__subclasses__(obj)] | ||
Matthias Bussonnier
|
r24858 | if len(names) < 10: | ||
all_names = ', '.join(names) | ||||
else: | ||||
all_names = ', '.join(names[:10]+['...']) | ||||
Chris Mentzel
|
r24820 | out['subclasses'] = all_names | ||
Fernando Perez
|
r2931 | # and class docstring for instances: | ||
Thomas Kluyver
|
r3856 | else: | ||
MinRK
|
r15711 | # reconstruct the function definition and print it: | ||
defln = self._getdef(obj, oname) | ||||
if defln: | ||||
Sylvain Corlay
|
r22460 | out['definition'] = defln | ||
MinRK
|
r15711 | |||
Fernando Perez
|
r2931 | # First, check whether the instance docstring is identical to the | ||
# class one, and print it separately if they don't coincide. In | ||||
# most cases they will, but it's nice to print all the info for | ||||
# objects which use instance-customized docstrings. | ||||
if ds: | ||||
try: | ||||
cls = getattr(obj,'__class__') | ||||
except: | ||||
class_ds = None | ||||
else: | ||||
class_ds = getdoc(cls) | ||||
# Skip Python's auto-generated docstrings | ||||
MinRK
|
r14835 | if class_ds in _builtin_type_docstrings: | ||
Fernando Perez
|
r2931 | class_ds = None | ||
if class_ds and ds != class_ds: | ||||
Fernando Perez
|
r3051 | out['class_docstring'] = class_ds | ||
Fernando Perez
|
r2931 | |||
# Next, try to show constructor docstrings | ||||
try: | ||||
init_ds = getdoc(obj.__init__) | ||||
# Skip Python's auto-generated docstrings | ||||
MinRK
|
r14835 | if init_ds == _object_init_docstring: | ||
Fernando Perez
|
r2931 | init_ds = None | ||
except AttributeError: | ||||
init_ds = None | ||||
if init_ds: | ||||
Fernando Perez
|
r3051 | out['init_docstring'] = init_ds | ||
Fernando Perez
|
r2931 | |||
# Call form docstring for callable instances | ||||
Thomas Kluyver
|
r15362 | if safe_hasattr(obj, '__call__') and not is_simple_callable(obj): | ||
Fernando Perez
|
r3051 | call_def = self._getdef(obj.__call__, oname) | ||
Matthias Bussonnier
|
r22465 | if call_def and (call_def != out.get('definition')): | ||
MinRK
|
r15711 | # it may never be the case that call def and definition differ, | ||
# but don't include the same signature twice | ||||
Matthias Bussonnier
|
r22465 | out['call_def'] = call_def | ||
Fernando Perez
|
r2931 | call_ds = getdoc(obj.__call__) | ||
# Skip Python's auto-generated docstrings | ||||
MinRK
|
r14835 | if call_ds == _func_call_docstring: | ||
Fernando Perez
|
r2931 | call_ds = None | ||
if call_ds: | ||||
Fernando Perez
|
r3051 | out['call_docstring'] = call_ds | ||
krassowski
|
r28650 | return out | ||
Fernando Perez
|
r2931 | |||
Antony Lee
|
r24118 | @staticmethod | ||
def _source_contains_docstring(src, doc): | ||||
""" | ||||
Check whether the source *src* contains the docstring *doc*. | ||||
This is is helper function to skip displaying the docstring if the | ||||
source already contains it, avoiding repetition of information. | ||||
""" | ||||
try: | ||||
Matthias Bussonnier
|
r28195 | (def_node,) = ast.parse(dedent(src)).body | ||
return ast.get_docstring(def_node) == doc # type: ignore[arg-type] | ||||
Antony Lee
|
r24118 | except Exception: | ||
# The source can become invalid or even non-existent (because it | ||||
# is re-fetched from the source file) so the above code fail in | ||||
# arbitrary ways. | ||||
return False | ||||
Ville M. Vainio
|
r1032 | def psearch(self,pattern,ns_table,ns_search=[], | ||
Matthias Bussonnier
|
r25004 | ignore_case=False,show_all=False, *, list_types=False): | ||
Ville M. Vainio
|
r1032 | """Search namespaces with wildcards for objects. | ||
Arguments: | ||||
- pattern: string containing shell-like wildcards to use in namespace | ||||
Thomas Kluyver
|
r12553 | searches and optionally a type specification to narrow the search to | ||
objects of that type. | ||||
Ville M. Vainio
|
r1032 | |||
- ns_table: dict of name->namespaces for search. | ||||
Optional arguments: | ||||
Bernardo B. Marques
|
r4872 | |||
Ville M. Vainio
|
r1032 | - ns_search: list of namespace names to include in search. | ||
- ignore_case(False): make the search case-insensitive. | ||||
- show_all(False): show all names, including those starting with | ||||
Thomas Kluyver
|
r12553 | underscores. | ||
Matthias Bussonnier
|
r27289 | |||
Andreas
|
r24996 | - list_types(False): list all available object types for object matching. | ||
Ville M. Vainio
|
r1032 | """ | ||
Antony Lee
|
r28756 | # print('ps pattern:<%r>' % pattern) # dbg | ||
Bernardo B. Marques
|
r4872 | |||
Ville M. Vainio
|
r1032 | # defaults | ||
type_pattern = 'all' | ||||
filter = '' | ||||
Andreas
|
r24996 | # list all object types | ||
if list_types: | ||||
page.page('\n'.join(sorted(typestr2type))) | ||||
return | ||||
Ville M. Vainio
|
r1032 | cmds = pattern.split() | ||
len_cmds = len(cmds) | ||||
if len_cmds == 1: | ||||
# Only filter pattern given | ||||
filter = cmds[0] | ||||
elif len_cmds == 2: | ||||
# Both filter and type specified | ||||
filter,type_pattern = cmds | ||||
else: | ||||
raise ValueError('invalid argument string for psearch: <%s>' % | ||||
pattern) | ||||
# filter search namespaces | ||||
for name in ns_search: | ||||
if name not in ns_table: | ||||
raise ValueError('invalid namespace <%s>. Valid names: %s' % | ||||
(name,ns_table.keys())) | ||||
Antony Lee
|
r28756 | # print('type_pattern:',type_pattern) # dbg | ||
Thomas Kluyver
|
r5550 | search_result, namespaces_seen = set(), set() | ||
Ville M. Vainio
|
r1032 | for ns_name in ns_search: | ||
ns = ns_table[ns_name] | ||||
Thomas Kluyver
|
r5550 | # Normally, locals and globals are the same, so we just check one. | ||
if id(ns) in namespaces_seen: | ||||
continue | ||||
namespaces_seen.add(id(ns)) | ||||
tmp_res = list_namespace(ns, type_pattern, filter, | ||||
ignore_case=ignore_case, show_all=show_all) | ||||
search_result.update(tmp_res) | ||||
page.page('\n'.join(sorted(search_result))) | ||||
Philipp A
|
r24841 | |||
Matthias Bussonnier
|
r25333 | def _render_signature(obj_signature, obj_name) -> str: | ||
Philipp A
|
r24847 | """ | ||
This was mostly taken from inspect.Signature.__str__. | ||||
Look there for the comments. | ||||
The only change is to add linebreaks when this gets too long. | ||||
""" | ||||
result = [] | ||||
pos_only = False | ||||
kw_only = True | ||||
for param in obj_signature.parameters.values(): | ||||
Matthias Bussonnier
|
r28167 | if param.kind == inspect.Parameter.POSITIONAL_ONLY: | ||
Philipp A
|
r24847 | pos_only = True | ||
elif pos_only: | ||||
Philipp A
|
r24841 | result.append('/') | ||
Philipp A
|
r24847 | pos_only = False | ||
Philipp A
|
r24841 | |||
Matthias Bussonnier
|
r28167 | if param.kind == inspect.Parameter.VAR_POSITIONAL: | ||
Philipp A
|
r24847 | kw_only = False | ||
Matthias Bussonnier
|
r28167 | elif param.kind == inspect.Parameter.KEYWORD_ONLY and kw_only: | ||
Philipp A
|
r24847 | result.append('*') | ||
kw_only = False | ||||
result.append(str(param)) | ||||
if pos_only: | ||||
result.append('/') | ||||
# add up name, parameters, braces (2), and commas | ||||
if len(obj_name) + sum(len(r) + 2 for r in result) > 75: | ||||
# This doesn’t fit behind “Signature: ” in an inspect window. | ||||
Philipp A
|
r24881 | rendered = '{}(\n{})'.format(obj_name, ''.join( | ||
' {},\n'.format(r) for r in result) | ||||
) | ||||
Philipp A
|
r24847 | else: | ||
rendered = '{}({})'.format(obj_name, ', '.join(result)) | ||||
Philipp A
|
r24841 | |||
Philipp A
|
r24848 | if obj_signature.return_annotation is not inspect._empty: | ||
anno = inspect.formatannotation(obj_signature.return_annotation) | ||||
Philipp A
|
r24847 | rendered += ' -> {}'.format(anno) | ||
Philipp A
|
r24841 | |||
Philipp A
|
r24847 | return rendered | ||