
    gL                    L   U d Z ddlmZ ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZddlZddlZddlmZ ddlmZ ddl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!m"Z" dd
l#m$Z$ ddl%m&Z& ddl'm(Z( ddl)m*Z* ddl+m,Z, dZ-de.d<   dZ/de.d<   dZ0de.d<   dZ1de.d<   e2ee3e4e   f   eegef   f   Z5de.d<   dZ6de.d<    G d de$      Z7	 	 d)	 	 	 	 	 	 	 	 	 d*dZ8 G d d      Z9 G d  d!      Z: e:       Z;d+d"Z<d,d#Z=d-d$Z> G d% d&      Z? G d' d(      Z@y).z0Hashing for st.cache_data and st.cache_resource.    )annotationsN)Enum)Pattern)MappingProxyType)AnyCallableFinalUnioncast)	TypeAlias)	type_utilutil)StreamlitAPIException)UnhashableTypeError)	CacheType)UploadedFile)HASHLIB_KWARGSi r	   _PANDAS_ROWS_LARGEi'  _PANDAS_SAMPLE_SIZEi@B _NP_SIZE_LARGE_NP_SAMPLE_SIZEr   HashFuncsDicts<   streamlit-57R34ML17-hesamagicalponyflyingthroughthesky-CYCLE_CYCLE_PLACEHOLDERc                  @     e Zd Z	 d	 d fdZd Z	 	 	 	 	 	 ddZ xZS )UserHashErrorc                    t        |      j                  | _        || _        || _        | j                  ||      }t        |   |       | j                  |j                         y N)
type__name__alternate_name	hash_func
cache_type_get_message_from_funcsuper__init__with_traceback__traceback__)selforig_excobject_to_hashr!   r"   msg	__class__s         V/var/www/openai/venv/lib/python3.12/site-packages/streamlit/runtime/caching/hashing.pyr%   zUserHashError.__init__B   sX     #8n55"$))(NCH223    c                N    | j                  ||      }d|z  j                  d      S )Nad  
%(orig_exception_desc)s

This error is likely due to a bug in %(hash_func_name)s, which is a
user-defined hash function that was passed into the `%(cache_primitive)s` decorator of
%(object_desc)s.

%(hash_func_name)s failed when hashing an object of type
`%(failed_obj_type_str)s`.  If you don't know where that object is coming from,
try looking at the hash chain below for an object that you do recognize, then
pass that to `hash_funcs` instead:

```
%(hash_stack)s
```

If you think this is actually a Streamlit bug, please
[file a bug report here](https://github.com/streamlit/streamlit/issues/new/choose).

)_get_error_message_argsstrip)r(   r)   cached_funcargss       r-   r#   z$UserHashError._get_message_from_funcR   s3    ++HkB& '( %++	r.   c                   t         j                  j                  }t        j                  |      }|d}nt        |d      rd|j                   d}nd}d}| j                  t        j                  u rd}n| j                  t        j                  u rd}t        | j                  d      rd| j                  j                   d}nd}t        |      |t         j                  j                         |||d	S )
N	somethingr   `z()`z
a function z@st.cache_resourcez@st.cache_data)orig_exception_descfailed_obj_type_str
hash_stackobject_desccache_primitivehash_func_name)hash_stackscurrenthash_sourcer   get_fqn_typehasattrr   r"   r   RESOURCEDATAr!   strpretty_print)r(   r)   
failed_objrA   r:   r<   decorator_namer>   s           r-   r1   z%UserHashError._get_error_message_argsl   s    
 "))55'44Z@%K{J/ !+"6"6!7s;*??i0001N__	.-N4>>:. !8!8 9=N)N $'x=#6%--::<&-,
 	
r.   r   )r"   zCacheType | None)r)   BaseExceptionrH   r   returnzdict[str, Any])r   
__module____qualname__r%   r#   r1   __classcell__)r,   s   @r-   r   r   A   s=     (,4
 %4 4#
#
 #
 
	#
r.   r   c                j    |t         j                  _        t        ||      }|j	                  ||        y)zcUpdates a hashlib hasher with the hash of val.

    This is the main entrypoint to hashing.py.
    N)r?   r@   rA   _CacheFuncHasherupdate)valhasherr"   rA   
hash_funcschs         r-   update_hashrV      s,     '2K#	*j	1BIIfcr.   c                  <    e Zd ZdZd Zd	dZd
dZd Zd
dZd	dZ	y)
_HashStacka  Stack of what has been hashed, for debug and circular reference detection.

    This internally keeps 1 stack per thread.

    Internally, this stores the ID of pushed objects rather than the objects
    themselves because otherwise the "in" operator inside __contains__ would
    fail for objects that don't return a boolean for "==" operator. For
    example, arr == 10 where arr is a NumPy array returns another NumPy array.
    This causes the "in" to crash since it expects a boolean.
    c                D    t        j                         | _        d | _        y r   )collectionsOrderedDict_stackrA   r(   s    r-   r%   z_HashStack.__init__   s    ?J?V?V?X 7;r.   c                ,    t        j                  |       S r   r   repr_r]   s    r-   __repr__z_HashStack.__repr__       zz$r.   c                4    || j                   t        |      <   y r   )r\   idr(   rR   s     r-   pushz_HashStack.push   s    "BsGr.   c                8    | j                   j                          y r   )r\   popitemr]   s    r-   popz_HashStack.pop   s    r.   c                0    t        |      | j                  v S r   )rd   r\   re   s     r-   __contains__z_HashStack.__contains__   s    #w$++%%r.   c                    dddj                  fdt        | j                  j                               D              S )Nc                j    	 dt        j                  |        dt        |        S # t        $ r Y yw xY w)NzObject of type z: z"<Unable to convert item to string>)r   rB   rF   	Exception)vs    r-   to_strz'_HashStack.pretty_print.<locals>.to_str   s<    <()?)?)B(C2c!fXNN <;<s   #& 	22r0   c              3  .   K   | ]  } |        y wr    ).0xrp   s     r-   	<genexpr>z*_HashStack.pretty_print.<locals>.<genexpr>   s     K,Jq,Js   )ro   r   rK   rF   )joinreversedr\   values)r(   rp   s    @r-   rG   z_HashStack.pretty_print   s1    	< yyKHT[[5G5G5I,JKKKr.   NrK   rF   )rR   r   )
r   rL   rM   __doc__r%   ra   rf   ri   rk   rG   rr   r.   r-   rX   rX      s&    	; #&Lr.   rX   c                  0    e Zd ZdZd ZddZedd       Zy)_HashStacksz@Stacks of what has been hashed, with at most 1 stack per thread.c                6    t        j                         | _        y r   )weakrefWeakKeyDictionary_stacksr]   s    r-   r%   z_HashStacks.__init__   s    %%' 	r.   c                ,    t        j                  |       S r   r_   r]   s    r-   ra   z_HashStacks.__repr__   rb   r.   c                    t        j                         }| j                  j                  |d       }|t	               }|| j                  |<   |S r   )	threadingcurrent_threadr   getrX   )r(   r   stacks      r-   r@   z_HashStacks.current   sE    "113  6=LE+0DLL(r.   Nry   )rK   rX   )r   rL   rM   rz   r%   ra   propertyr@   rr   r.   r-   r|   r|      s#    J

  	 	r.   r|   c                V    | j                         dz   dz  }| j                  |dd      S )N   littleT)signed)
bit_lengthto_bytes)i	num_bytess     r-   _int_to_bytesr      s,    !#)I::i$:77r.   c                .    dd l }|j                  d|       S )Nr   z<d)structpack)fr   s     r-   _float_to_bytesr      s     ;;tQr.   c                t   | yd } ||       r| S t        | t              rt        t        ||             r| S t        | t              r"t        t        ||             rdt        |       fS t        j                  |       s*t        j                  |       st        j                  |       rt        |       S t        S )zReturn key for memoization.Nc                   t        | t              xsz t        | t              xsh t        | t              xsV t        | t              xsD t        | t
              xs2 t        | t              xs  t        | t        j                        xs | d u S r   )	
isinstancebytes	bytearrayrF   floatintbooluuidUUID)objs    r-   	is_simplez_key.<locals>.is_simple   s    sE" #y)#s# #u% #s#	
 #t$ #tyy) d{		
