MCPcopy Index your code
hub / github.com/python/cpython / _lock_file

Function _lock_file

Lib/mailbox.py:2140–2180  ·  view source on GitHub ↗

Lock file f using lockf and dot locking.

(f, dotlock=True)

Source from the content-addressed store, hash-verified

2138
2139
2140def _lock_file(f, dotlock=True):
2141 """Lock file f using lockf and dot locking."""
2142 dotlock_done = False
2143 try:
2144 if fcntl:
2145 try:
2146 fcntl.lockf(f, fcntl.LOCK_EX | fcntl.LOCK_NB)
2147 except OSError as e:
2148 if e.errno in (errno.EAGAIN, errno.EACCES, errno.EROFS):
2149 raise ExternalClashError('lockf: lock unavailable: %s' %
2150 f.name)
2151 else:
2152 raise
2153 if dotlock:
2154 try:
2155 pre_lock = _create_temporary(f.name + '.lock')
2156 pre_lock.close()
2157 except OSError as e:
2158 if e.errno in (errno.EACCES, errno.EROFS):
2159 return # Without write access, just skip dotlocking.
2160 else:
2161 raise
2162 try:
2163 try:
2164 os.link(pre_lock.name, f.name + '.lock')
2165 dotlock_done = True
2166 except (AttributeError, PermissionError):
2167 os.rename(pre_lock.name, f.name + '.lock')
2168 dotlock_done = True
2169 else:
2170 os.unlink(pre_lock.name)
2171 except FileExistsError:
2172 os.remove(pre_lock.name)
2173 raise ExternalClashError('dot lock unavailable: %s' %
2174 f.name)
2175 except:
2176 if fcntl:
2177 fcntl.lockf(f, fcntl.LOCK_UN)
2178 if dotlock_done:
2179 os.remove(f.name + '.lock')
2180 raise
2181
2182def _unlock_file(f):
2183 """Unlock file f using lockf and dot locking."""

Callers 7

lockMethod · 0.85
flushMethod · 0.85
addMethod · 0.85
__setitem__Method · 0.85
get_messageMethod · 0.85
get_bytesMethod · 0.85
lockMethod · 0.85

Calls 6

ExternalClashErrorClass · 0.85
_create_temporaryFunction · 0.85
closeMethod · 0.45
renameMethod · 0.45
unlinkMethod · 0.45
removeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…