mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-06-23 15:30:45 -04:00
More explicit resource closing
This commit is contained in:
parent
6660bd828b
commit
5fc243a93d
@ -323,7 +323,10 @@ def find_tests():
|
|||||||
book_src = os.path.join(self.tdir, 'book.epub')
|
book_src = os.path.join(self.tdir, 'book.epub')
|
||||||
set_data('a')
|
set_data('a')
|
||||||
path = prepare_book(book_src, convert_func=convert_mock)
|
path = prepare_book(book_src, convert_func=convert_mock)
|
||||||
self.ae(open(os.path.join(path, 'sentinel'), 'rb').read(), b'test')
|
def read(x, mode='r'):
|
||||||
|
with open(x, mode) as f:
|
||||||
|
return f.read()
|
||||||
|
self.ae(read(os.path.join(path, 'sentinel'), 'rb'), b'test')
|
||||||
|
|
||||||
# Test that opening the same book uses the cache
|
# Test that opening the same book uses the cache
|
||||||
second_path = prepare_book(book_src, convert_func=convert_mock)
|
second_path = prepare_book(book_src, convert_func=convert_mock)
|
||||||
@ -366,11 +369,11 @@ def find_tests():
|
|||||||
book_src = os.path.join(self.tdir, 'book2.epub')
|
book_src = os.path.join(self.tdir, 'book2.epub')
|
||||||
set_data('bb')
|
set_data('bb')
|
||||||
path = prepare_book(book_src, convert_func=convert_mock)
|
path = prepare_book(book_src, convert_func=convert_mock)
|
||||||
self.ae(open(os.path.join(path, 'sentinel'), 'rb').read(), b'test')
|
self.ae(read(os.path.join(path, 'sentinel'), 'rb'), b'test')
|
||||||
bs = os.stat(book_src)
|
bs = os.stat(book_src)
|
||||||
set_data('cde')
|
set_data('cde')
|
||||||
update_book(book_src, bs, name_data_map={'sentinel': b'updated'})
|
update_book(book_src, bs, name_data_map={'sentinel': b'updated'})
|
||||||
self.ae(open(os.path.join(path, 'sentinel'), 'rb').read(), b'updated')
|
self.ae(read(os.path.join(path, 'sentinel'), 'rb'), b'updated')
|
||||||
self.ae(1, len(os.listdir(os.path.join(book_cache_dir(), 'f'))))
|
self.ae(1, len(os.listdir(os.path.join(book_cache_dir(), 'f'))))
|
||||||
with cache_lock() as f:
|
with cache_lock() as f:
|
||||||
metadata = json.loads(f.read())
|
metadata = json.loads(f.read())
|
||||||
|
@ -116,20 +116,27 @@ def find_tests():
|
|||||||
f.write(b'a' * 20 * 1024)
|
f.write(b'a' * 20 * 1024)
|
||||||
eq(fname, f.name)
|
eq(fname, f.name)
|
||||||
f = share_open(fname, 'rb')
|
f = share_open(fname, 'rb')
|
||||||
eq(f.read(1), b'a')
|
close = [f]
|
||||||
if iswindows:
|
try:
|
||||||
os.rename(fname, fname+'.moved')
|
eq(f.read(1), b'a')
|
||||||
os.remove(fname+'.moved')
|
if iswindows:
|
||||||
else:
|
os.rename(fname, fname+'.moved')
|
||||||
os.remove(fname)
|
os.remove(fname+'.moved')
|
||||||
eq(f.read(1), b'a')
|
else:
|
||||||
f2 = share_open(fname, 'w+b')
|
os.remove(fname)
|
||||||
f2.write(b'b' * 10 * 1024)
|
eq(f.read(1), b'a')
|
||||||
f2.seek(0)
|
f2 = share_open(fname, 'w+b')
|
||||||
eq(f.read(10000), b'a'*10000)
|
close.append(f2)
|
||||||
eq(f2.read(100), b'b' * 100)
|
f2.write(b'b' * 10 * 1024)
|
||||||
f3 = share_open(fname, 'rb')
|
f2.seek(0)
|
||||||
eq(f3.read(100), b'b' * 100)
|
eq(f.read(10000), b'a'*10000)
|
||||||
|
eq(f2.read(100), b'b' * 100)
|
||||||
|
f3 = share_open(fname, 'rb')
|
||||||
|
close.append(f3)
|
||||||
|
eq(f3.read(100), b'b' * 100)
|
||||||
|
finally:
|
||||||
|
for f in close:
|
||||||
|
f.close()
|
||||||
|
|
||||||
return unittest.defaultTestLoader.loadTestsFromTestCase(SharedFileTest)
|
return unittest.defaultTestLoader.loadTestsFromTestCase(SharedFileTest)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user