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

Function _fork_script

tests/test_read_write_fork.py:577–672  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

575
576
577def _fork_script() -> str:
578 return textwrap.dedent(
579 """
580 from __future__ import annotations
581
582 import gc
583 import os
584 import sqlite3
585 import sys
586 from typing import TYPE_CHECKING
587
588 if TYPE_CHECKING:
589 from _typeshed import Unused
590
591 lock_path, mode, fork_name, audit_hook_mode = sys.argv[1:]
592 if audit_hook_mode == "reject":
593 def reject_filelock_audit_hook(event: str, _args: Unused) -> None:
594 if event == "sys.addaudithook":
595 raise RuntimeError("audit hook registration rejected")
596
597 sys.addaudithook(reject_filelock_audit_hook)
598
599 from filelock import ReadWriteLock, Timeout
600
601 lock = ReadWriteLock(lock_path, is_singleton=False)
602 acquire = lock.acquire_read if mode == "read" else lock.acquire_write
603 proxy = acquire()
604 parent_to_child_read, parent_to_child_write = os.pipe()
605 child_to_parent_read, child_to_parent_write = os.pipe()
606
607 child_pid = getattr(os, fork_name)()
608 if child_pid == 0:
609 os.close(parent_to_child_write)
610 os.close(child_to_parent_read)
611 try:
612 with proxy:
613 pass
614 try:
615 lock.acquire_read()
616 except RuntimeError as error:
617 assert "was invalidated by fork()" in str(error)
618 else:
619 raise AssertionError("inherited acquisition succeeded")
620 lock.release()
621 lock.close()
622 del proxy
623 del lock
624 gc.collect()
625 sqlite_connects = 0
626
627 def count_sqlite_connects(event: str, _args: Unused) -> None:
628 global sqlite_connects
629 if event == "sqlite3.connect":
630 sqlite_connects += 1
631
632 sys.addaudithook(count_sqlite_connects)
633 try:
634 ReadWriteLock(lock_path, is_singleton=False)

Calls

no outgoing calls

Tested by

no test coverage detected