a dG@sdZddgZddlmZddlZddlZddlZddlZddlZddl Z ej dkr`ddl Z dZ n ddl Z dZ ejejBZd Ze rd Znd Zd d ZGdddZdZGdddZdS)zProvides shared memory for direct access across processes. The API of this package is currently provisional. Refer to the documentation for details. SharedMemory ShareableList)partialNntFTz/psm_wnsm_cCsBtttd}|dks Jdtt|}t|tks>J|S)z6Create a random filename for the shared memory object.z_SHM_NAME_PREFIX too long)_SHM_SAFE_NAME_LENGTHlen_SHM_NAME_PREFIXsecrets token_hex)nbytesnamer5/usr/lib64/python3.9/multiprocessing/shared_memory.py_make_filename's rc@seZdZdZdZdZdZdZej Z dZ e r.dndZ ddd Zd d Zd d ZddZeddZeddZeddZddZddZdS)raCreates 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.NiTFrc Cs|dkstd|r0ttjB|_|dkr0td|durL|jtj@sLtdtrD|durt}ztj ||j|j d|_ Wnt yYqZYn0||_ qqZn.|jrd|n|}tj ||j|j d|_ ||_ z<|r|rt|j |t|j }|j}t|j ||_Wnty&|Yn0ddlm}||j d n*|r |durZtn|}ttjtjtj|d ?d @|d @|}zjt}|tjkr|durt tj t!tj |tjnWt"|qJtjd ||d |_Wt"|n t"|0||_ qnqJnb||_ t#tj$d|}z"t%|tj$ddd} Wt"|n t"|0t&| }tjd ||d |_||_'t(|j|_)dS)Nrz!'size' must be a positive integerz4'size' must be a positive number different from zeroz&'name' can only be None if create=True)mode/)register shared_memory lr)tagnameF)* ValueError_O_CREXosO_RDWR_flagsO_EXCL _USE_POSIXr _posixshmemshm_open_mode_fdFileExistsError_name_prepend_leading_slash ftruncatefstatst_sizemmap_mmapOSErrorunlinkresource_trackerr_winapiCreateFileMappingINVALID_HANDLE_VALUENULLPAGE_READWRITE GetLastErrorERROR_ALREADY_EXISTSerrnoEEXISTstrerror CloseHandleOpenFileMapping FILE_MAP_READ MapViewOfFileVirtualQuerySize_size memoryview_buf) selfrcreatesizestatsr temp_nameh_maplast_error_codep_bufrrr__init__Js             zSharedMemory.__init__cCs$z |WntyYn0dSN)closer.rCrrr__del__s  zSharedMemory.__del__cCs|j|jd|jffS)NF) __class__rrErNrrr __reduce__s zSharedMemory.__reduce__cCs|jjd|jd|jdS)N(z, size=))rP__name__rrErNrrr__repr__szSharedMemory.__repr__cCs|jS)z4A memoryview of contents of the shared memory block.)rBrNrrrbufszSharedMemory.bufcCs.|j}tr*|jr*|jdr*|jdd}|S)z4Unique name that identifies the shared memory block.rrN)r'r!r( startswith)rC reported_namerrrrs   zSharedMemory.namecCs|jS)zSize in bytes.)r@rNrrrrEszSharedMemory.sizecCsX|jdur|jd|_|jdur4|jd|_trT|jdkrTt|jd|_dS)zkCloses access to the shared memory from this instance but does not destroy the shared memory block.Nrr)rBreleaser-rMr!r%rrNrrrrMs     zSharedMemory.closecCs2tr.|jr.ddlm}t|j||jddS)zRequests that the underlying shared memory block be destroyed. In order to ensure proper cleanup of resources, unlink should be called once (and only once) across all processes which have access to the shared memory block.r) unregisterrN)r!r'r0rZr" shm_unlink)rCrZrrrr/s   zSharedMemory.unlink)NFr)rT __module__ __qualname____doc__r'r%r-rBrrrr$r!r(rKrOrQrUpropertyrVrrErMr/rrrrr1s(  l     utf8c @seZdZdZedededededdj diZ dZ d d d d d d d d dZ e ddZd6ddddZddZddZddZddZddZddZd d!Zd"d#Zed$d%Zed&d'Zed(d)Zed*d+Zed,d-Zed.d/Zed0d1Zd2d3Z d4d5Z!e"e#j$Z%dS)7raPattern 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.qdzxxxxxxx?z%dsNzxxxxxx?xcCs|SrLrvaluerrrzShareableList.cCs|dtSN)rstripdecode _encodingrdrrrrfrgcCs |dSrh)rjrdrrrrfrgcCsdSrLr)_valuerrrrfrg)rrrcCs:t|ttdjfsdSt|tr$dSt|tr2dSdSdS)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.Nrrrrn) isinstancestrbytesrPrdrrr_extract_recreation_codes  z&ShareableList._extract_recreation_codercs|dus|dur|pd}fdd|D}t|_tdd|DjksPJd}dg_|D]6}||ddkrxjnt|dd7}j|q`fd d|D}td j d |j j }t |d |d _n t |_|durttjd j jjdjgjRtjd |jjjgfdd|DRtjj jjjgfdd|DRtjj jjjg|Rn$t_ttj jjd_dS)NrcsPg|]H}t|ttfs$jt|n&jt|jt|jdfqS)r)rorprq_types_mappingtype _alignmentr .0itemrNrr +s   z*ShareableList.__init__..css|]}t|dkVqdS)rcN)r )rxfmtrrr 4rgz)ShareableList.__init__..rrscsg|]}|qSr)rrrwrNrrrz=sraT)rDrEc3s&|]}t|tr|n|VqdSrL)rorpencoderxv_encrrr|Xrgc3s|]}|VqdSrL)rrrrrr|^rgrc)r _list_lensum_allocated_offsetsrvintappendstructcalcsize_format_size_metainfojoin_format_packing_metainfo_format_back_transform_codesrshmrl pack_intorV_offset_data_start_offset_packing_formats_offset_back_transform_codeslist unpack_from)rCsequencer_formatsoffsetr{_recreation_codesrequested_sizer)rrCrrK(s  &     zShareableList.__init__cCsj|dkr |n||j}||jks*|jdkr2tdtd|jj|j|dd}|d}|t }|S)z>Gets the packing format for a single value stored in the list.r Requested position out of range.8srcri) r IndexErrorrrrrVrrjrkrl)rCpositionrr{ fmt_as_strrrr_get_packing_formatqs   z!ShareableList._get_packing_formatcCsF||jks|jdkrtdtd|jj|j|d}|j|}|S)z9Gets the back transformation function for a single value.rrb)rrrrrrVr_back_transforms_mapping)rCrtransform_codetransform_functionrrr_get_back_transforms z!ShareableList._get_back_transformcCsh||jks|jdkrtdtd|jj|j|d|t| |}td|jj|j ||dS)zvSets the packing format and back transformation code for a single value in the list at the specified position.rrrrcrN) rrrrrrVrrrlrrr)rCrrrerrrr!_set_packing_format_and_transforms  z/ShareableList._set_packing_format_and_transformcCsv|dkr |n||j}z.|j|j|}t|||jj|\}Wnty^tdYn0| |}||}|S)Nrzindex out of range) rrrrrrrrVrr)rCrrrback_transformrrr __getitem__s   zShareableList.__getitem__c Cs|dkr |n||j}z"|j|}|j|}||}WntyRtdYn0t|ttfsv|jt |}|}n`|j|d|}t|tr| t n|}t ||krt d|ddkr|}n|jt|f}||||t||jj||dS)Nrzassignment index out of rangerz(bytes/str item exceeds available storagerr})rrrrrrorprqrtrurrlr rrrrrrV) rCrre item_offsetrcurrent_format new_format encoded_valueallocated_lengthrrr __setitem__s8      zShareableList.__setitem__cCst|j|jjddfS)Nrsr)rrPrrrNrrrrQszShareableList.__reduce__cCstd|jjddS)Nrar)rrrrVrNrrr__len__szShareableList.__len__cCs"|jjdt|d|jjdS)NrRz, name=rS)rPrTrrrrNrrrrUszShareableList.__repr__csdfddtjDS)z=The struct packing format used by all currently stored items.r~c3s|]}|VqdSrL)r)rxirNrrr|sz'ShareableList.format..)rrangerrNrrNrformatszShareableList.formatcCsd|jdS)z>The struct packing format used for the items' storage offsets.rarrrNrrrrsz#ShareableList._format_size_metainfocCs d|jS)z>The struct packing format used for the items' packing formats.rrrNrrrrsz&ShareableList._format_packing_metainfocCs d|jS)z>The struct packing format used for the items' back transforms.rrrNrrrrsz*ShareableList._format_back_transform_codescCs|jddS)NrrcrrNrrrrsz ShareableList._offset_data_startcCs|j|jdS)Nr)rrrNrrrrsz%ShareableList._offset_packing_formatscCs|j|jdS)Nrc)rrrNrrrrsz*ShareableList._offset_back_transform_codescstfdd|DS)zCL.count(value) -> integer -- return number of occurrences of value.c3s|]}|kVqdSrLr)rxentryrdrrr|rgz&ShareableList.count..)r)rCrerrdrcountszShareableList.countcCs4t|D]\}}||kr|Sqt|ddS)zpL.index(value) -> integer -- return first index of value. Raises ValueError if the value is not present.z not in this containerN) enumerater)rCrerrrrrindex s zShareableList.index)N)&rTr\r]r^rfloatboolrprqrPrtrvr staticmethodrrrKrrrrrrQrrUr_rrrrrrrrr classmethodtypes GenericAlias__class_getitem__rrrrrsT I!        )r^__all__ functoolsrr,rr8rr rrr1r!r"O_CREATr rr r rrrlrrrrrs.    E