U
    -eA                     @   s  d Z ddddddddd	d
ddddgZddlZddlZddlmZmZmZmZm	Z	m
Z
mZmZmZmZmZmZmZ ddlmZ ddlmZ dd Zdd Zdd Zdd ZdCddZdDddZdEddZd d! Zd"d# Zd$d% Zd&d' Z dFd)dZ!dGd+d,Z"dHd-dZ#e$ Z%e& Z'd.d Z(dId/d0Z)dJd1dZ*dKd2dZ+d3d	 Z,dLd4d5Z-dMd6d
Z.dNd7dZ/dOd8d9Z0dPd:d;Z1dQd<dZ2dRd=d>Z3dSd?d@Z4dTdAdBZ5e+Z6eZ7dS )Ua;  
Extensions to python's 'inspect' module, which can be used
to retrieve information from live python objects. The methods
defined in this module are augmented to facilitate access to
source code of interactively defined functions and classes,
as well as provide access to source code for objects defined
in a file.

findsourcegetsourcelines	getsourceindentoutdent_wrap
dumpsourcegetname
_namespace	getimport_importable
importable	isdynamic
isfrommain    N)getblockgetfile	getmodulegetsourcefile
indentsize	isbuiltinisclassiscodeisframe
isfunctionismethodismoduleistraceback)
TokenError   )
IS_IPYTHONc                 C   s   t | }|r|jdkrdS dS )z%check if object was built in __main____main__TF)r   __name__)objmodule r$   L/var/www/html/Darija-Ai-Train/env/lib/python3.8/site-packages/dill/source.pyr   #   s    c                 C   s>   zt | }W n tk
r$   d}Y nX |dkr:t| r:dS dS )z,check if object was built in the interpreterN<stdin>TF)r   	TypeErrorr   )r"   filer$   r$   r%   r   +   s      
c                    s  ddl m} ddl m}m}m} dd }ddd dd\}}ztd	||f t t  W n t	k
rx   | Y nX | j
|| j
  }	fd
d||  D s |	krdS dS tsdS || fddd d  D rtsdS fdd||  D r2ts2dS ddkr|||  kr|ddd dd\}
}ztd	|
|f t t W n t	k
r   |Y nX |j
|| j
  }	t t|	krdS tdd t |	D  t dkrdS dS tsdS   d  |	|	d t fddtdt D  tfddtdtD    krt  t krdS dS )z=check if lambda object 'func' matches raw line of code 'line'r   )code)freevars
globalvarsvarnamesc                   S   s   dS )NZ __this_is_a_big_dummy_function__r$   r$   r$   r$   r%   <lambda>8       z_matchlambda.<locals>.<lambda>lambda :zlambda %s : %sc                    s   g | ]}  |qS r$   count.0iliner$   r%   
<listcomp>A   s     z _matchlambda.<locals>.<listcomp>TFc                    s   g | ]}  |qS r$   r2   r4   r7   r$   r%   r9   J   s     r   c                    s   g | ]}  |qS r$   r2   r4   r7   r$   r%   r9   L   s     c                 s   s"   | ]\}}||kr||fV  qd S )Nr$   )r5   r6   jr$   r$   r%   	<genexpr>X   s      z_matchlambda.<locals>.<genexpr>c                 3   s"   | ]}t d  |  V  qdS z([\W\D\S])(.*)Nrematchgroupsr4   )_r$   r%   r;   b   s     c                 3   s"   | ]}t d  |  V  qdS r<   r=   r4   )_fr$   r%   r;   c   s     )detectr)   r*   r+   r,   splitevalglobalslocals	Exceptionco_codekeysallr3   lensetzipr   dictrangesortedvalues)funcr8   getcoder*   r+   r,   dummylhsrhsr)   Z_lhsZ_rhsr$   )rA   rB   r8   r%   _matchlambda4   sZ     
    " 
  
   ,rX   c              	      s  t | }zt|}W n tk
r,   d}Y nX |o@|jdko@| }tr|rzt| }t| }W nV tk
r   t| rtt| j	
 D ](}t|}|dsq|}t|} qqY nX |rt|}n"dt jj}dd | D }nb|rzddl d}	W nL tk
