Skip to content

Commit 9e6e7d7

Browse files
committed
Removed redunant code due to parital reimplementation of a file descriptor stream wrapper
1 parent 9b53ab0 commit 9e6e7d7

5 files changed

Lines changed: 58 additions & 49 deletions

File tree

db/loose.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,15 @@ def store(self, istream):
174174
write_object(istream.type, istream.size, istream.read, writer.write,
175175
chunk_size=self.stream_chunk_size)
176176
# END handle direct stream copies
177-
except:
177+
finally:
178178
if tmp_path:
179-
os.remove(tmp_path)
180-
raise
181-
# END assure tmpfile removal on error
182-
finally:
179+
writer.close()
180+
# END assure target stream is closed
181+
except:
183182
if tmp_path:
184-
writer.close()
185-
# END assure target stream is closed
183+
os.remove(tmp_path)
184+
raise
185+
# END assure tmpfile removal on error
186186

187187
hexsha = None
188188
if istream.sha:

stream.py

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -560,19 +560,38 @@ def close(self):
560560

561561
#} END stream interface
562562

563+
563564
class FDStream(object):
564-
"""Simple wrapper around a file descriptor"""
565-
__slots__ = "_fd"
565+
"""A simple wrapper providing the most basic functions on a file descriptor
566+
with the fileobject interface. Cannot use os.fdopen as the resulting stream
567+
takes ownership"""
568+
__slots__ = ("_fd", '_pos')
566569
def __init__(self, fd):
567570
self._fd = fd
571+
self._pos = 0
568572

569573
def write(self, data):
570-
return write(self._fd, data)
574+
self._pos += len(data)
575+
os.write(self._fd, data)
576+
577+
def read(self, count=0):
578+
if count == 0:
579+
count = os.path.getsize(self._filepath)
580+
# END handle read everything
581+
582+
bytes = os.read(self._fd, count)
583+
self._pos += len(bytes)
584+
return bytes
585+
586+
def fileno(self):
587+
return self._fd
588+
589+
def tell(self):
590+
return self._pos
571591

572592
def close(self):
573593
close(self._fd)
574-
575-
594+
576595

577596
class NullStream(object):
578597
"""A stream that does nothing but providing a stream interface.

test/lib.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ def wrapper(self):
3838
path = tempfile.mktemp(prefix=func.__name__)
3939
os.mkdir(path)
4040
try:
41-
return func(self, path)
42-
except Exception:
43-
print >> sys.stderr, "Test %s.%s failed, output is at %r" % (type(self).__name__, func.__name__, path)
44-
raise
45-
else:
41+
try:
42+
return func(self, path)
43+
except Exception:
44+
print >> sys.stderr, "Test %s.%s failed, output is at %r" % (type(self).__name__, func.__name__, path)
45+
raise
46+
finally:
4647
shutil.rmtree(path)
4748
# END handle exception
4849
# END wrapper

test/test_util.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,14 @@ def test_lockedfd(self):
8888
finally:
8989
os.remove(my_file)
9090
# END final cleanup
91+
92+
# try non-existing file for reading
93+
lfd = LockedFD(tempfile.mktemp())
94+
try:
95+
lfd.open(write=False)
96+
except OSError:
97+
assert not os.path.exists(lfd._lockfilepath())
98+
else:
99+
self.fail("expected OSError")
100+
# END handle exceptions
91101

util.py

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,6 @@ def _set_cache_(self, attr):
161161
in the single attribute."""
162162
pass
163163

164-
165-
class FDStreamWrapper(object):
166-
"""A simple wrapper providing the most basic functions on a file descriptor
167-
with the fileobject interface. Cannot use os.fdopen as the resulting stream
168-
takes ownership"""
169-
__slots__ = ("_fd", '_pos')
170-
def __init__(self, fd):
171-
self._fd = fd
172-
self._pos = 0
173-
174-
def write(self, data):
175-
self._pos += len(data)
176-
os.write(self._fd, data)
177-
178-
def read(self, count=0):
179-
if count == 0:
180-
count = os.path.getsize(self._filepath)
181-
# END handle read everything
182-
183-
bytes = os.read(self._fd, count)
184-
self._pos += len(bytes)
185-
return bytes
186-
187-
def fileno(self):
188-
return self._fd
189-
190-
def tell(self):
191-
return self._pos
192-
193164

194165
class LockedFD(object):
195166
"""This class facilitates a safe read and write operation to a file on disk.
@@ -240,7 +211,7 @@ def open(self, write=False, stream=False):
240211
binary = getattr(os, 'O_BINARY', 0)
241212
lockmode = os.O_WRONLY | os.O_CREAT | os.O_EXCL | binary
242213
try:
243-
fd = os.open(self._lockfilepath(), lockmode)
214+
fd = os.open(self._lockfilepath(), lockmode, 0600)
244215
if not write:
245216
os.close(fd)
246217
else:
@@ -253,11 +224,19 @@ def open(self, write=False, stream=False):
253224
# open actual file if required
254225
if self._fd is None:
255226
# we could specify exlusive here, as we obtained the lock anyway
256-
self._fd = os.open(self._filepath, os.O_RDONLY | binary)
227+
try:
228+
self._fd = os.open(self._filepath, os.O_RDONLY | binary)
229+
except:
230+
# assure we release our lockfile
231+
os.remove(self._lockfilepath())
232+
raise
233+
# END handle lockfile
257234
# END open descriptor for reading
258235

259236
if stream:
260-
return FDStreamWrapper(self._fd)
237+
# need delayed import
238+
from stream import FDStream
239+
return FDStream(self._fd)
261240
else:
262241
return self._fd
263242
# END handle stream

0 commit comments

Comments
 (0)