a _hk@sdZgdZddlZddlZddlZddlZ ddl Z ddl Z ddlmZddlZddlZddlZddlZejZe je jBe jBZee dree jOZeZ ee dre e j!OZ ee dre j"Z"ndZ"d Z#eZ$d d Z%d d Z&ddZ'GdddZ(ddZ)ddZ*da+ddZ,ddZ-ddZ.ddZ/da0ddZ1d d!Z2d=d#d$Z3d>d%d&Z4d'e#dfd(d)Z5Gd*d+d+Z6Gd,d-d-Z7d?dd1d2d3Z8e j9d4ksej:d5kre8Z;nee d6aZ?dS)AaTemporary files. This module provides generic, low- and high-level interfaces for creating temporary files and directories. All of the interfaces provided by this module can be used without fear of race conditions except for 'mktemp'. 'mktemp' is subject to race conditions and should not be used; it is provided for backward compatibility only. The default path names are returned as str. If you supply bytes as input, all return values will be in bytes. Ex: >>> tempfile.mkstemp() (4, '/tmp/tmptpu9nin8') >>> tempfile.mkdtemp(suffix=b'') b'/tmp/tmppbi8f0hy' This module also provides some data items to the user: TMP_MAX - maximum number of names that will be tried before giving up. tempdir - If this is set to a string before the first use of any routine from this module, it will be considered as another candidate location to store temporary files. ) NamedTemporaryFile TemporaryFileSpooledTemporaryFileTemporaryDirectorymkstempmkdtempmktempTMP_MAX gettempprefixtempdir gettempdirgettempprefixb gettempdirbN)Random O_NOFOLLOWO_BINARYri'tmpcCs,zt|Wnty"YdS0dSdS)NFT)_oslstatOSError)fnr)/opt/imh-python/lib/python3.9/tempfile.py_existsLs  rcGs\d}|D]B}|durqt|tr6|tur0tdt}q|turFtdt}q|durXtS|S)zBLook at the type of all args and divine their implied return type.Nz1Can't mix bytes and non-bytes in path components.) isinstancebytesstr TypeError)args return_typeargrrr_infer_return_typeUs r!cCsdt|||}|dur|}|dur:|tur0t}n tt}|durX|turRt}nt}||||fS)z9Common parameter processing for most APIs in this module.N)r!rtemplaterfsencoder r )prefixsuffixdir output_typerrr_sanitize_paramsjs  r(c@s0eZdZdZdZeddZddZddZd S) _RandomNameSequencea,An instance of _RandomNameSequence generates an endless sequence of unpredictable strings which can safely be incorporated into file names. Each string is eight characters long. Multiple threads can safely use the same instance at the same time. _RandomNameSequence is an iterator.%abcdefghijklmnopqrstuvwxyz0123456789_cCs,t}|t|ddkr&t|_||_|jS)N_rng_pid)rgetpidgetattr_Random_rngr+)selfcur_pidrrrrngs z_RandomNameSequence.rngcCs|SNrr0rrr__iter__sz_RandomNameSequence.__iter__cs0|j|jjfddtdD}d|S)Ncsg|] }qSrr).0dummycchooserr z0_RandomNameSequence.__next__..) charactersr2choicerangejoin)r0lettersrr8r__next__sz_RandomNameSequence.__next__N) __name__ __module__ __qualname____doc__r?propertyr2r5rDrrrrr)|s  r)c Csg}dD]}t|}|r||qtjdkrX|tjdtjdddddgn|gd z|tWn"t t fy|tj Yn0|S) z[Generate a list of candidate temporary directories which _get_default_tempdir will try.)TMPDIRTEMPTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmp)z/tmpz/var/tmpz/usr/tmp) rgetenvappendnameextendpath expanduser expandvarsgetcwdAttributeErrorrcurdir)dirlistenvnamedirnamerrr_candidate_tempdir_lists   r[c Csht}t}|D]@}|tjkr,tj|}tdD]}t|}tj||}zt |t d}zdzHt j |ddd}| dWdn1s0YWt |n t |0Wt|n t|0|WStyYq4ty6tjdkr,tj|r,t|tjr,Yq4YqYq4tyNYqYq40q4qttjd |dS) aqCalculate the default directory to use for temporary files. This routine should be called exactly once. We determine whether or not a candidate temp dir is usable by trying to create and write to a file in that directory. If this is successful, the test file is deleted. To prevent denial of service, the name of the test file must be randomized.dwbF)closefdsblatNrMz)No usable temporary directory found in %s)r)r[rrWrRabspathrAnextrBopen_bin_openflags_iowritecloseunlinkFileExistsErrorPermissionErrorrPisdiraccessW_OKrFileNotFoundError_errnoENOENT)namerrXr&seqrPfilenamefdfprrr_get_default_tempdirs@    *   rucCs:tdur6tztdur taWtn t0tS)z7Common setup sequence for all user-callable interfaces.N)_name_sequence _once_lockacquirer)releaserrrr_get_candidate_namessrzc Cst}|turttj|}ttD]}t|}tj ||||}t d|zt ||d} WnRt yxYq"Yn>tytjdkrtj|rt|tjrYq"nYn0| tj|fSt tjddS)z>Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.ztempfile.mkstempr]rMz#No usable temporary file name foundN)rzrmaprr#rArrarRrB_sysauditrbrhrirPrjrkrlr`rnEEXIST) r&presufflagsr'namesrqrPfilersrrr_mkstemp_inners*      rcCstS)z-The default prefix for temporary directories.)r"rrrrr sr cCs ttS)z6The default prefix for temporary directories as bytes.)rr#r rrrrr sr cCs:tdur6tztdur taWtn t0tS)zAccessor for tempfile.tempdir.N)r rwrxruryrrrrr sr cCs ttS)z)A bytes version of tempfile.gettempdir().)rr#r rrrrr $sr FcCs2t|||\}}}}|rt}nt}t|||||S)aUser-callable function to create and return a unique temporary file. The return value is a pair (fd, name) where fd is the file descriptor returned by os.open, and name is the filename. If 'suffix' is not None, the file name will end with that suffix, otherwise there will be no suffix. If 'prefix' is not None, the file name will begin with that prefix, otherwise a default prefix is used. If 'dir' is not None, the file will be created in that directory, otherwise a default directory is used. If 'text' is specified and true, the file is opened in text mode. Else (the default) the file is opened in binary mode. If any of 'suffix', 'prefix' and 'dir' are not None, they must be the same type. If they are bytes, the returned name will be bytes; str otherwise. The file is readable and writable only by the creating user ID. If the operating system uses permission bits to indicate whether a file is executable, the file is executable by no one. The file descriptor is not inherited by children of this process. Caller is responsible for deleting the file when done with it. )r(_text_openflagsrcr)r%r$r&textr'rrrrr(s rc Cst|||\}}}}t}|tur.ttj|}ttD]}t|}tj ||||}t d|zt |dWnRtyYq6Yn>tytjdkrtj |rt|tjrYq6nYn0|SttjddS)aUser-callable function to create and return a unique temporary directory. The return value is the pathname of the directory. Arguments are as for mkstemp, except that the 'text' argument is not accepted. The directory is readable, writable, and searchable only by the creating user. Caller is responsible for deleting the directory when done with it. ztempfile.mkdtemprMz(No usable temporary directory name foundN)r(rzrr{rr#rArrarRrBr|r}mkdirrhrirPrjrkrlrnr~)r%r$r&r'rrqrPrrrrrOs,       rr>cCs`|durt}t}ttD]2}t|}tj||||}t|s|Sqt t j ddS)aUser-callable function to return a unique temporary file name. The file is not created. Arguments are similar to mkstemp, except that the 'text' argument is not accepted, and suffix=None, prefix=None and bytes file names are not supported. THIS FUNCTION IS UNSAFE AND SHOULD NOT BE USED. The file name may refer to a file that did not exist at some point, but by the time you get around to creating it, someone else may have beaten you to the punch. Nz"No usable temporary filename found) r rzrArrarrRrBrrhrnr~)r%r$r&rrqrPrrrrrws  rc@sLeZdZdZdZdZd ddZejdkr@ej fdd Z d d Z nd d Z dS)_TemporaryFileCloserzA separate object allowing proper closing of a temporary file's underlying file object, without adding a __del__ method to the temporary file.NFTcCs||_||_||_dSr3)rrPdeleter0rrPrrrr__init__sz_TemporaryFileCloser.__init__rMcCsL|jsH|jdurHd|_z|jW|jrH||jn|jrF||j0dSNT) close_calledrrfrrP)r0rgrrrrfs  z_TemporaryFileCloser.closecCs |dSr3)rfr4rrr__del__sz_TemporaryFileCloser.__del__cCs|jsd|_|jdSr)rrrfr4rrrrfs)T) rErFrGrHrrrrrPrgrfrrrrrrs   rc@sBeZdZdZdddZddZddZd d Zd d Zd dZ dS)_TemporaryFileWrapperzTemporary file wrapper This class provides a wrapper around files opened for temporary use. In particular, it seeks to automatically remove the file when it is no longer needed. TcCs$||_||_||_t||||_dSr3)rrPrr_closerrrrrrsz_TemporaryFileWrapper.__init__cs^|jd}t||}t|drD|tfdd}|j|_|}t|tsZt||||S)Nr__call__cs|i|Sr3r)rkwargsfuncrr func_wrappersz7_TemporaryFileWrapper.__getattr__..func_wrapper) __dict__r-hasattr _functoolswrapsrrintsetattr)r0rPrarrrr __getattr__s     z!_TemporaryFileWrapper.__getattr__cCs|j|Sr3)r __enter__r4rrrrs z_TemporaryFileWrapper.__enter__cCs|j|||}||Sr3)r__exit__rf)r0excvaluetbresultrrrrsz_TemporaryFileWrapper.__exit__cCs|jdS)zA Close the temporary file, possibly deleting it. N)rrfr4rrrrfsz_TemporaryFileWrapper.closeccs|jD] }|VqdSr3)r)r0linerrrr5s z_TemporaryFileWrapper.__iter__N)T) rErFrGrHrrrrrfr5rrrrrs rw+bTerrorscCst|||\}}}} t} tjdkr0|r0| tjO} t|||| | \} } z$tj| |||||d} t| | |WSt yt | t | Yn0dS)aCreate and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'delete' -- whether the file is deleted on close (default True). 'errors' -- the errors argument to io.open (default None) The file is created as mkstemp() would do it. Returns an object with a file-like interface; the name of the file is accessible as its 'name' attribute. The file will be automatically deleted when it is closed unless the 'delete' argument is set to False. rM bufferingnewlineencodingrN) r(rcrrP O_TEMPORARYrrdrbr BaseExceptionrgrf)moderrrr%r$r&rrr'rrsrPrrrrrs     rposixcygwin O_TMPFILEc Cst|||\}}}}t} trz$| tjBtj@} t|| d} Wn&tyVdaYnDtyfYn40zt j| |||||dWSt | Yn0t |||| |\} } z"t | t j| |||||dWSt | Yn0dS)aCreate and return a temporary file. Arguments: 'prefix', 'suffix', 'dir' -- as for mkstemp. 'mode' -- the mode argument to io.open (default "w+b"). 'buffering' -- the buffer size argument to io.open (default -1). 'encoding' -- the encoding argument to io.open (default None) 'newline' -- the newline argument to io.open (default None) 'errors' -- the errors argument to io.open (default None) The file is created as mkstemp() would do it. Returns an object with a file-like interface. The file has no name, and will cease to exist when it is closed. r]FrN) r(rc_O_TMPFILE_WORKSrrO_CREATrbIsADirectoryErrorrrdrfrrg) rrrrr%r$r&rr'rflags2rsrPrrrr3s6       rc@seZdZdZdZd8dddd ZeejZ d d Z d d Z ddZ ddZ ddZddZeddZeddZeddZddZddZd d!Zed"d#Zed$d%Zed&d'Zd(d)Zd*d+Zd,d-Zd.d/Zd0d1Zd9d2d3Zd4d5Z d6d7Z!dS):rzTemporary file wrapper, specialized to switch from BytesIO or StringIO to a real file when it exceeds a certain size or when a fileno is needed. FrrrNrc  CsTd|vrt|_ntjt|| |d|_||_d|_|||||||| d|_dS)Nb)rrrF)rrr%r$rrr&r)rdBytesIO_file TextIOWrapper _max_size_rolled_TemporaryFileArgs) r0max_sizerrrrr%r$r&rrrrrvs  zSpooledTemporaryFile.__init__cCs,|jr dS|j}|r(||kr(|dSr3)rrtellrollover)r0rrrrr_checks zSpooledTemporaryFile._checkcCsv|jr dS|j}tfi|j}|_|`|}t|drR|j| n|| | |dd|_dS)NbufferrT) rrrrrrrredetachgetvalueseek)r0rnewfileposrrrrs   zSpooledTemporaryFile.rollovercCs|jjrtd|S)Nz%Cannot enter context with closed file)rclosed ValueErrorr4rrrrszSpooledTemporaryFile.__enter__cCs|jdSr3rrfr0rrrrrrrszSpooledTemporaryFile.__exit__cCs |jSr3)rr5r4rrrr5szSpooledTemporaryFile.__iter__cCs|jdSr3rr4rrrrfszSpooledTemporaryFile.closecCs|jjSr3)rrr4rrrrszSpooledTemporaryFile.closedcCs|jjSr3)rrr4rrrrszSpooledTemporaryFile.encodingcCs|jjSr3)rrr4rrrrszSpooledTemporaryFile.errorscCs||jSr3)rrfilenor4rrrrszSpooledTemporaryFile.filenocCs|jdSr3)rflushr4rrrrszSpooledTemporaryFile.flushcCs |jSr3)risattyr4rrrrszSpooledTemporaryFile.isattycCs,z |jjWSty&|jdYS0dS)Nr)rrrVrr4rrrrs  zSpooledTemporaryFile.modecCs$z |jjWStyYdS0dSr3)rrPrVr4rrrrPs  zSpooledTemporaryFile.namecCs|jjSr3)rnewlinesr4rrrrszSpooledTemporaryFile.newlinescGs |jj|Sr3)rreadr0rrrrrszSpooledTemporaryFile.readcGs |jj|Sr3)rreadlinerrrrrszSpooledTemporaryFile.readlinecGs |jj|Sr3)r readlinesrrrrrszSpooledTemporaryFile.readlinescGs |jj|Sr3)rrrrrrrszSpooledTemporaryFile.seekcCs |jSr3)rrr4rrrrszSpooledTemporaryFile.tellcCs6|dur|jn||jkr&||j|dSr3)rtruncaterr)r0sizerrrrs   zSpooledTemporaryFile.truncatecCs|j}||}|||Sr3)rrer)r0srrvrrrres  zSpooledTemporaryFile.writecCs|j}||}|||Sr3)r writelinesr)r0iterablerrrrrrs  zSpooledTemporaryFile.writelines)rrrNNNNN)N)"rErFrGrHrr classmethod_types GenericAlias__class_getitem__rrrrr5rfrIrrrrrrrrPrrrrrrrrerrrrrrosL         rc@s\eZdZdZdddZeddZeddZd d Zd d Z d dZ ddZ ee j ZdS)ra+Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager. For example: with TemporaryDirectory() as tmpdir: ... Upon exiting the context, the directory and everything contained in it are removed. NcCs0t||||_tj||j|jd|d|_dS)NzImplicitly cleaning up {!r}) warn_message)rrP_weakreffinalize_cleanupformat _finalizer)r0r%r$r&rrrr s  zTemporaryDirectory.__init__cs fdd}tj|ddS)Nc st|dtrdd}zT|kr0|tj|||zt|Wn ttfyf|Yn0Wqty|Yq0nt|dtrndS)NrcSs4zt|dWnty"Yn0t|ddS)Nrr)rchflagsrVchmod)rRrrr resetpermss  z?TemporaryDirectory._rmtree..onerror..resetperms) issubclassrirrRrZrgr_rmtreerm)rrRexc_inforclsrPrronerrors z+TemporaryDirectory._rmtree..onerror)r)_shutilrmtree)rrPrrrrrszTemporaryDirectory._rmtreecCs||t|tdSr3)r _warningswarnResourceWarning)rrPrrrrr.s zTemporaryDirectory._cleanupcCsd|jj|jS)Nz <{} {!r}>)r __class__rErPr4rrr__repr__3szTemporaryDirectory.__repr__cCs|jSr3)rPr4rrrr6szTemporaryDirectory.__enter__cCs |dSr3)cleanuprrrrr9szTemporaryDirectory.__exit__cCs|jr||jdSr3)rrrrPr4rrrr<s zTemporaryDirectory.cleanup)NNN)rErFrGrHrrrrrrrrrrrrrrrrs   r)NNNF)NNN)rrNNNNNT)rrNNNNN)@rH__all__ functoolsrwarningsriordosrshutilrerrnornrandomrr.sysr|typesrweakrefr_thread allocate_lock_allocate_lockO_RDWRrO_EXCLrrrrcrrr"rwrr!r(r)r[rurvrzrr r r r r rrrrrrrPplatformrrrobjectrrrrrsz        -  ' ( +? '  <