o
    l¨Êhd4  ã                   @   sØ   d Z ddlmZ ddlZddlZddl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	Ze d
e¡Zdd„ Zdd„ Zd"dd„Zdd„ Zdd„ Zdd„ Zdeƒ fdd„Zdd„ Zdd„ Zd#d d!„ZdS )$z>
My own variation on function-specific inspect-like features.
é    )ÚisliceNé   )Ú_basestring)Úpformat)Úopen_py_source)ÚPY3_OR_LATERzAargs varargs varkw defaults kwonlyargs kwonlydefaults annotationsÚFullArgSpecc                 C   s  d}zb| j }|j}tj |¡s8d t | ¡d ¡}d}| d¡r2t	 
d|¡ ¡ \}}t|ƒ}d| }|||fW S t|ƒ�}|j}tt||d dƒƒ}W d  ƒ n1 sTw   Y  d t |¡¡||fW S    t| dƒryt| j  ¡ ƒ|d	f Y S t| ƒ|d	f Y S )
al   Attempts to retrieve a reliable function code hash.

        The reason we don't use inspect.getsource is that it caches the
        source, whereas we want this to be modified on the fly when the
        function is modified.

        Returns
        -------
        func_code: string
            The function code
        source_file: string
            The path to the file in which the function is defined.
        first_line: int
            The first line of the code in the source file.

        Notes
        ------
        This function does a bit more magic than inspect, and is thus
        more robust.
    NÚ r   r   z	<doctest z\<doctest (.*\.rst)\[(.*)\]\>z<doctest %s>Ú__code__éÿÿÿÿ)r
   Úco_filenameÚosÚpathÚexistsÚjoinÚinspectÚgetsourcelinesÚ
startswithÚreÚmatchÚgroupsÚintr   Úco_firstlinenoÚlistr   ÚgetblockÚhasattrÚstrÚ__hash__Úrepr)ÚfuncÚsource_fileÚcodeÚsource_codeÚline_noÚsource_file_objÚ
first_lineÚsource_lines© r'   úE/var/www/html/env/lib/python3.10/site-packages/joblib/func_inspect.pyÚget_func_code   s2   
ÿÿ
ý
r)   c                 C   sH   ddl }t|dƒr|j}nddl}|jj}dD ]
}|  |||ƒ¡} q| S )z2Windows cannot encode some characters in filename.r   NÚquote)ú<ú>ú!ú:ú\)Úurllibr   r*   Úurllib.parseÚparseÚreplace)Ústringr0   r*   Úcharr'   r'   r(   Ú_clean_win_charsR   s   
r6   Tc                 C   sŒ  t | dƒr	| j}nzt | ¡}W n ty%   t | dƒr!| jj}nd}Y nw |du r,d}|dkrmztj t 	| ¡¡}W n   d}Y |durm| 
tj¡}|d  d¡rWd	|d< d
 |¡}| d¡rg|dd… }|d
 | }| 
d¡}t | dƒr{| j}nt | dƒr„| j}nd}|r�t | dƒr�|| jv r�| j| | ur�d| }t | ¡r°t | dƒr°| j}| |j¡ tjdkrÂ|rÂt|ƒ}dd„ |D ƒ}||fS )aâ   Return the function import path (as a list of module names), and
        a name for the function.

        Parameters
        ----------
        func: callable
            The func to inspect
        resolv_alias: boolean, optional
            If true, possible local aliases are indicated.
        win_characters: boolean, optional
            If true, substitute special characters using urllib.quote
            This is useful in Windows, as it cannot encode some filenames
    Ú
