]> granicus.if.org Git - python/commitdiff
Issue #28847: A deprecation warning is now emitted if the index file is missed
authorSerhiy Storchaka <storchaka@gmail.com>
Wed, 7 Dec 2016 09:11:12 +0000 (11:11 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Wed, 7 Dec 2016 09:11:12 +0000 (11:11 +0200)
and recreated in the 'r' and 'w' modes (will be an error in future Python
releases).

Lib/dbm/dumb.py
Lib/test/test_dbm_dumb.py
Misc/NEWS

index 2296dbfd547eb76ecf38f6189c1549fc112e87d1..c3c4a666011e9bd1edc97a4cd2a3ecbd5f8c13e5 100644 (file)
@@ -68,7 +68,7 @@ class _Database(collections.MutableMapping):
 
         # Handle the creation
         self._create(flag)
-        self._update()
+        self._update(flag)
 
     def _create(self, flag):
         if flag == 'n':
@@ -92,12 +92,17 @@ class _Database(collections.MutableMapping):
             f.close()
 
     # Read directory file into the in-memory index dict.
-    def _update(self):
+    def _update(self, flag):
         self._index = {}
         try:
             f = _io.open(self._dirfile, 'r', encoding="Latin-1")
         except OSError:
             self._modified = not self._readonly
+            if flag not in ('c', 'n'):
+                import warnings
+                warnings.warn("The index file is missing, the "
+                              "semantics of the 'c' flag will be used.",
+                              DeprecationWarning, stacklevel=4)
         else:
             self._modified = False
             with f:
index df531d64e4889d87795eafa3ecd43de4d5c57fec..c2703d7e0b6d57a3c27187711ee5fdfadb141ced 100644 (file)
@@ -252,6 +252,20 @@ class DumbDBMTestCase(unittest.TestCase):
                 f = dumbdbm.open(_fname, value)
             f.close()
 
+    def test_missing_index(self):
+        with dumbdbm.open(_fname, 'n') as f:
+            pass
+        os.unlink(_fname + '.dir')
+        for value in ('r', 'w'):
+            with self.assertWarnsRegex(DeprecationWarning,
+                                       "The index file is missing, the "
+                                       "semantics of the 'c' flag will "
+                                       "be used."):
+                f = dumbdbm.open(_fname, value)
+            f.close()
+            self.assertEqual(os.path.exists(_fname + '.dir'), value == 'w')
+            self.assertFalse(os.path.exists(_fname + '.bak'))
+
     def test_invalid_flag(self):
         for flag in ('x', 'rf', None):
             with self.assertWarnsRegex(DeprecationWarning,
index d45f035415b0bf4e9c29a850d2acd6d7794b1d60..d0760914815168600735d918bf412801ebf96ebc 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -166,7 +166,9 @@ Library
 -------
 
 - Issue #28847: dbm.dumb now supports reading read-only files and no longer
-  writes the index file when it is not changed.
+  writes the index file when it is not changed.  A deprecation warning is now
+  emitted if the index file is missed and recreated in the 'r' and 'w' modes
+  (will be an error in future Python releases).
 
 - Issue #27030: Unknown escapes consisting of ``'\'`` and an ASCII letter in
   re.sub() replacement templates regular expressions now are errors.