o ?Ogs@sdZgdZddlZddlZddlZddlZ ddl Z ddl Z ddlmZddlZddlZddlZddlZejZe je jBe jBZee drJee jOZeZ ee drVe e j!OZ ee dr_e j"Z"ndZ"d Z#eZ$d d Z%d d Z&ddZ'GdddZ(ddZ)ddZ*da+ddZ,ddZ-ddZ.ddZ/ddZ0d d!Z1da2d"d#Z3d$d%Z4d&d'Z5dCd)d*Z6dDd+d,Z7d-e#dfd.d/Z8Gd0d1d1Z9Gd2d3d3Z:   6dEdd7d8d9Z;e jnee d<a?   dFdd7d=d>Z>Gd?d@d@Z@GdAdBdBZAdS)GaTemporary 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|WdStyYdSw)NFT)_oslstatOSError)fnr//opt/alt/python310/lib64/python3.10/tempfile.py_existsLs   rcGsd}|D],}|dur qt|tjrt|}t|tr&|tur#tdt}q|tur.tdt}q|durBtdus>tttr@tStS|S)zBLook at the type of all args and divine their implied return type.Nz1Can't mix bytes and non-bytes in path components.) isinstancerPathLikefspathbytesstr TypeErrorr )argsZ return_typeargrrr_infer_return_typeUs$   r"cCsdt|||}|dur |}|dur|turt}ntt}|dur,|tur)t}nt}||||fS)z9Common parameter processing for most APIs in this module.N)r"rtemplaterfsencoder r )prefixsuffixdir output_typerrr_sanitize_paramsrs   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.Z%abcdefghijklmnopqrstuvwxyz0123456789_cCs,t}|t|ddkrt|_||_|jS)N_rng_pid)rgetpidgetattr_RandomZ_rngr+)selfZcur_pidrrrrngs z_RandomNameSequence.rngcCs|SNrr/rrr__iter__sz_RandomNameSequence.__iter__cCsd|jj|jddS)N)k)joinr0choices charactersr2rrr__next__sz_RandomNameSequence.__next__N) __name__ __module__ __qualname____doc__r9propertyr0r3r:rrrrr*s  r*c Csg}dD]}t|}|r||qtjdkr,|tjdtjdddddgn|gd z |tW|St t fyO|tj Y|Sw) z[Generate a list of candidate temporary directories which _get_default_tempdir will try.)ZTMPDIRZTEMPZTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmp)z/tmpz/var/tmpz/usr/tmp) rgetenvappendnameextendpath expanduser expandvarsgetcwdAttributeErrorrcurdir)dirlistZenvnamedirnamerrr_candidate_tempdir_lists$   rMc Cst}t}|D]~}|tjkrtj|}tdD]l}t|}tj||}z0t |t d}zz t |dWt |nt |wWt |nt |w|WSty_Yqty|tjdkrytj|ryt|tjryYqYn tyYnwqttjd|)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.dsblatr@z)No usable temporary directory found in %s)r*rMrrJrEabspathrangenextr7open_bin_openflagswritecloseunlinkFileExistsErrorPermissionErrorrCisdiraccessW_OKrFileNotFoundError_errnoZENOENT)ZnamerrKr'seqrCfilenamefdrrr_get_default_tempdirsB        rbcCJtdur#tztdurtaWttSWttStwtS)z7Common setup sequence for all user-callable interfaces.N)_name_sequence _once_lockacquirer*releaserrrr_get_candidate_namess rhc Cstj|}t}|turttj|}ttD]J}t |}tj ||||}t d|z t ||d} Wn$ty@Yqty[tjdkrZtj|rZt|tjrZYqw| |fSttjd)z>Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.ztempfile.mkstemprOr@z#No usable temporary file name found)rrErPrhrmapr$rQrrRr7_sysauditrSrXrYrCrZr[r\r^EEXIST) r'ZpreZsufflagsr(namesr_rCfilerarrr_mkstemp_inners.        rpcGsR|tjvr||g|RddidStjdkstj|s'||g|RdSdS)Nfollow_symlinksFr@)rsupports_follow_symlinksrCrEislink)funcrEr rrr_dont_follow_symlinkss rucCs<ztj}Wn tyYnwt||dttj|ddS)Nr)rchflagsrIruchmod)rErwrrr _resetpermss   rycC ttS)z7The default prefix for temporary directories as string.)rfsdecoder#rrrrr # r cCrz)z6The default prefix for temporary directories as bytes.)rr$r#rrrrr 'r|r cCrc)z&Private accessor for tempfile.tempdir.N)r rerfrbrgrrrr _gettempdir-s r}cC ttS)z Returns tempfile.tempdir as str.)rr{r}rrrrr 9 r cCr~)z"Returns tempfile.tempdir as bytes.)rr$r}rrrrr =rr 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_openflagsrTrp)r&r%r'textr(rmrrrrAs rc Cst|||\}}}}t}|turttj|}ttD]F}t|}tj ||||}t d|z t |dW|StyFYqtyatjdkr`tj |r`t|tjr`Yqwttjd)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.mkdtemprvr@z(No usable temporary directory name found)r)rhrrirr$rQrrRrEr7rjrkmkdirrXrYrCrZr[r\r^rl)r&r%r'r(rnr_rCrorrrrhs.        rr4cCs\|durt}t}ttD]}t|}tj||||}t|s'|Sqt t j d)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 rhrQrrRrrEr7rrXr^rl)r&r%r'rnr_rCrorrrrs rc@sNeZdZdZdZdZd ddZejdkr!ej fdd Z d d Z dSd 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||_||_||_dSr1)rorCdeleter/rorCrrrr__init__s z_TemporaryFileCloser.__init__r@cCsX|js(|jdur*d|_z|jW|jr||jdSdS|jr'||jwwdSdSNT) close_calledrorVrrC)r/rWrrrrVs  z_TemporaryFileCloser.closecC |dSr1)rVr2rrr__del__ z_TemporaryFileCloser.__del__cCs|js d|_|jdSdSr)rrorVr2rrrrVsT) r;r<r=r>rorrrrCrWrVrrrrrrs   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||||_dSr1)rorCrr_closerrrrrrsz_TemporaryFileWrapper.__init__cs^|jd}t||}t|dr"|tfdd}|j|_|}t|ts-t||||S)Nro__call__cs|i|Sr1r)r kwargsrtrr func_wrappersz7_TemporaryFileWrapper.__getattr__..func_wrapper) __dict__r-hasattr _functoolswrapsrrintsetattr)r/rCroarrrr __getattr__s     z!_TemporaryFileWrapper.__getattr__cCs|j|Sr1)ro __enter__r2rrrrs z_TemporaryFileWrapper.__enter__cCs|j|||}||Sr1)ro__exit__rV)r/excvaluetbresultrrrrsz_TemporaryFileWrapper.__exit__cCs|jdS)zA Close the temporary file, possibly deleting it. N)rrVr2rrrrV sz_TemporaryFileWrapper.closeccs|jD]}|VqdSr1)ro)r/linerrrr3s z_TemporaryFileWrapper.__iter__Nr) r;r<r=r>rrrrrVr3rrrrrs  rw+bTerrorsc st\ttjdkr|rtjOd|vr!t|}dfdd} z,tj|||||| d} zt| d| } t| d| } | _t | |WWS| durntjdkri|snt ) 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. r@bNcst\}|Sr1)rp)r rar'rmrCr(r%r&rropener:sz"NamedTemporaryFile..opener bufferingnewlineencodingrrbufferraw) r)rTrrCZ O_TEMPORARY_io text_encodingrSr-rrVrW) moderrrr&r%r'rrrrorrrrrs2      rposixcygwin O_TMPFILEc sd|vr t|}t\ttrSdfdd}ztj||||||d} t| d| } t| d| } | _| WStyJdaYn t yRYnwdfd d}tj||||||d} t| d| } t| d| } | _| S) 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. rNcs$tjBtj@}t|dS)NrO)rrO_CREATrS)r Zflags2)r'rarmrrrtszTemporaryFile..openerrrrFc sJt\}zt|WSty$}ztd}~wwr1)rprrW BaseExceptionrV)r rCer'rarmr(r%r&rrrs  ) rrr)rT_O_TMPFILE_WORKSrSr-rCIsADirectoryErrorr) rrrrr&r%r'rrrorrrrrZs>          rc@seZdZdZdZ   d8dddd 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  Cs^d|vr t|_nt|}tjt|| |d|_||_d|_|||||||| d|_dS)Nr)rrrF)rrr&r%rrr'r)rBytesIO_filer TextIOWrapper _max_size_rolled_TemporaryFileArgs) r/max_sizerrrrr&r%r'rrrrrs    zSpooledTemporaryFile.__init__cCs4|jrdS|j}|r||kr|dSdSdSr1)rrtellrollover)r/rorrrr_checks  zSpooledTemporaryFile._checkcCsv|jrdS|j}tdi|j}|_|`|}t|dr)|j| n|| | |dd|_dS)NrrTr) rrrrrrrrUdetachgetvalueseek)r/roZnewfileposrrrrs    zSpooledTemporaryFile.rollovercCs|jjrtd|S)Nz%Cannot enter context with closed file)rclosed ValueErrorr2rrrrszSpooledTemporaryFile.__enter__cC|jdSr1rrVr/rrrrrrrzSpooledTemporaryFile.__exit__cC |jSr1)rr3r2rrrr3 zSpooledTemporaryFile.__iter__cCrr1rr2rrrrVrzSpooledTemporaryFile.closecC|jjSr1)rrr2rrrrzSpooledTemporaryFile.closedcCrr1)rrr2rrrrrzSpooledTemporaryFile.encodingcCrr1)rrr2rrrrrzSpooledTemporaryFile.errorscCs||jSr1)rrfilenor2rrrrs zSpooledTemporaryFile.filenocCrr1)rflushr2rrrrrzSpooledTemporaryFile.flushcCrr1)risattyr2rrrrrzSpooledTemporaryFile.isattycCs(z|jjWSty|jdYSw)Nr)rrrIrr2rrrrs   zSpooledTemporaryFile.modecCs z|jjWStyYdSwr1)rrCrIr2rrrrCs   zSpooledTemporaryFile.namecCrr1)rnewlinesr2rrrr rzSpooledTemporaryFile.newlinescG |jj|Sr1)rreadr/r rrrrrzSpooledTemporaryFile.readcGrr1)rreadlinerrrrrrzSpooledTemporaryFile.readlinecGrr1)r readlinesrrrrrrzSpooledTemporaryFile.readlinescGrr1)rrrrrrrrzSpooledTemporaryFile.seekcCrr1)rrr2rrrrrzSpooledTemporaryFile.tellcCs8|dur |jdS||jkr||j|dSr1)rtruncaterr)r/sizerrrr s  zSpooledTemporaryFile.truncatecC|j}||}|||Sr1)rrUr)r/srorvrrrrU(  zSpooledTemporaryFile.writecCrr1)r writelinesr)r/iterablerorrrrr.rzSpooledTemporaryFile.writelines)rrrNNNNNr1)"r;r<r=r>rr classmethod_types GenericAlias__class_getitem__rrrrr3rVr?rrrrrrrrCrrrrrrrrUrrrrrrsN         rc@sdeZdZdZ  dddZedddZeddd Zd d Zd d Z ddZ 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. NFcCs:t||||_||_tj||j|jd||jd|_dS)NzImplicitly cleaning up {!r}) warn_message ignore_errors)rrC_ignore_cleanup_errors_weakrefZfinalize_cleanupformat _finalizer)r/r&r%r'Zignore_cleanup_errorsrrrrAs  zTemporaryDirectory.__init__cs"fdd}tj|ddS)Nc st|dtrAz/|krttj|t|z t|WWdSttfy3j|dYnwWdSt y@YdSwt|dt rJdSsMdS)Nrr) issubclassrYryrrErLrWr_rmtreer])rtrEexc_infoclsrrCrronerrorLs& z+TemporaryDirectory._rmtree..onerror)r)_shutilZrmtree)rrCrrrrrrJszTemporaryDirectory._rmtreecCs|j||dt|tdSNr)r _warningswarnResourceWarning)rrCrrrrrrbszTemporaryDirectory._cleanupcCsd|jj|jS)Nz <{} {!r}>)r __class__r;rCr2rrr__repr__gszTemporaryDirectory.__repr__cCs|jSr1)rCr2rrrrjszTemporaryDirectory.__enter__cCrr1)cleanuprrrrrmrzTemporaryDirectory.__exit__cCs2|js tj|jr|j|j|jddSdSr)rrrrEexistsrCrrr2rrrrpszTemporaryDirectory.cleanupNNNF)F)r;r<r=r>rrrrrrrrrrrrrrrr5s    rr)NNN)rrNNNNNT)rrNNNNN)Br>__all__ functoolsrwarningsriorosrZshutilrerrnor^Zrandomrr.sysrjtypesrweakrefr_thread allocate_lockZ_allocate_lockO_RDWRrO_EXCLrrrrTrrr#rerr"r)r*rMrbrdrhrpruryr r r r}r r rrrrrrrCplatformrrrrrrrrs        ,    '( +? 5  K