From: Andrew M. Kuchling Date: Tue, 7 Jun 2005 19:57:01 +0000 (+0000) Subject: [Bug #1172763] dumbdbm uses eval() on lines, so it chokes if there's an extra \r... X-Git-Tag: v2.4.2c1~199 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=997b36271e50196cc59aee18a46cc8ccfb0f866a;p=thirdparty%2FPython%2Fcpython.git [Bug #1172763] dumbdbm uses eval() on lines, so it chokes if there's an extra \r on the end of a line; fixed by stripping off trailing whitespace. --- diff --git a/Lib/dumbdbm.py b/Lib/dumbdbm.py index b85844dc611a..84a766589082 100644 --- a/Lib/dumbdbm.py +++ b/Lib/dumbdbm.py @@ -81,6 +81,7 @@ class _Database(UserDict.DictMixin): pass else: for line in f: + line = line.rstrip() key, pos_and_siz_pair = eval(line) self._index[key] = pos_and_siz_pair f.close() diff --git a/Lib/test/test_dumbdbm.py b/Lib/test/test_dumbdbm.py index 12df673eaa48..d320110b9079 100644 --- a/Lib/test/test_dumbdbm.py +++ b/Lib/test/test_dumbdbm.py @@ -74,6 +74,24 @@ class DumbDBMTestCase(unittest.TestCase): self.assertEqual(f['1'], 'hello2') f.close() + def test_line_endings(self): + # test for bug #1172763: dumbdbm would die if the line endings + # weren't what was expected. + f = dumbdbm.open(_fname) + f['1'] = 'hello' + f['2'] = 'hello2' + f.close() + + # Mangle the file by adding \r before each newline + data = open(_fname + '.dir').read() + data = data.replace('\n', '\r\n') + open(_fname + '.dir', 'wb').write(data) + + f = dumbdbm.open(_fname) + self.assertEqual(f['1'], 'hello') + self.assertEqual(f['2'], 'hello2') + + def read_helper(self, f): keys = self.keys_helper(f) for key in self._dict: