MCPcopy Create free account
hub / github.com/tox-dev/filelock / _async_fork_script

Function _async_fork_script

tests/test_read_write_fork.py:945–1029  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

943
944
945def _async_fork_script() -> str:
946 return textwrap.dedent(
947 r"""
948 from __future__ import annotations
949
950 import asyncio
951 import gc
952 import os
953 import sqlite3
954 import sys
955 import warnings
956
957 from filelock import AsyncAcquireReadWriteReturnProxy, AsyncReadWriteLock, ReadWriteLock, Timeout
958
959 warnings.filterwarnings("ignore", category=DeprecationWarning, message=r".*fork\(\).*")
960 lock_path, state = sys.argv[1:]
961
962 async def set_up_parent() -> tuple[AsyncReadWriteLock, AsyncAcquireReadWriteReturnProxy | None]:
963 parent_lock = AsyncReadWriteLock(lock_path, is_singleton=False)
964 parent_proxy = await parent_lock.acquire_write() if state == "held" else None
965 return parent_lock, parent_proxy
966
967 lock, proxy = asyncio.run(set_up_parent())
968 child_pid = os.fork()
969 if child_pid == 0:
970 async def check_child() -> None:
971 if proxy is not None:
972 async with proxy:
973 pass
974 try:
975 await lock.acquire_read()
976 except RuntimeError as error:
977 assert "was invalidated by fork()" in str(error)
978 else:
979 raise AssertionError("inherited async lock acquired")
980 await lock.release()
981 await lock.close()
982 if sys.implementation.name == "pypy":
983 try:
984 AsyncReadWriteLock(lock_path, is_singleton=False)
985 except RuntimeError as error:
986 assert str(error) == (
987 "ReadWriteLock is unavailable in a PyPy fork child; exec or exit before using it"
988 )
989 else:
990 raise AssertionError("PyPy child constructed an async lock after fork")
991 elif state == "idle":
992 fresh_lock = AsyncReadWriteLock(lock_path, is_singleton=False)
993 async with fresh_lock.write_lock():
994 pass
995 await fresh_lock.close()
996 else:
997 try:
998 ReadWriteLock(lock_path, is_singleton=False)
999 except RuntimeError as error:
1000 expected = (
1001 "ReadWriteLock is unavailable in a PyPy fork child"
1002 if sys.implementation.name == "pypy"

Calls

no outgoing calls

Tested by

no test coverage detected