o Ƌ|cr@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/da0ddZ1d d!Z2d"d#Z3d?d%d&Z4d@d'd(Z5d)e#dfd*d+Z6Gd,d-d-Z7Gd.d/d/Z8   2dAdd3d4d5Z9e j:d6ksej;d7kre9ZGd=d>d>Z?dS)CaTemporary 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/conda/lib/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)joinr0Zchoices charactersr2rrr__next__sz_RandomNameSequence.__next__N) __name__ __module__ __qualname____doc__r8propertyr0r3r9rrrrr*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$   rLc 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*rLrrIrDabspathrangenextr7open_bin_openflagswritecloseunlinkFileExistsErrorPermissionErrorrBisdiraccessW_OKrFileNotFoundError_errnoZENOENT)ZnamerrJr'seqrBfilenamefdrrr_get_default_tempdirsB        racCJtdur#tztdurtaWttSWttStwtS)z7Common setup sequence for all user-callable interfaces.N)_name_sequence _once_lockacquirer*releaserrrr_get_candidate_namess rgc 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.mkstemprNr?z#No usable temporary file name found)rrDrOrgrmapr$rPrrQr7_sysauditrRrWrXrBrYrZr[r]EEXIST) r'ZpreZsufflagsr(namesr^rBfiler`rrr_mkstemp_inners.        rocC ttS)z7The default prefix for temporary directories as string.)rfsdecoder#rrrrr  r cCrp)z6The default prefix for temporary directories as bytes.)rr$r#rrrrr rrr cCrb)z&Private accessor for tempfile.tempdir.N)r rdrerarfrrrr _gettempdirs rscC ttS)z Returns tempfile.tempdir as str.)rrqrsrrrrr ) r cCrt)z"Returns tempfile.tempdir as bytes.)rr$rsrrrrr -rur 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_openflagsrSro)r&r%r'textr(rlrrrr1s 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.mkdtempr?z(No usable temporary directory name found)r)rgrrhrr$rPrrQrDr7rirjmkdirrWrXrBrYrZr[r]rk)r&r%r'r(rmr^rBrnrrrrXs.        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 rgrPrrQrrDr7rrWr]rk)r&r%r'rmr^rBrnrrrrs 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)rnrBdeleter/rnrBr{rrr__init__s z_TemporaryFileCloser.__init__r?cCsX|js(|jdur*d|_z|jW|jr||jdSdS|jr'||jwwdSdSNT) close_calledrnrUr{rB)r/rVrrrrUs  z_TemporaryFileCloser.closecC |dSr1)rUr2rrr__del__ z_TemporaryFileCloser.__del__cCs|js d|_|jdSdSr~)rrnrUr2rrrrUsT) r:r;r<r=rnrr}rrBrVrUrrrrrrzs   rzc@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)rnrBr{rz_closerr|rrrr}sz_TemporaryFileWrapper.__init__cs^|jd}t||}t|dr"|tfdd}|j|_|}t|ts-t||||S)Nrn__call__cs|i|Sr1r)r kwargsfuncrr func_wrappersz7_TemporaryFileWrapper.__getattr__..func_wrapper) __dict__r-hasattr _functoolswrapsrrintsetattr)r/rBrnarrrr __getattr__s     z!_TemporaryFileWrapper.__getattr__cCs|j|Sr1)rn __enter__r2rrrrs z_TemporaryFileWrapper.__enter__cCs|j|||}||Sr1)rn__exit__rU)r/excvaluetbresultrrrrsz_TemporaryFileWrapper.__exit__cCs|jdS)zA Close the temporary file, possibly deleting it. N)rrUr2rrrrUsz_TemporaryFileWrapper.closeccs|jD]}|VqdSr1)rn)r/linerrrr3s z_TemporaryFileWrapper.__iter__Nr) r:r;r<r=r}rrrrUr3rrrrrs  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)ro)r r`r'rlrBr(r%r&rropener*sz"NamedTemporaryFile..opener bufferingnewlineencodingrrbufferraw) r)rSrrBZ O_TEMPORARY_io text_encodingrRr-rrUrV) moderrrr&r%r'r{rrrnrrrrr s2      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)NrN)rrO_CREATrR)r Zflags2)r'r`rlrrrdszTemporaryFile..openerrrrFc sJt\}zt|WSty$}ztd}~wwr1)rorrV BaseExceptionrU)r rBer'r`rlr(r%r&rrrs  ) rrr)rS_O_TMPFILE_WORKSrRr-rBIsADirectoryErrorr) rrrrr&r%r'rrrnrrrrrJs>          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'rrrrr}s    zSpooledTemporaryFile.__init__cCs4|jrdS|j}|r||kr|dSdSdSr1)rrtellrollover)r/rnrrrr_checks  zSpooledTemporaryFile._checkcCsv|jrdS|j}tdi|j}|_|`|}t|dr)|j| n|| | |dd|_dS)NrrTr) rrrrrrrrTdetachgetvalueseek)r/rnZnewfileposrrrrs    zSpooledTemporaryFile.rollovercCs|jjrtd|S)Nz%Cannot enter context with closed file)rclosed ValueErrorr2rrrrszSpooledTemporaryFile.__enter__cC|jdSr1rrUr/rrrrrrrzSpooledTemporaryFile.__exit__cC |jSr1)rr3r2rrrr3 zSpooledTemporaryFile.__iter__cCrr1rr2rrrrUrzSpooledTemporaryFile.closecC|jjSr1)rrr2rrrrzSpooledTemporaryFile.closedcCrr1)rrr2rrrrrzSpooledTemporaryFile.encodingcCrr1)rrr2rrrrrzSpooledTemporaryFile.errorscCs||jSr1)rrfilenor2rrrrs zSpooledTemporaryFile.filenocCrr1)rflushr2rrrrrzSpooledTemporaryFile.flushcCrr1)risattyr2rrrrrzSpooledTemporaryFile.isattycCs(z|jjWSty|jdYSw)Nr)rrrHrr2rrrrs   zSpooledTemporaryFile.modecCs z|jjWStyYdSwr1)rrBrHr2rrrrBs   zSpooledTemporaryFile.namecCrr1)rnewlinesr2rrrrrzSpooledTemporaryFile.newlinescG |jj|Sr1)rreadr/r rrrrrzSpooledTemporaryFile.readcGrr1)rreadlinerrrrrrzSpooledTemporaryFile.readlinecGrr1)r readlinesrrrrrrzSpooledTemporaryFile.readlinescGrr1)rrrrrrr rzSpooledTemporaryFile.seekcCrr1)rrr2rrrr rzSpooledTemporaryFile.tellcCs8|dur |jdS||jkr||j|dSr1)rtruncaterr)r/sizerrrrs  zSpooledTemporaryFile.truncatecC|j}||}|||Sr1)rrTr)r/srnrvrrrrT  zSpooledTemporaryFile.writecCrr1)r writelinesr)r/iterablernrrrrrrzSpooledTemporaryFile.writelines)rrrNNNNNr1)"r:r;r<r=rr} classmethod_types GenericAlias__class_getitem__rrrrr3rUr>rrrrrrrrBrrrrrrrrTrrrrrrsN         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)rrB_ignore_cleanup_errors_weakrefZfinalize_cleanupformat _finalizer)r/r&r%r'Zignore_cleanup_errorsrrrr}1s  zTemporaryDirectory.__init__cs"fdd}tj|ddS)Nc st|dtrEdd}z/|kr|tj|||z t|WWdSttfy7j|dYnwWdStyDYdSwt|dtrNdSsQdS)NrcSs4zt|dWn tyYnwt|ddS)Nrrx)rZchflagsrHchmod)rDrrr resetperms>s  z?TemporaryDirectory._rmtree..onerror..resetpermsr) issubclassrXrrDrKrVr_rmtreer\)rrDexc_inforclsrrBrronerror<s( z+TemporaryDirectory._rmtree..onerror)r)_shutilZrmtree)rrBrrrrrr:szTemporaryDirectory._rmtreecCs|j||dt|tdSNr)r _warningswarnResourceWarning)rrBrrrrrrYszTemporaryDirectory._cleanupcCsd|jj|jS)Nz <{} {!r}>)r __class__r:rBr2rrr__repr__^szTemporaryDirectory.__repr__cCs|jSr1)rBr2rrrraszTemporaryDirectory.__enter__cCrr1)cleanuprrrrrdrzTemporaryDirectory.__exit__cCs2|js tj|jr|j|j|jddSdSr)rrrrDexistsrBrrr2rrrrgszTemporaryDirectory.cleanupNNNF)F)r:r;r<r=r}rrrrrrrrrrrrrrr%s    rr)NNN)rrNNNNNT)rrNNNNN)@r=__all__ functoolsrwarningsriorosrZshutilrerrnor]Zrandomrr.sysritypesrweakrefr_thread allocate_lockZ_allocate_lockO_RDWRrO_EXCLrvrrrSrrr#rdrr"r)r*rLrarcrgror r r rsr r rrrrzrrrBplatformrrrrrrrrs~        ,    '( +? 5  K