Skip to content

Commit

Permalink
Merge pull request #9 from vbraun/typing-and-remove-fdReadDupClose
Browse files Browse the repository at this point in the history
Remove the undefined fdReadDupClose
  • Loading branch information
mkoeppe committed Feb 28, 2024
2 parents 351b924 + 823249e commit 7c77144
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 33 deletions.
7 changes: 7 additions & 0 deletions py/mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[mypy]

strict = True


[mypy-cysignals.pysignals]
ignore_missing_imports = True
66 changes: 33 additions & 33 deletions py/src/gnumake_tokenpool/tokenpool.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from contextlib import contextmanager
from datetime import datetime
from typing import List
from typing import List, Any, Iterator, Never

__version__ = '0.0.6'

Expand All @@ -22,17 +22,17 @@ def __init__(
self,
#makeflags: str or None = None, # TODO implement?
#fds = List[int] or None = None, # TODO implement?
named_pipes: List[str] or None = None,
max_jobs: int or None = None,
max_load: int or None = None,
debug: bool or None = None,
debug2: bool or None = None,
use_cysignals: bool = None,
named_pipes: List[str] | None = None,
max_jobs: int | None = None,
max_load: int | None = None,
debug: bool | None = None,
debug2: bool | None = None,
use_cysignals: bool | None = None,
):

self._fdRead = None
self._fdReadDup = None
self._fdWrite = None
self._fdRead: int | None = None
self._fdReadDup: int | None = None
self._fdWrite: int | None = None
self._fifoPath = None
self._fdFifo = None
self._maxJobs = None
Expand All @@ -43,9 +43,9 @@ def __init__(
self._debug = bool(os.environ.get("DEBUG_JOBCLIENT"))
self._debug2 = bool(os.environ.get("DEBUG_JOBCLIENT_2")) # more verbose

if debug != None:
if debug is not None:
self._debug = debug
if debug2 != None:
if debug2 is not None:
self._debug2 = debug2

self._log = self._get_log(self._debug)
Expand All @@ -59,7 +59,7 @@ def __init__(
raise
else:
self._log("init: using cysignals.pysignals.changesignal")
self._changesignal = changesignal
self._changesignal = changesignal # type: ignore

makeFlags = os.environ.get("MAKEFLAGS", "")
if makeFlags:
Expand Down Expand Up @@ -117,7 +117,7 @@ def __init__(
self._log(f"init failed: maxJobs == 1")
raise NoJobServer()

if self._fdRead == None or self._fdWrite == None:
if self._fdRead is None or self._fdWrite is None:
self._log(f"init failed: no fds")
raise NoJobServer()

Expand Down Expand Up @@ -156,7 +156,7 @@ def __init__(
self._log(f"init failed: read error: {e}")
raise NoJobServer()
raise e
if token == None:
if token is None:
self._log("init: test acquire failed. jobserver is full")
else:
self._log("init: test acquire ok")
Expand All @@ -166,22 +166,22 @@ def __init__(
self._log("init: test ok")


def __del__(self):
def __del__(self) -> None:
if self._fdFifo:
os.close(self._fdFifo)


@property
def maxJobs(self) -> int or None:
def maxJobs(self) -> int | None:
return self._maxJobs


@property
def maxLoad(self) -> int or None:
def maxLoad(self) -> int | None:
return self._maxLoad


def acquire(self) -> int or None:
def acquire(self) -> int | None:
# http://make.mad-scientist.net/papers/jobserver-implementation/

# check if fdRead is readable
Expand All @@ -194,16 +194,16 @@ def acquire(self) -> int or None:
# between select and read, another process can read from the pipe.
# when the pipe is empty, read can block forever.
# by closing fdReadDup, we interrupt read
if not self._fdReadDup:
if self._fdRead and not self._fdReadDup:
self._fdReadDup = os.dup(self._fdRead)

if not self._fdReadDup:
self._log(f"acquire: failed to duplicate fd")
return None

def read_timeout_handler(_signum, _frame):
def read_timeout_handler(_signum: int, _frame: Any) -> None:
self._log(f"acquire: read timeout")
fdReadDupClose()
assert self._fdReadDup
os.close(self._fdReadDup)

# SIGALRM = timer has fired = read timeout
Expand Down Expand Up @@ -246,7 +246,7 @@ def read_timeout_handler(_signum, _frame):

def release(self, token: int = 43) -> None:
# default token: int 43 = char +

assert self._fdWrite
self._validateToken(token)
buffer = token.to_bytes(1, byteorder='big') # int8 -> byte
while True: # retry loop
Expand All @@ -270,19 +270,19 @@ def _validateToken(self, token: int) -> None:
raise InvalidToken()


def _get_log(self, debug: bool):
def _get_log(self, debug: bool) -> Any:
if debug:
def _log(*a, **k):
def _log(*a: Any, **k: Any) -> None:
k['file'] = sys.stderr
print(f"debug jobclient.py {os.getpid()} {datetime.utcnow().strftime('%F %T.%f')[:-3]}:", *a, **k)
return _log
def _log(*a, **k):
pass
return _log
else:
def _log(*a: Any, **k: Any) -> None:
pass
return _log


def _check_access(self, s, check="r"):
#s = os.stat(path)
def _check_access(self, s: os.stat_result, check: str = "r") -> int:
u = os.geteuid()
g = os.getegid()
m = s.st_mode
Expand All @@ -301,13 +301,13 @@ def _check_access(self, s, check="r"):
raise ValueError("check must be r or w")


def _is_pipe(self, s):
def _is_pipe(self, s: os.stat_result) -> bool:
if not stat.S_ISFIFO(s.st_mode):
return False
return True


def _get_stat(self, fd):
def _get_stat(self, fd: int) -> os.stat_result:
try:
return os.stat(fd)
except OSError as e:
Expand All @@ -318,7 +318,7 @@ def _get_stat(self, fd):

@staticmethod
@contextmanager
def _changesignal(sig, action):
def _changesignal(sig: int, action: Any) -> Iterator[None]:
old_sig_handler = signal.signal(sig, action)
try:
yield
Expand Down

0 comments on commit 7c77144

Please sign in to comment.