Skip to content

Commit 4ca4e59

Browse files
committed
fix: close file properly in indexer_dbm.py:save_index()
Fix this error found in debug logs of gentoo packaging of round 2.2.0. /roundup/backends/indexer_dbm.py:253: ResourceWarning: unclosed file <_io.BufferedWriter name='test-index/indexes/index.db-'> open(self.indexdb+'-', 'wb').write(zlib.compress(marshal.dumps(dbfil))) Also added test that calls save_index(), reloads the index and tests that the original item. I am not sure how Gentoo hit this But they were missing a number of backends. So it's possible that indexer_dbm.py is not getting fully tested depending on what is installed on the system. Codecov from CI didnt show indexer_dbm.py:save_index() being covered.
1 parent 20d498c commit 4ca4e59

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Fixed:
7373
Rouillard)
7474
- fix repeated password id with user.item.html in all templates except
7575
jinja2. (John Rouillard)
76+
- fix unclosed file when saving index in indexer_dbm.py. (John Rouillard)
7677

7778
Features:
7879

roundup/backends/indexer_dbm.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,9 @@ def save_index(self):
250250

251251
# First write the much simpler filename/fileid dictionaries
252252
dbfil = {'WORDS': None, 'FILES': self.files, 'FILEIDS': self.fileids}
253-
open(self.indexdb+'-', 'wb').write(zlib.compress(marshal.dumps(dbfil)))
253+
marshal_fh = open(self.indexdb+'-', 'wb')
254+
marshal_fh.write(zlib.compress(marshal.dumps(dbfil)))
255+
marshal_fh.close()
254256

255257
# The hard part is splitting the word dictionary up, of course
256258
letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ#_"

test/test_indexer.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ def test_basics(self):
104104
self.assertSeqEqual(self.dex.find(['blah', 'hello']), [])
105105
self.assertSeqEqual(self.dex.find([]), [])
106106

107+
def test_save_load(self):
108+
109+
# only run for anydbm test
110+
if ( not type(self) is IndexerTest ):
111+
pytest.skip("test_save_load tested only for anydbm backend")
112+
113+
self.dex.add_text(('test', '1', 'foo'), 'b the hello world')
114+
self.assertSeqEqual(self.dex.find(['hello']), [('test', '1', 'foo')])
115+
self.dex.save_index()
116+
117+
# reopen saved db.
118+
from roundup.backends.indexer_dbm import Indexer
119+
self.dex = Indexer(db)
120+
121+
# verify index is unloaded
122+
self.assertEqual(self.dex.index_loaded(), False)
123+
124+
# add also calls load_index(), so it should load the first item.
125+
self.dex.add_text(('test', '2', 'foo'), 'b the olleh world')
126+
127+
# note find also does a load_index() if not loaded.
128+
self.assertSeqEqual(self.dex.find(['hello']), [('test', '1', 'foo')])
129+
self.assertSeqEqual(self.dex.find(['olleh']), [('test', '2', 'foo')])
130+
107131
def test_change(self):
108132
self.dex.add_text(('test', '1', 'foo'), 'a the hello world')
109133
self.dex.add_text(('test', '2', 'foo'), 'blah blah the world')

0 commit comments

Comments
 (0)