Our code makes extensive use of the fcntl
-module to lock files -- to prevent multiple instances of the program from stepping over each other. This works fine on Linux, but, as we just discovered, there is no fcntl on Windows...
Fortunately, we don't need the multiple instance-safety on Windows and could just fake it there. But replacing the existing use of fcntl
-module, its constants (like fcntl.LOCK_EX
) and function (fcntl.flock
) is something, I want to avoid.
So I tried creating an fcntl.py
of my own with something like:
import sys
import os
if os.name == 'posix':
syspath = sys.path
sys.path = sys.path[1:] # Remove current directory from search path
import fcntl as realfcntl
LOCK_EX = realfcntl.LOCK_EX
LOCK_SH = realfcntl.LOCK_SH
LOCK_UN = realfcntl.LOCK_UN
sys.path = syspath
def flock(fd, mode):
return realfcntl.flock(fd, mode)
else:
# Fake it:
LOCK_EX = -1
LOCK_SH = -1
LOCK_UN = -1
def flock(fd, mode):
print('Pretending to manipulate locking on FD %s' % fd, file = sys.stderr)
To my dismay, this fails at the import time on Unix, on line 8: module 'fcntl' has no attribute 'LOCK_EX'
, which tells me, my attempt to trick it into loading the real fcntl failed.
How can the wrapper like mine load the real module (being wrapped)?