
    F\hI                        S r SS/rSSKJr  SSKrSSKrSSKrSSKrSSKrSSK	r	\R                  S:X  a  SSKrSrOSSKrSrS	S
KJr  \R                   \R"                  -  rSr\(       a  SrOSrS r " S S5      rSr " S S5      rg)zProvides shared memory for direct access across processes.

The API of this package is currently provisional. Refer to the
documentation for details.
SharedMemoryShareableList    )partialNntFT   )resource_tracker   z/psm_wnsm_c                      [         [        [        5      -
  S-  n U S:  d   S5       e[        [        R                  " U 5      -   n[        U5      [         ::  d   eU$ )z6Create a random filename for the shared memory object.   z_SHM_NAME_PREFIX too long)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXsecrets	token_hex)nbytesnames     4/usr/lib/python3.13/multiprocessing/shared_memory.py_make_filenamer   (   sX     $c*:&;;AFQ;333;g//77Dt9----K    c                       \ rS rSrSrSrSrSrSr\	R                  rSr\(       a  SOSrSrSSS.S	 jjrS
 rS rS r\S 5       r\S 5       r\S 5       rS rS rSrg)r   2   a  Creates a new shared memory block or attaches to an existing
shared memory block.

Every shared memory block is assigned a unique name.  This enables
one process to create a shared memory block with a particular name
so that a different process can attach to that same shared memory
block using that same name.

As a resource for sharing data across processes, shared memory blocks
may outlive the original process that created them.  When one process
no longer needs access to a shared memory block that might still be
needed by other processes, the close() method should be called.
When a shared memory block is no longer needed by any process, the
unlink() method should be called to ensure proper cleanup.Ni  TF)trackc                   US:  d  [        S5      eU(       a-  [        [        R                  -  U l        US:X  a  [        S5      eUc-  U R                  [        R
                  -  (       d  [        S5      eX@l        [        (       GaH  UcB   [        5       n [        R                  " UU R                  U R                  S9U l        Xl        OMU R                  (       a  SU-   OUn[        R                  " UU R                  U R                  S9U l        Xl         U(       a(  U(       a!  [        R                   " U R                  U5        [        R"                  " U R                  5      nUR$                  n[&        R&                  " U R                  U5      U l        U R                  (       a!  [.        R0                  " U R                  S5        GOU(       Ga'   Uc
  [        5       OUn[2        R4                  " [2        R6                  [2        R8                  [2        R:                  US-	  S	-  US	-  U5      n [2        R<                  " 5       nU[2        R>                  :X  ah  UbL  [        [@        RB                  [        RD                  " [@        RB                  5      U[2        R>                  5      e [2        RF                  " U5        M  [&        R&                  " S
X6S9U l        [2        RF                  " U5        X`l        OXl        [2        RH                  " [2        RJ                  SU5      n [2        RL                  " U[2        RJ                  SSS5      n	[2        RF                  " U5         [2        RN                  " U	5      n[2        RP                  " U	5        [&        R&                  " S
X1S9U l        X0l)        [U        U R(                  5      U l+        g ! [         a     GMU  f = f! [*         a    U R-                  5         e f = f! [2        RF                  " U5        f = f! [2        RF                  " U5        f = f! [2        RP                  " U	5        f = f)Nr   z!'size' must be a positive integerz4'size' must be a positive number different from zeroz&'name' can only be None if create=True)mode/shared_memory    l    r   )tagnameF),
ValueError_O_CREXosO_RDWR_flagsO_EXCL_track
_USE_POSIXr   _posixshmemshm_open_mode_fdFileExistsError_name_prepend_leading_slash	ftruncatefstatst_sizemmap_mmapOSErrorunlinkr   register_winapiCreateFileMappingINVALID_HANDLE_VALUENULLPAGE_READWRITEGetLastErrorERROR_ALREADY_EXISTSerrnoEEXISTstrerrorCloseHandleOpenFileMappingFILE_MAP_READMapViewOfFileVirtualQuerySizeUnmapViewOfFile_size
memoryview_buf)
selfr   createsizer   stats	temp_nameh_maplast_error_codep_bufs
             r   __init__SharedMemory.__init__L   sP   qy@AA!BII-DKqy !WXX<bii 7EFF: |)+D!#.#7#7  KK!%$ "&J%)%@%@sTzd&//KK
 "
dLL40*}}!YYtxx6
 {{ ))$**oF 48L 0dI $5544..z1z)!E3*1*>*>*@*g.J.JJ#/&5$)LL$&KK$=$($+$@$@	'" !" !)  ++E2 &*YYr4%K
++E2!*J "
  //))
	/#11--E ''.3"33E:D++E2!YYr4>

tzz*	k + ! !$  J  ++E2, ''. ++E2sJ   /O BO$ 7A8P P ,(P +P9 
O! O!$P PP69Qc                 F     U R                  5         g ! [         a     g f = fN)closer5   rK   s    r   __del__SharedMemory.__del__   s"    	JJL 		s    
  c                 L    U R                   U R                  SU R                  44$ )NF)	__class__r   rM   rX   s    r   
__reduce__SharedMemory.__reduce__   s)    NN				
 	
r   c                 j    U R                   R                   SU R                  < SU R                   S3$ )N(z, size=))r\   __name__r   rM   rX   s    r   __repr__SharedMemory.__repr__   s.    ..))*!DII=		{!LLr   c                     U R                   $ )z4A memoryview of contents of the shared memory block.)rJ   rX   s    r   bufSharedMemory.buf   s     yyr   c                     U R                   n[        (       a@  U R                  (       a/  U R                   R                  S5      (       a  U R                   SS nU$ )z4Unique name that identifies the shared memory block.r   r   N)r.   r(   r/   
startswith)rK   reported_names     r   r   SharedMemory.name   sD     

:$55zz$$S)) $

12r   c                     U R                   $ )zSize in bytes.)rH   rX   s    r   rM   SharedMemory.size   s     zzr   c                 D   U R                   b!  U R                   R                  5         SU l         U R                  b!  U R                  R                  5         SU l        [        (       a9  U R
                  S:  a(  [        R                  " U R
                  5        SU l        ggg)zcCloses access to the shared memory from this instance but does
not destroy the shared memory block.Nr   r   )rJ   releaser4   rW   r(   r,   r#   rX   s    r   rW   SharedMemory.close   ss     99 IIDI::!JJDJ:$((a-HHTXXDH (:r   c                     [         (       af  U R                  (       aT  [        R                  " U R                  5        U R                  (       a"  [
        R                  " U R                  S5        gggg)a  Requests that the underlying shared memory block be destroyed.

Unlink should be called once (and only once) across all handles
which have access to the shared memory block, even if these
handles belong to different processes. Closing and unlinking may
happen in any order, but trying to access data inside a shared
memory block after unlinking may result in memory errors,
depending on platform.

This method has no effect on Windows, where the only way to
delete a shared memory block is to close all handles.r   N)r(   r.   r)   
shm_unlinkr'   r   
unregisterrX   s    r   r6   SharedMemory.unlink   sH     :$**""4::.{{ ++DJJH  %:r   )rJ   r,   r%   r4   r.   rH   r'   )NFr   )rb   
__module____qualname____firstlineno____doc__r.   r,   r4   rJ   r#   r$   r%   r+   r(   r/   r'   rS   rY   r]   rc   propertyrf   r   rM   rW   r6   __static_attributes__ r   r   r   r   2   s    B  E
CEDYYFE%/TUFm+ m+^
M      Ir   utf8c                   T   \ rS rSrSr\S\S\S\S\	SSR                  S0rS	rS
 S S S S.r\S 5       rS$SS.S jjrS rS rS rS rS rS rS rS r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S 5       r\S  5       r S! r!S" r"\#" \$RJ                  5      r&S#r'g)%r   i  aq  Pattern for a mutable list-like object shareable via a shared
memory block.  It differs from the built-in list type in that these
lists can not change their overall length (i.e. no append, insert,
etc.)

Because values are packed into a memoryview as bytes, the struct
packing format for any storable value must require no more than 8
characters to describe its format.qdzxxxxxxx?z%dsNzxxxxxx?x   c                     U $ rV   r{   values    r   <lambda>ShareableList.<lambda>   s    r   c                 J    U R                  S5      R                  [        5      $ N    )rstripdecode	_encodingr   s    r   r   r   !  s    g.55i@r   c                 $    U R                  S5      $ r   )r   r   s    r   r   r   "  s    g.r   c                     g rV   r{   )_values    r   r   r   #  s    $r   )r   r   r      c                     [        U [        [        SR                  45      (       d  g[        U [        5      (       a  g[        U [        5      (       a  gg)zUsed in concert with _back_transforms_mapping to convert values
into the appropriate Python objects when retrieving them from
the list as well as when storing them.Nr   r   r   r   )
isinstancestrbytesr\   r   s    r   _extract_recreation_code&ShareableList._extract_recreation_code&  sB    
 %#udnn!=>>s##u%%r   r   c                  ^	 Ub  UGb  U=(       d    SnU Vs/ s Hx  n[        U[        [        45      (       d  U R                  [	        U5         OAU R                  [	        U5         U R
                  [        U5      U R
                  -  S-   -  4-  PMz     nn[        U5      U l        [        S U 5       5      U R                  :X  d   eSnS/U l	        U HC  nXVS   S:w  a  U R
                  O[        US S 5      -  nU R                  R                  U5        ME     U Vs/ s H  o0R                  U5      PM     nn[        R                  " SU R                  -   SR!                  U5      -   U R"                  -   U R$                  -   5      n['        US	US
9U l        O['        U5      U l        UGb2  [*        m	[        R,                  " SU R                  -   U R(                  R.                  SU R                  /U R                  Q76   [        R,                  " SR!                  W5      U R(                  R.                  U R0                  /U	4S jU 5       Q76   [        R,                  " U R"                  U R(                  R.                  U R2                  /U	4S jU 5       Q76   [        R,                  " U R$                  U R(                  R.                  U R4                  /WQ76   g [        U 5      U l        [7        [        R8                  " U R                  U R(                  R.                  S5      5      U l	        g s  snf s  snf )Nr{   r   c              3   >   #    U  H  n[        U5      S :*  v   M     g7f)r   N)r   ).0fmts     r   	<genexpr>)ShareableList.__init__.<locals>.<genexpr>@  s     9s3x1}s   r   r   sr~    T)rL   rM   c              3   t   >#    U  H-  n[        U[        5      (       a  UR                  T5      OUv   M/     g 7frV   )r   r   encoder   v_encs     r   r   r   d  s*     Px!Jq#$6$6!((4.A=xs   58c              3   D   >#    U  H  oR                  T5      v   M     g 7frV   )r   r   s     r   r   r   j  s     3(Q((4..(s    r   )r   r   r   _types_mappingtype
_alignmentr   	_list_lensum_allocated_offsetsintappendr   structcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesr   shmr   	pack_intorf   _offset_data_start_offset_packing_formats_offset_back_transform_codeslistunpack_from)
rK   sequencer   item_formatsoffsetr   _recreation_codesrequested_sizer   s
            @r   rS   ShareableList.__init__4  s   <8/~2H % %D	 &dS%L99 ##DJ/,,T$Z83t9+G!+KL< 
 %   !]DN999T^^KKKF ()cD#W^$//S"XN''..v6   AI!@H--d3  ! $__d000!"--. 112N $DNKDH#D)DHDd000	
 )) !'' QxP	 --,, 4(3	 1111 $	 !YDN&*""..HHLL'D#}$!s   A?L9"L>c                 P   US:  a  UOXR                   -   nXR                   :  d  U R                   S:  a  [        S5      e[        R                  " SU R                  R
                  U R                  US-  -   5      S   nUR                  S5      nUR                  [        5      nU$ )z>Gets the packing format for a single value stored in the list.r    Requested position out of range.8sr   r   )
r   
IndexErrorr   r   r   rf   r   r   r   r   )rK   positionr   r   
fmt_as_strs        r   _get_packing_format!ShareableList._get_packing_format}  s    '1}8(^^2K&DNNQ,>?@@HHLL((8a<7
 	
 hhwZZ	*
r   c                     XR                   :  d  U R                   S:  a  [        S5      e[        R                  " SU R                  R
                  U R                  U-   5      S   nU R                  U   nU$ )z9Gets the back transformation function for a single value.r   r   b)r   r   r   r   r   rf   r   _back_transforms_mapping)rK   r   transform_codetransform_functions       r   _get_back_transform!ShareableList._get_back_transform  st     &DNNQ,>?@@++HHLL--8
 	
 "::>J!!r   c                    XR                   :  d  U R                   S:  a  [        S5      e[        R                  " SU R                  R
                  U R                  US-  -   UR                  [        5      5        U R                  U5      n[        R                  " SU R                  R
                  U R                  U-   U5        g)znSets the packing format and back transformation code for a
single value in the list at the specified position.r   r   r   r   r   N)r   r   r   r   r   rf   r   r   r   r   r   )rK   r   r   r   r   s        r   !_set_packing_format_and_transform/ShareableList._set_packing_format_and_transform  s     &DNNQ,>?@@HHLL((8a<7i(		
 66u=HHLL--8		
r   c                 J   US:  a  UOXR                   -   n U R                  U R                  U   -   n[        R                  " U R                  U5      U R                  R                  U5      u  nU R                  U5      nU" U5      nU$ ! [         a    [        S5      ef = f)Nr   zindex out of range)
r   r   r   r   r   r   r   rf   r   r   )rK   r   r   r   back_transforms        r   __getitem__ShareableList.__getitem__  s    '1}8(^^2K	3,,t/F/Fx/PPF%%((2DQ 11(;1  	3122	3s   AB B"c                    US:  a  UOXR                   -   n U R                  U   nU R                  U-   nU R                  U5      n[        U[        [        45      (       d  U R                  [        U5         nUnO~U R                  US-      U-
  n[        U[        5      (       a  UR                  [        5      OUn[        U5      U:  a  [        S5      eUS   S:X  a  UnOU R                  [           U4-  nU R                  UUU5        [        R                   " X`R"                  R$                  XG5        g ! [         a    [	        S5      ef = f)Nr   zassignment index out of ranger   z(bytes/str item exceeds available storager   r   )r   r   r   r   r   r   r   r   r   r   r   r   r   r!   r   r   r   r   rf   )	rK   r   r   item_offsetr   current_format
new_formatencoded_valueallocated_lengths	            r   __setitem__ShareableList.__setitem__  sO   '1}8(^^2K	>11(;K,,{:F!55h?N %#u..,,T%[9J!M#66x!|D{R !+5# 6 6 #\\)4<A =!$44 !KLLb!S(+
!005$9 
 	..	

 	XX\\6I3  	><==	>s   /D: :Ec                 V    [        U R                  U R                  R                  S9S4$ )Nr   r{   )r   r\   r   r   rX   s    r   r]   ShareableList.__reduce__  s     t~~DHHMM:B>>r   c                 `    [         R                  " SU R                  R                  S5      S   $ )Nr~   r   )r   r   r   rf   rX   s    r   __len__ShareableList.__len__  s$    !!#txx||Q7::r   c                 |    U R                   R                   S[        U 5       SU R                  R                  < S3$ )Nr`   z, name=ra   )r\   rb   r   r   r   rX   s    r   rc   ShareableList.__repr__  s3    ..))*!DJ<wtxx}}>OqQQr   c                 `   ^  SR                  U 4S j[        T R                  5       5       5      $ )z=The struct packing format used by all currently stored items.r   c              3   F   >#    U  H  nTR                  U5      v   M     g 7frV   )r   )r   irK   s     r   r   'ShareableList.format.<locals>.<genexpr>  s#      
1FAD$$Q''1Fs   !)r   ranger   rX   s   `r   formatShareableList.format  s,     ww 
16t~~1F
 
 	
r   c                 &    SU R                   S-   -  $ )z>The struct packing format used for the items' storage offsets.r~   r   r   rX   s    r   r   #ShareableList._format_size_metainfo  s     dnnq())r   c                      SU R                   -  $ )z>The struct packing format used for the items' packing formats.r   r   rX   s    r   r   &ShareableList._format_packing_metainfo  s     dnn$$r   c                      SU R                   -  $ )z>The struct packing format used for the items' back transforms.r   r   rX   s    r   r   *ShareableList._format_back_transform_codes  s     T^^##r   c                 &    U R                   S-   S-  $ )Nr   r   r   rX   s    r   r    ShareableList._offset_data_start  s     "a''r   c                 :    U R                   U R                  S   -   $ )Nr   )r   r   rX   s    r   r   %ShareableList._offset_packing_formats	  s    &&)@)@)DDDr   c                 :    U R                   U R                  S-  -   $ )Nr   )r   r   rX   s    r   r   *ShareableList._offset_back_transform_codes  s    ++dnnq.@@@r   c                 .   ^ [        U4S jU  5       5      $ )zCL.count(value) -> integer -- return number of occurrences of value.c              3   .   >#    U  H
  nTU:H  v   M     g 7frV   r{   )r   entryr   s     r   r   &ShareableList.count.<locals>.<genexpr>  s     4te5E>ts   )r   )rK   r   s    `r   countShareableList.count  s     4t444r   c                 Z    [        U 5       H  u  p#X:X  d  M  Us  $    [        U< S35      e)zhL.index(value) -> integer -- return first index of value.
Raises ValueError if the value is not present.z not in this container)	enumerater!   )rK   r   r   r   s       r   indexShareableList.index  s3      )OH~  / y(>?@@r   )r   r   r   rV   )(rb   ru   rv   rw   rx   r   floatboolr   r   r\   r   r   r   staticmethodr   rS   r   r   r   r   r   r]   r   rc   ry   r   r   r   r   r   r   r   r   r  classmethodtypesGenericAlias__class_getitem__rz   r{   r   r   r   r     sI   *& 	SsjUu
N J@.	   Gd GR "
,"JB?;R 
 
 * * % % $ $ ( (
 E E A A5
A $E$6$67r   )rx   __all__	functoolsr   r3   r#   r?   r   r   r  r   r8   r(   r)   r   r   O_CREATr&   r"   r   r   r   r   r   r   r{   r   r   <module>r     s    O
-   	    77d?JJ 
**ryy
    LI LI^ 	]8 ]8r   