a
    2PÎjs  ã                   @   s`   g d ¢Z ddlZddlZddlmZ ddlmZ ddd„Zdd	d
„Z	ddd„Z
ddd„ZeZdS ))ÚparentÚ	referenceÚatÚparentsÚchildrené    Né   )Ú_proxy_helper)Ú_locate_object© c                 C   s*   d}t | |||ƒ}| ¡ }|| u r&dS |S )al  
>>> listiter = iter([4,5,6,7])
>>> obj = parent(listiter, list)
>>> obj == [4,5,6,7]  # actually 'is', but don't have handle any longer
True

NOTE: objtype can be a single type (e.g. int or list) or a tuple of types.

WARNING: if obj is a sequence (e.g. list), may produce unexpected results.
Parent finds *one* parent (e.g. the last member of the sequence).
    r   N)r   Úpop)ÚobjÚobjtypeÚignoreÚdepthÚchainr   r
   r
   ú]/mounts/lovelace/software/anaconda3/envs/metadmg/lib/python3.9/site-packages/dill/pointers.pyr      s    r   c                    sP   t j}‡ fdd„}t|dƒs"|fn|}dd„ |D ƒ}t| |||ƒddd… }|S )zúFind the chain of referents for obj. Chain will end with obj.

    objtype: an object type or tuple of types to search for
    depth: search depth (e.g. depth=2 is 'grandparents')
    ignore: an object or tuple of objects to ignore in the search
    c                    s
   t | ˆ ƒS ©N©Ú
isinstance©Úx©r   r
   r   Ú<lambda>-   ó    zparents.<locals>.<lambda>Ú__len__c                 s   s   | ]}t |ƒV  qd S r   ©Úid©Ú.0r   r
   r
   r   Ú	<genexpr>0   r   zparents.<locals>.<genexpr>Néÿÿÿÿ)ÚgcZget_referentsÚhasattrÚ
find_chain©r   r   r   r   Ú	edge_funcÚ	predicater   r
   r   r   r   %   s    r   c                    sH   t j}‡ fdd„}t|dƒs"|fn|}dd„ |D ƒ}t| ||||ƒ}|S )a  Find the chain of referrers for obj. Chain will start with obj.

    objtype: an object type or tuple of types to search for
    depth: search depth (e.g. depth=2 is 'grandchildren')
    ignore: an object or tuple of objects to ignore in the search

    NOTE: a common thing to ignore is all globals, 'ignore=(globals(),)'

    NOTE: repeated calls may yield different results, as python stores
    the last value in the special variable '_'; thus, it is often good
    to execute something to replace '_' (e.g. >>> 1+1).
    c                    s
   t | ˆ ƒS r   r   r   r   r
   r   r   D   r   zchildren.<locals>.<lambda>r   c                 s   s   | ]}t |ƒV  qd S r   r   r   r
   r
   r   r   G   r   zchildren.<locals>.<genexpr>)r!   Zget_referrersr"   r#   r$   r
   r   r   r   6   s    r   é   c                 C   sh  | g}t | ƒdi}t | ƒd i}t|ƒ}| t |ƒ¡ | t |ƒ¡ | t |ƒ¡ | t |ƒ¡ | t |ƒ¡ | t t ¡ ƒ¡ | t t d¡ƒ¡ t ¡  |�rb| d¡}	||	ƒrä|	g}
|t |	ƒ d urà|t |	ƒ }	|
 |	¡ q¸|
S |t |	ƒ }||k rš||	ƒ}| t |ƒ¡ |D ]L}t |ƒ|v �r(�qt |ƒ|v�r|d |t |ƒ< |	|t |ƒ< | |¡ �qqš| gS )Nr   r   )	r   ÚsetÚaddÚsysÚ	_getframer!   Zcollectr   Úappend)r   r&   r%   Ú	max_depthZextra_ignoreÚqueuer   r   r   Útargetr   ZtdepthZ	referrersÚsourcer
   r
   r   r#   S   s@    
r#   )r
   )r   r
   )r   r
   )r'   r
   )Ú__all__r!   r*   Z_dillr   r   r	   r   r   r   r   r#   Z	refobjectr
   r
   r
   r   Ú<module>	   s   



$