o
    ~]..                     @   s   d Z ddlZddlmZmZmZmZmZ ddlm	Z	m
Z
mZmZmZ ddlmZ ddlmZ g dZd'd
dZdd Zd(ddZdd Zd(ddZdd Zd(ddZd)ddZd)ddZdd Zd*dd Zd+d!d"Zd+d#d$Zd+d%d&Z dS ),z=
Methods for detecting objects leading to pickling failures.
    N)ismethod
isfunctionistracebackisframeiscode   )parent	referenceatparentschildren)_trace)PY3)baditems
badobjectsbadtypescodeerrorsfreevars	getmodule
globalvars
nestedcodenestedglobals	outermostreferredglobalsreferrednestedtracevarnamesFc                 C   sh   ddl m} || |}|s|s|S trd}nd}t|}ddlm} || dd}|t| v r2|S d	S )
zget the module of the objectr   )r   builtins__builtin__r   )getnameT)forceN)inspectr   r   
__import__sourcer    varskeys)object	_filenamer!   getmodmoduler   r    name r,   D/var/www/Datamplify/venv/lib/python3.10/site-packages/dill/detect.pyr      s   

r   c                    s   t rt| r| jjpi }nt| r| jpi }ndS | }nt| r)| jjp'i }nt| r3| jp1i }ndS | }ddl	m
} z
|| dd\}}W n	   g d}}Y d|  fdd|D }|D ]\}}z||||fkru|W   S W qc   Y qcdS )	zget outermost enclosing object (i.e. the outer function in a closure)

    NOTE: this is the object-equivalent of getsource(func, enclosing=True)
    Nr   )getsourcelinesT)	enclosing c                 3   s$    | ]\}}| v r||fV  qd S Nr,   ).0r+   objr   r,   r-   	<genexpr>?      " zoutermost.<locals>.<genexpr>)r   r   __func____globals__r   itemsim_funcfunc_globals	iteritemsr$   r.   join)func_globalsr.   lineslnum_localsr+   r3   r,   r4   r-   r   #   s2   

r   Tc                 C   sf   t | } t| s
g S t }| jD ]}|du rqt |}|r.|| |r.|tt|ddO }qt|S )zAget the code objects for any nested functions (e.g. in a closure)NTrecurse)r   r   set	co_constsaddr   list)r>   rD   nestedcor,   r,   r-   r   H   s   


r   c                 C   sf   t rd}d}nd}d}t| rt| |} t| rt| |} t| r$| j} t| r+| j} t| r1| S dS )z~get the code object for the given function or method

    NOTE: use dill.source.getsource(CODEOBJ) to get the source code
    r7   __code__r:   	func_codeN)	r   r   getattrr   r   tb_framer   f_coder   )r>   r:   rL   r,   r,   r-   r   U   s   r   c           	      C   s   t rd}d}nd}d}ddl}t }t| |D ]H}||D ]@}t||d}t||d|u r4|| qt||d|u rB|| qt|dd|u rP|| qt|dr^||u r^|| qqt|S )	aJ  get functions defined inside of func (e.g. inner functions in a closure)

    NOTE: results may differ if the function has been executed or not.
    If len(nestedcode(func)) > len(referrednested(func)), try calling func().
    If possible, python builds code objects, but delays building functions
    until func() is called.
    rK   r7   rL   r:   r   NrO   co_code)	r   gcrE   r   get_referrersrM   rG   hasattrrH   )	r>   rD   att1att0rQ   funcsrJ   r3   _r,   r,   r-   r   h   s"   r   c                 C   sp   t r	d}d}d}nd}d}d}t| rt| |} t| r*t| |p"d}t| |j} ni S tdd	 t| |D S )
zgget objects defined in enclosing code that are referred to by func

    returns a dict of {name:object}r7   rK   __closure__r:   rL   func_closurer,   c                 s   s    | ]
\}}||j fV  qd S r1   )cell_contents)r2   r+   cr,   r,   r-   r5      s    zfreevars.<locals>.<genexpr>)r   r   rM   r   co_freevarsdictzip)r>   r:   rL   rY   closuresr,   r,   r-   r      s   r   c                 C   s   t | } | du rt S ddlm} t }|d}t|  W d   n1 s(w   Y  |  D ]}d|v rJ|dd dd	 }|	| q3t
| d
t D ]}|re|ret|re|t|dd qRt|S )z.get the names of any globals found within funcNr   )capturestdout_GLOBAL()r   rF   TrC   )r   rH   tempr`   rE   disgetvalue
splitlinessplitrG   rM   tupler   updater   )r>   rD   r`   namesoutliner+   rJ   r,   r,   r-   r      s"   

r   c                 C   s   t | || S )z@get the names of objects in the global scope referred to by func)r   r&   )r>   rD   builtinr,   r,   r-   r      s   r   c                    s  t rd}d}d}d}nd}d}d}d}t| rt| |} t| r|r*ttt ni  | t }} t||p8i D ]}t	|j
||pCi }	| |	  |	 q9 t||pWi  |se| t||j na| tt|| |  D ]}
 |
}||u rqs| t	|d	| qsn<t| r|rttt ni  |s| j} n&| j}tt| } |  D ]}
|
|u rq |
}| t	|d	| qni S t fd
d| D S )zdget objects defined in global scope that are referred to by func

    return a dict of {name:object}r7   rK   r8   rX   r:   rL   r;   rY   Tc                 3   s$    | ]}| v r| | fV  qd S r1   r,   )r2   r+   globsr,   r-   r5      r6   zglobalvars.<locals>.<genexpr>)r   r   rM   r   r%   r   sumcopyrE   r   rZ   rl   co_namesr   getr   co_namer]   )r>   rD   rp   r:   rL   r;   rY   	orig_funcr3   _varskeynested_funcr,   rq   r-   r      sR   


r   c                 C   s    t | } t| s
dS | j| jfS )ztget names of variables defined by func

    returns a tuple (local vars, local vars referrenced by nested functions)r,   )r   r   co_varnamesco_cellvars)r>   r,   r,   r-   r      s   r   c                    sd   t | dsdd t| dfD S t| ddr|  n| } g   fdd| D  dd  D S )	z'get items in object that fail to pickle__iter__c                 S      g | ]}|d ur|qS r1   r,   r2   jr,   r,   r-   
<listcomp>       zbaditems.<locals>.<listcomp>r   valuesNc              	      s(   g | ]}| vr  t|d qS )r   )appendr   )r2   i_objexactsafer,   r-   r     s   ( c                 S   r   r1   r,   r   r,   r,   r-   r     r   )rS   r   rM   r   )r3   r   r   r,   r   r-   r      s   
r   c                    sF   ddl m  srdS S t fddtD S )zget objects that fail to pickler   picklesNc                 3   >    | ]}t |s|tt | d  fV  qdS r   N)rM   r   r2   attrdepthr   r3   r   r   r,   r-   r5         &zbadobjects.<locals>.<genexpr>)dillr   r]   dirr3   r   r   r   r,   r   r-   r     s   r   c                    sJ   ddl m  srdS tS t fddtD S )z)get types for objects that fail to pickler   r   Nc                 3   r   r   )rM   r   r   r   r,   r-   r5     r   zbadtypes.<locals>.<genexpr>)r   r   typer]   r   r   r,   r   r-   r     s   r   c              	   C   s   ddl m}m} |sGz)|| }|r|| ksJ d|| f t|t| ks1J dt|t| f W dS  tyF   ddl}| d  Y S w i }t| D ]0}	zt| |	}
W n tyk   ddl}| d ||	< Y qMw ||
||s}t	|
|d ||||	< qM|S )z*get errors for objects that fail to pickler   )r   rt   z$Unpickling produces %s instead of %sNr   )
r   r   rt   r   	Exceptionsysexc_infor   rM   r   )r3   r   r   r   r   rt   pikr   _dictr   _attrr,   r,   r-   r     s:   

r   )NF)T)TF)FF)r   FF)!__doc__rg   r"   r   r   r   r   r   pointersr   r	   r
   r   r   _dillr   r   r   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r,   r,   r-   <module>   s*   

%
&


:




		