rN   ddl}
|
 d	 jd }	|
jdd
 dkrJ|	d7 }	Y nX |	r^t|	  } fddtd	|D }nz>t| stt| rt|}t|}nt| }t| }W n* ttfk
r   t| }t| }Y nX |s|dd	 |dd  dkrtd|r |n|}t | |}|rDt||j	}n
t|}|s\tdt| rn|dfS d } }}d}t| r| j}|dkrd}nd}| j } t| r| j}|dkrd}| }nd}| j!} t"| r| j#} t$| r| j%} t&| rt'| dstd| j(dk}|r<t)|d	 }|sJd}n| j*d	 }d}t+,|}t+,|}|dkr || }|-|r|sq |dkrt.||rq nR||krd}t|d	 ddD ]&}|-|| r|d	7 }n qq|| }q |d	 }q^||fS z"t| s(tt| r(| j/} W n tk
r@   Y nX t| r| j}t+,d| d }g }tt)|d	 ddD ]L}|-|| }|r||| d dkr||f  S |0|1d	|f q||r|2  ||d d	 fS tdtddS )a  Return the entire source file and starting line number for an object.
    For interactively-defined objects, the 'file' is the interpreter's history.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of all the lines
    in the file and the line number indexes a line in that list.  An IOError
    is raised if the source code cannot be retrieved, while a TypeError is
    raised for objects where the source code is unavailable (e.g. builtins).Nr    z<ipython-input-
c                 S   s   g | ]}|d  qS rY   r$   r5   r8   r$   r$   r%   r9      s     zfindsource.<locals>.<listcomp>r    r      winz, please install 'pyreadline'c                    s   g | ]}  |d  qS rZ   )Zget_history_itemr4   readliner$   r%   r9      s     r0   z<>zsource code not availablezcould not extract source code^(\s*@)<lambda>z(.*(?<!\w)lambda(:|\s))z^(\s*def\s)co_firstlinenoz"could not find function definitionr&   z+^(\s*def\s)|(.*(?<!\w)lambda(:|\s))|^(\s*@)z^(\s*)class\s*z\bczcould not find class definitionzcould not find code object)3r   r   r'   r!   r   r   r   filterr   __dict__rR   
startswith	linecachegetlinesjoinget_ipythonZhistory_managerZinput_hist_parsed
splitlinesr`   ImportErrorsysexc_infoargsplatformIOErrorget_current_history_lengthrP   typeAttributeErrorr   r   __func____code__r   tb_framer   f_coder   hasattrco_filenamerL   rc   r>   compiler?   rX   	__class__appendgroupsort)objectr#   r(   Zis_module_mainZ
sourcefileZobject_methodZfile_candidatelineshistoryerrrn   Zlbufnamepat1r"   pat2stdinlnumr8   Zhats_lnumpat
candidatesr6   r?   r$   r_   r%   r   i   s    
  


$




 





 

 

 
 
 


 
Fc                 C   sH  t | \}}t| r:|r t|}|dkr4|gdgfS |gS t|| }t||d }|r^|s|rjt|}|dkr~|g|gfS |gS d}t|}d}	t|	}	d}
d}g }g }d|}||kr||| r||
szt||d }W n  t	k
r   || g}Y nX t|| |kr4|t
||
 7 }nB|d|krn|| |||
  |t
||
 7 }n|d7 }d}
q|	|| rzt||d }W n  t	k
r   || g}Y nX d}
|dd D ] }|	|s q|
d7 }
q||
7 }q|d7 }d}
q|s|g}|g}|r2dd	 |D }|dkrD||fS |S )
a  Return a list of source lines and starting line number for an object.
    Interactively-defined objects refer to lines in the interpreter's history.

    If enclosing=True, then also return any enclosing code.
    If lstrip=True, ensure there is no indentation in the first line of code.
    If locate=True, then also return the line number for the block of code.

    DEPRECATED: use 'getsourcelines' instead
    Tr   Nz#^(\s*def\s)|(.*(?<!\w)lambda(:|\s))ra   r\   r   c                 S   s   g | ]}t |qS r$   )_outdent)r5   blockr$   r$   r%   r9   G  s     zgetblocks.<locals>.<listcomp>)r   r   r   r   r   r>   r|   rj   r?   r   rL   r~   )r   lstrip	enclosinglocater   r   r   r   r   r   skipr8   blocksr   targetr)   _liner$   r$   r%   	getblocks  sl    
   
 
 

  
   
 r   c                 C   s$   t | ||dd\}}|d |d fS )a  Return a list of source lines and starting line number for an object.
    Interactively-defined objects refer to lines in the interpreter's history.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a list of the lines
    corresponding to the object and the line number indicates where in the
    original source file the first line of code was found.  An IOError is
    raised if the source code cannot be retrieved, while a TypeError is
    raised for objects where the source code is unavailable (e.g. builtins).

    If lstrip=True, ensure there is no indentation in the first line of code.
    If enclosing=True, then also return any enclosing code.T)r   r   r   r0   )r   )r   r   r   r)   nr$   r$   r%   r   L  s    r\   c              	   C   s  t | }t| }zt| |d\}}	W n ttfk
r(   |sB t| s|s`t| ||d Y S t| |d}
t| dd}|rd| nd}||krd}|
| d|   Y S |st| ||d Y S | jj	}| j
}|dkrt| ||d Y S d	||||f gd
 }}	t|d
 |d }t||d\}}	Y nX |s6|r>t|}|rdt| krb|d|   n2t| d|dd}dd |dD dd d
 }}	|r|rbd
}|D ] }|ds q|d7 }q||  dr || j	kr`|d|| j	f  n`d|| kr@||| dd
  kr`d||| f ||< n || j	kr|d|| j	f  nh|r||d dd
  krd| |d  |d< n0t| ddp| j	}||kr|d||f  d|S )a+  Return the text of the source code for an object. The source code for
    interactively-defined objects are extracted from the interpreter's history.

    The argument may be a module, class, method, function, traceback, frame,
    or code object.  The source code is returned as a single string.  An
    IOError is raised if the source code cannot be retrieved, while a
    TypeError is raised for objects where the source code is unavailable
    (e.g. builtins).

    If alias is provided, then add a line of code that renames the object.
    If lstrip=True, ensure there is no indentation in the first line of code.
    If enclosing=True, then also return any enclosing code.
    If force=True, catch (TypeError,IOError) and try to use import hooks.
    If builtin=True, force an import for any builtins
    )r   )builtinTforce%s = r\   %s
builtins__builtin__z*%s = __import__('%s', fromlist=['%s']).%s
r   z = (z%r
F)aliasnewenclosec                 S   s   g | ]}|d  qS rZ   r$   r[   r$   r$   r%   r9     s     zgetsource.<locals>.<listcomp>rY   Nr0   @r   zdef z	
%s = %s
r/   =z%s = %s)_hascode_isinstancer   r'   rr   r   r
   r   r}   r!   
__module__rE   r   r   reprr~   r   rD   rg   striprj   )r   r   r   r   r   r   Zhascodeinstancer   r   _importr   _aliasr#   r"   r   r8   r$   r$   r%   r   ^  sl        " 
c                 C   s   t | ddpt | ddS )z9True if object has an attribute that stores it's __code__rw   N	func_code)getattrr   r$   r$   r%   r     s    r   c                 C   s   t | st| st| rdS t| s4t| s4t| r8dS t| sVtt| jdkrVdS d}t	t| 
|spdS t| r| jdkst| dddkrdS dS )z>True if object is a class instance type (and is not a builtin)F)numpyT)z<class z<type 'instance'>r   r   )array)r   r   r   r   r   r   r   rt   r!   r   rg   r   r   )r   _typesr$   r$   r%   r     s    "r   c                 C   sB   ddl }t| tdk	r$t| dd} | dkr0d} t|| r>dS dS )	z(check if object is in the 'types' moduler   Nr\   Tr   ellipsisEllipsisTypeF)typesrt   r   rz   )r   r   r$   r$   r%   _intypes  s     r   c                 C   s   t | ttfS )z%check if object is a string-like type)
isinstancestrbytesr   r$   r$   r%   	_isstring  s    r      c                 C   s   t | }t|tkrd| }t |}| d}tt|D ]*}t || }||krTq:|||  ||< q:|d  dkr~d|d< d|S )z<indent a block of code with whitespace (default is 4 spaces) rY   r0   r\   )r   rt   intrD   rP   rL   r   rj   )r)   spacesr   Znspacesr   r6   _indentr$   r$   r%   r     s     
  Tc                 C   sx   t | d }|dks$||ks$|dk r(|}t|r6t| ndD ]6}t | | }||krZ|}n|}| | |d | |< q<| S )zAoutdent lines of code, accounting for docs and line continuationsr   Nr   )r   rP   rL   )r   r   rK   r   r6   r   Z_spacesr$   r$   r%   r     s      r   c                 C   sN   t | }|dks ||ks |dk r$|}|s4| |d S dt| d||dS )zDoutdent a block of code (default is to strip all leading whitespace)Nr   rY   )r   rK   )r   rj   r   rD   )r)   r   rK   r   r$   r$   r%   r     s      c                    s     fdd} j |_  j|_|S )z, encapsulate a function and it's __import__ c                     sF   zt t ddtt W n" tk
r:   td j Y nX t| |S )NrA   r   zcannot import name )execgetimportable__globals__
__locals__rH   rm   r!   rA   )rp   kwdsfr$   r%   rS     s
    z_wrap.<locals>.func)r!   __doc__)r   rS   r$   r   r%   r     s    c                 C   s\   d}d}d| }|t t| |ddd7 }|t d| 7 }|rH|d| 7 }|d||f 7 }|S )	z<create a function enclosure around the source of some objectZ*__this_is_a_big_dummy_enclosing_function____this_is_a_stub_variable__z
def %s():
Tr   r   r   z
return %s
r   z%s(); del %s
)r   r   )r   r   rU   stubr)   r$   r$   r%   _enclose%  s     r   c                 C   s   ddl m} t|| }d}|r2d}d| }d}n|}|rBd| n|}|rRt| sd||d|  7 }n4|t| jdd	d	d
7 }t| j}	||d||	f  7 }|rd}
t|
|d}
|
d}
|
d d t	| d
|
dd  }|S )a/  'dump to source', where the code includes a pickled object.

    If new=True and object is a class instance, then create a new
    instance using the unpacked class source code. If enclose, then
    create the object inside a function enclosure (thus minimizing
    any global namespace pollution).
    r   )dumpszimport dill
r   r   Fzdill.loads(%s)
r\   Tr   z4dill.loads(%s.replace(b%s,bytes(__name__,"UTF-8")))
Z__this_is_a_big_dummy_object__r   rY   N)Zdillr   r   r   r   r}   r   r   rD   r   rj   )r   r   r   r   r   Zpikr)   r   premodrU   r$   r$   r%   r   3  s(    

&c                 C   s   |rd t| S t| }|s.|s&dS t| S zD| j}|dkrXt| ddd  W S |jdkrn|dkrnd	}|W S  tk
r   |sY dS t| }|	d
rY dS | Y S X dS )zEget the name of the object. for lambdas, get the name of the pointer .Nrb   r   r   r   r   r   r   <)
rj   r	   r   r   r!   r   rD   r   ru   rg   )r"   r   Zfqnr#   r   r$   r$   r%   r   Z  s,      
  
c                 C   s>  zzt t|  d ddd }}|d}t| rD|W S t| pP| j}|dkrpt|rpdg|g W S ||g W S  tk
r   Y nX t | dkrd	gt | g S t	| j
d
d}t | j
}z||dd d }W n tk
r   Y nX |d}|dkr:|d dkrd|d< t|d r0d}|g| }|S )a  _namespace(obj); return namespace hierarchy (as a list of names)
    for the given object.  For an instance, find the class hierarchy.

    For example:

    >>> from functools import partial
    >>> p = partial(int, base=2)
    >>> _namespace(p)
    ['functools', 'partial']
    r   >"'r   r   r   )infnanZInfNaNr   r   Nr0   r   r   )r   r   rD   r   r   r   r!   r   rH   r   r}   index
ValueError)r"   r#   qualr   r$   r$   r%   r	   s  s8    *
  
  

  
c                 C   sF  |dkr| dkrt j} n8|dkrP| dkrP|r4d| nd}||krDd}|d|  S | dkr|dkrdd	}t|rrd
} n,|s|rd| nd}||krd}|d|  S | sd| }nd| |f }|rd| nd}||krd}||7 }|rB| dsBzt| W nF tk
r@   | ddd }|s$ || kr<t||||}Y nX |S )ad  helper to build a likely import string from head and tail of namespace.
    ('head','tail') are used in the following context: "from head import tail"

    If verify=True, then test the import string before returning it.
    If builtin=True, then force an import for builtins where possible.
    If alias is provided, then rename the object on import.
    )EllipsisNotImplemented)r   )Noner   r\   r   r   r   r   r   z	import %szfrom %s import %sz as %s
rY   zdill.r   r   r   )rL   r   r   rg   r   rm   rsplit
_getimport)headtailr   verifyr   r   _strZ_headr$   r$   r%   r     sD    	     
   
r   c                 C   s  |r$ddl m} || }|r |n| } t| }d|dd }|d }	z(t| ddd ddd }
d}
W n8 tk
r   |dkrt| }
nt| d	d
 }
Y nX |
r"zt||
|||W S  tk
r   Y nN t	k
r    |dkr|rd| nd}||
krd}|d|
   Y S Y nX zt||	|||W S  tk
rL    Y nR t	k
r   |dkr|rtd| nd}||	krd}|d|	   Y S  Y nX dS )al  get the likely import string for the given object

    obj is the object to inspect
    If verify=True, then test the import string before returning it.
    If builtin=True, then force an import for builtins where possible.
    If enclosing=True, get the import for the outermost enclosing callable.
    If alias is provided, then rename the object on import.
    r   )	outermostr   Nr0   r   r   r   r   r   r   r\   r   )
rC   r   r	   rj   r   rD   rH   r   rm   SyntaxError)r"   r   r   r   r   r   _objr   r   r   r   r   r$   r$   r%   r
     sJ    	 
  

 

 c           
      C   sZ  |dkrt | rdnd}|rHzt| |||||dW S  tk
rF   Y nX zvt| sht| ||| |dW S t| || |d}t| dd}|std|rd| nd	}	||krd	}	||	 d
|  W S  tk
r   Y nX |szt| |||||dW S  tk
r   Y nX t| |d} | r&| dr.td|r<d| nd	}	|| krNd	}	|	d
|   S )a\  get an import string (or the source code) for the given object

    This function will attempt to discover the name of the object, or the repr
    of the object, or the source code for the object. To attempt to force
    discovery of the source code, use source=True, to attempt to force the
    use of an import, use source=False; otherwise an import will be sought
    for objects not defined in __main__. The intent is to build a string
    that can be imported from a python file. obj is the object to inspect.
    If alias is provided, then rename the object with the given alias.

    If source=True, use these options:
      If enclosing=True, then also return any enclosing code.
      If force=True, catch (TypeError,IOError) and try to use import hooks.
      If lstrip=True, ensure there is no indentation in the first line of code.

    If source=False, use these options:
      If enclosing=True, get the import for the outermost enclosing callable.
      If force=True, then don't test the import string before returning it.
      If builtin=True, then force an import for builtins where possible.
    NTF)r   r   r   r   )r   r   r   r   z!object has no atribute '__name__'r   r\   r   r   )r   r   rH   r   r
   r   ru   rg   )
r"   r   sourcer   r   r   r   r   r   r   r$   r$   r%   r     s\              
 c                    s  ddl ddlm}m} || }i }t| D ]\}t|sDq0|t}|	 
drtt||d}n||  dt  d t tt }	t \}
} fd	d
|
D }|stt}	t\}
}fdd
|
D }t|std|d }|ddd  d  t|	||d}||< q0|s|| tt}	|	rn| krt| ddt| ||d}nft\}
}fdd
|
D }t|std|d }|ddd  d  t|	||d}||< |S )zEget import for closured objects; return a dict of 'name' and 'import'r   Nr   )r*   r   r   r   r   .*[\w\s]=\s*\(c                    s(   g | ] }t  |kr|r|qS r$   r   r?   r[   )enclr   r>   r$   r%   r9   T  s     z#_closuredimport.<locals>.<listcomp>c                    s(   g | ] }t  |kr|r|qS r$   r   r[   )fobjr   r>   r$   r%   r9   Z  s     zimport could not be foundr0   r   Tr   c                    s4   g | ],}t  |krd t   d |r|qS )r   r   r   r[   )r   r>   r$   r%   r9   j  s     )r>   rC   r*   r   listitemsr   popr   r   rg   r
   r   r   r   rL   r'   rD   r   r   )rS   r   r   r*   r   	free_vars	func_varsr"   srcr   r   rA   	candidater$   )r   r   r   r   r>   r%   _closuredimport?  sR     

 

 r   c           
      C   s   ddl m} || }i }t| D ]\}}t|sHt|d|d||< q$||}t||}| dst	||d}t| |ddd}	||	f}n$t| ddd}	t	||dd	}|	|f}|||< q$d

| }|st| |dddd}	||	f}n|df}||d< |S )zSget source code for closured objects; return a dict of 'name'
    and 'code blocks'r   )r*   T)r   r   r   r   F)r   r   )r   r\   )r   r   r   N)rC   r*   r   r   r   r   r   r   rg   r   rj   rR   )
rS   r   r*   r   r   r   r"   r   r   orgr$   r$   r%   _closuredsourcet  s0    




r   c                 C   s  |dkrt | rdnd}n|r*t| r*d}d }}|szbt| rRt| |d|dW S t| ||d}t|dkrttdt|dkrtdt| d W S  t	k
r   |r d}Y nX z t
| |d	}t|dkrtdd
d }||d}| r|gng }| D ]}	||	}	|	r
||	 q
t|s:d}n"t|dkrR|d }n
d|}ddlm}
 |
| } tdd |  D } | rd| nd} | s|W S |s| W S | | W S  t	k
r   |rԂ d}| }Y q2X q2dS )a  get an importable string (i.e. source code or the import string)
    for the given object, including any required objects from the enclosing
    and global scope

    This function will attempt to discover the name of the object, or the repr
    of the object, or the source code for the object. To attempt to force
    discovery of the source code, use source=True, to attempt to force the
    use of an import, use source=False; otherwise an import will be sought
    for objects not defined in __main__. The intent is to build a string
    that can be imported from a python file.

    obj is the object to inspect. If alias is provided, then rename the
    object with the given alias. If builtin=True, then force an import for
    builtins where possible.
    NTFr   r   r   r   znot implementedr   r   c                 S   sH   | d r| d rd | } n(| d r.| d } n| d r@| d } nd} | S )z,stitch together the strings in tuple 'block'r   r0   rY   r\   )rj   )r   r$   r$   r%   _code_stitcher  s      
 
z"importable.<locals>._code_stitcherr\   rY   )r+   c                 s   s(   | ] \}}t |st||d dV  qdS )Tr   N)r   r   )r5   r   r   r$   r$   r%   r;     s      zimportable.<locals>.<genexpr>)r   r   r   r   r   rL   NotImplementedErrorr   rR   rH   r   r   r~   rj   rC   r+   r   )r"   r   r   r   Ztried_sourceZtried_importr   r   Z_srcZxxxr+   r$   r$   r%   r     sd     
 


  
 c                 C   s   t | || |dS )Nr   )r   )r"   r   Zbynameexplicitr$   r$   r%   r     s    r   c                 C   s   t | | |dS N)r   r   )r
   )r"   passiver   r$   r$   r%   likely_import  s    r  c                 C   s   t | || |dS r   )r   )firstlastr   r   r$   r$   r%   _likely_import  s    r  )FFF)FF)r\   FFFF)r   )NT)NT)r\   )r\   FT)FF)r\   TF)r\   TFF)r\   NFTTT)r\   F)r\   )r\   NT)r\   TF)FF)FT)8r   __all__rh   r>   inspectr   r   r   r   r   r   r   r   r   r   r   r   r   tokenizer   Z_dillr   r   r   rX   r   r   r   r   r   r   r   r   r   r   r   rF   r   rG   r   r   r   r   r   r	   r   r
   r   r   r   r   r   r  r  Z	_get_nameZgetblocks_from_historyr$   r$   r$   r%   <module>   sn   	
       <	5 
G
    
\






'
+
0
3    
>
5
'
R


