Skip to content

Commit 52580b4

Browse files
committed
Create a lock file on Windows
1 parent afc0eb3 commit 52580b4

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

bpython/filelock.py

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
# The MIT License
44
#
5-
# Copyright (c) 2015-2016 Sebastian Ramacher
5+
# Copyright (c) 2015-2019 Sebastian Ramacher
66
#
77
# Permission is hereby granted, free of charge, to any person obtaining a copy
88
# of this software and associated documentation files (the "Software"), to deal
@@ -33,6 +33,7 @@
3333

3434
try:
3535
import msvcrt
36+
import os
3637
has_msvcrt = True
3738
except ImportError:
3839
has_msvcrt = False
@@ -42,7 +43,7 @@ class BaseLock(object):
4243
"""Base class for file locking
4344
"""
4445

45-
def __init__(self, fileobj):
46+
def __init__(self, fileobj, mode=None, filename=None):
4647
self.fileobj = fileobj
4748
self.locked = False
4849

@@ -69,7 +70,7 @@ class UnixFileLock(BaseLock):
6970
"""Simple file locking for Unix using fcntl
7071
"""
7172

72-
def __init__(self, fileobj, mode=None):
73+
def __init__(self, fileobj, mode=None, filename=None):
7374
super(UnixFileLock, self).__init__(fileobj)
7475

7576
if mode is None:
@@ -93,16 +94,29 @@ class WindowsFileLock(BaseLock):
9394
"""Simple file locking for Windows using msvcrt
9495
"""
9596

96-
def __init__(self, fileobj, mode=None):
97-
super(WindowsFileLock, self).__init__(fileobj)
97+
def __init__(self, fileobj, mode=None, filename=None):
98+
super(WindowsFileLock, self).__init__(None)
99+
self.filename = "{}.lock".format(filename)
98100

99101
def acquire(self):
100-
msvcrt.locking(self.fileobj.fileno(), msvcrt.LK_NBLCK, 1)
102+
# create a lock file and lock it
103+
self.fileobj = os.open(self.filename, os.O_RDWR | os.O_CREAT | os.O_TRUNC)
104+
msvcrt.locking(self.fileobj, msvcrt.LK_NBLCK, 1)
105+
101106
self.locked = True
102107

103108
def release(self):
104109
self.locked = False
105-
msvcrt.locking(self.fileobj.fileno(), msvcrt.LK_UNLCK, 1)
110+
111+
# unlock lock file and remove it
112+
msvcrt.locking(self.fileobj, msvcrt.LK_UNLCK, 1)
113+
self.fileobj.close()
114+
self.fileobj = None
115+
116+
try:
117+
os.remove(self.filename)
118+
except OSError:
119+
pass
106120

107121

108122
if has_fcntl:

bpython/history.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def reset(self):
172172
def load(self, filename, encoding):
173173
with io.open(filename, 'r', encoding=encoding,
174174
errors='ignore') as hfile:
175-
with FileLock(hfile):
175+
with FileLock(hfile, filename=filename):
176176
self.entries = self.load_from(hfile)
177177

178178
def load_from(self, fd):
@@ -186,7 +186,7 @@ def save(self, filename, encoding, lines=0):
186186
stat.S_IRUSR | stat.S_IWUSR)
187187
with io.open(fd, 'w', encoding=encoding,
188188
errors='ignore') as hfile:
189-
with FileLock(hfile):
189+
with FileLock(hfile, filename=filename):
190190
self.save_to(hfile, self.entries, lines)
191191

192192
def save_to(self, fd, entries=None, lines=0):
@@ -205,7 +205,7 @@ def append_reload_and_write(self, s, filename, encoding):
205205
stat.S_IRUSR | stat.S_IWUSR)
206206
with io.open(fd, 'a+', encoding=encoding,
207207
errors='ignore') as hfile:
208-
with FileLock(hfile):
208+
with FileLock(hfile, filename=filename):
209209
# read entries
210210
hfile.seek(0, os.SEEK_SET)
211211
entries = self.load_from(hfile)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy