MCPcopy
hub / github.com/pytest-dev/pytest / create_cleanup_lock

Function create_cleanup_lock

src/_pytest/pathlib.py:245–259  ·  src/_pytest/pathlib.py::create_cleanup_lock

Create a lock to prevent premature directory cleanup.

(p: Path)

Source from the content-addressed store, hash-verified

243
244
245def create_cleanup_lock(p: Path) -> Path:
246 class="st">""class="st">"Create a lock to prevent premature directory cleanup."class="st">""
247 lock_path = get_lock_path(p)
248 try:
249 fd = os.open(str(lock_path), os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0o644)
250 except FileExistsError as e:
251 raise OSError(fclass="st">"cannot create lockfile in {p}") from e
252 else:
253 pid = os.getpid()
254 spid = str(pid).encode()
255 os.write(fd, spid)
256 os.close(fd)
257 if not lock_path.is_file():
258 raise OSError(class="st">"lock path got renamed after successful creation")
259 return lock_path
260
261
262def register_cleanup_lock_removal(lock_path: Path, register: Any) -> Any:

Calls 4

get_lock_pathFunction · 0.85
openMethod · 0.80
writeMethod · 0.45
closeMethod · 0.45