0001 "Test posix functions" 0002 0003 from test import test_support 0004 0005 try: 0006 import posix 0007 except ImportError: 0008 raise test_support.TestSkipped, "posix is not available" 0009 0010 import time 0011 import os 0012 import sys 0013 import unittest 0014 import warnings 0015 warnings.filterwarnings('ignore', '.* potential security risk .*', 0016 RuntimeWarning) 0017 0018 class PosixTester(unittest.TestCase): 0019 0020 def setUp(self): 0021 # create empty file 0022 fp = open(test_support.TESTFN, 'w+') 0023 fp.close() 0024 0025 def tearDown(self): 0026 os.unlink(test_support.TESTFN) 0027 0028 def testNoArgFunctions(self): 0029 # test posix functions which take no arguments and have 0030 # no side-effects which we need to cleanup (e.g., fork, wait, abort) 0031 NO_ARG_FUNCTIONS = [ "ctermid", "getcwd", "getcwdu", "uname", 0032 "times", "getloadavg", "tmpnam", 0033 "getegid", "geteuid", "getgid", "getgroups", 0034 "getpid", "getpgrp", "getppid", "getuid", 0035 ] 0036 0037 for name in NO_ARG_FUNCTIONS: 0038 posix_func = getattr(posix, name, None) 0039 if posix_func is not None: 0040 posix_func() 0041 self.assertRaises(TypeError, posix_func, 1) 0042 0043 def test_statvfs(self): 0044 if hasattr(posix, 'statvfs'): 0045 self.assert_(posix.statvfs(os.curdir)) 0046 0047 def test_fstatvfs(self): 0048 if hasattr(posix, 'fstatvfs'): 0049 fp = open(test_support.TESTFN) 0050 try: 0051 self.assert_(posix.fstatvfs(fp.fileno())) 0052 finally: 0053 fp.close() 0054 0055 def test_ftruncate(self): 0056 if hasattr(posix, 'ftruncate'): 0057 fp = open(test_support.TESTFN, 'w+') 0058 try: 0059 # we need to have some data to truncate 0060 fp.write('test') 0061 fp.flush() 0062 posix.ftruncate(fp.fileno(), 0) 0063 finally: 0064 fp.close() 0065 0066 def test_dup(self): 0067 if hasattr(posix, 'dup'): 0068 fp = open(test_support.TESTFN) 0069 try: 0070 fd = posix.dup(fp.fileno()) 0071 self.assert_(isinstance(fd, int)) 0072 os.close(fd) 0073 finally: 0074 fp.close() 0075 0076 def test_dup2(self): 0077 if hasattr(posix, 'dup2'): 0078 fp1 = open(test_support.TESTFN) 0079 fp2 = open(test_support.TESTFN) 0080 try: 0081 posix.dup2(fp1.fileno(), fp2.fileno()) 0082 finally: 0083 fp1.close() 0084 fp2.close() 0085 0086 def fdopen_helper(self, *args): 0087 fd = os.open(test_support.TESTFN, os.O_RDONLY) 0088 fp2 = posix.fdopen(fd, *args) 0089 fp2.close() 0090 0091 def test_fdopen(self): 0092 if hasattr(posix, 'fdopen'): 0093 self.fdopen_helper() 0094 self.fdopen_helper('r') 0095 self.fdopen_helper('r', 100) 0096 0097 def test_fstat(self): 0098 if hasattr(posix, 'fstat'): 0099 fp = open(test_support.TESTFN) 0100 try: 0101 self.assert_(posix.fstat(fp.fileno())) 0102 finally: 0103 fp.close() 0104 0105 def test_stat(self): 0106 if hasattr(posix, 'stat'): 0107 self.assert_(posix.stat(test_support.TESTFN)) 0108 0109 def test_chdir(self): 0110 if hasattr(posix, 'chdir'): 0111 posix.chdir(os.curdir) 0112 self.assertRaises(OSError, posix.chdir, test_support.TESTFN) 0113 0114 def test_lsdir(self): 0115 if hasattr(posix, 'lsdir'): 0116 self.assert_(test_support.TESTFN in posix.lsdir(os.curdir)) 0117 0118 def test_access(self): 0119 if hasattr(posix, 'access'): 0120 self.assert_(posix.access(test_support.TESTFN, os.R_OK)) 0121 0122 def test_umask(self): 0123 if hasattr(posix, 'umask'): 0124 old_mask = posix.umask(0) 0125 self.assert_(isinstance(old_mask, int)) 0126 posix.umask(old_mask) 0127 0128 def test_strerror(self): 0129 if hasattr(posix, 'strerror'): 0130 self.assert_(posix.strerror(0)) 0131 0132 def test_pipe(self): 0133 if hasattr(posix, 'pipe'): 0134 reader, writer = posix.pipe() 0135 os.close(reader) 0136 os.close(writer) 0137 0138 def test_tempnam(self): 0139 if hasattr(posix, 'tempnam'): 0140 self.assert_(posix.tempnam()) 0141 self.assert_(posix.tempnam(os.curdir)) 0142 self.assert_(posix.tempnam(os.curdir, 'blah')) 0143 0144 def test_tmpfile(self): 0145 if hasattr(posix, 'tmpfile'): 0146 fp = posix.tmpfile() 0147 fp.close() 0148 0149 def test_utime(self): 0150 if hasattr(posix, 'utime'): 0151 now = time.time() 0152 posix.utime(test_support.TESTFN, None) 0153 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, None)) 0154 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (now, None)) 0155 self.assertRaises(TypeError, posix.utime, test_support.TESTFN, (None, now)) 0156 posix.utime(test_support.TESTFN, (int(now), int(now))) 0157 posix.utime(test_support.TESTFN, (now, now)) 0158 0159 def test_main(): 0160 test_support.run_unittest(PosixTester) 0161 0162 if __name__ == '__main__': 0163 test_main() 0164
Generated by PyXR 0.9.4