__module__Ú	__class__ÚunknownNr	   Ú__main__r   z<ipython-inputz__ipython-input__ú-z.pyéýÿÿÿÚ.Ú	func_nameÚ__name__Úfunc_globalsz%s-aliasÚim_classÚntc                 S   s   g | ]}t |ƒ‘qS r'   )r6   )Ú.0Úsr'   r'   r(   Ú
<listcomp>¡   ó    z!get_func_name.<locals>.<listcomp>)r   r7   r   Ú	getmoduleÚ	TypeErrorr8   r   r   ÚabspathÚgetsourcefileÚsplitÚsepr   r   Úendswithr>   r?   r@   ÚismethodrA   ÚappendÚnamer6   )r   Úresolv_aliasÚwin_charactersÚmoduleÚfilenameÚpartsrP   Úklassr'   r'   r(   Úget_func_name`   sV   


€ü






rW   c                 C   sH   zt  | ¡W S  ty#   t  | ¡}t|j|j|j|jg di d� Y S w )a>  Compatibility function to provide inspect.getfullargspec in Python 2

    This should be rewritten using a backport of Python 3 signature
    once we drop support for Python 2.6. We went for a simpler
    approach at the time of writing because signature uses OrderedDict
    which is not available in Python 2.6.
    N)ÚargsÚvarargsÚvarkwÚdefaultsÚ
kwonlyargsÚkwonlydefaultsÚannotations)	r   ÚgetfullargspecÚAttributeErrorÚ
getargspecÚfull_argspec_typerX   rY   Úkeywordsr[   )r   Úarg_specr'   r'   r(   r_   ¥   s   

úþr_   c                 C   s*   |dt rdnd… }tj|Ž }d | |¡S )z.Helper function to output a function signatureNé   é   z{}{})r   r   ÚformatargspecÚformat)Úfunction_namerd   Úarg_spec_for_formatÚarg_spec_strr'   r'   r(   Ú_signature_strº   s   
rl   c                 C   s:   d}t |ƒdd… }d dd„ | ¡ D ƒ¡}| | ||¡S )z)Helper function to output a function callz{0}({1}, {2})r   r   ú, c                 s   s    � | ]\}}d ||f V  qdS )ú%s=%sNr'   )rC   ÚkÚvr'   r'   r(   Ú	<genexpr>É   s   € ÿz'_function_called_str.<locals>.<genexpr>)r   r   Úitemsrh   )ri   rX   ÚkwargsÚtemplate_strÚargs_strÚ
kwargs_strr'   r'   r(   Ú_function_called_strÄ   s   
ÿÿrw   r'   c                    sH  t |ƒ}t|tƒrtd|t|ƒf ƒ‚t | ¡s-t | ¡s-|r(tj	d|  dd� ||dœS t
| ƒ‰ ˆ jˆ j }ˆ jp;d}ˆ jrM|t‡ fdd„ˆ jD ƒƒ }ˆ j}ˆ j}t | ¡r^| jg| }t| d	d
�\}}	tƒ }
d}t|ƒD ]W\}}|t|ƒk r•|ˆ jvr…|| |
|< qotd|t|	ˆ ƒt|	||ƒf ƒ‚|t|ƒ }||v r¦|| |
|< qoz|| |
|< W qo ttfyÆ   tdt|	ˆ ƒt|	||ƒf ƒ‚w tƒ }t| ¡ ƒD ]\}}||
v rÝ||
|< qÐ|duræ|||< qÐtd|	|f ƒ‚|durö||
d< |du�r||d d… }||
d< |D ]}||
v �r|
 |¡ �q	td|t|	ˆ ƒf ƒ‚|
S )a™   Filters the given args and kwargs using a list of arguments to
        ignore, and a function specification.

        Parameters
        ----------
        func: callable
            Function giving the argument specification
        ignore_lst: list of strings
            List of arguments to ignore (either a name of an argument
            in the function spec, or '*', or '**')
        *args: list
            Positional arguments passed to the function.
        **kwargs: dict
            Keyword arguments passed to the function

        Returns
        -------
        filtered_args: list
            List of filtered positional and keyword arguments.
    zHignore_lst must be a list of parameters to ignore %s (type %s) was givenz4Cannot inspect object %s, ignore list will not work.é   )Ú
stacklevel)Ú*ú**r'   c                 3   s$   � | ]}|ˆ j v rˆ j | V  qd S )N)r]   )rC   ro   ©rd   r'   r(   rq   ô   s   € 
þþzfilter_args.<locals>.<genexpr>F)rQ   r   zZKeyword-only parameter '%s' was passed as positional parameter for %s:
     %s was called.z5Wrong number of arguments for %s:
     %s was called.NzAIgnore list for %s() contains an unexpected keyword argument '%s'r{   r   rz   z9Ignore list: argument '%s' is not defined for function %s)r   Ú
isinstancer   Ú
ValueErrorÚtyper   rN   Ú
isfunctionÚwarningsÚwarnr_   rX   r\   r[   r]   ÚtuplerY   rZ   Ú__self__rW   ÚdictÚ	enumerateÚlenrl   rw   Ú
IndexErrorÚKeyErrorÚsortedrr   rH   Úpop)r   Ú
ignore_lstrX   rs   Ú	arg_namesÚarg_defaultsÚarg_varargsÚ	arg_varkwÚ_rP   Úarg_dictÚarg_positionÚarg_nameÚpositionÚ	varkwargsÚ	arg_valuerY   Úitemr'   r|   r(   Úfilter_argsÏ   sœ   

ÿÿÿÿ


ÿ


þýÿ

ÿþÿþ	

ÿ

ÿþr™   c                 C   s,   t | dd�}t|ƒdkrd|d d…  }|S )Nrx   )ÚindentiÜ  z%s...i¼  )r   r‡   )ÚargÚformatted_argr'   r'   r(   Ú_format_argA  s   r�   c                 O   s¬   t | ƒ\}}dd„ |D ƒ}|r| |¡ d |¡}n|}tƒ }d}|D ]}t|ƒ}	|dkr1d|	 }	t|	ƒ}| |	¡ q#| dd„ | ¡ D ƒ¡ d |¡}d	||f }
||
fS )
Nc                 S   s   g | ]}|r|‘qS r'   r'   )rC   Úmr'   r'   r(   rE   K  rF   z$format_signature.<locals>.<listcomp>r=   r   éP   z
%sc                 S   s    g | ]\}}d |t |ƒf ‘qS )rn   )r�   )rC   rp   Úir'   r'   r(   rE   Y  s     rm   z%s(%s))rW   rO   r   r   r�   r‡   Úextendrr   )r   rX   rs   rS   rP   Úmodule_pathÚarg_strÚprevious_lengthr›   rœ   Ú	signaturer'   r'   r(   Úformat_signatureH  s$   

r¦   ÚMemoryc                 C   s.   t | g|¢R i |¤Ž\}}dd|||f }|S )zi Returns a nicely formatted statement displaying the function
        call with the given arguments.
    z%s
[%s] Calling %s...
%sÚP________________________________________________________________________________)r¦   )r   rX   rs   Úobject_namer   r¥   Úmsgr'   r'   r(   Úformat_call`  s
   ÿr«   )TT)r§   )Ú__doc__Ú	itertoolsr   r   r�   r   r   ÚcollectionsÚ_compatr   Úloggerr   Ú_memory_helpersr   r   Úfull_argspec_fieldsÚ
namedtuplerb   r)   r6   rW   r_   rl   rw   r…   r™   r�   r¦   r«   r'   r'   r'   r(   Ú<module>   s.    8
E
r