evals / django-locks
import-order
Expected
Hidden from the reviewer, like the grading note below.
- posix-lock-results
- holds
Only the Windows ctypes import is reformatted. POSIX lock behavior is unchanged. Failing the contract is an invented failure.
Change
django/core/files/locks.py
-4+2
27 unmodified lines28293031323334313233343527 unmodified lines
if os.name == 'nt': import msvcrt from ctypes import ( POINTER, Structure, Union, byref, c_int64, c_ulong, c_void_p, sizeof, windll, ) from ctypes import (sizeof, c_ulong, c_void_p, c_int64, Structure, Union, POINTER, windll, byref) from ctypes.wintypes import BOOL, DWORD, HANDLE
LOCK_SH = 0 # the defaultContracts
posix-lock-results
---
name: posix-lock-results
message: Report lock contention as False and let other POSIX lock failures raise.
---
## Rules
On POSIX, `fcntl.flock` returns `None` on success and raises `OSError` on failure. `lock()` returns `True` when
`flock` succeeds. It returns `False` only when a non-blocking lock is held elsewhere, which `flock` reports as
`BlockingIOError`. Every other `OSError`, such as a bad file descriptor, propagates to the caller. Catching
`OSError` would hide those errors and change behavior for existing callers.
`unlock()` returns `True` when `flock` succeeds and lets failures propagate.
## Checks
On Linux, lock a temporary file with `LOCK_EX | LOCK_NB` from two file objects. The second call returns `False`.
Calling `lock()` with a closed file descriptor number raises `OSError`.