summaryrefslogtreecommitdiff
path: root/django/core/files/locks.py
diff options
context:
space:
mode:
authorJacob Kaplan-Moss <jacob@jacobian.org>2008-08-11 16:51:18 +0000
committerJacob Kaplan-Moss <jacob@jacobian.org>2008-08-11 16:51:18 +0000
commit58cd4902a71a3695dd6c21dc957f59c333db364c (patch)
tree3af68e7cfb7ad5cbb3e46c4d242f73262e11c99d /django/core/files/locks.py
parentab1a442a01eb3ee1ca210cd689c9b578d31d7366 (diff)
downloaddjango-58cd4902a71a3695dd6c21dc957f59c333db364c.tar.gz
Fixed #4948, a race condition in file saving. Thanks to Martin von Löwis, who diagnosed the problem and pointed the way to a fix.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@8306 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/core/files/locks.py')
-rw-r--r--django/core/files/locks.py12
1 files changed, 8 insertions, 4 deletions
diff --git a/django/core/files/locks.py b/django/core/files/locks.py
index 212b51a73d..98a11551a7 100644
--- a/django/core/files/locks.py
+++ b/django/core/files/locks.py
@@ -40,20 +40,24 @@ try:
except (ImportError, AttributeError):
pass
+def fd(f):
+ """Get a filedescriptor from something which could be a file or an fd."""
+ return hasattr(f, 'fileno') and f.fileno() or f
+
if system_type == 'nt':
def lock(file, flags):
- hfile = win32file._get_osfhandle(file.fileno())
+ hfile = win32file._get_osfhandle(fd(file))
win32file.LockFileEx(hfile, flags, 0, -0x10000, __overlapped)
def unlock(file):
- hfile = win32file._get_osfhandle(file.fileno())
+ hfile = win32file._get_osfhandle(fd(file))
win32file.UnlockFileEx(hfile, 0, -0x10000, __overlapped)
elif system_type == 'posix':
def lock(file, flags):
- fcntl.flock(file.fileno(), flags)
+ fcntl.flock(fd(file), flags)
def unlock(file):
- fcntl.flock(file.fileno(), fcntl.LOCK_UN)
+ fcntl.flock(fd(file), fcntl.LOCK_UN)
else:
# File locking is not supported.
LOCK_EX = LOCK_SH = LOCK_NB = None