diff -r 135e1a8144ec Lib/test/eintrdata/eintr_tester.py --- a/Lib/test/eintrdata/eintr_tester.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/eintrdata/eintr_tester.py Mon Jan 09 15:10:24 2017 +0100 @@ -20,7 +20,6 @@ import unittest from test import support -android_not_root = support.android_not_root @contextlib.contextmanager def kill_on_error(proc): @@ -312,14 +311,14 @@ # https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=203162 @support.requires_freebsd_version(10, 3) @unittest.skipUnless(hasattr(os, 'mkfifo'), 'needs mkfifo()') - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def _test_open(self, do_open_close_reader, do_open_close_writer): filename = support.TESTFN # Use a fifo: until the child opens it for reading, the parent will # block when trying to open it for writing. support.unlink(filename) - os.mkfifo(filename) + with support.supported_operation(): + os.mkfifo(filename) self.addCleanup(support.unlink, filename) code = '\n'.join(( diff -r 135e1a8144ec Lib/test/support/__init__.py --- a/Lib/test/support/__init__.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/support/__init__.py Mon Jan 09 15:10:24 2017 +0100 @@ -91,9 +91,10 @@ "requires_IEEE_754", "skip_unless_xattr", "requires_zlib", "anticipate_failure", "load_package_tests", "detect_api_mismatch", "check__all__", "requires_android_level", "requires_multiprocessing_queue", + "supported_operation", # sys "is_jython", "is_android", "check_impl_detail", "unix_shell", - "setswitchinterval", "android_not_root", + "setswitchinterval", # network "HOST", "IPV6_ENABLED", "find_unused_port", "bind_port", "open_urlresource", "bind_unix_socket", @@ -784,7 +785,6 @@ except AttributeError: # sys.getandroidapilevel() is only available on Android is_android = False -android_not_root = (is_android and os.geteuid() != 0) if sys.platform != 'win32': unix_shell = '/system/bin/sh' if is_android else '/bin/sh' @@ -1823,6 +1823,14 @@ msg = "requires a functioning shared semaphore implementation" return test if _have_mp_queue else unittest.skip(msg)(test) +@contextlib.contextmanager +def supported_operation(): + """Context manager to skip tests when the operation is not supported.""" + try: + yield + except PermissionError as e: + raise unittest.SkipTest(str(e)) + def _parse_guards(guards): # Returns a tuple ({platform_name: run_me}, default_value) if not guards: diff -r 135e1a8144ec Lib/test/test_genericpath.py --- a/Lib/test/test_genericpath.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/test_genericpath.py Mon Jan 09 15:10:24 2017 +0100 @@ -8,7 +8,6 @@ import unittest import warnings from test import support -android_not_root = support.android_not_root def create_file(filename, data=b'foo'): @@ -213,9 +212,9 @@ def test_samefile_on_symlink(self): self._test_samefile_on_link_func(os.symlink) - @unittest.skipIf(android_not_root, "hard links not allowed, non root user") def test_samefile_on_link(self): - self._test_samefile_on_link_func(os.link) + with support.supported_operation(): + self._test_samefile_on_link_func(os.link) def test_samestat(self): test_fn1 = support.TESTFN @@ -253,9 +252,9 @@ def test_samestat_on_symlink(self): self._test_samestat_on_link_func(os.symlink) - @unittest.skipIf(android_not_root, "hard links not allowed, non root user") def test_samestat_on_link(self): - self._test_samestat_on_link_func(os.link) + with support.supported_operation(): + self._test_samestat_on_link_func(os.link) def test_sameopenfile(self): filename = support.TESTFN diff -r 135e1a8144ec Lib/test/test_pathlib.py --- a/Lib/test/test_pathlib.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/test_pathlib.py Mon Jan 09 15:10:24 2017 +0100 @@ -10,7 +10,6 @@ import unittest from test import support -android_not_root = support.android_not_root TESTFN = support.TESTFN try: @@ -1865,10 +1864,10 @@ self.assertFalse((P / 'fileA' / 'bah').is_fifo()) @unittest.skipUnless(hasattr(os, "mkfifo"), "os.mkfifo() required") - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def test_is_fifo_true(self): P = self.cls(BASE, 'myfifo') - os.mkfifo(str(P)) + with support.supported_operation(): + os.mkfifo(str(P)) self.assertTrue(P.is_fifo()) self.assertFalse(P.is_socket()) self.assertFalse(P.is_file()) diff -r 135e1a8144ec Lib/test/test_posix.py --- a/Lib/test/test_posix.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/test_posix.py Mon Jan 09 15:10:24 2017 +0100 @@ -1,7 +1,6 @@ "Test posix functions" from test import support -android_not_root = support.android_not_root # Skip these tests if there is no posix module. posix = support.import_module('posix') @@ -423,15 +422,14 @@ posix.stat, list(os.fsencode(support.TESTFN))) @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()") - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def test_mkfifo(self): support.unlink(support.TESTFN) - posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) + with support.supported_operation(): + posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR) self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) @unittest.skipUnless(hasattr(posix, 'mknod') and hasattr(stat, 'S_IFIFO'), "don't have mknod()/S_IFIFO") - @unittest.skipIf(android_not_root, "mknod not allowed, non root user") def test_mknod(self): # Test using mknod() to create a FIFO (the only use specified # by POSIX). @@ -442,7 +440,7 @@ except OSError as e: # Some old systems don't allow unprivileged users to use # mknod(), or only support creating device nodes. - self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) + self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES)) else: self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) @@ -452,7 +450,7 @@ posix.mknod(path=support.TESTFN, mode=mode, device=0, dir_fd=None) except OSError as e: - self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) + self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES)) @unittest.skipUnless(hasattr(posix, 'stat'), 'test needs posix.stat()') @unittest.skipUnless(hasattr(posix, 'makedev'), 'test needs posix.makedev()') @@ -910,11 +908,12 @@ posix.close(f) @unittest.skipUnless(os.link in os.supports_dir_fd, "test needs dir_fd support in os.link()") - @unittest.skipIf(android_not_root, "hard link not allowed, non root user") def test_link_dir_fd(self): f = posix.open(posix.getcwd(), posix.O_RDONLY) try: - posix.link(support.TESTFN, support.TESTFN + 'link', src_dir_fd=f, dst_dir_fd=f) + with support.supported_operation(): + posix.link(support.TESTFN, support.TESTFN + 'link', + src_dir_fd=f, dst_dir_fd=f) # should have same inodes self.assertEqual(posix.stat(support.TESTFN)[1], posix.stat(support.TESTFN + 'link')[1]) @@ -934,7 +933,6 @@ @unittest.skipUnless((os.mknod in os.supports_dir_fd) and hasattr(stat, 'S_IFIFO'), "test requires both stat.S_IFIFO and dir_fd support for os.mknod()") - @unittest.skipIf(android_not_root, "mknod not allowed, non root user") def test_mknod_dir_fd(self): # Test using mknodat() to create a FIFO (the only use specified # by POSIX). @@ -946,7 +944,7 @@ except OSError as e: # Some old systems don't allow unprivileged users to use # mknod(), or only support creating device nodes. - self.assertIn(e.errno, (errno.EPERM, errno.EINVAL)) + self.assertIn(e.errno, (errno.EPERM, errno.EINVAL, errno.EACCES)) else: self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) finally: @@ -1018,12 +1016,13 @@ posix.close(f) @unittest.skipUnless(os.mkfifo in os.supports_dir_fd, "test needs dir_fd support in os.mkfifo()") - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def test_mkfifo_dir_fd(self): support.unlink(support.TESTFN) f = posix.open(posix.getcwd(), posix.O_RDONLY) try: - posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, dir_fd=f) + with support.supported_operation(): + posix.mkfifo(support.TESTFN, stat.S_IRUSR | stat.S_IWUSR, + dir_fd=f) self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode)) finally: posix.close(f) diff -r 135e1a8144ec Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/test_shutil.py Mon Jan 09 15:10:24 2017 +0100 @@ -22,8 +22,7 @@ import zipfile from test import support -from test.support import (TESTFN, check_warnings, captured_stdout, - android_not_root) +from test.support import TESTFN, check_warnings, captured_stdout TESTFN2 = TESTFN + "2" @@ -770,7 +769,6 @@ @unittest.skipIf(os.name == 'nt', 'temporarily disabled on Windows') @unittest.skipUnless(hasattr(os, 'link'), 'requires os.link') - @unittest.skipIf(android_not_root, "hard links not allowed, non root user") def test_dont_copy_file_onto_link_to_itself(self): # bug 851123. os.mkdir(TESTFN) @@ -779,7 +777,8 @@ try: with open(src, 'w') as f: f.write('cheddar') - os.link(src, dst) + with support.supported_operation(): + os.link(src, dst) self.assertRaises(shutil.SameFileError, shutil.copyfile, src, dst) with open(src, 'r') as f: self.assertEqual(f.read(), 'cheddar') @@ -823,9 +822,9 @@ # Issue #3002: copyfile and copytree block indefinitely on named pipes @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def test_copyfile_named_pipe(self): - os.mkfifo(TESTFN) + with support.supported_operation(): + os.mkfifo(TESTFN) try: self.assertRaises(shutil.SpecialFileError, shutil.copyfile, TESTFN, TESTFN2) @@ -834,7 +833,6 @@ finally: os.remove(TESTFN) - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") @unittest.skipUnless(hasattr(os, "mkfifo"), 'requires os.mkfifo()') @support.skip_unless_symlink def test_copytree_named_pipe(self): @@ -843,7 +841,8 @@ subdir = os.path.join(TESTFN, "subdir") os.mkdir(subdir) pipe = os.path.join(subdir, "mypipe") - os.mkfifo(pipe) + with support.supported_operation(): + os.mkfifo(pipe) try: shutil.copytree(TESTFN, TESTFN2) except shutil.Error as e: diff -r 135e1a8144ec Lib/test/test_stat.py --- a/Lib/test/test_stat.py Mon Jan 09 11:21:37 2017 +0100 +++ b/Lib/test/test_stat.py Mon Jan 09 15:10:24 2017 +0100 @@ -1,7 +1,7 @@ import unittest import os import sys -from test.support import TESTFN, import_fresh_module, android_not_root +from test.support import TESTFN, import_fresh_module, supported_operation c_stat = import_fresh_module('stat', fresh=['_stat']) py_stat = import_fresh_module('stat', blocked=['_stat']) @@ -168,9 +168,9 @@ self.assertS_IS("LNK", st_mode) @unittest.skipUnless(hasattr(os, 'mkfifo'), 'os.mkfifo not available') - @unittest.skipIf(android_not_root, "mkfifo not allowed, non root user") def test_fifo(self): - os.mkfifo(TESTFN, 0o700) + with supported_operation(): + os.mkfifo(TESTFN, 0o700) st_mode, modestr = self.get_mode() self.assertEqual(modestr, 'prwx------') self.assertS_IS("FIFO", st_mode)