11# The MIT License
22#
3- # Copyright (c) 2015-2019 Sebastian Ramacher
3+ # Copyright (c) 2015-2021 Sebastian Ramacher
44#
55# Permission is hereby granted, free of charge, to any person obtaining a copy
66# of this software and associated documentation files (the "Software"), to deal
2020# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2121# THE SOFTWARE.
2222
23-
23+ has_fcntl = True
2424try :
2525 import fcntl
2626 import errno
27-
28- has_fcntl = True
2927except ImportError :
3028 has_fcntl = False
3129
30+ has_msvcrt = True
3231try :
3332 import msvcrt
3433 import os
35-
36- has_msvcrt = True
3734except ImportError :
3835 has_msvcrt = False
3936
4037
4138class BaseLock :
4239 """Base class for file locking"""
4340
44- def __init__ (self , fileobj , mode = None , filename = None ):
41+ def __init__ (self , fileobj = None ):
4542 self .fileobj = fileobj
4643 self .locked = False
4744
@@ -67,12 +64,9 @@ def __del__(self):
6764class UnixFileLock (BaseLock ):
6865 """Simple file locking for Unix using fcntl"""
6966
70- def __init__ (self , fileobj , mode = None , filename = None ):
67+ def __init__ (self , fileobj , mode = 0 ):
7168 super ().__init__ (fileobj )
72-
73- if mode is None :
74- mode = fcntl .LOCK_EX
75- self .mode = mode
69+ self .mode = mode | fcntl .LOCK_EX
7670
7771 def acquire (self ):
7872 try :
@@ -90,8 +84,8 @@ def release(self):
9084class WindowsFileLock (BaseLock ):
9185 """Simple file locking for Windows using msvcrt"""
9286
93- def __init__ (self , fileobj , mode = None , filename = None ):
94- super ().__init__ (None )
87+ def __init__ (self , filename ):
88+ super ().__init__ ()
9589 self .filename = f"{ filename } .lock"
9690
9791 def acquire (self ):
@@ -117,11 +111,12 @@ def release(self):
117111 pass
118112
119113
120- if has_fcntl :
121- FileLock = UnixFileLock
122- elif has_msvcrt :
123- FileLock = WindowsFileLock
124- else :
125- FileLock = BaseLock
114+ def FileLock (fileobj , mode = 0 , filename = None ):
115+ if has_fcntl :
116+ return UnixFileLock (fileobj , mode )
117+ elif has_msvcrt :
118+ return WindowsFileLock (filename )
119+ return BaseLock (fileobj )
120+
126121
127122# vim: sw=4 ts=4 sts=4 ai et
0 commit comments