r.   __l)r   tupleallmaplistinspect	isbuiltin	isroutineiscoderd   NoResult)r   r   s     r-   _keyr      s     {

 ~
#us9c"#J#ts9c"#5:&&!2!23!77>>#;N#wOr.   c                  :    e Zd ZdZdd	dZd
dZddZddZddZy)rP   z+A hasher that can hash objects with cycles.Nc                    |  |rO|j                         D ci c]-  \  }}t        |t              r|nt        j                  |      |/ c}}| _        ni | _        i | _        d| _        || _        y c c}}w )Nr   )	itemsr   rF   r   get_fqn_hash_funcs_hashessizer"   )r(   r"   rT   kro   s        r-   r%   z_CacheFuncHasher.__init__  sz     	 ',,. .DAq  3'Y->->q-A1D. D
  "D)+ 	$ s   2A1c                ,    t        j                  |       S r   r_   r]   s    r-   ra   z_CacheFuncHasher.__repr__1  rb   r.   c                X   t        |      j                  j                         }|t        |      f}|d   t        ur|| j
                  v r| j
                  |   S |t        j                  v rt        S t        j                  j                  |       	 d|| j                  |      fz  }| xj                  t        j                  |      z  c_        |d   t        ur|| j
                  |<   t        j                  j                          |S # t        j                  j                          w xY w)zKAdd memoization to _to_bytes and protect against cycles in data structures.   s   %s:%s)r   rM   encoder   r   r   r?   r@   r   rf   	_to_bytesr   sys	getsizeofri   )r(   r   tnamekeybs        r-   r   z_CacheFuncHasher.to_bytes4  s    S	&&--/d3i  q6!dll"||C(( +%%%%%  %	&E4>>##677A IIq))I1vX%$%S!
 ##% ##%s   AD	 	 D)c                H    | j                  |      }|j                  |       y)z6Update the provided hasher with the hash of an object.N)r   rQ   )r(   rS   r   r   s       r-   rQ   z_CacheFuncHasher.updateV  s    MM#ar.   c                   t        j                  di t        }t        j                  |d      st        j                  |d      r| j                  t        |            S t        |t              st        |t              r|S t        j                  |      | j                  v r<| j                  t        j                  |         }	  ||      }| j                  |      S t        |t              r|j!                         S t        |t"              rt%        |      S t        |t&              rt)        |      S t        |t*        j,                        r|j                  S t        |t.        j.                        r|j1                         j!                         S t        |t2        t4        f      r)|D ]  }| j7                  ||        |j9                         S t        |t:              r7|j=                         D ]  }| j7                  ||        |j9                         S |y|du ry|du ryt        |t>              s9tA        jB                  |      r$| j                  tA        jD                  |            S t        |tF              rt        |      j!                         S t        j                  |d	      rd
