[Misc] fix unique_filepath (#25732)

Signed-off-by: zjy0516 <riverclouds.zhu@qq.com>
Co-authored-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
This commit is contained in:
Jiangyun Zhu
2025-09-27 00:56:15 +08:00
committed by GitHub
parent 8d52f2b3a7
commit 56aafa8c0b
2 changed files with 42 additions and 8 deletions

View File

@@ -45,6 +45,7 @@ from concurrent.futures import ThreadPoolExecutor
from concurrent.futures.process import ProcessPoolExecutor
from dataclasses import dataclass, field
from functools import cache, lru_cache, partial, wraps
from pathlib import Path
from types import MappingProxyType
from typing import (TYPE_CHECKING, Any, Callable, Generic, Literal, NamedTuple,
Optional, TextIO, TypeVar, Union, cast, overload)
@@ -3536,3 +3537,23 @@ def set_env_var(key, value):
del os.environ[key]
else:
os.environ[key] = old
def unique_filepath(fn: Callable[[int], Path]) -> Path:
"""
unique_filepath returns a unique path by trying
to include an integer in increasing order.
fn should be a callable that returns a path that
includes the passed int at a fixed location.
Note: This function has a TOCTOU race condition.
Caller should use atomic operations (e.g., open with 'x' mode)
when creating the file to ensure thread safety.
"""
i = 0
while True:
p = fn(i)
if not p.exists():
return p
i += 1