X Tutup
Skip to content

Commit 0b626d4

Browse files
committed
Add file locking for history file
Signed-off-by: Sebastian Ramacher <sebastian+dev@ramacher.at>
1 parent f9c5e7d commit 0b626d4

File tree

2 files changed

+67
-13
lines changed

2 files changed

+67
-13
lines changed

bpython/filelock.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# The MIT License
2+
#
3+
# Copyright (c) 2015 the bpython authors.
4+
#
5+
# Permission is hereby granted, free of charge, to any person obtaining a copy
6+
# of this software and associated documentation files (the "Software"), to deal
7+
# in the Software without restriction, including without limitation the rights
8+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
# copies of the Software, and to permit persons to whom the Software is
10+
# furnished to do so, subject to the following conditions:
11+
#
12+
# The above copyright notice and this permission notice shall be included in
13+
# all copies or substantial portions of the Software.
14+
#
15+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
# THE SOFTWARE.
22+
23+
24+
try:
25+
import fcntl
26+
has_fcntl = True
27+
except ImportError:
28+
has_fcntl = False
29+
30+
31+
class FileLock(object):
32+
"""Simple file locking
33+
34+
On platforms without fcntl, all operations in this class are no-ops.
35+
"""
36+
37+
def __init__(self, fd, mode=fcntl.LOCK_EX):
38+
self.fd = fd
39+
self.mode = mode
40+
41+
def __enter__(self):
42+
if has_fcntl:
43+
fcntl.flock(self.fd, self.mode)
44+
return self
45+
46+
def __exit__(self, *args):
47+
if has_fcntl:
48+
fcntl.flock(self.fd, fcntl.LOCK_UN)
49+
50+
# vim: sw=4 ts=4 sts=4 ai et

bpython/history.py

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import os
2626

2727
from bpython.translations import _
28+
from bpython.filelock import FileLock
2829

2930

3031
class History(object):
@@ -173,7 +174,8 @@ def reset(self):
173174

174175
def load(self, filename, encoding):
175176
with codecs.open(filename, 'r', encoding, 'ignore') as hfile:
176-
self.entries = self.load_from(hfile)
177+
with FileLock(hfile) as lock:
178+
self.entries = self.load_from(hfile)
177179

178180

179181
def load_from(self, fd):
@@ -185,7 +187,8 @@ def load_from(self, fd):
185187

186188
def save(self, filename, encoding, lines=0):
187189
with codecs.open(filename, 'w', encoding, 'ignore') as hfile:
188-
self.save_to(hfile, self.entries, lines)
190+
with FileLock(hfile) as lock:
191+
self.save_to(hfile, self.entries, lines)
189192

190193

191194
def save_to(self, fd, entries=None, lines=0):
@@ -202,17 +205,18 @@ def append_reload_and_write(self, s, filename, encoding):
202205

203206
try:
204207
with codecs.open(filename, 'rw+', encoding, 'ignore') as hfile:
205-
# read entries
206-
hfile.seek(0, os.SEEK_SET)
207-
entries = self.load_from(hfile)
208-
self.append_to(entries, s)
209-
210-
# write new entries
211-
hfile.seek(0, os.SEEK_SET)
212-
hfile.truncate()
213-
self.save_to(hfile, entries, self.hist_size)
214-
215-
self.entries = entries
208+
with FileLock(hfile) as lock:
209+
# read entries
210+
hfile.seek(0, os.SEEK_SET)
211+
entries = self.load_from(hfile)
212+
self.append_to(entries, s)
213+
214+
# write new entries
215+
hfile.seek(0, os.SEEK_SET)
216+
hfile.truncate()
217+
self.save_to(hfile, entries, self.hist_size)
218+
219+
self.entries = entries
216220
except EnvironmentError as err:
217221
raise RuntimeError(_('Error occurded while writing to file %s (%s)')
218222
% (filename, err.strerror))

0 commit comments

Comments
 (0)
X Tutup