dl$}tK        |jL                  |      }| j7                  ||jN                         | j7                  ||jP                  jR                         tU        |      tV        k\  r|jY                  tZ        d
      }	 | j7                  ||j\                  j_                  |      j`                  jc                                |j9                         S t        j                  |d      rd
dl$}tK        |jl                  |      }| j7                  ||jn                         tU        |      tV        k\  r|jY                  tZ        d
      }	 | j                  |j\                  j_                  |jp                              }| j7                  ||       | j                  |j\                  j_                  |            }	| j7                  ||	       |j9                         S t        j                  |d      rd
dl9}
tK        d|      }| j7                  ||jn                         | j7                  |t        |jP                               |jN                  tt        k\  r@d
dl9}
|
jv                  jy                  d
      }|j{                  |j|                  t~              }| j7                  ||jc                                |j9                         S t        j                  |d      rHd
dl9}
d
dl@mA} tK        ||      }|
j                  |jc                         d      }| j                  |      S t        j                  |      r#t        |j                  j!                               S t        |t              s$t        |t        j                  j                        r| j                  t;        |            S t        j                  |d      r#t        |j                  j!                               S t        |t              rl| j7                  ||jR                         | j7                  ||j                                | j7                  ||j                                |j9                         S t        |d      rt        |t        j                        st        |t        j                        r~t        |dd      }| j7                  ||       | j7                  |t        j                  j                  |             | j7                  ||j                                |j9                         S t        |t              r'| j                  |j                  |j                  g      S t        |t        j                        st        |t        j                        rP| j7                  ||j                                | j7                  ||j                                |j9                         S t        j                  |d      r#t        |j                  j!                               S t        j                  |      r| j                  |j                        S t        j                  |      r| j                  |j                        S t        |t        j                        rd| j7                  ||j                         | j7                  ||j                         | j7                  ||j                         |j9                         S 	 |j                         }|D ]  }| j7                  ||        |j9                         S # t        $ r}t        |||| j                        |d}~ww xY w# td        $ r* dtg        jh                  |tf        jj                        z  cY S w xY w# td        $ r* dtg        jh                  |tf        jj                        z  cY S w xY w# t        $ r}t               |d}~ww xY w)zHash objects to bytes, including code with dependencies.

        Python's built in `hash` does not produce consistent results across
        runs.
        zunittest.mock.Mockzunittest.mock.MagicMock)r!   r"   N   0T   1Fzpandas.core.series.Seriesr   )nrandom_states   %szpandas.core.frame.DataFrameznumpy.ndarrayznp.ndarray[Any, Any])r   zPIL.Image.Image)Imageuint8)dtypezbuiltins.getset_descriptorname
wonthappenznumpy.ufunc)md5)ehashlibnewr   r   is_typer   rd   r   r   r   rB   r   rn   r   r"   rF   r   r   r   r   r   r   r   datetime	isoformatr   r   rQ   digestdictr   r   dataclassesis_dataclassasdictr   pandasr   Seriesr   r   r   lenr   sampler   r   hash_pandas_objectrx   tobytes	TypeErrorpickledumpsHIGHEST_PROTOCOL	DataFrameshapedtypesnumpyr   randomRandomStatechoiceflatr   	PIL.Imager   
frombufferr   r   r   r   rZ   abc	ItemsViewrM   r   tellgetvaluerC   ioIOBasetempfile_TemporaryFileWrappergetattrospathgetmtimer   patternflagsStringIOBytesIOismoduleisclass	functoolspartialr4   funckeywords
__reduce__r   )r(   r   hr!   outputexitempdcolumn_hash_bytesvalues_hash_bytesnpstater   np_arrayobj_namereduce_datas                   r-   r   z_CacheFuncHasher._to_bytes[  s    KK00S"679;L;L*<

 ==C))U#z#y'AJ##C(D,<,<<(()?)?)DEI"3
 ==((S!::<U#"3''S! %%TYY'99X../==?))++dE]+At$ 88:T"		At$ $88:[D[E\C&;+C+CC+H==!3!3C!899T"s8??$$s$?@ryy#&CKK388$KK399>>*3x--jj#6QjGJArww99#>EEMMOPxxz! s$ABr||S)CKK399%3x--jj#6QjGJ$(MMGG..szz:%! A01$(MM"''2L2LS2Q$R!A01xxz! sO4 -s3CKK399%KK3syy>*xx>)"		--a0ll388/lBKK3;;=)88:s$56'uc"C }}S[[]'}BH==**s#,,.//-.***3
 ==c++s$@A))00233\* KK388$KK388:&KK3<<>*88:S&!sBII&#x==> sFL9HKK8$KK277++H56KK388:&88:W%==#++syy!9::R[[)ZRZZ-H KK388:&KK3<<>*88:sM2,,.//c" ==..__S! ==..Y../ KK388$KK388$KK3<<(88:4!nn. $At$ $88:O  #yT__t  J v||C1H1HIIIJ*  J v||C1H1HIIIJf  4)+34sV   g 4Ah =Bh= #i3 	h%g??h0h:9h:=0i0/i03	j<jjr   )r"   r   rT   HashFuncsDict | Nonery   )r   r   rK   r   )r   r   rK   None)	r   rL   rM   rz   r%   ra   r   rQ   r   rr   r.   r-   rP   rP     s     5%.  D
_r.   rP   c                      e Zd ZdZy)r   z<Placeholder class for return values when None is meaningful.N)r   rL   rM   rz   rr   r.   r-   r   r   =  s    Fr.   r   )NN)
rR   r   r"   r   rA   zCallable[..., Any] | NonerT   r  rK   r  )r   r   rK   r   )r   r   rK   r   )r   z
Any | NonerK   r   )Arz   
__future__r   rZ   collections.abcr   r   r   r   r   r   r   r   r   r   r   r   r~   enumr   rer   typesr   typingr   r   r	   r
   r   typing_extensionsr   	streamlitr   r   streamlit.errorsr   &streamlit.runtime.caching.cache_errorsr   $streamlit.runtime.caching.cache_typer   'streamlit.runtime.uploaded_file_managerr   streamlit.utilr   r   __annotations__r   r   r   r   rF   r   r   r   r   rV   rX   r|   r?   r   r   r   rP   r   rr   r.   r-   <module>r     s^   7 "        	 	  
       " 4 4 ' % 2 F : @ ) # E "" U "    c49n 5xs
7K KLy L
 D E 
N
) N
j .2'+	  +	
 % 
$%L %LP 0 m8
  Fc cL		 	r.   