o
    ˷e                     @   sN  d Z ddgZddlZddlZddlmZ ddlZddlZddlZddlm	Z	 ddl
Z
ddlZddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddlm Z  ddlm!Z! ddl"m#Z#m$Z$m%Z% ddl&m'Z' ddl(m)Z) ddl*m+Z+ ddl,m-Z- ddl.m/Z/ ddl0m1Z1 dd Z2e
j3j4j Z5e6j7j Z8dd e
j9e
j:e
j3e;fD Z<e=e>Z?e=e@jAZBe#ZCejDZEg dZFdd  ZGd!d" ZHd#ee@df fd$d%ZId;d#ee@df fd'd(ZJd)d* ZKe+d+d, ZLe+d-d. ZMe+d<d0d1ZNd2d3 ZOd#e@fd4d5ZPd6d7 ZQG d8d de)ZRd#e@fd9d:ZSdS )=zTools 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.
	InspectorInspectColors    N)	signature)dedent)Union)page)pretty)skip_doctest)
PyColorize)openpy)	py3compat)safe_hasattr)compress_user)indent)list_namespace)typestr2type)
TermColorsColorSchemeColorSchemeTable)cast_unicode)	Colorable)undoc)	highlight)PythonLexer)HtmlFormatterc                 C   s   t | t tddS )NT)	noclasses)r   r   r   )code r   L/var/www/ideatree/venv/lib/python3.10/site-packages/IPython/core/oinspect.pypylight1   s   r   c                 C   s   h | ]}t |qS r   )inspectgetdoc).0tr   r   r   	<setcomp>7   s    
r$   )	type_name
base_classstring_form	namespacelengthfile
definition	docstringsourceinit_definitionclass_docstringinit_docstringcall_defcall_docstringismagicisaliasisclassfoundnamec                  K   s   dd t D }||  |S )z1Make an object info dict with all fields present.c                 S   s   i | ]}|d qS Nr   )r"   kr   r   r   
<dictcomp>V       zobject_info.<locals>.<dictcomp>)info_fieldsupdate)kwinfodictr   r   r   object_infoT   s   
r@   c                 C   st   t | }|du r
dS |drdS tj|sdS t|d}t|j	\}}W d   |S 1 s3w   Y  |S )znGet encoding for python source file defining obj

    Returns None if obj is not defined in a sourcefile.
    Nz.soz.dllz.pydrb)
	find_fileendswithospathisfile	stdlib_ioopenr   detect_encodingreadline)objofilebufferencodinglinesr   r   r   get_encoding[   s   

rQ   returnc                 C   sB   z|   }W n	 ty   Y nw t|trt|S t | }|S )a=  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.
    )r!   	Exception
isinstancestrr    cleandoc)rL   dsdocstrr   r   r   r!   r   s   



r!    c                 C   s
  t | trPg }dD ]=}t| |}|durFt|}|rd| nd}|dd||f t|r:|tt	| q	|d||t
|f  q	|rNd|S dS t| } zt	| }W |S  ty{   z
t	| j}W Y |S  ttfyz   Y Y dS w  ty   Y dS w )al  Wrapper around inspect.getsource.

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

    Parameters
    ----------
    obj : object
        an object whose source code we will attempt to extract
    oname : str
        (optional) a name under which the object is known

    Returns
    -------
    src : unicode or None

    )fgetfsetfdelNz%s.rY   z# z
%s%s = %s

)rT   propertygetattrrQ   appendjoinr    
isfunctionr   	getsourcer   _get_wrapped	TypeError	__class__OSError)rL   onamesourcesattrnamefnrO   oname_prefixsrcr   r   r   rc      s>   



rc   c                 C   s(   t | pt | pt| tpt| tS )zTrue if obj is a function ())r    rb   ismethodrT   _builtin_func_type_builtin_meth_typerL   r   r   r   is_simple_callable   s
   rr   c                 C   s2   t jdtdd t| drt| s| j} t| S )zWrapper around :func:`inspect.getfullargspec`

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

    DEPRECATED: Deprecated since 7.10. Do not use, will be removed.
    z]`getargspec` function is deprecated as of IPython 7.10and will be removed in future versions.   
stacklevel__call__)warningswarnDeprecationWarningr   rr   rv   r    getfullargspecrq   r   r   r   
getargspec   s   

r{   c                 C   s0   t jdtdd t| d | d | d | d S )a  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.

    DEPRECATED (since 7.10): Do not use; will be removed in future versions.
    za`format_argspec` function is deprecated as of IPython 7.10and will be removed in future versions.rs   rt   argsvarargsvarkwdefaults)rw   rx   ry   r    formatargspec)argspecr   r   r   format_argspec   s   
r   Tc              	   C   s   t jdtdd | d}|du rd}n*z
|d d dk}W n ttfy)   Y nw |r6|d d	d |d< | d
 t| }| d}|du rL| d}|du rV| dd}||fS )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.rs   rt   r   Nr|   r   self   r7   r2   r0   r,   rY   )rw   rx   ry   getKeyError
IndexErrorr   )oinfoformat_callr   	call_linehas_selfdocr   r   r   call_tip   s,   


r   c                 C   s:   | }d}t | dr| j} |d7 }|dkr|S t | ds	| S )aN  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
    r   __wrapped__r   d   )r   r   )rL   orig_objir   r   r   rd     s   

rd   c                 C   s   t | } d}z
t| }W t|S  ty5   zt| j}W n ttfy*   Y nw Y t|S Y t|S  ty@   Y t|S w )a^  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.
    N)rd   r    
getabsfilere   rf   rg   r   )rL   fnamer   r   r   rC   #  s&   rC   c                 C   sr   t | } z
t| d }W |S  ty/   zt| jd }W Y |S  ttfy.   Y Y dS w  ty8   Y dS w )aW  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.
    r   N)rd   r    getsourcelinesre   rf   rg   )rL   linenor   r   r   find_source_linesF  s   
r   c                       s  e Zd Zeejddddf fdd	Zd,deedf fddZ	defd	d
Z
dd Zdd Zd,ddZed-ddZd,ddZd,ddZd.dedefddZdd Z	d/ddZ						d0d d!Zd1d"d#Zd1defd$d%Zed&d' Zg d(d(fd(d)d*d+Z  ZS )2r   Nr   c                    sJ   t t| j||d || _tjd| |d| _| jj| _|| _| 	| d S )N)parentconfigrU   )outr   style)
superr   __init__color_tabler
   Parserparserformatstr_detail_levelset_active_scheme)r   r   code_color_tableschemer   r   r   rf   r   r   r   g  s   
zInspector.__init__rY   rR   c                 C   s   zt t||W S    Y dS )zReturn the call signature for any callable object.

        If any exception is generated, None is returned instead and the
        exception is suppressed.N)_render_signaturer   )r   rL   rh   r   r   r   _getdefs  s   zInspector._getdefc                 C   s   d| j jj|| j jjf S )z*Return a header string with proper colors.z%s%s%s)r   active_colorsheadernormal)r   hr   r   r   __head}  s   zInspector.__headc                 C   s*   |d ur| j | | jj | d S d S r8   )r   r   r   )r   r   r   r   r   r     s   zInspector.set_active_schemec                 C   s.   t d| dd |rt d|  dS t   dS )z-Generic message when no information is found.zNo %s found endzfor %sN)print)r   msgrh   r   r   r   noinfo  s   
zInspector.noinfoc                 C   sh   t |s
td dS d}t|r| d}| ||}|du r(| d| dS t|| |dd dS )zwPrint the call signature for any callable object.

        If the object is a class, print the constructor information.zObject is not callable.NrY   zClass constructor information:
zdefinition headerr   r   )callabler   r    r5   _Inspector__headr   r   r   )r   rL   rh   r   outputr   r   r   pdef  s   

zInspector.pdefc           	      C   s   | j }g }t|}|r||d|}|r#||d |t| t|rEt|drEt|j}|durD||d |t| nt|dr_t|j	}|r_||d |t| |si| 
d| dS td	| dS )
a  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
        z
plain/textzClass docstring:r   NzInit docstring:rv   zCall docstring:documentationr]   )r   r!   r   r`   r   r    r5   hasattrr   rv   r   r   ra   )	r   rL   rh   	formatterheadrP   rW   init_dscall_dsr   r   r   pdoc  s,   "


zInspector.pdocc                 C   s\   t   zt||d}W n ty   d}Y nw |du r$| d| dS t| | dS )z$Print the source code for an object.)rh   Nr-   )	linecache
checkcacherc   rS   r   r   r   )r   rL   rh   rm   r   r   r   psource  s   zInspector.psourcec                 C   s   t |}|du r| d| dS t|}|dr!td|  dS tj|s/td|  dS t| 	t
j|dd|d  dS )	z0Show the whole file where an object was defined.Nr*   rA   z File %r is binary, not printing.z%File %r does not exist, not printing.F)skip_encoding_cookier   )r   r   rC   rD   r   rE   rF   rG   r   r   r   read_py_file)r   rL   rh   r   rM   r   r   r   pfile  s   
$zInspector.pfiletextc                 C   sR   |d| d d}|du r|S ||}t |ts!|d| d dS t|fi |S )aR  Return a mime bundle representation of the input text.

        - if `formatter` is None, the returned mime bundle has
           a ``text/plain`` field, with the input text.
           a ``text/html`` field with a ``<pre>`` tag containing the input text.

        - 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.

        Note:

        Formatters returning strings are supported but this behavior is deprecated.

        z<pre>z</pre>
text/plain	text/htmlN)rT   dict)r   r   r   r   	formattedr   r   r   _mime_format  s   


zInspector._mime_formatc           
      C   s   |d }d}t t| \}}tdd |D }t||D ](\}}|d}d|v r+dnd}	|| |d |t| d  |	 | d 7 }q||d< |S )Nr   rY   c                 s   s    | ]}t |V  qd S r8   len)r"   r   r   r   r   	<genexpr>1      z(Inspector.format_mime.<locals>.<genexpr>r]   r   :)listzipmaxstripr   r   )
r   bundle
text_plainr   headsbodies_lenr   bodydelimr   r   r   format_mime+  s   
0zInspector.format_mimer   c           
         s  j || |d g dd}d-dtdtf fdd}fd	d
}	 d r-||dd nÈ d rK|dkr=||dd|	 n||dd| ||dd n d sSt|r||dd|	 ||dd|	 ||dd| |dkrx d rx||dd|	 n||dd| ||dd ||dd ||dd  n^||dd|	 ||d!d"|	 ||dd ||d#d  d$ d%kr||d&d$ ||d'd( ||dd |dkrԈ d r||dd|	 n||dd| ||d)d*| ||dd| ||d+d,| |S ).a9  Retrieve an info dict and format it.

        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.
        omit_sections : container[str]
            Titles or keys to omit from output (can be set, tuple, etc., anything supporting `in`)
        rh   infodetail_levelrY   r   Ntitlekeyc                    sr   |v s|v r
d S  | }|d ur7 ||}| d ||d f | d  d| d |d  d 7  < d S d S )Nr   r   z<h1>z</h1>
r]   )r   r`   )r   r   r   r   fieldformatted_fieldr   omit_sectionsr   r   r   append_fieldV  s   (z)Inspector._get_info.<locals>.append_fieldc                    s     | t| dS )Nr   )r   r   )r   )r   r   r   code_formatter_  s   z+Inspector._get_info.<locals>.code_formatterr4   Reprr'   r3   r   Sourcer-   	Docstringr,   Filer*   r5   	Signaturer+   zInit signaturer.   zInit docstringr0   Typer%   
Subclasses
subclasseszCall signaturer1   zString formr(   Interactive	NamespaceLengthr)   zClass docstringr/   zCall docstringr2   r8   )r   rU   rr   r   )
r   rL   rh   r   r   r   r   _mimer   r   r   r   r   	_get_info;  sL   	
zInspector._get_infoTc                 C   s.   | j ||||||d}|s|d= t| dS )a   Show detailed information about an object.

        Optional arguments:

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

        - 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
              in the form of a dictionary.

              Although the support of custom formatter returning a string
              instead of a mime type bundle is deprecated.

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

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

        - omit_sections: set of section keys and titles to omit
        )r   r   N)r   r   )r   rL   rh   r   r   r   enable_html_pagerr   r   r   r   pinfo  s   !zInspector.pinfoc                 C   s"   t jdtdd | j||||dS )z
        Inspector.info() was likely improperly marked as deprecated
        while only a parameter was deprecated. We "un-deprecate" it.
        zThe `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.rs   rt   r   )rw   rx   ry   r   )r   rL   rh   r   r   r   r   r   _info  s   zInspector._infoc                 C   s  |du rd}d}d}n	|j }|j}|j}|r?t|s.zd|d  }W n&   dt| }Y ndt| }|jr>|d|j 7 }n
t|}|du rId	}t|d
||dd}	d}
t|
d d }|rcd|	d< n|rjd|	d< nt	|j
|	d< z|j}t||	d< W n   Y || jkrz8t|}d}|st||
kr|d| d || d  }ddt|   dd |dD }||	|< W n   Y |r||	d< z
tt||	d< W n	 ty   Y nw d}t|}|du rd
}n|drd
}n|drd}t||	d< |r.t  zt|ts|s"t||}|dur| }||	d< W n
 ty-   Y nw |r?| |	d|s?||	d < t|rd
|	d!< z| ||}W n t y]   d}Y nw z|j!}W n t yo   d}Y n$w |du rz| ||}W n
 t y   Y nw t|}|t"krd}|r||	d"< |r||	d#< d$d% t	#|D }t|d&k rd'|}nd'|dd& d(g }||	d)< n| ||}|r||	d*< |rzt$|d+}W n   d}Y nt|}|t%v rd}|r||kr||	d,< zt|j!}|t"krd}W n t y   d}Y nw |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.

        Parameters
        ----------
        obj : any
            An object to find information about
        oname : str (default: '')
            Name of the variable pointing to `obj`.
        info : (default: None)
            A struct (dict like with attr access) with some information fields
            which may have been precomputed already.
        detail_level : int (default:0)
            If set to 1, more information is given.

        Returns
        -------
        An object info dict with known fields from `info_fields`. Keys are
        strings, values are string or None.
        NFrY   z!Alias to the system command:
  %sr   zAlias: z	Alias to z
Docstring:
z<no docstring>T)r7   r6   r4   r3   r         rs   zMagic functionr%   zSystem aliasr&   r'   z <...> r]   r   c                 s   s    | ]}|  V  qd S r8   )r   )r"   qr   r   r   r     r   z!Inspector.info.<locals>.<genexpr>r(   r)   rA   z<string>z9Dynamically generated function. No source code available.r*   r-   r,   r5   r.   r0   c                 S   s   g | ]}|j qS r   )__name__)r"   subr   r   r   
<listcomp>j  r;   z"Inspector.info.<locals>.<listcomp>
   , z...r   r+   rf   r/   rv   r1   r2   r   )+r3   r4   r(   r   rU   __doc__r!   r   inttyper   rf   r   r   
expandtabsra   splitrS   rC   rD   r   r   r   rT   r^   rc   rstrip_source_contains_docstringr   r    r5   r   AttributeErrorr   _object_init_docstring__subclasses__r_   _builtin_type_docstringsr   rr   rv   _func_call_docstringr@   )r   rL   rh   r   r   r3   r4   ospacerW   r   
string_maxshalfbclassostrstr_headbinary_filer   rm   init_defobj_initr   names	all_namesdeflnclsclass_dsr1   r   r   r   r   r     s  















zInspector.infoc                 C   s8   zt t| j\}t ||kW S  ty   Y dS w )z
        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.
        F)astparser   r   get_docstringrS   )rm   r   def_noder   r   r   r    s   z$Inspector._source_contains_docstringF)
list_typesc                C   s   d}d}|rt  dtt dS | }	t|	}
|
dkr#|	d }n|
dkr,|	\}}ntd| |D ]}||vrDtd	|| f q4t t }}|D ]"}|| }t	||v r[qN|
t	| t|||||d
}|| qNt  dt| dS )a  Search namespaces with wildcards for objects.

        Arguments:

        - pattern: string containing shell-like wildcards to use in namespace
          searches and optionally a type specification to narrow the search to
          objects of that type.

        - ns_table: dict of name->namespaces for search.

        Optional arguments:

          - 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
            underscores.

          - list_types(False): list all available object types for object matching.
        allrY   r]   Nr   r   rs   z)invalid argument string for psearch: <%s>z'invalid namespace <%s>. Valid names: %s)ignore_caseshow_all)r   ra   sortedr   r  r   
ValueErrorkeyssetidaddr   r=   )r   patternns_table	ns_searchr  r   r  type_patternfiltercmdslen_cmdsr7   search_resultnamespaces_seenns_namenstmp_resr   r   r   psearch  s>   


zInspector.psearchrY   )rY   Nr8   )rY   NNr   r   )rY   NNr   Tr   )rY   Nr   )r   
__module____qualname__r   r
   ANSICodeColorsr   r   rU   r   r   r   r   r   r	   r   r   r   r   r   r   r   r   r   r   staticmethodr  r3  __classcell__r   r   r   r   r   e  sJ    


9
&
a

( T
c                 C   s  g }d}d}| j  D ]3}|jtjkrd}n	|r|d d}|jtjkr(d}n|jtjkr7|r7|d d}|t| q|rF|d t	|t
dd |D  dkrdd|d	d
d |D }n	d|d|}| jtjurt| j}|d|7 }|S )z
    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.
    FT/*c                 s   s    | ]	}t |d  V  qdS )rs   Nr   r"   rr   r   r   r     s    z$_render_signature.<locals>.<genexpr>K   z{}(
{})rY   c                 s   s    | ]}d  |V  qdS )z    {},
N)r   r<  r   r   r   r     s    

z{}({})r   z -> {})
parametersvalueskindr    _POSITIONAL_ONLYr`   _VAR_POSITIONAL_KEYWORD_ONLYrU   r   sumr   ra   return_annotation_emptyformatannotation)obj_signatureobj_nameresultpos_onlykw_onlyparamrenderedannor   r   r   r     s4   


r   r4  )T)Tr   __all__r  r    r   r   rw   rE   textwrapr   typesiorH   typingr   IPython.corer   IPython.lib.prettyr   IPython.testing.skipdoctestr	   IPython.utilsr
   r   r   IPython.utils.dir2r   IPython.utils.pathr   IPython.utils.textr   IPython.utils.wildcardr   r   IPython.utils.coloransir   r   r   IPython.utils.py3compatr   IPython.utils.colorabler   IPython.utils.decoratorsr   pygmentsr   pygments.lexersr   pygments.formattersr   r   FunctionTyperv   r
  objectr   r  
ModuleType
MethodTyper^   r	  r  r  ro   rU   upperrp   Colorsr7  r   r<   r@   rQ   r!   rc   rr   r{   r   r   rd   rC   r   r   r   r   r   r   r   <module>   s   


<

%#     