o
    *ήc"                     @   s   d Z G dd deZe ZejZejZddlmZmZ dd Z	dd Z
d	d
 ZdddZedd ZeddZe dkrAdZnzeeZW n eyV   edde  w edkr^e
Znedkrge	eZnedkrpeeZnede dd ZdS )z Caching facility for SymPy c                   @   s    e Zd ZdZdd Zdd ZdS )_cachez List of cached functions c                 C   sT   | D ]%}|j }|}t|dr t|dr| }n
|j}t|dsd}t|| qdS )zprint cache info__wrapped__
cache_infoN)__name__hasattrr   r   print)selfitemnamemyfuncinfo r   7/tmp/pip-target-vg8gfxp4/lib/python/sympy/core/cache.pyprint_cache   s   


z_cache.print_cachec                 C   s@   | D ]}|}t |drt |dr|  n|j}t |dsqdS )zclear cache contentr   cache_clearN)r   r   r   )r   r   r
   r   r   r   clear_cache   s   


z_cache.clear_cacheN)r   
__module____qualname____doc__r   r   r   r   r   r   r      s    r       )	lru_cachewrapsc                        fdd}|S )a  caching decorator.

        important: the result of cached function must be *immutable*


        Examples
        ========

        >>> from sympy import cacheit
        >>> @cacheit
        ... def f(a, b):
        ...    return a+b

        >>> @cacheit
        ... def f(a, b): # noqa: F811
        ...    return [a, b] # <-- WRONG, returns mutable object

        to force cacheit to check returned results mutability and consistency,
        set environment variable SYMPY_USE_CACHE to 'debug'
    c                    sD   t dd t fdd} j|_ j|_t| |S )NT)typedc               
      sb   z
 | i |}W |S  t y0 } z|jr|jd ds | i |}W Y d }~|S d }~ww )Nr   zunhashable type:)	TypeErrorargs
startswith)r   kwargsretvalecfuncfuncr   r   wrapperC   s   z0__cacheit.<locals>.func_wrapper.<locals>.wrapper)r   r   r   r   CACHEappendr!   r"   maxsizer   r   func_wrapper@   s   	
z__cacheit.<locals>.func_wrapperr   r'   r(   r   r&   r   	__cacheit+   s   r*   c                 C   s   | S Nr   )r!   r   r   r   __cacheit_nocacheW   s   r,   c                    r   )z)cacheit + code to check cache consistencyc                    s&   t  t fdd}|S )Nc                     s@   | i |} | i |}t |t |f ||krtd|S )Nz Returned values are not the same)hashRuntimeError)r   kw_argsr1r2r   r   r   r"   `   s   z6__cacheit_debug.<locals>.func_wrapper.<locals>.wrapper)r*   r   r%   r&   r   r   r(   ]   s   z%__cacheit_debug.<locals>.func_wrapperr   r)   r   r&   r   __cacheit_debug[   s   r2   Nc                 C   s   ddl m} || |S )Nr   )getenv)osr3   )keydefaultr3   r   r   r   _getenvy   s   
r7   SYMPY_USE_CACHEyesSYMPY_CACHE_SIZE1000nonez2SYMPY_CACHE_SIZE must be a valid integer or None. zGot: %snodebugz*unrecognized value for SYMPY_USE_CACHE: %sc                    s(   dj  t   fdd}t|S )z"Decorator to cache property method__c                    s,   t |  }| u r| }t| | |S r+   )getattrsetattr)r   val_cached_property_sentinelattrnamer!   r   r   propfunc   s
   z!cached_property.<locals>.propfunc)r   objectproperty)r!   rF   r   rC   r   cached_property   s   
rI   r+   )r   listr   r#   r   r   	functoolsr   r   r*   r,   r2   r7   lower	USE_CACHEscsr:   int
ValueErrorr.   cacheitrI   r   r   r   r   <module>   sB    !,



