o
    g                     @   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rMdZnzeeZW n eyb   edde  w edkrjeZnedkrseeZnedkr|eeZnede dd Zdededef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   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   G/var/www/visachat/venv/lib/python3.10/site-packages/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   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                    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$   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   r4   Nc                 C   s   ddl m} || |S )Nr   )getenv)osr5   )keydefaultr5   r   r   r   _getenv{   s   
r9   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#   rH   r   rE   r   cached_property   s   
rK   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-   rB   r   r   r#   rL   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                    ,     j }|d| jj d d d7 }|S Nz

Note: this is a z wrapper of '.'r   	__class__r   r
   	docstringrP   rL   r   r   r   r          z/lazy_function.<locals>.LazyFunctionMeta.__doc__N)r   r   r   rJ   r   r   rY   r   r   LazyFunctionMeta   s    r[   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-   rN   )r
   r   r   rO   r   r   __call__   s   z,lazy_function.<locals>.LazyFunction.__call__c                    rQ   rR   rU   rW   rY   r   r   r      rZ   z+lazy_function.<locals>.LazyFunction.__doc__c                    s
       S r-   )__str__r
   )rP   r   r   r]      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 'rS   rT   )r   idr^   )rV   rL   r   r   r   __repr__   s   &z,lazy_function.<locals>.LazyFunction.__repr__)	r   r   r   r\   rJ   r   r]   rb   __classcell__r   rP   r#   rL   r   )rV   r   LazyFunction   s    re   )	metaclass)type)rL   r   r[   re   r   rd   r   lazy_function   s
   rh   r-   )r   	importlibr   typingr   listr   r%   r   r   	functoolsr   r   r,   r.   r4   r9   lower	USE_CACHEscsr<   int
ValueErrorr0   cacheitrK   strrh   r   r   r   r   <module>   sH    !,



