Plan 9 from Bell Labs’s /usr/web/sources/contrib/bichued/root/sys/lib/python/tempfile.pyc

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


�
��c@s�dZddddddddd	g	Zd
dkZd
dkZd
dklZei	d
jo"d
dk
iZd
dk
iZnyd
dkZWnej
od�ZnXd�Zyd
dkZWnej
od
dkZnXeiZeieiBeiBZeed�oeeiOZneed�oeeiOZneZ eed�oe ei!OZ need�o
ei"Z"ndZ"dZ#da%e�Z&eed�o
ei'Z(n'eed�o
ei)Z(n
d�Z(d�Z*dd,d��YZ+d�Z,d�Z-da.d�Z/d�Z0d�Z1da%d �Z2d!e#de3d"�Z4d!e#dd#�Z5d!e#dd$�Z6d%d-d&��YZ7d'd
d!e#dd(�Z8ei	d)jpei9i:d*jo
e8Z;nd'd
d!e#dd+�Z;dS(.s�Temporary files.

This module provides generic, low- and high-level interfaces for
creating temporary files and directories.  The interfaces listed
as "safe" just below can be used without fear of race conditions.
Those listed as "unsafe" cannot, and are provided for backward
compatibility only.

This module also provides some data items to the user:

  TMP_MAX  - maximum number of names that will be tried before
             giving up.
  template - the default prefix for all temporary names.
             You may change this to control the default prefix.
  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.
tNamedTemporaryFilet
TemporaryFiletmkstemptmkdtemptmktemptTMP_MAXt
gettempprefixttempdirt
gettempdiri�N(tRandomtmaccCsdS(N((tfd((s/sys/lib/python/tempfile.pyt_set_cloexec*scCsYyti|tid�}Wntj
on%X|tiO}ti|ti|�dS(Ni(t_fcntltfcntltF_GETFDtIOErrort
FD_CLOEXECtF_SETFD(Rtflags((s/sys/lib/python/tempfile.pyR-s
tO_NOINHERITt
O_NOFOLLOWtO_BINARYi'ttmptlstattstatcCs=yt|�}Wntj
oti�nX|i�dS(N(topenRt_osterrortclose(tfntf((s/sys/lib/python/tempfile.pyt_stat\s
cCs3yt|�Wntij
otSnXtSdS(N(R RRtFalsetTrue(R((s/sys/lib/python/tempfile.pyt_existscs
	t_RandomNameSequencecBs7eZdZdddZd�Zd�Zd�ZRS(s*An instance of _RandomNameSequence generates an endless
    sequence of unpredictable strings which can safely be incorporated
    into file names.  Each string is six characters long.  Multiple
    threads can safely use the same instance at the same time.

    _RandomNameSequence is an iterator.tabcdefghijklmnopqrstuvwxyztABCDEFGHIJKLMNOPQRSTUVWXYZs0123456789-_cCs+t�|_t�|_tii|_dS(N(t_allocate_locktmutext_RandomtrngRtpathtnormcase(tself((s/sys/lib/python/tempfile.pyt__init__wscCs|S(N((R-((s/sys/lib/python/tempfile.pyt__iter__|scCsw|i}|i}|ii}|i�z+g}dD]}|||�q6~}Wd|i�X|idi|��S(Nt123456t(R(t
charactersR*tchoicetacquiretreleaseR,tjoin(R-tmtctchooset_[1]tdummytletters((s/sys/lib/python/tempfile.pytnexts		
+(t__name__t
__module__t__doc__R2R.R/R=(((s/sys/lib/python/tempfile.pyR$ks		cCskg}x5dD]-}ti|�}|o|i|�q
q
WtidjoUy8tititid�}|i	�}|i|�Wq!ti
j
oq!Xntidjo+tid�}|o|i|�q!nDtidjo|id	d
ddg�n|id
ddg�y|iti��Wn,t
ti
fj
o|iti�nX|S(s[Generate a list of candidate temporary directories which
    _get_default_tempdir will try.tTMPDIRtTEMPtTMPR
itriscoss
Wimp$ScrapDirtntsc:\tempsc:\tmps\temps\tmps/tmps/var/tmps/usr/tmp(RARBRC(Rtgetenvtappendtnamet_FoldertFSFindFoldert_Folderst
kOnSystemDisktkTemporaryFolderTypetas_pathnameRtextendtgetcwdtAttributeErrortcurdir(tdirlisttenvnametdirnametfsr((s/sys/lib/python/tempfile.pyt_candidate_tempdir_list�s0	c
	Cs=t�}t�}t}x|D]}|tijo"tiitii|��}nx�td�D]�}|i	�}tii
||�}yYti||d�}ti|d�}|i
d�|i�ti|�~~|SWqdttfj
o"}	|	dtijoPqqdXqdWqWttid|f�dS(sqCalculate 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.idi�twtblatis)No usable temporary directory found in %sN(R$RWt_text_openflagsRRRR+R,tabspathtxrangeR=R6RtfdopentwriteRtunlinktOSErrorRt_errnotEEXISTtENOENT(
tnamerRSRtdirtseqRHtfilenameRtfpte((s/sys/lib/python/tempfile.pyt_get_default_tempdir�s2			"




	cCsKtdjo:ti�ztdjo
t�anWdti�XntS(s7Common setup sequence for all user-callable interfaces.N(t_name_sequencetNonet
_once_lockR4R$R5(((s/sys/lib/python/tempfile.pyt_get_candidate_names�s


c
Cs�t�}x�tt�D]�}|i�}tii||||�}y9ti||d�}t|�|tii	|�fSWqt
j
o&}	|	iti
joqn�qXqWtti
df�dS(s>Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.i�s#No usable temporary file name foundN(RnR\RR=RR+R6RRR[R`terrnoRaRbR(
RetpretsufRtnamesRfRHtfileRRi((s/sys/lib/python/tempfile.pyt_mkstemp_inner�s	

cCstS(sAccessor for tempdir.template.(ttemplate(((s/sys/lib/python/tempfile.pyR�scCsKtdjo:ti�ztdjo
t�anWdti�XntS(sAccessor for tempdir.tempdir.N(RRlRmR4RjR5(((s/sys/lib/python/tempfile.pyR�s


R1cCsD|djo
t�}n|o
t}nt}t||||�S(sVmkstemp([suffix, [prefix, [dir, [text]]]])
    User-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 specified, the file name will end with that suffix,
    otherwise there will be no suffix.

    If 'prefix' is specified, the file name will begin with that prefix,
    otherwise a default prefix is used.

    If 'dir' is specified, 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.  On
    some operating systems, this makes no difference.

    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.
    N(RlRRZt_bin_openflagsRt(tsuffixtprefixRettextR((s/sys/lib/python/tempfile.pyRs


cCs�|djo
t�}nt�}x�tt�D]|}|i�}tii||||�}yti	|d�|SWq0t
j
o&}|iti
joq0n�q0Xq0Wtti
df�dS(s�mkdtemp([suffix, [prefix, [dir]]])
    User-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.
    i�s(No usable temporary directory name foundN(RlRRnR\RR=RR+R6tmkdirR`RoRaRbR(RwRxReRrRfRHRsRi((s/sys/lib/python/tempfile.pyR1s

	
cCs�|djo
t�}nt�}xRtt�D]D}|i�}tii||||�}t	|�p|Sq0q0Wt
tidf�dS(s�mktemp([suffix, [prefix, [dir]]])
    User-callable function to return a unique temporary file name.  The
    file is not created.

    Arguments are as for mkstemp, except that the 'text' argument is
    not accepted.

    This function is unsafe and should not be used.  The file name
    refers 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.
    s"No usable temporary filename foundN(
RlRRnR\RR=RR+R6R#RRaRb(RwRxReRrRfRHRs((s/sys/lib/python/tempfile.pyRQs

	

t_TemporaryFileWrappercBsOeZdZd�Zd�ZeidjoeiZd�Zd�Z	nRS(s�Temporary 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.
    cCs||_||_t|_dS(N(RsRHR!tclose_called(R-RsRH((s/sys/lib/python/tempfile.pyR.ws		cCsM|id}t||�}t|�td�jot|||�n|S(NRsi(t__dict__tgetattrttypetsetattr(R-RHRsta((s/sys/lib/python/tempfile.pyt__getattr__|s

REcCs8|ip*t|_|ii�|i|i�ndS(N(R|R"RsRR_RH(R-((s/sys/lib/python/tempfile.pyR�s
	
cCs|i�dS(N(R(R-((s/sys/lib/python/tempfile.pyt__del__�s(
R>R?R@R.R�RRHR_RR�(((s/sys/lib/python/tempfile.pyR{os		
		sw+bc	Cs�|djo
t�}nd|jo
t}nt}tidjo|tiO}nt||||�\}}ti|||�}t	||�S(s�Create and return a temporary file.
    Arguments:
    'prefix', 'suffix', 'dir' -- as for mkstemp.
    'mode' -- the mode argument to os.fdopen (default "w+b").
    'bufsize' -- the buffer size argument to os.fdopen (default -1).
    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 file.name.  The file will be automatically deleted
    when it is closed.
    tbREN(
RlRRvRZRRHtO_TEMPORARYRtR]R{(	tmodetbufsizeRwRxReRRRHRs((s/sys/lib/python/tempfile.pyR�s



tposixtcygwincCs�|djo
t�}nd|jo
t}nt}t||||�\}}y$ti|�ti|||�SWnti|��nXdS(s�Create and return a temporary file.
        Arguments:
        'prefix', 'suffix', 'dir' -- as for mkstemp.
        'mode' -- the mode argument to os.fdopen (default "w+b").
        'bufsize' -- the buffer size argument to os.fdopen (default -1).
        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�N(	RlRRvRZRtRR_R]R(R�R�RwRxReRRRH((s/sys/lib/python/tempfile.pyR�s






(((<R@t__all__tosRRoRatrandomR	R)RHt
Carbon.FoldertFolderRItCarbon.FolderstFoldersRKRR
tImportErrorRtthreadt_threadtdummy_threadt
allocate_lockR'tO_RDWRtO_CREATtO_EXCLRZthasattrRRRvRRRuRlRRmRR RR#R$RWRjRkRnRtRRR!RRRR{RtsystplatformR(((s/sys/lib/python/tempfile.pys<module>sx		
	

		!	$	#				& )	#
	

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to webmaster@9p.io.