
    &ebI                     ~   d 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ZddlmZ ddlmZmZmZmZmZmZmZ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Z+dZ,dZ-eee.ee         f         eegef         f         Z/dZ0 G d de!          Z1	 	 d&dede%deedef                  dee/         ddf
dZ2 G d d          Z3 G d d          Z4 e4            Z5de6de7fdZ8de9de7fdZ:d ee         defd!Z; G d" d#          Z< G d$ d%          Z=dS )'z0Hashing for st.cache_data and st.cache_resource.    N)Enum)AnyCallableDictListOptionalPatternTypeUnion)	type_utilutil)StreamlitAPIException)UnhashableTypeError)	CacheType)UploadedFile)HASHLIB_KWARGSi i'  i@B s<   streamlit-57R34ML17-hesamagicalponyflyingthroughthesky-CYCLEc                   ^     e Zd Z	 d	dee         f fdZd Zdedede	e
ef         fdZ xZS )
UserHashErrorN
cache_typec                     t          |          j        | _        || _        || _        |                     ||          }t                                          |           |                     |j	                   d S N)
type__name__alternate_name	hash_funcr   _get_message_from_funcsuper__init__with_traceback__traceback__)selforig_excobject_to_hashr   r   msg	__class__s         Alib/python3.11/site-packages/streamlit/runtime/caching/hashing.pyr   zUserHashError.__init__:   sn     #8nn5"$))(NCCH233333    c                 ^    |                      ||          }d|z                      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_funcJ   s8    ++HkBB& '( %+++	r'   r"   
failed_objreturnc                    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                                        |||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"   r.   r<   r5   r7   decorator_namer9   s           r&   r*   z%UserHashError._get_error_message_argsd   s    
 ")5'4Z@@%KK{J// +;+"6;;;*?i0001NN_	..-N4>:.. 	*=!8===NN)N $'x==#6%-::<<&-,
 
 	
r'   r   )r   
__module____qualname__r   r   r   r   BaseExceptionr   r   rA   r*   __classcell__)r%   s   @r&   r   r   9   s         +/4 4
 Y'4 4 4 4 4 4   4#
#
 #
 
c3h	#
 #
 #
 #
 #
 #
 #
 #
r'   r   valr   r<   .
hash_funcsr/   c                 t    |t           j        _        t          ||          }|                    ||            dS )zcUpdates a hashlib hasher with the hash of val.

    This is the main entrypoint to hashing.py.
    N)r:   r;   r<   _CacheFuncHasherupdate)rH   hasherr   r<   rI   chs         r&   update_hashrO      s8     '2K#	*j	1	1BIIfcr'   c                   N    e Zd ZdZd ZdefdZdefdZd Z	defdZ
defd	Zd
S )
_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 | _        d S r   )collectionsOrderedDict_stackr<   r!   s    r&   r   z_HashStack.__init__   s"    ?J?V?X?X :>r'   r/   c                 *    t          j        |           S r   r   repr_rV   s    r&   __repr__z_HashStack.__repr__       z$r'   rH   c                 4    || j         t          |          <   d S r   )rU   idr!   rH   s     r&   pushz_HashStack.push   s    "BsGGr'   c                 8    | j                                          d S r   )rU   popitemrV   s    r&   popz_HashStack.pop   s    r'   c                 .    t          |          | j        v S r   )r]   rU   r^   s     r&   __contains__z_HashStack.__contains__   s    #ww$+%%r'   c                     dt           dt          fdd                    fdt          | j                                                  D                       S )Nvr/   c                 t    	 dt          j        |            dt          |            S # t          $ r Y dS w xY w)NzObject of type z: z"<Unable to convert item to string>)r   r=   rA   	Exception)rf   s    r&   to_strz'_HashStack.pretty_print.<locals>.to_str   sR    <N)?)B)BNNc!ffNNN < < <;;;<s   &) 
77r)   c              3   .   K   | ]} |          V  d S r    ).0xri   s     r&   	<genexpr>z*_HashStack.pretty_print.<locals>.<genexpr>   s+      KKqKKKKKKr'   )r   rA   joinreversedrU   values)r!   ri   s    @r&   rB   z_HashStack.pretty_print   sd    	<c 	<c 	< 	< 	< 	< yyKKKKHT[5G5G5I5I,J,JKKKKKKr'   N)r   rD   rE   __doc__r   rA   rZ   r   r_   rb   rd   rB   rk   r'   r&   rQ   rQ      s        	 	> > > #        # # # # #  & & & & &Lc L L L L L Lr'   rQ   c                   @    e Zd ZdZd ZdefdZedefd            Z	dS )_HashStacksz@Stacks of what has been hashed, with at most 1 stack per thread.c                 6    t          j                    | _        d S r   )weakrefWeakKeyDictionary_stacksrV   s    r&   r   z_HashStacks.__init__   s     %'' 	r'   r/   c                 *    t          j        |           S r   rX   rV   s    r&   rZ   z_HashStacks.__repr__   r[   r'   c                     t          j                    }| j                            |d           }|t	                      }|| j        |<   |S r   )	threadingcurrent_threadrx   getrQ   )r!   r|   stacks      r&   r;   z_HashStacks.current   sF    "133  66=LLE+0DL(r'   N)
r   rD   rE   rr   r   rA   rZ   propertyrQ   r;   rk   r'   r&   rt   rt      sk        JJ( ( (
 #         	 	 	 	 X	 	 	r'   rt   ic                 f    |                                  dz   dz  }|                     |dd          S )N   littleT)signed)
bit_lengthto_bytes)r   	num_bytess     r&   _int_to_bytesr      s1    !#)I::i$:777r'   fc                 ,    t          j        d|           S )Nz<d)structpack)r   s    r&   _float_to_bytesr      s    ;tQr'   objc                    | dS 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                    pt          | t                    pqt          | t                    p\t          | t                    pGt          | t
                    p2t          | t                    pt          | t          j                  p| d u S r   )	
isinstancebytes	bytearrayrA   floatintbooluuidUUID)r   s    r&   	is_simplez_key.<locals>.is_simple   s    sE"" #y))#s## #u%% #s##	
 #t$$ #ty)) d{		
r'   __l)r   tupleallmaplistinspect	isbuiltin	isroutineiscoder]   NoResult)r   r   s     r&   _keyr      s     {t

 

 

 y~~ 
#u s9c""## 	J#t 's9c""## 	'5::&& !23!7!7 7>#;N;N #wwOr'   c                   n    e Zd ZdZ	 ddedee         fdZdefdZ	de
defd	Zde
ddfd
Zde
defdZdS )rK   z+A hasher that can hash objects with cycles.Nr   rI   c                     |  |r$d |                                 D             | _        ni | _        i | _        d| _        || _        d S )Nc                 l    i | ]1\  }}t          |t                    r|nt          j        |          |2S rk   )r   rA   r   get_fqn)rl   krf   s      r&   
<dictcomp>z-_CacheFuncHasher.__init__.<locals>.<dictcomp>  sL          Aq  3''AY->q-A-A1     r'   r   )items_hash_funcs_hashessizer   )r!   r   rI   s      r&   r   z_CacheFuncHasher.__init__  sg     	' 	"   &,,..     D
  "D)+ 	$r'   r/   c                 *    t          j        |           S r   rX   rV   s    r&   rZ   z_CacheFuncHasher.__repr__(  r[   r'   r   c                 V   t          |          j                                        }|t          |          f}|d         t          ur|| j        v r| j        |         S |t          j        v rt          S t          j        	                    |           	 d|| 
                    |          fz  }| xj        t          j        |          z  c_        |d         t          ur
|| j        |<   t          j                                         n## t          j                                         w xY w|S )zKAdd memoization to _to_bytes and protect against cycles in data structures.   s   %s:%s)r   rE   encoder   r   r   r:   r;   _CYCLE_PLACEHOLDERr_   	_to_bytesr   sys	getsizeofrb   )r!   r   tnamekeybs        r&   r   z_CacheFuncHasher.to_bytes+  s   S		&--//d3ii  q6!!dl""|C(( +%%%%%  %%%	&E4>>##6#677A IIq)))II1vX%%$%S!
 ##%%%%K##%%%%s   AD  D&c                 Z    |                      |          }|                    |           dS )z6Update the provided hasher with the hash of an object.N)r   rL   )r!   rM   r   r   s       r&   rL   z_CacheFuncHasher.updateM  s+    MM#ar'   c                    t          j        di t          }t          |t          j        j                  r"|                     t          |                    S t          |t                    st          |t                    r|S t          j        |          | j        v rl| j        t          j        |                   }	  ||          }n+# t          $ r}t          |||| j                  |d}~ww xY w|                     |          S t          |t"                    r|                                S t          |t&                    rt)          |          S t          |t*                    rt-          |          S t          |t.          j                  r|j	        S t          |t2          j                  r&|                                                                S t          |t6          t8          f          r/|D ]}|                     ||           |                                S t          |t>                    rA|                                 D ]}|                     ||           |                                S |dS |du rdS |du rdS tC          j"        |          r'|                     tC          j#        |                    S t          |tH                    r!t#          |                                          S t          j%        |d          rd	dl&}|                     ||j'                   |                     ||j(        j)                   tU          |          tV          k    r|,                    tZ          d	
          }	 |                     ||j.        /                    |          j0        1                                           |                                S # td          $ r% dtg          j4        |tf          j5                  z  cY S w xY wt          j%        |d          r%d	dl&}|                     ||j6                   tU          |          tV          k    r|,                    tZ          d	
          }	 |                     |j.        /                    |j7                            }|                     ||           |                     |j.        /                    |                    }	|                     ||	           |                                S # td          $ r% dtg          j4        |tf          j5                  z  cY S w xY wt          j%        |d          r|                     ||j6                   |                     |t#          |j(                             |j'        tp          k    r?d	dl9}
|
j:        ;                    d	          }|<                    |j=        t|                    }|                     ||1                                           |                                S t          j%        |d          rBd	dl9}
|
?                    |1                                d          }|                     |          S t          jA        |          r&t          |jB                                                  S t          j%        |d          st          j%        |d          r"|                     t?          |                    S t          j%        |d          r&t          |jC                                                  S t          |t                    r|                     ||j)                   |                     ||E                                           |                     ||F                                           |                                S t          |d          rt          |t          jI                  st          |t          jK                  rt          |dd          }|                     ||           |                     |t          jN        O                    |                     |                     ||E                                           |                                S t          |t                    r!|                     |jQ        |jR        g          S t          |t          jS                  st          |t          jT                  rd|                     ||E                                           |                     ||F                                           |                                S t          j%        |d          r&t          |jB                                                  S t          jU        |          r|                     |jB                  S t          jV        |          r|                     |jB                  S t          |t          jX                  re|                     ||jY                   |                     ||jZ                   |                     ||j[                   |                                S 	 |\                                }n!# t          $ r}t                      |d}~ww xY w|D ]}|                     ||           |                                S )zHash objects to bytes, including code with dependencies.

        Python's built in `hash` does not produce consistent results across
        runs.
        md5)r   r   N   0T   1Fzpandas.core.series.Seriesr   )nrandom_states   %szpandas.core.frame.DataFrameznumpy.ndarray)r   zPIL.Image.Imageuint8)dtypezbuiltins.mappingproxyzbuiltins.dict_itemszbuiltins.getset_descriptorname
wonthappenznumpy.ufunc)r   )^hashlibnewr   r   unittestmockMockr   r]   r   r   r   r=   r   rh   r   r   rA   r   r   r   r   r   r   r   datetime	isoformatr   r   rL   digestdictr   dataclassesis_dataclassasdictr   is_typepandasr   r   r   len_PANDAS_ROWS_LARGEsample_PANDAS_SAMPLE_SIZEr   hash_pandas_objectrq   tobytes	TypeErrorpickledumpsHIGHEST_PROTOCOLshapedtypes_NP_SIZE_LARGEnumpyrandomRandomStatechoiceflat_NP_SAMPLE_SIZE
frombufferr   r   r   rE   r   tellgetvaluer>   ioIOBasetempfile_TemporaryFileWrappergetattrospathgetmtimer	   patternflagsStringIOBytesIOismoduleisclass	functoolspartialr-   funckeywords
__reduce__r   )r!   r   hr   outputexitempdcolumn_hash_bytesvalues_hash_bytesnpstatenp_arrayobj_namereduce_datas                  r&   r   z_CacheFuncHasher._to_bytesR  s	    K0000c8=-.. K	 ==C)))U## F	z#y'A'A F	J#C((D,<<<()?)D)DEI"3   #yT_   ==(((S!! x	::<<U## u	"3'''S!! r	 %%%TY'' o	9X.// l	==??))+++dE]++ i	 % %At$$$$88::T"" d			 % %At$$$$88::[4D[[4E\\4%c** V	==!3C!8!8999T"" S	s88??$$$s$?@@ P	KK38$$$KK39>***3xx---jj#6QjGGJArw99#>>EMMOOPPPxxzz! J J J v|C1HIIIIIIJ
 s$ABB 	KK39%%%3xx---jj#6QjGGJ$(MMG..sz::% %! A0111$(MM"'2L2LS2Q2Q$R$R!A0111xxzz! J J J v|C1HIIIIIIJ
 sO44 k	KK39%%%KK3sy>>***x>))""""	--a00ll38/lBBKK3;;==)))88::s$566 _	 }}S[[]]'}BBH==***s## W	,,..///s$;<< T		@Q&A
 A
 T	 ==c+++s$@AA O	)0022333\** L	 KK38$$$KK388::&&&KK3<<>>***88::S&!! C	sBI&&C	 #x=>>C	 sFL99HKK8$$$KK27++H55666KK388::&&&88::W%% 3	==#+sy!9:::R[)) 0	ZRZ-H-H 0	 KK388::&&&KK3<<>>***88::sM22 )	,,..///c"" %	 ==..._S!! 	 ==...Y.// 	 KK38$$$KK38$$$KK3<(((88::4!nn.. 4 4 4)++34 $ % %At$$$$88::sV   ?C 
C3C..C3AO! !,PP=BT ,U
Uj1 1
k;k

kr   )r   rD   rE   rr   r   r   HashFuncsDictr   rA   rZ   r   r   r   rL   r   rk   r'   r&   rK   rK     s        55 LP% %#%19-1H% % % %2 #         C  E        D# $    
TS TU T T T T T Tr'   rK   c                       e Zd ZdZdS )r   z<Placeholder class for return values when None is meaningful.N)r   rD   rE   rr   rk   r'   r&   r   r   )  s        FFDr'   r   )NN)>rr   rS   r   r   r   r   r   r   r   r   r   r   r   r{   unittest.mockr   r   rv   enumr   typingr   r   r   r   r   r	   r
   r   	streamlitr   r   streamlit.errorsr   &streamlit.runtime.caching.cache_errorsr   $streamlit.runtime.caching.cache_typer   'streamlit.runtime.uploaded_file_managerr   streamlit.utilr   r   r   r   r   rA   r  r   r   rO   rQ   rt   r:   r   r   r   r   r   r   rK   r   rk   r'   r&   <module>r     s   7 6                				 				   



                  L L L L L L L L L L L L L L L L L L L L % % % % % % % % 2 2 2 2 2 2 F F F F F F : : : : : : @ @ @ @ @ @ ) ) ) ) ) )    U3S	>*HcUCZ,@@A U N
 N
 N
 N
 N
) N
 N
 N
j 15*. 	  (38,-	
 ' 
   $%L %L %L %L %L %L %L %LP       0 kmm8S 8U 8 8 8 8
 u          
 hsm          FZ Z Z Z Z Z Z Zz	 	 	 	 	 	 	 	 	 	r'   