From 9ef0ef5b729b88491be1d28ab46248b645645c44 Mon Sep 17 00:00:00 2001 From: "Andrew M. Kuchling" Date: Fri, 22 Dec 2006 15:16:58 +0000 Subject: [PATCH] [Bug #802128 continued] Modify mode depending on the process umask. Is there really no other way to read the umask than to set it? Hope this works on Windows... --- Lib/dumbdbm.py | 11 +++++++++++ Lib/test/test_dumbdbm.py | 16 ++++++++++------ 2 files changed, 21 insertions(+), 6 deletions(-) diff --git a/Lib/dumbdbm.py b/Lib/dumbdbm.py index 9e9ffcc117f..2c7931d1847 100644 --- a/Lib/dumbdbm.py +++ b/Lib/dumbdbm.py @@ -236,4 +236,15 @@ def open(file, flag=None, mode=0666): """ # flag argument is currently ignored + + # Modify mode depending on the umask + try: + um = _os.umask(0) + _os.umask(um) + except AttributeError: + pass + else: + # Turn off any bits that are set in the umask + mode = mode & (~um) + return _Database(file, mode) diff --git a/Lib/test/test_dumbdbm.py b/Lib/test/test_dumbdbm.py index a1e34da34b7..e5dfe1d7e78 100644 --- a/Lib/test/test_dumbdbm.py +++ b/Lib/test/test_dumbdbm.py @@ -40,17 +40,21 @@ class DumbDBMTestCase(unittest.TestCase): def test_dumbdbm_creation_mode(self): # On platforms without chmod, don't do anything. - if not hasattr(os, 'chmod'): + if not (hasattr(os, 'chmod') and hasattr(os, 'umask')): return - f = dumbdbm.open(_fname, 'c', 0632) - f.close() - + try: + old_umask = os.umask(0002) + f = dumbdbm.open(_fname, 'c', 0637) + f.close() + finally: + os.umask(old_umask) + import stat st = os.stat(_fname + '.dat') - self.assertEqual(stat.S_IMODE(st.st_mode), 0632) + self.assertEqual(stat.S_IMODE(st.st_mode), 0635) st = os.stat(_fname + '.dir') - self.assertEqual(stat.S_IMODE(st.st_mode), 0632) + self.assertEqual(stat.S_IMODE(st.st_mode), 0635) def test_close_twice(self): f = dumbdbm.open(_fname)