a
    kh                     @   s&  d Z ddlmZ ddlm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rdZn0zeeZW n" ey   edde  Y n0 edkreZn0edkreeZnedkreeZnede dd ZeeedddZdS ) z Caching facility for SymPy     )import_module)Callablec                   @   s    e Zd ZdZdd Zdd ZdS )_cachez List of cached functions c                 C   sN   | D ]D}|j }|}t|dr:t|dr2| }q>q|j}qd}t|| qdS )zprint cache info__wrapped__
cache_infoN)__name__hasattrr   r   print)selfitemnamemyfuncinfo r   >/var/www/auris/lib/python3.9/site-packages/sympy/core/cache.pyprint_cache   s    

z_cache.print_cachec                 C   s:   | D ]0}|}t |drt |dr,|  qq|j}q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                    s    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 nJ t y\ } z2|jr8|jd ds: | i |}W Y d }~n
d }~0 0 |S )Nr   zunhashable type:)	TypeErrorargs
startswith)r   kwargsretvaleZcfuncfuncr   r   wrapperE   s    $z0__cacheit.<locals>.func_wrapper.<locals>.wrapper)r   r   r   r   CACHEappendr!   r"   maxsizer    r   func_wrapperB   s    	
z__cacheit.<locals>.func_wrapperr   r'   r(   r   r&   r   	__cacheit-   s    r*   c                 C   s   | S Nr   )r!   r   r   r   __cacheit_nocacheY   s    r,   c                    s    fdd}|S )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   Zkw_argsZr1Zr2r    r   r   r"   b   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    r/   Nc                 C   s   ddl m} || |S )Nr   )getenv)osr0   )keydefaultr0   r   r   r   _getenv{   s    r4   ZSYMPY_USE_CACHEyesSYMPY_CACHE_SIZEZ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Z_cached_property_sentinelattrnamer!   r   r   propfunc   s
    z!cached_property.<locals>.propfunc)r   objectproperty)r!   r@   r   r>   r   cached_property   s    
rC   )moduler   returnc                    sN   dfdd G  fdddt }G  fddd|d}| S )	zCreate a lazy proxy for a function in a module.

    The module containing the function is not imported until the function is used.

    Nc                      s    d u rt t  S r+   r;   r   r   r!   rD   r   r   r   _get_function   s    z$lazy_function.<locals>._get_functionc                       s    e Zd Ze fddZdS )z'lazy_function.<locals>.LazyFunctionMetac                    s,     j }|d| jj d d d7 }|S Nz

Note: this is a z wrapper of '.'r   	__class__r   r
   	docstringrH   rD   r   r   r   r      s     z/lazy_function.<locals>.LazyFunctionMeta.__doc__N)r   r   r   rB   r   r   rP   r   r   LazyFunctionMeta   s   rQ   c                       sP   e Zd ZfddZefddZfddZ fddZ  ZS )	z#lazy_function.<locals>.LazyFunctionc                    s$    d u rt t  |i |S r+   rF   )r
   r   r   rG   r   r   __call__   s    z,lazy_function.<locals>.LazyFunction.__call__c                    s,     j }|d| jj d d d7 }|S rI   rL   rN   rP   r   r   r      s     z+lazy_function.<locals>.LazyFunction.__doc__c                    s
       S r+   )__str__r
   )rH   r   r   rS      s    z+lazy_function.<locals>.LazyFunction.__str__c              	      s&   d j  dt| dd d d	S )N<z object at 0xxz>: wrapping 'rJ   rK   )r   idrT   )rM   rD   r   r   r   __repr__   s    z,lazy_function.<locals>.LazyFunction.__repr__)	r   r   r   rR   rB   r   rS   rX   __classcell__r   rH   r!   rD   r   )rM   r   LazyFunction   s
   r[   )	metaclass)type)rD   r   rQ   r[   r   rZ   r   lazy_function   s
    r^   )N)r   	importlibr   typingr   listr   r#   r   r   	functoolsr   r   r*   r,   r/   r4   lowerZ	USE_CACHEZscsr6   int
ValueErrorr.   ZcacheitrC   strr^   r   r   r   r   <module>   sD   !,




