mirror of https://github.com/openXC7/prjxray.git
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:
parent
19d29f9c84
commit
10381074b2
|
|
@ -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)
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue