U e5dk @sdZddddddddd d d d d g ZddlZddlZddlZddlZ ddl Z ddl Z ddlmZddlZddlZddlZejZe je jBe jBZee dree jOZeZee dree jOZee dre j Z ndZ dZ!eZ"ddZ#ddZ$ddZ%GdddZ&ddZ'dd Z(da)d!d"Z*d#d$Z+d%d Z,d&d Z-da.d'd Z/d(d Z0d=d*dZ1d>d+dZ2d,e!dfd-dZ3Gd.d/d/Z4Gd0d1d1Z5d?dd5d6dZ6e j7d7ksej8d8kre6Z9nee d9a:d@dd5d:dZ9Gd;ddZ;Gd>> 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_BINARYi'ZtmpcCs.zt|Wntk r$YdSXdSdS)NFT)_oslstatOSError)fnr /usr/lib64/python3.8/tempfile.py_existsKs rcGs\d}|D]B}|dkrqt|tr6|tkr0tdt}q|tkrFtdt}q|dkrXtS|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)argsZ return_typeargrrr_infer_return_typeTs rcCsdt|||}|dkr|}|dkr:|tkr0t}n tt}|dkrX|tkrRt}nt}||||fS)z9Common parameter processing for most APIs in this module.N)rrtemplaterfsencoder r )prefixsuffixdir output_typerrr_sanitize_paramsis  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|ddkr&t|_||_|jS)N_rng_pid)rgetpidgetattr_RandomZ_rngr()selfZcur_pidrrrrngs z_RandomNameSequence.rngcCs|SNrr,rrr__iter__sz_RandomNameSequence.__iter__cs0|j|jjfddtdD}d|S)Ncsg|] }qSrr).0ZdummycZchooserr sz0_RandomNameSequence.__next__..) charactersr-Zchoicerangejoin)r,Zlettersrr2r__next__sz_RandomNameSequence.__next__N) __name__ __module__ __qualname____doc__r7propertyr-r0r:rrrrr'{s  r'c Csg}dD]}t|}|r||qtjdkrX|tjdtjdddddgn|d d d gz|tWn$t t fk r|tj YnX|S) z[Generate a list of candidate temporary directories which _get_default_tempdir will try.)ZTMPDIRZTEMPZTMPntz~\AppData\Local\Tempz%SYSTEMROOT%\Tempzc:\tempzc:\tmpz\tempz\tmpz/tmpz/var/tmpz/usr/tmp) rgetenvappendnameextendpath expanduser expandvarsgetcwdAttributeErrorrcurdir)dirlistZenvnamedirnamerrr_candidate_tempdir_lists&    rMc CsFt}t}|D]}|tjkr,tj|}tdD]}t|}tj||}zft |t d}zCode common to mkstemp, TemporaryFile, and NamedTemporaryFile.ztempfile.mkstemprOr@z#No usable temporary file name foundN)rjrmaprr!r8rrSrEr9_sysauditrTrZr[rCr\r]r^rRr`EEXIST) r$ZpreZsufflagsr%namesrarCfilercrrr_mkstemp_inners*    rrcCstS)z-The default prefix for temporary directories.)r rrrrr scCs ttS)z6The default prefix for temporary directories as bytes.)rr!r rrrrr scCs2tdkr.tztdkr taW5tXtS)zAccessor for tempfile.tempdir.N)r rgrhrirerrrrr s  cCs ttS)z)A bytes version of tempfile.gettempdir().)rr!r rrrrr #sFcCs2t|||\}}}}|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_openflagsrUrr)r#r"r$textr%rorrrr's c Cst|||\}}}}t}|tkr.ttj|}ttD]}t|}tj ||||}t d|zt |dWnVtk rYq6Yn@tk rtjdkrtj |rt|tjrYq6nYnX|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.mkdtempr@z(No usable temporary directory name foundN)r&rjrrkrr!r8rrSrEr9rlrmmkdirrZr[rCr\r]r^r`rn)r#r"r$r%rprarCrqrrrrNs,     r6cCs`|dkrt}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 rjr8rrSrrEr9rrZr`rn)r#r"r$rprarCrqrrrrvs  c@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||_||_||_dSr.)rqrCdeleter,rqrCrxrrr__init__sz_TemporaryFileCloser.__init__r@cCs<|js8|jdk r8d|_z|jW5|jr6||jXdSNT) close_calledrqrxrCrW)r,rVrrrrWs z_TemporaryFileCloser.closecCs |dSr.)rWr/rrr__del__sz_TemporaryFileCloser.__del__cCs|jsd|_|jdSr{)r|rqrWr/rrrrWs)T) r;r<r=r>rqr|rzrrCrVrWr}rrrrrws   rwc@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||||_dSr.)rqrCrxrw_closerryrrrrzsz_TemporaryFileWrapper.__init__cs^|jd}t||}t|drD|tfdd}|j|_|}t|tsZt||||S)Nrq__call__cs ||Sr.r)rkwargsfuncrr func_wrappersz7_TemporaryFileWrapper.__getattr__..func_wrapper) __dict__r*hasattr _functoolswrapsrrintsetattr)r,rCrqarrrr __getattr__s     z!_TemporaryFileWrapper.__getattr__cCs|j|Sr.)rq __enter__r/rrrrs z_TemporaryFileWrapper.__enter__cCs|j|||}||Sr.)rq__exit__rW)r,excvaluetbresultrrrrsz_TemporaryFileWrapper.__exit__cCs|jdS)zA Close the temporary file, possibly deleting it. N)rrWr/rrrrWsz_TemporaryFileWrapper.closeccs|jD] }|VqdSr.)rq)r,linerrrr0s z_TemporaryFileWrapper.__iter__N)T) r;r<r=r>rzrrrrWr0rrrrr~s r~w+bTerrorscCst|||\}}}} t} tjdkr0|r0| tjO} t|||| | \} } z$tj| |||||d} t| | |WSt k rt | t | YnXdS)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@ bufferingnewlineencodingrN) r&rUrrCZ O_TEMPORARYrrrXrTr~ BaseExceptionrVrW)moderrrr#r"r$rxrr%rorcrCrqrrrrs     posixcygwin O_TMPFILEc Cst|||\}}}}t} trz$| tjBtj@} t|| d} Wn*tk rXdaYnFtk rjYn4Xzt j| |||||dWSt | YnXt |||| |\} } z"t | t j| |||||dWSt | YnXdS)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. rOFrN) r&rU_O_TMPFILE_WORKSrrO_CREATrTIsADirectoryErrorrrXrWrrrV) rrrrr#r"r$rr%roZflags2rcrCrrrr2s<     c@seZdZdZdZd:dddd Zd d Zd d ZddZddZ ddZ ddZ e ddZ e ddZe ddZddZddZd d!Ze d"d#Ze d$d%Ze d&d'Zd(d)Zd*d+Zd,d-Zd.d/Ze d0d1Zd2d3Zd;d4d5Zd6d7Zd8d9ZdS)rrzrrrrr0rWr?rrrrrrrrCrrrrrrrrrYrrrrrrnsT         c@sReZdZdZdddZeddZeddZd d Zd d Z d dZ ddZ dS)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)rrC_weakrefZfinalize_cleanupformat _finalizer)r,r#r"r$rrrrz szTemporaryDirectory.__init__cs fdd}tj|ddS)Nc st|dtrdd}zV|kr0|tj|||zt|Wn"ttfk rh|YnXWqtk rYqXnt|dtrndS)NrcSs6zt|dWntk r$YnXt|ddS)Nrru)rZchflagsrIchmod)rErrr resetpermss z?TemporaryDirectory._rmtree..onerror..resetperms) issubclassr[rrErLrVr_rmtreer_)rrEexc_inforclsrCrronerrorsz+TemporaryDirectory._rmtree..onerror)r)_shutilZrmtree)rrCrrrrrszTemporaryDirectory._rmtreecCs||t|tdSr.)r _warningswarnResourceWarning)rrCrrrrr/s zTemporaryDirectory._cleanupcCsd|jj|jS)Nz <{} {!r}>)r __class__r;rCr/rrr__repr__4szTemporaryDirectory.__repr__cCs|jSr.)rCr/rrrr7szTemporaryDirectory.__enter__cCs |dSr.)cleanuprrrrr:szTemporaryDirectory.__exit__cCs|jr||jdSr.)rrrrCr/rrrr=s zTemporaryDirectory.cleanup)NNN) r;r<r=r>rz classmethodrrrrrrrrrrrs   )NNNF)NNN)rrNNNNNT)rrNNNNN)>r>__all__ functoolsrwarningsriorXosrZshutilrerrnor`Zrandomrr+sysrlweakrefr_thread allocate_lockZ_allocate_lockO_RDWRrO_EXCLrsrrrUrrr rgrrr&r'rMrerfrjrrr r r r r rrrrwr~rrCplatformrrrobjectrrrrrs        -  ' ( +? '  <