/ lib / IPython / core / oinspect.pyc
oinspect.pyc
  1  o

  2  .��ck��@sBdZddgZddlZddlZddlmZddlZddlZddlZddlm	Z	ddl
  3  Z
  4  ddlZddl
mZddlmZdd	lmZdd
  5  lmZddlmZddlmZdd
lmZddlmZddlmZddlmZddlm Z ddl!m"Z"m#Z#m$Z$ddl%m&Z&ddl'm(Z(ddl)m*Z*ddl+m,Z,ddl-m.Z.ddl/m0Z0dd�Z1e
  6  j2j3jZ4e5j6jZ7dd�e
  7  j8e
  8  j9e
  9  j2e:fD�Z;e<e=�Z>e<e?j@�ZAe"ZBejCZDgd�ZEdd�ZFd d!�ZGd"ee?dffd#d$�ZHd:d"ee?dffd&d'�ZId(d)�ZJe*d*d+��ZKe*d,d-��ZLe*d;d/d0��ZMd1d2�ZNd"e?fd3d4�ZOd5d6�ZPGd7d�de(�ZQd"e?fd8d9�ZRdS)<z�Tools for inspecting Python objects.
 10  
 11  Uses syntax highlighting for presenting the various information elements.
 12  
 13  Similar in spirit to the inspect module, but all calls take a name argument to
 14  reference the name under which an object is being read.
 15  �	Inspector�
InspectColors�N)�	signature)�dedent)�Union)�page)�pretty)�skip_doctest)�
 16  PyColorize)�openpy)�safe_hasattr)�
compress_user)�indent)�list_namespace)�typestr2type)�
 17  TermColors�ColorScheme�ColorSchemeTable)�cast_unicode)�	Colorable)�undoc)�	highlight)�PythonLexer)�
HtmlFormattercCst|t�tdd��S)NT)�	noclasses)rrr)�code�r��C:\Users\Jacks.GUTTSPC\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.10_qbz5n2kfra8p0\LocalCache\local-packages\Python310\site-packages\IPython\core\oinspect.py�pylight0srcCsh|]}t�|��qSr)�inspect�getdoc)�.0�trrr�	<setcomp>6s
 18  �r#)�	type_name�
 19  base_class�string_form�	namespace�length�file�
 20  definition�	docstring�source�init_definition�class_docstring�init_docstring�call_def�call_docstring�ismagic�isalias�isclass�found�namecKsdd�tD�}|�|�|S)z1Make an object info dict with all fields present.cSsi|]}|d�qS�Nr)r!�krrr�
 21  <dictcomp>U�zobject_info.<locals>.<dictcomp>)�info_fields�update)�kwZinfodictrrr�object_infoSs
 22  r>cCstt|�}|dur
 23  dS|�d�rdStj�|�sdSt�|d��}t�|j	�\}}Wd�|S1s3wY|S)znGet encoding for python source file defining obj
 24  
 25      Returns None if obj is not defined in a sourcefile.
 26      N�z.soz.dllz.pyd�rb)
 27  �	find_file�endswith�os�path�isfile�	stdlib_io�openr�detect_encoding�readline)�obj�ofile�buffer�encoding�linesrrr�get_encodingZs
 28  
 29  ��rO�returncCsBz|��}Wn	tyYnwt|t�rt�|�St�|�}|S)a=Stable wrapper around inspect.getdoc.
 30  
 31      This can't crash because of attribute problems.
 32  
 33      It also attempts to call a getdoc() method on the given object.  This
 34      allows objects which provide their docstrings via non-standard mechanisms
 35      (like Pyro proxies) to still be inspected by ipython's ? system.
 36      )r �	Exception�
 37  isinstance�strr�cleandoc)rJ�ds�docstrrrrr qs
 38  �
 39  
 40  
 41  r �cCs
 42  t|t�rPg}dD]=}t||�}|durFt|�}|rd|nd}|�d�d||f��t�|�r:|�tt	|���q	|�d||t
 43  |�f�q	|rNd�|�SdSt|�}zt�	|�}W|Sty{z
 44  t�	|j
�}WY|SttfyzYYdSwty�YdSw)alWrapper around inspect.getsource.
 45  
 46      This can be modified by other projects to provide customized source
 47      extraction.
 48  
 49      Parameters
 50      ----------
 51      obj : object
 52          an object whose source code we will attempt to extract
 53      oname : str
 54          (optional) a name under which the object is known
 55  
 56      Returns
 57      -------
 58      src : unicode or None
 59  
 60      )�fget�fset�fdelNz%s.rWz# z
 61  %s%s = %s
 62  �
 63  )rR�property�getattrrO�append�joinr�
 64  isfunctionr�	getsourcer�_get_wrapped�	TypeError�	__class__�OSError)rJ�oname�sources�attrname�fnrMZoname_prefix�srcrrrra�s>
 65  
 66  
 67  ��
 68  ����racCs(t�|�pt�|�pt|t�pt|t�S)zTrue if obj is a function ())rr`�ismethodrR�_builtin_func_type�_builtin_meth_type�rJrrr�is_simple_callable�s
 69  ��rocCs2tjdtdd�t|d�rt|�s|j}t�|�S)z�Wrapper around :func:`inspect.getfullargspec`
 70  
 71      In addition to functions and methods, this can also handle objects with a
 72      ``__call__`` attribute.
 73  
 74      DEPRECATED: Deprecated since 7.10. Do not use, will be removed.
 75      z]`getargspec` function is deprecated as of IPython 7.10and will be removed in future versions.���
 76  stacklevel�__call__)�warnings�warn�DeprecationWarningrrorsr�getfullargspecrnrrr�
 77  getargspec�s
 78  �
 79  rxcCs0tjdtdd�t�|d|d|d|d�S)a
Format argspect, convenience wrapper around inspect's.
 80  
 81      This takes a dict instead of ordered arguments and calls
 82      inspect.format_argspec with the arguments in the necessary order.
 83  
 84      DEPRECATED (since 7.10): Do not use; will be removed in future versions.
 85      za`format_argspec` function is deprecated as of IPython 7.10and will be removed in future versions.rprq�args�varargs�varkw�defaults)rtrurvr�
formatargspec)�argspecrrr�format_argspec�s
 86  ��rTc	Cs�tjdtdd�|�d�}|durd}n*z
 87  |dddk}Wnttfy)Yn
w|r6|dd	d�|d<|d
 88  t|�}|�d�}|durL|�d�}|durV|�d
d�}||fS)z?DEPRECATED since 6.0. Extract call tip data from an oinfo dict.zZ`call_tip` function is deprecated as of IPython 6.0and will be removed in future versions.rprqr~Nryr�self�r6r1r/r+rW)rtrurv�get�KeyError�
 89  IndexErrorr)�oinfoZformat_callr~Z	call_lineZhas_self�docrrr�call_tip�s,�
 90  �
 91  
 92  r�cCs:|}d}t|d�r|j}|d7}|dkr|St|d�s	|S)aNGet the original object if wrapped in one or more @decorators
 93  
 94      Some objects automatically construct similar objects on any unrecognised
 95      attribute access (e.g. unittest.mock.call). To protect against infinite loops,
 96      this will arbitrarily cut off after 100 levels of obj.__wrapped__
 97      attribute access. --TK, Jan 2016
 98      r�__wrapped__r��d)rr�)rJZorig_obj�irrrrbs
 99  
100  �rbcCs�t|�}d}z
101  t�|�}Wt|�Sty5zt�|j�}Wnttfy*YnwYt|�SYt|�Sty@Yt|�Sw)a^Find the absolute path to the file where an object was defined.
102  
103      This is essentially a robust wrapper around `inspect.getabsfile`.
104  
105      Returns None if no file can be found.
106  
107      Parameters
108      ----------
109      obj : any Python object
110  
111      Returns
112      -------
113      fname : str
114          The absolute path to the file where the object was defined.
115      N)rbr�
116  getabsfilercrdrer)rJ�fnamerrrrA"s&������rAcCsrt|�}z
117  t�|�d}W|Sty/zt�|j�d}WY|Sttfy.YYdSwty8YdSw)aWFind the line number in a file where an object was defined.
118  
119      This is essentially a robust wrapper around `inspect.getsourcelines`.
120  
121      Returns None if no file can be found.
122  
123      Parameters
124      ----------
125      obj : any Python object
126  
127      Returns
128      -------
129      lineno : int
130          The line number where the object definition starts.
131      r�N)rbr�getsourcelinesrcrdre)rJ�linenorrr�find_source_linesEs
132  ����r�cseZdZeejddddf�fdd�	Zd,deedffdd�Z	defd	d
133  �Z
134  dd�Zd
d�Zd,dd�Z
ed-dd��Zd,dd�Zd,dd�Zd.dedefdd�Zdd�Z	d/dd�Z						d0d d!�Zd1d"d#�Zd1defd$d%�Zed&d'��Zgd(d(fd(d)�d*d+�Z�ZS)2rNrcsJtt|�j||d�||_tjd||d�|_|jj|_||_|�	|�dS)N)�parent�configrS)�outr��style)
135  �superr�__init__�color_tabler
136  �Parser�parser�format�str_detail_level�set_active_scheme)r�r�Zcode_color_table�schemer�r�r��rdrrr�fs
137  zInspector.__init__rWrPcCsztt|�|�WSYdS)z�Return the call signature for any callable object.
138  
139          If any exception is generated, None is returned instead and the
140          exception is suppressed.N)�_render_signaturer)r�rJrfrrr�_getdefrszInspector._getdefcCsd|jjj||jjjfS)z*Return a header string with proper colors.z%s%s%s)r��
active_colors�header�normal)r��hrrrZ__head|s�zInspector.__headcCs*|dur|j�|�|jj�|�dSdSr7)r�r�r�)r�r�rrrr��s�zInspector.set_active_schemecCs.td|dd�|rtd|�dSt�dS)z-Generic message when no information is found.zNo %s found� ��endzfor %sN)�print)r��msgrfrrr�noinfo�s
141  zInspector.noinfocCsht|�s
142  td�dSd}t�|�r|�d�}|�||�}|dur(|�d|�dSt||�|�dd�dS)zwPrint the call signature for any callable object.
143  
144          If the object is a class, print the constructor information.zObject is not callable.NrWzClass constructor information:
145  zdefinition headerr�r�)�callabler�rr4�_Inspector__headr�r�r�)r�rJrfr��outputrrr�pdef�s
146  
147  zInspector.pdefc	Cs�|j}g}t|�}|r||��d|�}|r#|�|d��|�t|��t�|�rEt|d�rEt|j�}|durD|�|d��|�t|��nt|d�r_t|j	�}|r_|�|d��|�t|��|si|�
148  d|�dSt�d	�|��dS)
149  a�Print the docstring for any object.
150  
151          Optional:
152          -formatter: a function to run the docstring through for specially
153          formatted docstrings.
154  
155          Examples
156          --------
157          In [1]: class NoInit:
158             ...:     pass
159  
160          In [2]: class NoDoc:
161             ...:     def __init__(self):
162             ...:         pass
163  
164          In [3]: %pdoc NoDoc
165          No documentation found for NoDoc
166  
167          In [4]: %pdoc NoInit
168          No documentation found for NoInit
169  
170          In [5]: obj = NoInit()
171  
172          In [6]: %pdoc obj
173          No documentation found for obj
174  
175          In [5]: obj2 = NoDoc()
176  
177          In [6]: %pdoc obj2
178          No documentation found for obj2
179          z
180  plain/textzClass docstring:r�NzInit docstring:rszCall docstring:�
documentationr[)
r�r r�r^rrr4�hasattrr�rsr�rr_)	r�rJrf�	formatter�headrNrU�init_ds�call_dsrrr�pdoc�s,"
181  �
182  
183  zInspector.pdoccCs\t��zt||d�}Wntyd}Ynw|dur$|�d|�dSt�|�|��dS)z$Print the source code for an object.)rfNr,)�	linecache�
184  checkcacherarQr�rr�)r�rJrfrjrrr�psource�s�zInspector.psourcecCs�t|�}|dur|�d|�dSt|�}|�d�r!td|�dStj�|�s/td|�dSt�|�	t
185  j|dd��|d�dS)	z0Show the whole file where an object was defined.Nr)r?z File %r is binary, not printing.z%File %r does not exist, not printing.F)�skip_encoding_cookier�)r�r�rArBr�rCrDrErr�r�read_py_file)r�rJrfr�rKrrr�pfile�s
186  $zInspector.pfile�textcCsR|d|dd�}|dur|S||�}t|t�s!|d|dd�St|fi|��S)aRReturn a mime bundle representation of the input text.
187  
188          - if `formatter` is None, the returned mime bundle has
189             a ``text/plain`` field, with the input text.
190             a ``text/html`` field with a ``<pre>`` tag containing the input text.
191  
192          - if ``formatter`` is not None, it must be a callable transforming the
193            input text into a mime bundle. Default values for ``text/plain`` and
194            ``text/html`` representations are the ones described above.
195  
196          Note:
197  
198          Formatters returning strings are supported but this behavior is deprecated.
199  
200          z<pre>z</pre>��
201  text/plain�	text/htmlN)rR�dict)r�r�r�r|�	formattedrrr�_mime_formats
202  �
203  
204  �zInspector._mime_formatc
205  Cs�|d}d}tt|��\}}tdd�|D��}t||�D](\}}|�d�}d|vr+dnd}	||�|d�|t|�d|	|d7}q||d<|S)Nr�rWcss�|]}t|�VqdSr7��len)r!r�rrr�	<genexpr>0��z(Inspector.format_mime.<locals>.<genexpr>r[r��:)�list�zip�max�stripr�r�)
206  r��bundleZ
207  text_plainr��heads�bodies�_lenr��body�delimrrr�format_mime*s
208  0zInspector.format_mimerc
209  s��j||�|d��gdd�}d-dtdtf���fdd�
}�fd	d
210  �}	�dr-||dd
�nÈdrK|dkr=||dd|	�n||dd|�||dd�n��dsSt|�r�||dd|	�||dd|	�||dd|�|dkrx�drx||dd|	�n||dd|�||dd�||dd�||dd �n^||dd|	�||d!d"|	�||dd�||d#d
��d$d%kr�||d&d$�||d'd(�||dd�|dkrԈdr�||dd|	�n||dd|�||d)d*|�||dd|�||d+d,|���|�S).a9Retrieve an info dict and format it.
211  
212          Parameters
213          ----------
214          obj : any
215              Object to inspect and return info from
216          oname : str (default: ''):
217              Name of the variable pointing to `obj`.
218          formatter : callable
219          info
220              already computed information
221          detail_level : integer
222              Granularity of detail level, if set to 1, give more information.
223          omit_sections : container[str]
224              Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
225          �rf�info�detail_levelrWr�N�title�keycsr|�vs|�vr
226  dS�|}|dur7��||�}|d�||df�|dd|d|dd7<dSdS)Nr�r�z<h1>z</h1>
227  r[)r�r^)r�r�r�r��fieldZformatted_field�r��
omit_sectionsr�rr�append_fieldUs(�z)Inspector._get_info.<locals>.append_fieldcs��|�t|�d�S)Nr�)r�r)r�)r�rr�code_formatter^s�z+Inspector._get_info.<locals>.code_formatterr3�Reprr&r2r�Sourcer,Z	Docstringr+�Filer)r4�	Signaturer*zInit signaturer-zInit docstringr/�Typer$Z
228  Subclasses�
229  subclasseszCall signaturer0zString formr'�Interactive�	Namespace�Lengthr(zClass docstringr.zCall docstringr1r7)r�rSror�)
230  r�rJrfr�r�r�r��_mimer�r�rr�r�	_get_info:sL�	
231  zInspector._get_infoTcCs.|j||||||d�}|s|d=t�|�dS)a Show detailed information about an object.
232  
233          Optional arguments:
234  
235          - oname: name of the variable pointing to the object.
236  
237          - formatter: callable (optional)
238                A special formatter for docstrings.
239  
240                The formatter is a callable that takes a string as an input
241                and returns either a formatted string or a mime type bundle
242                in the form of a dictionary.
243  
244                Although the support of custom formatter returning a string
245                instead of a mime type bundle is deprecated.
246  
247          - info: a structure with some information fields which may have been
248            precomputed already.
249  
250          - detail_level: if set to 1, more information is given.
251  
252          - omit_sections: set of section keys and titles to omit
253          )r�r�N)r�r)r�rJrfr�r�r��enable_html_pagerr�rrr�pinfo�s!�zInspector.pinfocCs"tjdtdd�|j||||d�S)z�
254          Inspector.info() was likely improperly marked as deprecated
255          while only a parameter was deprecated. We "un-deprecate" it.
256          z�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.rprqr�)rtrurvr�)r�rJrfr�r�rrr�_info�s�zInspector._infocCs�|durd}d}d}n	|j}|j}|j}|r?t|�s.zd|d}Wn&dt|�}Yndt|�}|jr>|d|j7}n
257  t|�}|durId	}t|d
258  ||dd�}	d}
259  t|
260  d
d�}|rcd|	d<n|rjd|	d<nt	|�j
261  |	d<z|j}t|�|	d<WnY||jkr�z8t|�}
d}|s�t
|
�|
262  kr�|
d|�d|
|d�}
ddt
|����dd�|
�d�D��}
|
|	|<WnY|r�||	d<z
263  tt
|��|	d<Wn	ty�Ynwd}t|�}|dur�d
264  }n|�d�r�d
265  }n|�d�r�d}t|�|	d<|�r.t��zt|t��s|�s"t||�}|du�r|��}||	d<Wn
266  t�y-Ynw|�r?|�|	�d�|��s?||	d <t�|��r�d
267  |	d!<z|�||�}Wnt �y]d}Ynwz|j!}Wnt �yod}Yn$w|du�r�z|�||�}Wn
268  t �y�Ynwt|�}|t"k�r�d}|�r�||	d"<|�r�||	d#<d$d%�t	�#|�D�}t
|�d&k�r�d'�|�}nd'�|dd&�d(g�}||	d)<n�|�||�}|�r�||	d*<|�r�zt$|d+�}Wnd}Ynt|�}|t%v�r�d}|�r�||k�r�||	d,<zt|j!�}|t"k�rd}Wnt �yd}Ynw|�r!||	d#<t&|d-��rUt'|��sU|�|j(|�}|�rB||	�d*�k�rB||	d.<t|j(�}|t)k�rNd}|�rU||	d/<t*d0i|	��S)1a�Compute a dict with detailed information about an object.
269  
270          Parameters
271          ----------
272          obj : any
273              An object to find information about
274          oname : str (default: '')
275              Name of the variable pointing to `obj`.
276          info : (default: None)
277              A struct (dict like with attr access) with some information fields
278              which may have been precomputed already.
279          detail_level : int (default:0)
280              If set to 1, more information is given.
281  
282          Returns
283          -------
284          An object info dict with known fields from `info_fields`. Keys are
285          strings, values are string or None.
286          NFrWz!Alias to the system command:
287    %sr�zAlias: z	Alias to z
288  Docstring:
289  z<no docstring>T)r6r5r3r2r����rpzMagic functionr$zSystem aliasr%r&z <...> r[r�css�|]}|��VqdSr7)r�)r!�qrrrr�r�z!Inspector.info.<locals>.<genexpr>r'r(r?z<string>z9Dynamically generated function. No source code available.r)r,r+r4r-r/cSsg|]}|j�qSr)�__name__)r!�subrrr�
290  <listcomp>ir:z"Inspector.info.<locals>.<listcomp>�
291  �, z...r�r*rdr.rsr0r1r)+r2r3r'r�rS�__doc__r r��int�typer�rdr�r��
292  expandtabsr_�splitrQrArBr
r�r�rRr\ra�rstrip�_source_contains_docstringr�rr4r��AttributeErrorr��_object_init_docstring�__subclasses__r]�_builtin_type_docstringsrrors�_func_call_docstringr>)r�rJrfr�r�r2r3�ospacerUr�Z
293  string_maxZshalfZbclassZostrZstr_head�binary_filer�rjZinit_defZobj_initr��namesZ	all_namesZdefln�clsZclass_dsr0r�rrrr��s�
294  
295  
296  ��
297  
298  
299  
300  ���
301  �
302  �
303  
304  
305  
306  
307  ��
308  
309  zInspector.infocCs8zt�t|��j\}t�|�|kWStyYdSw)z�
310          Check whether the source *src* contains the docstring *doc*.
311  
312          This is is helper function to skip displaying the docstring if the
313          source already contains it, avoiding repetition of information.
314          F)�ast�parserr��
get_docstringrQ)rjr�Zdef_noderrrr��s�z$Inspector._source_contains_docstringF)�
315  list_typescCs�d}d}|rt�d�tt���dS|��}	t|	�}
316  |
317  dkr#|	d}n|
318  dkr,|	\}}ntd|��|D]}||vrDtd	||��f��q4t�t�}}
|D]"}||}t	|�|
vr[qN|
�
319  t	|��t|||||d
320  �}|�|�qNt�d�t|���dS)a�Search namespaces with wildcards for objects.
321  
322          Arguments:
323  
324          - pattern: string containing shell-like wildcards to use in namespace
325            searches and optionally a type specification to narrow the search to
326            objects of that type.
327  
328          - ns_table: dict of name->namespaces for search.
329  
330          Optional arguments:
331  
332            - ns_search: list of namespace names to include in search.
333  
334            - ignore_case(False): make the search case-insensitive.
335  
336            - show_all(False): show all names, including those starting with
337              underscores.
338  
339            - list_types(False): list all available object types for object matching.
340          �allrWr[Nr�rrpz)invalid argument string for psearch: <%s>z'invalid namespace <%s>. Valid names: %s)�ignore_case�show_all)
rr_�sortedrr�r��
341  ValueError�keys�set�id�addrr<)r��pattern�ns_table�	ns_searchrrr�type_pattern�filter�cmdsZlen_cmdsr6�
search_resultZnamespaces_seenZns_name�nsZtmp_resrrr�psearch�s>
342  
343  �
344  ���zInspector.psearch�rW)rWNr7)rWNNrr)rWNNrTr)rWNr)r��
345  __module__�__qualname__rr
346  �ANSICodeColorsr�rrSr�r�r�r�r�r	r�r�r�r�r�r�r�r�r�r��staticmethodr�r�
__classcell__rrr�rrdsJ�
347  
348  
349  9
350  &
351  �a
352353  (T
354  ��cCsg}d}d}|j��D]3}|jtjkrd}n	|r|�d�d}|jtjkr(d}n|jtjkr7|r7|�d�d}|�t|��q|rF|�d�t	|�t
355  dd�|D��dkrdd�|d	�d
356  d�|D���}n	d�|d�|��}|j
tjur�t�|j
�}|d
�|�7}|S)z�
357      This was mostly taken from inspect.Signature.__str__.
358      Look there for the comments.
359      The only change is to add linebreaks when this gets too long.
360      FT�/�*css�|]	}t|�dVqdS)rpNr��r!�rrrrr�s�z$_render_signature.<locals>.<genexpr>�Kz{}(
361  {})rWcss�|]}d�|�VqdS)z    {},
362  N)r�r rrrr�s�
363  
364  �z{}({})r�z -> {})�
365  parameters�values�kindr�_POSITIONAL_ONLYr^�_VAR_POSITIONAL�
_KEYWORD_ONLYrSr��sumr�r_�return_annotation�_empty�formatannotation)Z
obj_signature�obj_name�resultZpos_only�kw_only�param�rendered�annorrrr��s4
366  
367  
368  �r�r)T)Sr��__all__rrrr�rtrC�textwrapr�types�iorF�typingr�IPython.corer�IPython.lib.prettyr�IPython.testing.skipdoctestr	�
IPython.utilsr
369  r�IPython.utils.dir2r�IPython.utils.pathr
�IPython.utils.textr�IPython.utils.wildcardrr�IPython.utils.coloransirrr�IPython.utils.py3compatr�IPython.utils.colorabler�IPython.utils.decoratorsr�pygmentsr�pygments.lexersr�pygments.formattersrr�FunctionTypersr��objectr�r��
370  ModuleType�
371  MethodTyper\r�r�rrlrS�upperrm�Colorsrrr;r>rOr rarorxrr�rbrAr�rr�rrrr�<module>s~
372  ��
373  
374  <
375  
376  %#