util: make OpenSafeFile work without fcntl (Windows)

prjxray/util.py imported fcntl at module level, so merely importing
prjxray (e.g. from fasm2frames) failed on Windows, where fcntl does
not exist. The only user is OpenSafeFile's advisory flock, whose
timeout also relies on SIGALRM — equally POSIX-only.

Guard the import and skip the locking when fcntl is unavailable:
OpenSafeFile degrades to a plain open on Windows, while POSIX
behaviour is unchanged.
This commit is contained in:
Carlos Venegas Arrabé 2026-07-30 16:19:34 +02:00
parent 19d29f9c84
commit 10381074b2
No known key found for this signature in database
GPG Key ID: EC73A5348B584C4C
1 changed files with 10 additions and 1 deletions

View File

@ -8,7 +8,12 @@
# https://opensource.org/licenses/ISC
#
# SPDX-License-Identifier: ISC
import fcntl
try:
import fcntl
except ImportError:
# Windows: no fcntl (and no SIGALRM); OpenSafeFile degrades to a plain
# open without inter-process locking.
fcntl = None
import math
import os
import random
@ -47,6 +52,8 @@ class OpenSafeFile:
def lock_file(self):
assert self.fd is not None
if fcntl is None:
return
try:
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(self.timeout)
@ -58,6 +65,8 @@ class OpenSafeFile:
def unlock_file(self):
assert self.fd is not None
if fcntl is None:
return
fcntl.flock(self.fd.fileno(), fcntl.LOCK_UN)