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)
| 182 | return dirlist |
| 183 | |
| 184 | def _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 |
no test coverage detected
searching dependent graphs…