Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.
(dir, pre, suf, flags, output_type)
| 240 | |
| 241 | |
| 242 | def _mkstemp_inner(dir, pre, suf, flags, output_type): |
| 243 | """Code common to mkstemp, TemporaryFile, and NamedTemporaryFile.""" |
| 244 | |
| 245 | dir = _os.path.abspath(dir) |
| 246 | names = _get_candidate_names() |
| 247 | if output_type is bytes: |
| 248 | names = map(_os.fsencode, names) |
| 249 | |
| 250 | for seq in range(TMP_MAX): |
| 251 | name = next(names) |
| 252 | file = _os.path.join(dir, pre + name + suf) |
| 253 | _sys.audit("tempfile.mkstemp", file) |
| 254 | try: |
| 255 | fd = _os.open(file, flags, 0o600) |
| 256 | except FileExistsError: |
| 257 | continue # try again |
| 258 | except PermissionError: |
| 259 | # See the comment in mkdtemp(). |
| 260 | if _os.name == 'nt' and _os.path.isdir(dir) and seq < TMP_MAX - 1: |
| 261 | continue |
| 262 | else: |
| 263 | raise |
| 264 | return fd, file |
| 265 | |
| 266 | raise FileExistsError(_errno.EEXIST, |
| 267 | "No usable temporary file name found") |
| 268 | |
| 269 | def _dont_follow_symlinks(func, path, *args): |
| 270 | # Pass follow_symlinks=False, unless not supported on this platform. |