aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_posix.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_posix.py')
-rw-r--r--Lib/test/test_posix.py207
1 files changed, 101 insertions, 106 deletions
diff --git a/Lib/test/test_posix.py b/Lib/test/test_posix.py
index 7214efa323d..b936dda44bc 100644
--- a/Lib/test/test_posix.py
+++ b/Lib/test/test_posix.py
@@ -1,9 +1,9 @@
"Test posix functions"
-from test import test_support
+from test import support
# Skip these tests if there is no posix module.
-posix = test_support.import_module('posix')
+posix = support.import_module('posix')
import errno
import sys
@@ -13,45 +13,44 @@ import platform
import pwd
import shutil
import stat
-import sys
import tempfile
import unittest
import warnings
_DUMMY_SYMLINK = os.path.join(tempfile.gettempdir(),
- test_support.TESTFN + '-dummy-symlink')
-
-warnings.filterwarnings('ignore', '.* potential security risk .*',
- RuntimeWarning)
+ support.TESTFN + '-dummy-symlink')
class PosixTester(unittest.TestCase):
def setUp(self):
# create empty file
- fp = open(test_support.TESTFN, 'w+')
+ fp = open(support.TESTFN, 'w+')
fp.close()
- self.teardown_files = [ test_support.TESTFN ]
+ 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):
for teardown_file in self.teardown_files:
- os.unlink(teardown_file)
+ support.unlink(teardown_file)
+ self._warnings_manager.__exit__(None, None, None)
def testNoArgFunctions(self):
# test posix functions which take no arguments and have
# no side-effects which we need to cleanup (e.g., fork, wait, abort)
- NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname",
- "times", "getloadavg", "tmpnam",
+ NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdb", "uname",
+ "times", "getloadavg",
"getegid", "geteuid", "getgid", "getgroups",
"getpid", "getpgrp", "getppid", "getuid",
]
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", "", DeprecationWarning)
- for name in NO_ARG_FUNCTIONS:
- posix_func = getattr(posix, name, None)
- if posix_func is not None:
- posix_func()
- self.assertRaises(TypeError, posix_func, 1)
+ for name in NO_ARG_FUNCTIONS:
+ posix_func = getattr(posix, name, None)
+ if posix_func is not None:
+ posix_func()
+ self.assertRaises(TypeError, posix_func, 1)
if hasattr(posix, 'getresuid'):
def test_getresuid(self):
@@ -126,7 +125,7 @@ class PosixTester(unittest.TestCase):
def test_fstatvfs(self):
if hasattr(posix, 'fstatvfs'):
- fp = open(test_support.TESTFN)
+ fp = open(support.TESTFN)
try:
self.assertTrue(posix.fstatvfs(fp.fileno()))
finally:
@@ -134,7 +133,7 @@ class PosixTester(unittest.TestCase):
def test_ftruncate(self):
if hasattr(posix, 'ftruncate'):
- fp = open(test_support.TESTFN, 'w+')
+ fp = open(support.TESTFN, 'w+')
try:
# we need to have some data to truncate
fp.write('test')
@@ -145,7 +144,7 @@ class PosixTester(unittest.TestCase):
def test_dup(self):
if hasattr(posix, 'dup'):
- fp = open(test_support.TESTFN)
+ fp = open(support.TESTFN)
try:
fd = posix.dup(fp.fileno())
self.assertIsInstance(fd, int)
@@ -160,59 +159,48 @@ class PosixTester(unittest.TestCase):
def test_dup2(self):
if hasattr(posix, 'dup2'):
- fp1 = open(test_support.TESTFN)
- fp2 = open(test_support.TESTFN)
+ fp1 = open(support.TESTFN)
+ fp2 = open(support.TESTFN)
try:
posix.dup2(fp1.fileno(), fp2.fileno())
finally:
fp1.close()
fp2.close()
- def fdopen_helper(self, *args):
- fd = os.open(test_support.TESTFN, os.O_RDONLY)
- fp2 = posix.fdopen(fd, *args)
- fp2.close()
-
- def test_fdopen(self):
- if hasattr(posix, 'fdopen'):
- self.fdopen_helper()
- self.fdopen_helper('r')
- self.fdopen_helper('r', 100)
-
def test_osexlock(self):
if hasattr(posix, "O_EXLOCK"):
- fd = os.open(test_support.TESTFN,
+ fd = os.open(support.TESTFN,
os.O_WRONLY|os.O_EXLOCK|os.O_CREAT)
- self.assertRaises(OSError, os.open, test_support.TESTFN,
+ self.assertRaises(OSError, os.open, support.TESTFN,
os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
os.close(fd)
if hasattr(posix, "O_SHLOCK"):
- fd = os.open(test_support.TESTFN,
+ fd = os.open(support.TESTFN,
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
- self.assertRaises(OSError, os.open, test_support.TESTFN,
+ self.assertRaises(OSError, os.open, support.TESTFN,
os.O_WRONLY|os.O_EXLOCK|os.O_NONBLOCK)
os.close(fd)
def test_osshlock(self):
if hasattr(posix, "O_SHLOCK"):
- fd1 = os.open(test_support.TESTFN,
+ fd1 = os.open(support.TESTFN,
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
- fd2 = os.open(test_support.TESTFN,
+ fd2 = os.open(support.TESTFN,
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
os.close(fd2)
os.close(fd1)
if hasattr(posix, "O_EXLOCK"):
- fd = os.open(test_support.TESTFN,
+ fd = os.open(support.TESTFN,
os.O_WRONLY|os.O_SHLOCK|os.O_CREAT)
- self.assertRaises(OSError, os.open, test_support.TESTFN,
+ self.assertRaises(OSError, os.open, support.TESTFN,
os.O_RDONLY|os.O_EXLOCK|os.O_NONBLOCK)
os.close(fd)
def test_fstat(self):
if hasattr(posix, 'fstat'):
- fp = open(test_support.TESTFN)
+ fp = open(support.TESTFN)
try:
self.assertTrue(posix.fstat(fp.fileno()))
finally:
@@ -220,7 +208,29 @@ class PosixTester(unittest.TestCase):
def test_stat(self):
if hasattr(posix, 'stat'):
- self.assertTrue(posix.stat(test_support.TESTFN))
+ self.assertTrue(posix.stat(support.TESTFN))
+
+ @unittest.skipUnless(hasattr(posix, 'mkfifo'), "don't have mkfifo()")
+ def test_mkfifo(self):
+ support.unlink(support.TESTFN)
+ 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")
+ def test_mknod(self):
+ # Test using mknod() to create a FIFO (the only use specified
+ # by POSIX).
+ support.unlink(support.TESTFN)
+ mode = stat.S_IFIFO | stat.S_IRUSR | stat.S_IWUSR
+ try:
+ posix.mknod(support.TESTFN, mode, 0)
+ 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))
+ else:
+ self.assertTrue(stat.S_ISFIFO(posix.stat(support.TESTFN).st_mode))
def _test_all_chown_common(self, chown_func, first_param):
"""Common code for chown, fchown and lchown tests."""
@@ -251,19 +261,19 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'chown'), "test needs os.chown()")
def test_chown(self):
# raise an OSError if the file does not exist
- os.unlink(test_support.TESTFN)
- self.assertRaises(OSError, posix.chown, test_support.TESTFN, -1, -1)
+ os.unlink(support.TESTFN)
+ self.assertRaises(OSError, posix.chown, support.TESTFN, -1, -1)
# re-create the file
- open(test_support.TESTFN, 'w').close()
- self._test_all_chown_common(posix.chown, test_support.TESTFN)
+ open(support.TESTFN, 'w').close()
+ self._test_all_chown_common(posix.chown, support.TESTFN)
@unittest.skipUnless(hasattr(posix, 'fchown'), "test needs os.fchown()")
def test_fchown(self):
- os.unlink(test_support.TESTFN)
+ os.unlink(support.TESTFN)
# re-create the file
- test_file = open(test_support.TESTFN, 'w')
+ test_file = open(support.TESTFN, 'w')
try:
fd = test_file.fileno()
self._test_all_chown_common(posix.fchown, fd)
@@ -272,23 +282,28 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'lchown'), "test needs os.lchown()")
def test_lchown(self):
- os.unlink(test_support.TESTFN)
+ os.unlink(support.TESTFN)
# create a symlink
- os.symlink(_DUMMY_SYMLINK, test_support.TESTFN)
- self._test_all_chown_common(posix.lchown, test_support.TESTFN)
+ os.symlink(_DUMMY_SYMLINK, support.TESTFN)
+ self._test_all_chown_common(posix.lchown, support.TESTFN)
def test_chdir(self):
if hasattr(posix, 'chdir'):
posix.chdir(os.curdir)
- self.assertRaises(OSError, posix.chdir, test_support.TESTFN)
+ self.assertRaises(OSError, posix.chdir, support.TESTFN)
- def test_lsdir(self):
- if hasattr(posix, 'lsdir'):
- self.assertIn(test_support.TESTFN, posix.lsdir(os.curdir))
+ def test_listdir(self):
+ if hasattr(posix, 'listdir'):
+ self.assertTrue(support.TESTFN in posix.listdir(os.curdir))
+
+ def test_listdir_default(self):
+ # When listdir is called without argument, it's the same as listdir(os.curdir)
+ if hasattr(posix, 'listdir'):
+ self.assertTrue(support.TESTFN in posix.listdir())
def test_access(self):
if hasattr(posix, 'access'):
- self.assertTrue(posix.access(test_support.TESTFN, os.R_OK))
+ self.assertTrue(posix.access(support.TESTFN, os.R_OK))
def test_umask(self):
if hasattr(posix, 'umask'):
@@ -306,30 +321,15 @@ class PosixTester(unittest.TestCase):
os.close(reader)
os.close(writer)
- def test_tempnam(self):
- if hasattr(posix, 'tempnam'):
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", "tempnam", DeprecationWarning)
- self.assertTrue(posix.tempnam())
- self.assertTrue(posix.tempnam(os.curdir))
- self.assertTrue(posix.tempnam(os.curdir, 'blah'))
-
- def test_tmpfile(self):
- if hasattr(posix, 'tmpfile'):
- with warnings.catch_warnings():
- warnings.filterwarnings("ignore", "tmpfile", DeprecationWarning)
- fp = posix.tmpfile()
- fp.close()
-
def test_utime(self):
if hasattr(posix, 'utime'):
now = time.time()
- posix.utime(test_support.TESTFN, None)
- self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None))
- self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None))
- self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now))
- posix.utime(test_support.TESTFN, (int(now), int(now)))
- posix.utime(test_support.TESTFN, (now, now))
+ posix.utime(support.TESTFN, None)
+ self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, None))
+ self.assertRaises(TypeError, posix.utime, support.TESTFN, (now, None))
+ self.assertRaises(TypeError, posix.utime, support.TESTFN, (None, now))
+ 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)
@@ -356,19 +356,19 @@ class PosixTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'chflags'), 'test needs os.chflags()')
def test_chflags(self):
- self._test_chflags_regular_file(posix.chflags, test_support.TESTFN)
+ 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, test_support.TESTFN)
+ 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(test_support.TESTFN)
+ testfn_st = os.stat(support.TESTFN)
self.assertTrue(hasattr(testfn_st, 'st_flags'))
- os.symlink(test_support.TESTFN, _DUMMY_SYMLINK)
+ os.symlink(support.TESTFN, _DUMMY_SYMLINK)
self.teardown_files.append(_DUMMY_SYMLINK)
dummy_symlink_st = os.lstat(_DUMMY_SYMLINK)
@@ -383,7 +383,7 @@ class PosixTester(unittest.TestCase):
self.skipTest(msg)
try:
- new_testfn_st = os.stat(test_support.TESTFN)
+ 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)
@@ -392,11 +392,20 @@ class PosixTester(unittest.TestCase):
finally:
posix.lchflags(_DUMMY_SYMLINK, dummy_symlink_st.st_flags)
+ def test_environ(self):
+ if os.name == "nt":
+ item_type = str
+ else:
+ item_type = bytes
+ for k, v in posix.environ.items():
+ self.assertEqual(type(k), item_type)
+ self.assertEqual(type(v), item_type)
+
def test_getcwd_long_pathnames(self):
if hasattr(posix, 'getcwd'):
dirname = 'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
curdir = os.getcwd()
- base_path = os.path.abspath(test_support.TESTFN) + '.getcwd'
+ base_path = os.path.abspath(support.TESTFN) + '.getcwd'
try:
os.mkdir(base_path)
@@ -405,34 +414,20 @@ class PosixTester(unittest.TestCase):
# Just returning nothing instead of the SkipTest exception,
# because the test results in Error in that case.
# Is that ok?
-# raise unittest.SkipTest, "cannot create directory for testing"
+# raise unittest.SkipTest("cannot create directory for testing")
return
- try:
def _create_and_do_getcwd(dirname, current_path_length = 0):
try:
os.mkdir(dirname)
except:
- raise unittest.SkipTest, "mkdir cannot create directory sufficiently deep for getcwd test"
+ raise unittest.SkipTest("mkdir cannot create directory sufficiently deep for getcwd test")
os.chdir(dirname)
try:
os.getcwd()
- if current_path_length < 4099:
+ if current_path_length < 1027:
_create_and_do_getcwd(dirname, current_path_length + len(dirname) + 1)
- except OSError as e:
- expected_errno = errno.ENAMETOOLONG
- # The following platforms have quirky getcwd()
- # behaviour -- see issue 9185 and 15765 for
- # more information.
- quirky_platform = (
- 'sunos' in sys.platform or
- 'netbsd' in sys.platform or
- 'openbsd' in sys.platform
- )
- if quirky_platform:
- expected_errno = errno.ERANGE
- self.assertEqual(e.errno, expected_errno)
finally:
os.chdir('..')
os.rmdir(dirname)
@@ -441,7 +436,7 @@ class PosixTester(unittest.TestCase):
finally:
os.chdir(curdir)
- shutil.rmtree(base_path)
+ support.rmtree(base_path)
@unittest.skipUnless(hasattr(os, 'getegid'), "test needs os.getegid()")
def test_getgroups(self):
@@ -491,13 +486,13 @@ class PosixGroupsTester(unittest.TestCase):
@unittest.skipUnless(hasattr(posix, 'setgroups'),
"test needs posix.setgroups()")
def test_setgroups(self):
- for groups in [[0], range(16)]:
+ for groups in [[0], list(range(16))]:
posix.setgroups(groups)
self.assertListEqual(groups, posix.getgroups())
def test_main():
- test_support.run_unittest(PosixTester, PosixGroupsTester)
+ support.run_unittest(PosixTester, PosixGroupsTester)
if __name__ == '__main__':
test_main()