MCPcopy Index your code
hub / github.com/python/cpython / _get_default_tempdir

Function _get_default_tempdir

Lib/tempfile.py:184–224  ·  view source on GitHub ↗

Calculate 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

(dirlist=None)

Source from the content-addressed store, hash-verified

182 return dirlist
183
184def _get_default_tempdir(dirlist=None):
185 """Calculate the default directory to use for temporary files.
186 This routine should be called exactly once.
187
188 We determine whether or not a candidate temp dir is usable by
189 trying to create and write to a file in that directory. If this
190 is successful, the test file is deleted. To prevent denial of
191 service, the name of the test file must be randomized."""
192
193 namer = _RandomNameSequence()
194 if dirlist is None:
195 dirlist = _candidate_tempdir_list()
196
197 for dir in dirlist:
198 if dir != _os.curdir:
199 dir = _os.path.abspath(dir)
200 for seq in range(TMP_MAX):
201 name = next(namer)
202 filename = _os.path.join(dir, name)
203 try:
204 fd = _os.open(filename, _bin_openflags, 0o600)
205 try:
206 try:
207 _os.write(fd, b'blat')
208 finally:
209 _os.close(fd)
210 finally:
211 _os.unlink(filename)
212 return dir
213 except FileExistsError:
214 pass
215 except PermissionError:
216 # See the comment in mkdtemp().
217 if _os.name == 'nt' and _os.path.isdir(dir):
218 continue
219 break # no point trying more names in this directory
220 except OSError:
221 break # no point trying more names in this directory
222 raise FileNotFoundError(_errno.ENOENT,
223 "No usable temporary directory found in %s" %
224 dirlist)
225
226_name_sequence = None
227

Callers 1

_gettempdirFunction · 0.85

Calls 9

_RandomNameSequenceClass · 0.85
_candidate_tempdir_listFunction · 0.85
abspathMethod · 0.45
joinMethod · 0.45
openMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45
unlinkMethod · 0.45
isdirMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…