diff options
author | Ned Deily <nad@acm.org> | 2011-06-28 07:00:28 (GMT) |
---|---|---|
committer | Ned Deily <nad@acm.org> | 2011-06-28 07:00:28 (GMT) |
commit | 3eb67d58d61cec42c09242a7d801072c3e448dcf (patch) | |
tree | 821fab0f068bbbd184ac8abec600a9cb3eaf16fc /Lib | |
parent | 11f00f3b00a0667c41509318f49f0da75ab5c977 (diff) | |
download | cpython-3eb67d58d61cec42c09242a7d801072c3e448dcf.zip cpython-3eb67d58d61cec42c09242a7d801072c3e448dcf.tar.gz cpython-3eb67d58d61cec42c09242a7d801072c3e448dcf.tar.bz2 |
Issue #8746: Correct faulty configure checks so that os.chflags() and
os.lchflags() are once again built on systems that support these
functions (*BSD and OS X). Also add new stat file flags for OS X
(UF_HIDDEN and UF_COMPRESSED). Also add additional tests for
os.chflags() and os.lchflags(). (Tests by Garrett Cooper)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/stat.py | 2 | ||||
-rw-r--r-- | Lib/test/test_posix.py | 59 |
2 files changed, 49 insertions, 12 deletions
diff --git a/Lib/stat.py b/Lib/stat.py index d29c63c..78ccd5e 100644 --- a/Lib/stat.py +++ b/Lib/stat.py @@ -87,6 +87,8 @@ UF_IMMUTABLE = 0x00000002 UF_APPEND = 0x00000004 UF_OPAQUE = 0x00000008 UF_NOUNLINK = 0x00000010 +UF_COMPRESSED = 0x00000020 # OS X: file is hfs-compressed +UF_HIDDEN = 0x00008000 # OS X: file should not be displayed SF_ARCHIVED = 0x00010000 SF_IMMUTABLE = 0x00020000 SF_APPEND = 0x00040000 diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py index 45b3afc..5563050 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -15,6 +15,7 @@ import stat import unittest import warnings +_DUMMY_SYMLINK = '%s/dummy-symlink' % os.getenv('TMPDIR', '/tmp') class PosixTester(unittest.TestCase): @@ -22,13 +23,15 @@ class PosixTester(unittest.TestCase): # create empty file fp = open(support.TESTFN, 'w+') fp.close() + self.teardown_files = [ support.TESTFN ] self._warnings_manager = support.check_warnings() self._warnings_manager.__enter__() warnings.filterwarnings('ignore', '.* potential security risk .*', RuntimeWarning) def tearDown(self): - support.unlink(support.TESTFN) + for teardown_file in self.teardown_files: + support.unlink(teardown_file) self._warnings_manager.__exit__(None, None, None) def testNoArgFunctions(self): @@ -268,7 +271,7 @@ class PosixTester(unittest.TestCase): def test_lchown(self): os.unlink(support.TESTFN) # create a symlink - os.symlink('/tmp/dummy-symlink-target', support.TESTFN) + os.symlink(_DUMMY_SYMLINK, support.TESTFN) self._test_all_chown_common(posix.lchown, support.TESTFN) def test_chdir(self): @@ -315,17 +318,49 @@ class PosixTester(unittest.TestCase): posix.utime(support.TESTFN, (int(now), int(now))) posix.utime(support.TESTFN, (now, now)) + def _test_chflags_regular_file(self, chflags_func, target_file): + st = os.stat(target_file) + self.assertTrue(hasattr(st, 'st_flags')) + chflags_func(target_file, st.st_flags | stat.UF_IMMUTABLE) + try: + new_st = os.stat(target_file) + self.assertEqual(st.st_flags | stat.UF_IMMUTABLE, new_st.st_flags) + try: + fd = open(target_file, 'w+') + except IOError as e: + self.assertEqual(e.errno, errno.EPERM) + finally: + posix.chflags(target_file, st.st_flags) + + @unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()') def test_chflags(self): - if hasattr(posix, 'chflags'): - st = os.stat(support.TESTFN) - if hasattr(st, 'st_flags'): - posix.chflags(support.TESTFN, st.st_flags) - - def test_lchflags(self): - if hasattr(posix, 'lchflags'): - st = os.stat(support.TESTFN) - if hasattr(st, 'st_flags'): - posix.lchflags(support.TESTFN, st.st_flags) + self._test_chflags_regular_file(posix.chflags, support.TESTFN) + + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') + def test_lchflags_regular_file(self): + self._test_chflags_regular_file(posix.lchflags, support.TESTFN) + + @unittest.skipUnless(hasattr(posix, 'lchflags'), 'test needs os.lchflags()') + def test_lchflags_symlink(self): + testfn_st = os.stat(support.TESTFN) + + self.assertTrue(hasattr(testfn_st, 'st_flags')) + + os.symlink(support.TESTFN, _DUMMY_SYMLINK) + self.teardown_files.append(_DUMMY_SYMLINK) + dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) + + posix.lchflags(_DUMMY_SYMLINK, + dummy_symlink_st.st_flags | stat.UF_IMMUTABLE) + try: + new_testfn_st = os.stat(support.TESTFN) + new_dummy_symlink_st = os.lstat(_DUMMY_SYMLINK) + + self.assertEqual(testfn_st.st_flags, new_testfn_st.st_flags) + self.assertEqual(dummy_symlink_st.st_flags | stat.UF_IMMUTABLE, + new_dummy_symlink_st.st_flags) + finally: + posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags) def test_environ(self): if os.name == "nt": |